Source for file overrides.php

Documentation is available at overrides.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_languages
  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.  * Languages Overrides Model
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_languages
  17.  * @since       2.5
  18.  */
  19. {
  20.     /**
  21.      * Constructor
  22.      *
  23.      * @param       array    An optional associative array of configuration settings
  24.      *
  25.      * @return  void 
  26.      *
  27.      * @since        2.5
  28.      */
  29.     public function __construct($config array())
  30.     {
  31.         parent::__construct($config);
  32.  
  33.         $this->filter_fields = array('key''text');
  34.     }
  35.  
  36.     /**
  37.      * Retrieves the overrides data
  38.      *
  39.      * @param   boolean  $all  True if all overrides shall be returned without considering pagination, defaults to false
  40.      *
  41.      * @return  array  Array of objects containing the overrides of the override.ini file
  42.      *
  43.      * @since   2.5
  44.      */
  45.     public function getOverrides($all false)
  46.     {
  47.         // Get a storage key
  48.         $store $this->getStoreId();
  49.  
  50.         // Try to load the data from internal storage
  51.         if (!empty($this->cache[$store]))
  52.         {
  53.             return $this->cache[$store];
  54.         }
  55.  
  56.         $client in_array($this->state->get('filter.client')array(0'site')) strtoupper('site'strtoupper('administrator');
  57.  
  58.         // Parse the override.ini file in order to get the keys and strings
  59.         $filename constant('JPATH_' $client'/language/overrides/' $this->getState('filter.language''.override.ini';
  60.         $strings LanguagesHelper::parseFile($filename);
  61.  
  62.         // Filter the loaded strings according to the search box
  63.         $search $this->getState('filter.search');
  64.  
  65.         if ($search != '')
  66.         {
  67.             $search preg_quote($search'~');
  68.             $matchvals preg_grep('~' $search '~i'$strings);
  69.             $matchkeys array_intersect_key($stringsarray_flip(preg_grep('~' $search '~i',  array_keys($strings))));
  70.             $strings array_merge($matchvals$matchkeys);
  71.         }
  72.  
  73.         // Consider the odering
  74.         if ($this->getState('list.ordering'== 'text')
  75.         {
  76.             if (strtoupper($this->getState('list.direction')) == 'DESC')
  77.             {
  78.                 arsort($strings);
  79.             }
  80.             else
  81.             {
  82.                 asort($strings);
  83.             }
  84.         }
  85.         else
  86.         {
  87.             if (strtoupper($this->getState('list.direction')) == 'DESC')
  88.             {
  89.                 krsort($strings);
  90.             }
  91.             else
  92.             {
  93.                 ksort($strings);
  94.             }
  95.         }
  96.  
  97.         // Consider the pagination
  98.         if (!$all && $this->getState('list.limit'&& $this->getTotal($this->getState('list.limit'))
  99.         {
  100.             $strings array_slice($strings$this->getStart()$this->getState('list.limit')true);
  101.         }
  102.  
  103.         // Add the items to the internal cache
  104.         $this->cache[$store$strings;
  105.  
  106.         return $this->cache[$store];
  107.     }
  108.  
  109.     /**
  110.      * Method to get the total number of overrides
  111.      *
  112.      * @return  int    The total number of overrides
  113.      *
  114.      * @since        2.5
  115.      */
  116.     public function getTotal()
  117.     {
  118.         // Get a storage key
  119.         $store $this->getStoreId('getTotal');
  120.  
  121.         // Try to load the data from internal storage
  122.         if (!empty($this->cache[$store]))
  123.         {
  124.             return $this->cache[$store];
  125.         }
  126.  
  127.         // Add the total to the internal cache
  128.         $this->cache[$storecount($this->getOverrides(true));
  129.  
  130.         return $this->cache[$store];
  131.     }
  132.  
  133.     /**
  134.      * Method to auto-populate the model state.
  135.      *
  136.      * Note. Calling getState in this method will result in recursion.
  137.      *
  138.      * @param   string  $ordering   An optional ordering field.
  139.      * @param   string  $direction  An optional direction (asc|desc).
  140.      *
  141.      * @return  void 
  142.      *
  143.      * @since   2.5
  144.      */
  145.     protected function populateState($ordering null$direction null)
  146.     {
  147.         $app JFactory::getApplication();
  148.  
  149.         // Use default language of frontend for default filter
  150.         $default JComponentHelper::getParams('com_languages')->get('site').'0';
  151.  
  152.         $old_language_client $app->getUserState('com_languages.overrides.filter.language_client''');
  153.         $language_client     $this->getUserStateFromRequest('com_languages.overrides.filter.language_client''filter_language_client'$default'cmd');
  154.  
  155.         if ($old_language_client != $language_client)
  156.         {
  157.             $client   substr($language_client-1);
  158.             $language substr($language_client0-1);
  159.         }
  160.         else
  161.         {
  162.             $client   $app->getUserState('com_languages.overrides.filter.client'0);
  163.             $language $app->getUserState('com_languages.overrides.filter.language''en-GB');
  164.         }
  165.  
  166.         // Sets the search filter
  167.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  168.         $this->setState('filter.search'$search);
  169.  
  170.         $this->setState('filter.language_client'$language.$client);
  171.         $this->setState('filter.client'$client 'administrator' 'site');
  172.         $this->setState('filter.language'$language);
  173.  
  174.         // Add filters to the session because they won't be stored there by 'getUserStateFromRequest' if they aren't in the current request
  175.         $app->setUserState('com_languages.overrides.filter.client'$client);
  176.         $app->setUserState('com_languages.overrides.filter.language'$language);
  177.  
  178.         // List state information
  179.         parent::populateState('key''asc');
  180.     }
  181.  
  182.     /**
  183.      * Method to get all found languages of frontend and backend.
  184.      *
  185.      * The resulting array has entries of the following style:
  186.      * <Language Tag>0|1 => <Language Name> - <Client Name>
  187.      *
  188.      * @return  array  Sorted associative array of languages
  189.      *
  190.      * @since        2.5
  191.      */
  192.     public function getLanguages()
  193.     {
  194.         // Try to load the data from internal storage
  195.         if (!empty($this->cache['languages']))
  196.         {
  197.             return $this->cache['languages'];
  198.         }
  199.  
  200.         // Get all languages of frontend and backend
  201.         $languages                 array();
  202.         $site_languages     JLanguage::getKnownLanguages(JPATH_SITE);
  203.         $admin_languages    JLanguage::getKnownLanguages(JPATH_ADMINISTRATOR);
  204.  
  205.         // Create a single array of them
  206.         foreach ($site_languages as $tag => $language)
  207.         {
  208.             $languages[$tag.'0'JText::sprintf('COM_LANGUAGES_VIEW_OVERRIDES_LANGUAGES_BOX_ITEM'$language['name']JText::_('JSITE'));
  209.         }
  210.         foreach ($admin_languages as $tag => $language)
  211.         {
  212.             $languages[$tag.'1'JText::sprintf('COM_LANGUAGES_VIEW_OVERRIDES_LANGUAGES_BOX_ITEM'$language['name']JText::_('JADMINISTRATOR'));
  213.         }
  214.  
  215.         // Sort it by language tag and by client after that
  216.         ksort($languages);
  217.  
  218.         // Add the languages to the internal cache
  219.         $this->cache['languages'$languages;
  220.  
  221.         return $this->cache['languages'];
  222.     }
  223.  
  224.     /**
  225.      * Method to delete one or more overrides
  226.      *
  227.      * @param       array        Array of keys to delete
  228.      *
  229.      * @return  integer       Number of successfully deleted overrides, boolean false if an error occured
  230.      *
  231.      * @since        2.5
  232.      */
  233.     public function delete($cids)
  234.     {
  235.         // Check permissions first
  236.         if (!JFactory::getUser()->authorise('core.delete''com_languages'))
  237.         {
  238.             $this->setError(JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'));
  239.  
  240.             return false;
  241.         }
  242.  
  243.         jimport('joomla.filesystem.file');
  244.         require_once JPATH_COMPONENT.'/helpers/languages.php';
  245.  
  246.         $client in_array($this->state->get('filter.client')array(0'site')) strtoupper('site'strtoupper('administrator');
  247.  
  248.         // Parse the override.ini file in oder to get the keys and strings
  249.         $filename constant('JPATH_' $client'/language/overrides/' $this->getState('filter.language''.override.ini';
  250.         $strings LanguagesHelper::parseFile($filename);
  251.  
  252.         // Unset strings that shall be deleted
  253.         foreach ($cids as $key)
  254.         {
  255.             if (isset($strings[$key]))
  256.             {
  257.                 unset($strings[$key]);
  258.             }
  259.         }
  260.  
  261.         foreach ($strings as $key => $string)
  262.         {
  263.             $strings[$keystr_replace('"''"_QQ_"'$string);
  264.         }
  265.  
  266.         // Write override.ini file with the left strings
  267.         $registry new JRegistry;
  268.         $registry->loadObject($strings);
  269.         $reg $registry->toString('INI');
  270.  
  271.         $filename constant('JPATH_' $client'/language/overrides/' $this->getState('filter.language''.override.ini';
  272.  
  273.         if (!JFile::write($filename$reg))
  274.         {
  275.             return false;
  276.         }
  277.  
  278.         $this->cleanCache();
  279.  
  280.         return count($cids);
  281.     }
  282. }

Documentation generated on Tue, 19 Nov 2013 15:09:58 +0100 by phpDocumentor 1.4.3