Source for file modules.php

Documentation is available at modules.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_modules
  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.  * Modules Component Module Model
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_modules
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * Constructor.
  22.      *
  23.      * @param   array  An optional associative array of configuration settings.
  24.      * @see     JController
  25.      * @since   1.6
  26.      */
  27.     public function __construct($config array())
  28.     {
  29.         if (empty($config['filter_fields']))
  30.         {
  31.             $config['filter_fields'array(
  32.                 'id''a.id',
  33.                 'title''a.title',
  34.                 'checked_out''a.checked_out',
  35.                 'checked_out_time''a.checked_out_time',
  36.                 'published''a.published',
  37.                 'access''a.access''access_level',
  38.                 'ordering''a.ordering',
  39.                 'module''a.module',
  40.                 'language''a.language''language_title',
  41.                 'publish_up''a.publish_up',
  42.                 'publish_down''a.publish_down',
  43.                 'client_id''a.client_id',
  44.                 'position''a.position',
  45.                 'pages',
  46.                 'name''e.name',
  47.             );
  48.         }
  49.  
  50.         parent::__construct($config);
  51.     }
  52.  
  53.     /**
  54.      * Method to auto-populate the model state.
  55.      *
  56.      * Note. Calling getState in this method will result in recursion.
  57.      *
  58.      * @since   1.6
  59.      */
  60.     protected function populateState($ordering null$direction null)
  61.     {
  62.         $app JFactory::getApplication('administrator');
  63.  
  64.         // Load the filter state.
  65.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  66.         $this->setState('filter.search'$search);
  67.  
  68.         $accessId $this->getUserStateFromRequest($this->context . '.filter.access''filter_access'null'int');
  69.         $this->setState('filter.access'$accessId);
  70.  
  71.         $state $this->getUserStateFromRequest($this->context . '.filter.state''filter_state''''string');
  72.         $this->setState('filter.state'$state);
  73.  
  74.         $position $this->getUserStateFromRequest($this->context . '.filter.position''filter_position''''string');
  75.         $this->setState('filter.position'$position);
  76.  
  77.         $module $this->getUserStateFromRequest($this->context . '.filter.module''filter_module''''string');
  78.         $this->setState('filter.module'$module);
  79.  
  80.         $clientId $this->getUserStateFromRequest($this->context . '.filter.client_id''filter_client_id'0'int'false);
  81.         $previousId $app->getUserState($this->context . '.filter.client_id_previous'null);
  82.         if ($previousId != $clientId || $previousId === null)
  83.         {
  84.             $this->getUserStateFromRequest($this->context . '.filter.client_id_previous''filter_client_id_previous'0'int'true);
  85.             $app->setUserState($this->context . '.filter.client_id_previous'$clientId);
  86.         }
  87.         $this->setState('filter.client_id'$clientId);
  88.  
  89.         $language $this->getUserStateFromRequest($this->context . '.filter.language''filter_language''');
  90.         $this->setState('filter.language'$language);
  91.  
  92.         // Load the parameters.
  93.         $params JComponentHelper::getParams('com_modules');
  94.         $this->setState('params'$params);
  95.  
  96.         // List state information.
  97.         parent::populateState('position''asc');
  98.     }
  99.  
  100.     /**
  101.      * Method to get a store id based on model configuration state.
  102.      *
  103.      * This is necessary because the model is used by the component and
  104.      * different modules that might need different sets of data or different
  105.      * ordering requirements.
  106.      *
  107.      * @param   string    A prefix for the store id.
  108.      *
  109.      * @return  string    A store id.
  110.      */
  111.     protected function getStoreId($id '')
  112.     {
  113.         // Compile the store id.
  114.         $id .= ':' $this->getState('filter.search');
  115.         $id .= ':' $this->getState('filter.access');
  116.         $id .= ':' $this->getState('filter.state');
  117.         $id .= ':' $this->getState('filter.position');
  118.         $id .= ':' $this->getState('filter.module');
  119.         $id .= ':' $this->getState('filter.client_id');
  120.         $id .= ':' $this->getState('filter.language');
  121.  
  122.         return parent::getStoreId($id);
  123.     }
  124.  
  125.     /**
  126.      * Returns an object list
  127.      *
  128.      * @param   string The query
  129.      * @param   int    Offset
  130.      * @param   int    The number of records
  131.      * @return  array 
  132.      */
  133.     protected function _getList($query$limitstart 0$limit 0)
  134.     {
  135.         $ordering $this->getState('list.ordering''ordering');
  136.         if (in_array($orderingarray('pages''name')))
  137.         {
  138.             $this->_db->setQuery($query);
  139.             $result $this->_db->loadObjectList();
  140.             $this->translate($result);
  141.             JArrayHelper::sortObjects($result$ordering$this->getState('list.direction'== 'desc' ? -1truetrue);
  142.             $total count($result);
  143.             $this->cache[$this->getStoreId('getTotal')$total;
  144.             if ($total $limitstart)
  145.             {
  146.                 $limitstart 0;
  147.                 $this->setState('list.start'0);
  148.             }
  149.             return array_slice($result$limitstart$limit $limit null);
  150.         }
  151.         else
  152.         {
  153.             if ($ordering == 'ordering')
  154.             {
  155.                 $query->order('a.position ASC');
  156.                 $ordering 'a.ordering';
  157.             }
  158.             if ($ordering == 'language_title')
  159.             {
  160.                 $ordering 'l.title';
  161.             }
  162.             $query->order($this->_db->quoteName($ordering' ' $this->getState('list.direction'));
  163.             if ($ordering == 'position')
  164.             {
  165.                 $query->order('a.ordering ASC');
  166.             }
  167.             $result parent::_getList($query$limitstart$limit);
  168.             $this->translate($result);
  169.             return $result;
  170.         }
  171.     }
  172.  
  173.     /**
  174.      * Translate a list of objects
  175.      *
  176.      * @param   array The array of objects
  177.      * @return  array The array of translated objects
  178.      */
  179.     protected function translate(&$items)
  180.     {
  181.         $lang JFactory::getLanguage();
  182.         $client $this->getState('filter.client_id''administrator' 'site';
  183.  
  184.         foreach ($items as $item)
  185.         {
  186.             $extension $item->module;
  187.             $source constant('JPATH_' strtoupper($client)) "/modules/$extension";
  188.             $lang->load("$extension.sys"constant('JPATH_' strtoupper($client))nullfalsetrue)
  189.                 || $lang->load("$extension.sys"$sourcenullfalsetrue);
  190.             $item->name JText::_($item->name);
  191.             if (is_null($item->pages))
  192.             {
  193.                 $item->pages JText::_('JNONE');
  194.             }
  195.             elseif ($item->pages 0)
  196.             {
  197.                 $item->pages JText::_('COM_MODULES_ASSIGNED_VARIES_EXCEPT');
  198.             }
  199.             elseif ($item->pages 0)
  200.             {
  201.                 $item->pages JText::_('COM_MODULES_ASSIGNED_VARIES_ONLY');
  202.             }
  203.             else
  204.             {
  205.                 $item->pages JText::_('JALL');
  206.             }
  207.         }
  208.     }
  209.  
  210.     /**
  211.      * Build an SQL query to load the list data.
  212.      *
  213.      * @return  JDatabaseQuery 
  214.      */
  215.     protected function getListQuery()
  216.     {
  217.         // Create a new query object.
  218.         $db $this->getDbo();
  219.         $query $db->getQuery(true);
  220.  
  221.         // Select the required fields from the table.
  222.         $query->select(
  223.             $this->getState(
  224.                 'list.select',
  225.                 'a.id, a.title, a.note, a.position, a.module, a.language,' .
  226.                     'a.checked_out, a.checked_out_time, a.published+2*(e.enabled-1) as published, a.access, a.ordering, a.publish_up, a.publish_down'
  227.             )
  228.         );
  229.         $query->from($db->quoteName('#__modules'' AS a');
  230.  
  231.         // Join over the language
  232.         $query->select('l.title AS language_title')
  233.             ->join('LEFT'$db->quoteName('#__languages'' AS l ON l.lang_code = a.language');
  234.  
  235.         // Join over the users for the checked out user.
  236.         $query->select('uc.name AS editor')
  237.             ->join('LEFT''#__users AS uc ON uc.id=a.checked_out');
  238.  
  239.         // Join over the asset groups.
  240.         $query->select('ag.title AS access_level')
  241.             ->join('LEFT''#__viewlevels AS ag ON ag.id = a.access');
  242.  
  243.         // Join over the module menus
  244.         $query->select('MIN(mm.menuid) AS pages')
  245.             ->join('LEFT''#__modules_menu AS mm ON mm.moduleid = a.id');
  246.  
  247.         // Join over the extensions
  248.         $query->select('e.name AS name')
  249.             ->join('LEFT''#__extensions AS e ON e.element = a.module')
  250.             ->group(
  251.                 'a.id, a.title, a.note, a.position, a.module, a.language,a.checked_out,' .
  252.                     'a.checked_out_time, a.published, a.access, a.ordering,l.title, uc.name, ag.title, e.name,' .
  253.                     'l.lang_code, uc.id, ag.id, mm.moduleid, e.element, a.publish_up, a.publish_down,e.enabled'
  254.             );
  255.  
  256.         // Filter by access level.
  257.         if ($access $this->getState('filter.access'))
  258.         {
  259.             $query->where('a.access = ' . (int) $access);
  260.         }
  261.  
  262.         // Filter by published state
  263.         $state $this->getState('filter.state');
  264.         if (is_numeric($state))
  265.         {
  266.             $query->where('a.published = ' . (int) $state);
  267.         }
  268.         elseif ($state === '')
  269.         {
  270.             $query->where('(a.published IN (0, 1))');
  271.         }
  272.  
  273.         // Filter by position
  274.         $position $this->getState('filter.position');
  275.         if ($position && $position != 'none')
  276.         {
  277.             $query->where('a.position = ' $db->quote($position));
  278.         }
  279.  
  280.         elseif ($position == 'none')
  281.         {
  282.             $query->where('a.position = ' $db->quote(''));
  283.         }
  284.  
  285.         // Filter by module
  286.         $module $this->getState('filter.module');
  287.         if ($module)
  288.         {
  289.             $query->where('a.module = ' $db->quote($module));
  290.         }
  291.  
  292.         // Filter by client.
  293.         $clientId $this->getState('filter.client_id');
  294.         if (is_numeric($clientId))
  295.         {
  296.             $query->where('a.client_id = ' . (int) $clientId ' AND e.client_id =' . (int) $clientId);
  297.         }
  298.  
  299.         // Filter by search in title
  300.         $search $this->getState('filter.search');
  301.         if (!empty($search))
  302.         {
  303.             if (stripos($search'id:'=== 0)
  304.             {
  305.                 $query->where('a.id = ' . (int) substr($search3));
  306.             }
  307.             else
  308.             {
  309.                 $search $db->quote('%' $db->escape($searchtrue'%');
  310.                 $query->where('(' 'a.title LIKE ' $search ' OR a.note LIKE ' $search ')');
  311.             }
  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.         //echo nl2br(str_replace('#__','jos_',$query));
  321.         return $query;
  322.     }
  323. }

Documentation generated on Tue, 19 Nov 2013 15:08:52 +0100 by phpDocumentor 1.4.3