Source for file menus.php

Documentation is available at menus.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 List Model for Menus.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_menus
  17.  * @since       1.6
  18.  */
  19. class MenusModelMenus extends JModelList
  20. {
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param   array  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.                 'title''a.title',
  36.                 'menutype''a.menutype',
  37.             );
  38.         }
  39.  
  40.         parent::__construct($config);
  41.     }
  42.  
  43.     /**
  44.      * Overrides the getItems method to attach additional metrics to the list.
  45.      *
  46.      * @return  mixed  An array of data items on success, false on failure.
  47.      *
  48.      * @since   1.6.1
  49.      */
  50.     public function getItems()
  51.     {
  52.         // Get a storage key.
  53.         $store $this->getStoreId('getItems');
  54.  
  55.         // Try to load the data from internal storage.
  56.         if (!empty($this->cache[$store]))
  57.         {
  58.             return $this->cache[$store];
  59.         }
  60.  
  61.         // Load the list items.
  62.         $items parent::getItems();
  63.  
  64.         // If emtpy or an error, just return.
  65.         if (empty($items))
  66.         {
  67.             return array();
  68.         }
  69.  
  70.         // Getting the following metric by joins is WAY TOO SLOW.
  71.         // Faster to do three queries for very large menu trees.
  72.  
  73.         // Get the menu types of menus in the list.
  74.         $db $this->getDbo();
  75.         $menuTypes JArrayHelper::getColumn($items'menutype');
  76.  
  77.         // Quote the strings.
  78.         $menuTypes implode(
  79.             ',',
  80.             array_map(array($db'quote')$menuTypes)
  81.         );
  82.  
  83.         // Get the published menu counts.
  84.         $query $db->getQuery(true)
  85.             ->select('m.menutype, COUNT(DISTINCT m.id) AS count_published')
  86.             ->from('#__menu AS m')
  87.             ->where('m.published = 1')
  88.             ->where('m.menutype IN (' $menuTypes ')')
  89.             ->group('m.menutype');
  90.  
  91.         $db->setQuery($query);
  92.  
  93.         try
  94.         {
  95.             $countPublished $db->loadAssocList('menutype''count_published');
  96.         }
  97.         catch (RuntimeException $e)
  98.         {
  99.             $this->setError($e->getMessage());
  100.             return false;
  101.         }
  102.  
  103.         // Get the unpublished menu counts.
  104.         $query->clear('where')
  105.             ->where('m.published = 0')
  106.             ->where('m.menutype IN (' $menuTypes ')');
  107.         $db->setQuery($query);
  108.  
  109.         try
  110.         {
  111.             $countUnpublished $db->loadAssocList('menutype''count_published');
  112.         }
  113.         catch (RuntimeException $e)
  114.         {
  115.             $this->setError($e->getMessage());
  116.             return false;
  117.         }
  118.  
  119.         // Get the trashed menu counts.
  120.         $query->clear('where')
  121.             ->where('m.published = -2')
  122.             ->where('m.menutype IN (' $menuTypes ')');
  123.         $db->setQuery($query);
  124.  
  125.         try
  126.         {
  127.             $countTrashed $db->loadAssocList('menutype''count_published');
  128.         }
  129.         catch (RuntimeException $e)
  130.         {
  131.             $this->setError($e->getMessage);
  132.             return false;
  133.         }
  134.  
  135.         // Inject the values back into the array.
  136.         foreach ($items as $item)
  137.         {
  138.             $item->count_published = isset($countPublished[$item->menutype]$countPublished[$item->menutype0;
  139.             $item->count_unpublished = isset($countUnpublished[$item->menutype]$countUnpublished[$item->menutype0;
  140.             $item->count_trashed = isset($countTrashed[$item->menutype]$countTrashed[$item->menutype0;
  141.         }
  142.  
  143.         // Add the items to the internal cache.
  144.         $this->cache[$store$items;
  145.  
  146.         return $this->cache[$store];
  147.     }
  148.  
  149.     /**
  150.      * Method to build an SQL query to load the list data.
  151.      *
  152.      * @return  string  An SQL query
  153.      *
  154.      * @since   1.6
  155.      */
  156.     protected function getListQuery()
  157.     {
  158.         // Create a new query object.
  159.         $db $this->getDbo();
  160.         $query $db->getQuery(true);
  161.  
  162.         // Select all fields from the table.
  163.         $query->select($this->getState('list.select''a.*'))
  164.             ->from($db->quoteName('#__menu_types'' AS a')
  165.  
  166.             ->group('a.id, a.menutype, a.title, a.description');
  167.  
  168.         // Filter by search in title or menutype
  169.         if ($search trim($this->getState('filter.search')))
  170.         {
  171.             $search $db->quote('%' $db->escape($searchtrue'%');
  172.             $query->where('(' 'a.title LIKE ' $search ' OR a.menutype LIKE ' $search ')');
  173.         }
  174.  
  175.         // Add the list ordering clause.
  176.         $query->order($db->escape($this->getState('list.ordering''a.id')) ' ' $db->escape($this->getState('list.direction''ASC')));
  177.  
  178.         return $query;
  179.     }
  180.  
  181.     /**
  182.      * Method to auto-populate the model state.
  183.      *
  184.      * Note. Calling getState in this method will result in recursion.
  185.      *
  186.      * @param   string  $ordering   An optional ordering field.
  187.      * @param   string  $direction  An optional direction (asc|desc).
  188.      *
  189.      * @return  void 
  190.      *
  191.      * @since   1.6
  192.      */
  193.     protected function populateState($ordering null$direction null)
  194.     {
  195.         $search $this->getUserStateFromRequest($this->context . '.search''filter_search');
  196.         $this->setState('filter.search'$search);
  197.  
  198.         // List state information.
  199.         parent::populateState('a.id''asc');
  200.     }
  201.  
  202.     /**
  203.      * Gets the extension id of the core mod_menu module.
  204.      *
  205.      * @return  integer 
  206.      *
  207.      * @since   2.5
  208.      */
  209.     public function getModMenuId()
  210.     {
  211.         $db $this->getDbo();
  212.         $query $db->getQuery(true)
  213.             ->select('e.extension_id')
  214.             ->from('#__extensions AS e')
  215.             ->where('e.type = ' $db->quote('module'))
  216.             ->where('e.element = ' $db->quote('mod_menu'))
  217.             ->where('e.client_id = 0');
  218.         $db->setQuery($query);
  219.  
  220.         return $db->loadResult();
  221.     }
  222.  
  223.     /**
  224.      * Gets a list of all mod_mainmenu modules and collates them by menutype
  225.      *
  226.      * @return  array 
  227.      */
  228.     public function &getModules()
  229.     {
  230.         $model JModelLegacy::getInstance('Menu''MenusModel'array('ignore_request' => true));
  231.         $result $model->getModules();
  232.  
  233.         return $result;
  234.     }
  235. }

Documentation generated on Tue, 19 Nov 2013 15:07:59 +0100 by phpDocumentor 1.4.3