Source for file items.php

Documentation is available at items.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_menus
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. /**
  13.  * Menu Item List Model for Menus.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_menus
  17.  * @since       1.6
  18.  */
  19. class MenusModelItems extends JModelList
  20. {
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param   array  $config  An optional associative array of configuration settings.
  25.      *
  26.      * @see     JController
  27.      * @since   1.6
  28.      */
  29.     public function __construct($config = array())
  30.     {
  31.         if (empty($config['filter_fields']))
  32.         {
  33.             $config['filter_fields'] = array(
  34.                 'id', 'a.id',
  35.                 'menutype', 'a.menutype',
  36.                 'title', 'a.title',
  37.                 'alias', 'a.alias',
  38.                 'published', 'a.published',
  39.                 'access', 'a.access', 'access_level',
  40.                 'language', 'a.language',
  41.                 'checked_out', 'a.checked_out',
  42.                 'checked_out_time', 'a.checked_out_time',
  43.                 'lft', 'a.lft',
  44.                 'rgt', 'a.rgt',
  45.                 'level', 'a.level',
  46.                 'path', 'a.path',
  47.                 'client_id', 'a.client_id',
  48.                 'home', 'a.home',
  49.                 'a.ordering'
  50.             );
  51.  
  52.             $app = JFactory::getApplication();
  53.             $assoc = JLanguageAssociations::isEnabled();
  54.  
  55.             if ($assoc)
  56.             {
  57.                 $config['filter_fields'][] = 'association';
  58.             }
  59.         }
  60.  
  61.         parent::__construct($config);
  62.     }
  63.  
  64.     /**
  65.      * Method to auto-populate the model state.
  66.      *
  67.      * Note. Calling getState in this method will result in recursion.
  68.      *
  69.      * @return  void 
  70.      * @since   1.6
  71.      */
  72.     protected function populateState($ordering = null, $direction = null)
  73.     {
  74.         $app = JFactory::getApplication('administrator');
  75.  
  76.         $parentId = $this->getUserStateFromRequest($this->context . '.filter.parent_id', 'filter_parent_id', 0, 'int');
  77.         $this->setState('filter.parent_id', $parentId);
  78.  
  79.         $search = $this->getUserStateFromRequest($this->context . '.search', 'filter_search');
  80.         $this->setState('filter.search', $search);
  81.  
  82.         $published = $this->getUserStateFromRequest($this->context . '.published', 'filter_published', '');
  83.         $this->setState('filter.published', $published);
  84.  
  85.         $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int');
  86.         $this->setState('filter.access', $access);
  87.  
  88.         $parentId = $this->getUserStateFromRequest($this->context . '.filter.parent_id', 'filter_parent_id', 0, 'int');
  89.         $this->setState('filter.parent_id', $parentId);
  90.  
  91.         $level = $this->getUserStateFromRequest($this->context . '.filter.level', 'filter_level', 0, 'int');
  92.         $this->setState('filter.level', $level);
  93.  
  94.         $menuType = $app->input->getString('menutype', null);
  95.  
  96.         if ($menuType)
  97.         {
  98.             if ($menuType != $app->getUserState($this->context . '.menutype'))
  99.             {
  100.                 $app->setUserState($this->context . '.menutype', $menuType);
  101.                 $app->input->set('limitstart', 0);
  102.             }
  103.         }
  104.         else
  105.         {
  106.             $menuType = $app->getUserState($this->context . '.menutype');
  107.  
  108.             if (!$menuType)
  109.             {
  110.                 $menuType = $this->getDefaultMenuType();
  111.             }
  112.         }
  113.  
  114.         $this->setState('filter.menutype', $menuType);
  115.  
  116.         $language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
  117.         $this->setState('filter.language', $language);
  118.  
  119.         // Component parameters.
  120.         $params = JComponentHelper::getParams('com_menus');
  121.         $this->setState('params', $params);
  122.  
  123.         // List state information.
  124.         parent::populateState('a.lft', 'asc');
  125.     }
  126.  
  127.     /**
  128.      * Method to get a store id based on model configuration state.
  129.      *
  130.      * This is necessary because the model is used by the component and
  131.      * different modules that might need different sets of data or different
  132.      * ordering requirements.
  133.      *
  134.      * @param   string  $id  A prefix for the store id.
  135.      *
  136.      * @return  string  A store id.
  137.      *
  138.      * @since   1.6
  139.      */
  140.     protected function getStoreId($id = '')
  141.     {
  142.         // Compile the store id.
  143.         $id .= ':' . $this->getState('filter.access');
  144.         $id .= ':' . $this->getState('filter.published');
  145.         $id .= ':' . $this->getState('filter.language');
  146.         $id .= ':' . $this->getState('filter.search');
  147.         $id .= ':' . $this->getState('filter.parent_id');
  148.         $id .= ':' . $this->getState('filter.menutype');
  149.  
  150.         return parent::getStoreId($id);
  151.     }
  152.  
  153.     /**
  154.      * Finds the default menu type.
  155.      *
  156.      * In the absence of better information, this is the first menu ordered by title.
  157.      *
  158.      * @return  string    The default menu type
  159.      *
  160.      * @since   1.6
  161.      */
  162.     protected function getDefaultMenuType()
  163.     {
  164.         // Create a new query object.
  165.         $db = $this->getDbo();
  166.         $query = $db->getQuery(true)
  167.             ->select('menutype')
  168.             ->from('#__menu_types')
  169.             ->order('title');
  170.         $db->setQuery($query, 0, 1);
  171.         $menuType = $db->loadResult();
  172.  
  173.         return $menuType;
  174.     }
  175.  
  176.     /**
  177.      * Builds an SQL query to load the list data.
  178.      *
  179.      * @return  JDatabaseQuery    A query object.
  180.      */
  181.     protected function getListQuery()
  182.     {
  183.         // Create a new query object.
  184.         $db = $this->getDbo();
  185.         $query = $db->getQuery(true);
  186.         $user = JFactory::getUser();
  187.         $app = JFactory::getApplication();
  188.  
  189.         // Select all fields from the table.
  190.         $query->select(
  191.             $this->getState(
  192.                 'list.select',
  193.                 $db->quoteName(
  194.                     array('a.id', 'a.menutype', 'a.title', 'a.alias', 'a.note', 'a.path', 'a.link', 'a.type', 'a.parent_id', 'a.level', 'a.published', 'a.component_id', 'a.checked_out', 'a.checked_out_time', 'a.browserNav', 'a.access', 'a.img', 'a.template_style_id', 'a.params', 'a.lft', 'a.rgt', 'a.home', 'a.language', 'a.client_id'),
  195.                     array(null, null, null, null, null, null, null, null, null, null, 'apublished', null, null, null, null, null, null, null, null, null, null, null, null, null)
  196.                 )
  197.             )
  198.         );
  199.         $query->select(
  200.             'CASE a.type' .
  201.                 ' WHEN ' . $db->quote('component') . ' THEN a.published+2*(e.enabled-1) ' .
  202.                 ' WHEN ' . $db->quote('url') . ' THEN a.published+2 ' .
  203.                 ' WHEN ' . $db->quote('alias') . ' THEN a.published+4 ' .
  204.                 ' WHEN ' . $db->quote('separator') . ' THEN a.published+6 ' .
  205.                 ' WHEN ' . $db->quote('heading') . ' THEN a.published+8 ' .
  206.                 ' END AS published'
  207.         );
  208.         $query->from($db->quoteName('#__menu') . ' AS a');
  209.  
  210.         // Join over the language
  211.         $query->select('l.title AS language_title, l.image as image')
  212.             ->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
  213.  
  214.         // Join over the users.
  215.         $query->select('u.name AS editor')
  216.             ->join('LEFT', $db->quoteName('#__users') . ' AS u ON u.id = a.checked_out');
  217.  
  218.         // Join over components
  219.         $query->select('c.element AS componentname')
  220.             ->join('LEFT', $db->quoteName('#__extensions') . ' AS c ON c.extension_id = a.component_id');
  221.  
  222.         // Join over the asset groups.
  223.         $query->select('ag.title AS access_level')
  224.             ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  225.  
  226.         // Join over the associations.
  227.         $assoc = JLanguageAssociations::isEnabled();
  228.  
  229.         if ($assoc)
  230.         {
  231.             $query->select('COUNT(asso2.id)>1 as association')
  232.                 ->join('LEFT', '#__associations AS asso ON asso.id = a.id AND asso.context=' . $db->quote('com_menus.item'))
  233.                 ->join('LEFT', '#__associations AS asso2 ON asso2.key = asso.key')
  234.                 ->group('a.id');
  235.         }
  236.  
  237.         // Join over the extensions
  238.         $query->select('e.name AS name')
  239.             ->join('LEFT', '#__extensions AS e ON e.extension_id = a.component_id');
  240.  
  241.         // Exclude the root category.
  242.         $query->where('a.id > 1')
  243.             ->where('a.client_id = 0');
  244.  
  245.         // Filter on the published state.
  246.         $published = $this->getState('filter.published');
  247.  
  248.         if (is_numeric($published))
  249.         {
  250.             $query->where('a.published = ' . (int) $published);
  251.         }
  252.         elseif ($published === '')
  253.         {
  254.             $query->where('(a.published IN (0, 1))');
  255.         }
  256.  
  257.         // Filter by search in title, alias or id
  258.         if ($search = trim($this->getState('filter.search')))
  259.         {
  260.             if (stripos($search, 'id:') === 0)
  261.             {
  262.                 $query->where('a.id = ' . (int) substr($search, 3));
  263.             }
  264.             elseif (stripos($search, 'link:') === 0)
  265.             {
  266.                 if ($search = substr($search, 5))
  267.                 {
  268.                     $search = $db->quote('%' . $db->escape($search, true) . '%');
  269.                     $query->where('a.link LIKE ' . $search);
  270.                 }
  271.             }
  272.             else
  273.             {
  274.                 $search = $db->quote('%' . $db->escape($search, true) . '%');
  275.                 $query->where('(' . 'a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ' OR a.note LIKE ' . $search . ')');
  276.             }
  277.         }
  278.  
  279.         // Filter the items over the parent id if set.
  280.         $parentId = $this->getState('filter.parent_id');
  281.  
  282.         if (!empty($parentId))
  283.         {
  284.             $query->where('p.id = ' . (int) $parentId);
  285.         }
  286.  
  287.         // Filter the items over the menu id if set.
  288.         $menuType = $this->getState('filter.menutype');
  289.  
  290.         if (!empty($menuType))
  291.         {
  292.             $query->where('a.menutype = ' . $db->quote($menuType));
  293.         }
  294.  
  295.         // Filter on the access level.
  296.         if ($access = $this->getState('filter.access'))
  297.         {
  298.             $query->where('a.access = ' . (int) $access);
  299.         }
  300.  
  301.         // Implement View Level Access
  302.         if (!$user->authorise('core.admin'))
  303.         {
  304.             $groups = implode(',', $user->getAuthorisedViewLevels());
  305.             $query->where('a.access IN (' . $groups . ')');
  306.         }
  307.  
  308.         // Filter on the level.
  309.         if ($level = $this->getState('filter.level'))
  310.         {
  311.             $query->where('a.level <= ' . (int) $level);
  312.         }
  313.  
  314.         // Filter on the language.
  315.         if ($language = $this->getState('filter.language'))
  316.         {
  317.             $query->where('a.language = ' . $db->quote($language));
  318.         }
  319.  
  320.         // Add the list ordering clause.
  321.         $query->order($db->escape($this->getState('list.ordering', 'a.lft')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
  322.  
  323.         return $query;
  324.     }
  325. }

Documentation generated on Tue, 19 Nov 2013 15:06:01 +0100 by phpDocumentor 1.4.3