Source for file override.php

Documentation is available at override.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 Override Model
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_languages
  17.  * @since       2.5
  18.  */
  19. {
  20.     /**
  21.      * Method to get the record form.
  22.      *
  23.      * @param       array        $data            Data for the form.
  24.      * @param       boolean    $loadData    True if the form is to load its own data (default case), false if not.
  25.      *
  26.      * @return  mixed      A JForm object on success, false on failure
  27.      *
  28.      * @since        2.5
  29.      */
  30.     public function getForm($data array()$loadData true)
  31.     {
  32.         // Get the form
  33.         $form $this->loadForm('com_languages.override''override'array('control' => 'jform''load_data' => $loadData));
  34.         if (empty($form))
  35.         {
  36.             return false;
  37.         }
  38.  
  39.         $client        $this->getState('filter.client''site');
  40.         $language    $this->getState('filter.language''en-GB');
  41.         $langName    JLanguage::getInstance($language)->getName();
  42.         if (!$langName)
  43.         {
  44.             // If a language only exists in frontend, it's meta data cannot be
  45.             // loaded in backend at the moment, so fall back to the language tag
  46.             $langName $language;
  47.         }
  48.         $form->setValue('client'nullJText::_('COM_LANGUAGES_VIEW_OVERRIDE_CLIENT_'.strtoupper($client)));
  49.         $form->setValue('language'nullJText::sprintf('COM_LANGUAGES_VIEW_OVERRIDE_LANGUAGE'$langName$language));
  50.         $form->setValue('file'nullJPath::clean(constant('JPATH_'.strtoupper($client)) '/language/overrides/' $language '.override.ini'));
  51.  
  52.         return $form;
  53.     }
  54.  
  55.     /**
  56.      * Method to get the data that should be injected in the form.
  57.      *
  58.      * @return  mixed The data for the form
  59.      *
  60.      * @since        2.5
  61.      */
  62.     protected function loadFormData()
  63.     {
  64.         // Check the session for previously entered form data.
  65.         $data JFactory::getApplication()->getUserState('com_languages.edit.override.data'array());
  66.  
  67.         if (empty($data))
  68.         {
  69.             $data $this->getItem();
  70.         }
  71.  
  72.         $this->preprocessData('com_languages.override'$data);
  73.  
  74.         return $data;
  75.     }
  76.  
  77.     /**
  78.      * Method to get a single record.
  79.      *
  80.      * @param       string    $pk    The key name.
  81.      *
  82.      * @return  mixed      Object on success, false otherwise.
  83.      *
  84.      * @since        2.5
  85.      */
  86.     public function getItem($pk null)
  87.     {
  88.         require_once JPATH_COMPONENT.'/helpers/languages.php';
  89.  
  90.         $input JFactory::getApplication()->input;
  91.         $pk    (!empty($pk)) $pk $input->get('id');
  92.         $filename constant('JPATH_'.strtoupper($this->getState('filter.client'))) '/language/overrides/' $this->getState('filter.language''en-GB').'.override.ini';
  93.         $strings LanguagesHelper::parseFile($filename);
  94.  
  95.         $result new stdClass;
  96.         $result->key      '';
  97.         $result->override '';
  98.         if (isset($strings[$pk]))
  99.         {
  100.             $result->key      $pk;
  101.             $result->override $strings[$pk];
  102.         }
  103.  
  104.         return $result;
  105.     }
  106.  
  107.     /**
  108.      * Method to save the form data.
  109.      *
  110.      * @param       array        $data                            The form data.
  111.      * @param       boolean    $opposite_client    Indicates whether the override should not be created for the current client
  112.      *
  113.      * @return  boolean  True on success, false otherwise.
  114.      *
  115.      * @since        2.5
  116.      */
  117.     public function save($data$opposite_client false)
  118.     {
  119.         $app JFactory::getApplication();
  120.         require_once JPATH_COMPONENT.'/helpers/languages.php';
  121.         jimport('joomla.filesystem.file');
  122.  
  123.         $client        $app->getUserState('com_languages.overrides.filter.client'0);
  124.         $language    $app->getUserState('com_languages.overrides.filter.language''en-GB');
  125.  
  126.         // If the override should be created for both
  127.         if ($opposite_client)
  128.         {
  129.             $client $client;
  130.         }
  131.  
  132.         $client $client 'administrator' 'site';
  133.  
  134.         // Parse the override.ini file in oder to get the keys and strings
  135.         $filename    constant('JPATH_'.strtoupper($client)) '/language/overrides/' $language '.override.ini';
  136.         $strings    LanguagesHelper::parseFile($filename);
  137.  
  138.         if (isset($strings[$data['id']]))
  139.         {
  140.             // If an existent string was edited check whether
  141.             // the name of the constant is still the same
  142.             if ($data['key'== $data['id'])
  143.             {
  144.                 // If yes, simply override it
  145.                 $strings[$data['key']] $data['override'];
  146.             }
  147.             else
  148.             {
  149.                 // If no, delete the old string and prepend the new one
  150.                 unset($strings[$data['id']]);
  151.                 $strings array($data['key'=> $data['override']$strings;
  152.             }
  153.         }
  154.         else
  155.         {
  156.             // If it is a new override simply prepend it
  157.             $strings array($data['key'=> $data['override']$strings;
  158.         }
  159.  
  160.         foreach ($strings as $key => $string)
  161.         {
  162.             $strings[$keystr_replace('"''"_QQ_"'$string);
  163.         }
  164.  
  165.         // Write override.ini file with the strings
  166.         $registry new JRegistry;
  167.         $registry->loadObject($strings);
  168.         $reg $registry->toString('INI');
  169.  
  170.         if (!JFile::write($filename$reg))
  171.         {
  172.             return false;
  173.         }
  174.  
  175.         // If the override should be stored for both clients save
  176.         // it also for the other one and prevent endless recursion
  177.         if (isset($data['both']&& $data['both'&& !$opposite_client)
  178.         {
  179.             return $this->save($datatrue);
  180.         }
  181.  
  182.         return true;
  183.     }
  184.  
  185.     /**
  186.      * Method to auto-populate the model state.
  187.      *
  188.      * Note. Calling getState in this method will result in recursion.
  189.      *
  190.      * @return  void 
  191.      *
  192.      * @since        2.5
  193.      */
  194.     protected function populateState()
  195.     {
  196.         $app JFactory::getApplication();
  197.  
  198.         $client $app->getUserStateFromRequest('com_languages.overrides.filter.client''filter_client'0'int''administrator' 'site';
  199.         $this->setState('filter.client'$client);
  200.  
  201.         $language $app->getUserStateFromRequest('com_languages.overrides.filter.language''filter_language''en-GB''cmd');
  202.         $this->setState('filter.language'$language);
  203.     }
  204. }

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