Component-Builder/admin/models/get_snippets.php

141 lines
4.0 KiB
PHP

<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Utilities\ArrayHelper;
use VDM\Joomla\Utilities\ArrayHelper as UtilitiesArrayHelper;
/**
* Componentbuilder List Model for Get_snippets
*/
class ComponentbuilderModelGet_snippets extends ListModel
{
/**
* Model user data.
*
* @var strings
*/
protected $user;
protected $userId;
protected $guest;
protected $groups;
protected $levels;
protected $app;
protected $input;
protected $uikitComp;
/**
* Method to build an SQL query to load the list data.
*
* @return string An SQL query
*/
protected function getListQuery()
{
// Get the current user for authorisation checks
$this->user = Factory::getUser();
$this->userId = $this->user->get('id');
$this->guest = $this->user->get('guest');
$this->groups = $this->user->get('groups');
$this->authorisedGroups = $this->user->getAuthorisedGroups();
$this->levels = $this->user->getAuthorisedViewLevels();
$this->app = Factory::getApplication();
$this->input = $this->app->input;
$this->initSet = true;
// Make sure all records load, since no pagination allowed.
$this->setState('list.limit', 0);
// Get a db connection.
$db = Factory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Get from #__componentbuilder_snippet as a
$query->select($db->quoteName(
array('a.id','a.heading','a.name','a.url','a.created','a.modified'),
array('id','heading','name','url','created','modified')));
$query->from($db->quoteName('#__componentbuilder_snippet', 'a'));
// Get from #__componentbuilder_snippet_type as b
$query->select($db->quoteName(
array('b.name'),
array('type')));
$query->join('LEFT', ($db->quoteName('#__componentbuilder_snippet_type', 'b')) . ' ON (' . $db->quoteName('a.type') . ' = ' . $db->quoteName('b.id') . ')');
// Get from #__componentbuilder_library as c
$query->select($db->quoteName(
array('c.name'),
array('library')));
$query->join('LEFT', ($db->quoteName('#__componentbuilder_library', 'c')) . ' ON (' . $db->quoteName('a.library') . ' = ' . $db->quoteName('c.id') . ')');
// return the query object
return $query;
}
/**
* Method to get an array of data items.
*
* @return mixed An array of data items on success, false on failure.
*/
public function getItems()
{
$user = Factory::getUser();
// check if this user has permission to access items
if (!$user->authorise('get_snippets.access', 'com_componentbuilder'))
{
$app = Factory::getApplication();
$app->enqueueMessage(Text::_('Not authorised!'), 'error');
// redirect away if not a correct (TODO for now we go to default view)
$app->redirect('index.php?option=com_componentbuilder');
return false;
}
// load parent items
$items = parent::getItems();
// Get the global params
$globalParams = ComponentHelper::getParams('com_componentbuilder', true);
// Insure all item fields are adapted where needed.
if (UtilitiesArrayHelper::check($items))
{
foreach ($items as $nr => &$item)
{
// Always create a slug for sef URL's
$item->slug = ($item->id ?? '0') . (isset($item->alias) ? ':' . $item->alias : '');
}
}
// return items
return $items;
}
/**
* Get the uikit needed components
*
* @return mixed An array of objects on success.
*
*/
public function getUikitComp()
{
if (isset($this->uikitComp) && UtilitiesArrayHelper::check($this->uikitComp))
{
return $this->uikitComp;
}
return false;
}
}