/administrator/components/com_languages/models/override.php

https://bitbucket.org/kraymitchell/fcd
PHP | 201 lines | 102 code | 26 blank | 73 comment | 12 complexity | 345a8ba86dea7193263f5f10b186ac89 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_languages
  5. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // Check to ensure this file is included in Joomla!
  9. defined('_JEXEC') or die;
  10. jimport('joomla.application.component.modeladmin');
  11. /**
  12. * Languages Override Model
  13. *
  14. * @package Joomla.Administrator
  15. * @subpackage com_languages
  16. * @since 2.5
  17. */
  18. class LanguagesModelOverride extends JModelAdmin
  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. $client = $this->getState('filter.client', 'site');
  39. $language = $this->getState('filter.language', 'en-GB');
  40. $langName = JLanguage::getInstance($language)->getName();
  41. if (!$langName)
  42. {
  43. // If a language only exists in frontend, it's meta data cannot be
  44. // loaded in backend at the moment, so fall back to the language tag
  45. $langName = $language;
  46. }
  47. $form->setValue('client', null, JText::_('COM_LANGUAGES_VIEW_OVERRIDE_CLIENT_'.strtoupper($client)));
  48. $form->setValue('language', null, JText::sprintf('COM_LANGUAGES_VIEW_OVERRIDE_LANGUAGE', $langName, $language));
  49. $form->setValue('file', null, JPath::clean(constant('JPATH_'.strtoupper($client)) . '/language/overrides/' . $language . '.override.ini'));
  50. return $form;
  51. }
  52. /**
  53. * Method to get the data that should be injected in the form.
  54. *
  55. * @return mixed The data for the form
  56. *
  57. * @since 2.5
  58. */
  59. protected function loadFormData()
  60. {
  61. // Check the session for previously entered form data.
  62. $data = JFactory::getApplication()->getUserState('com_languages.edit.override.data', array());
  63. if (empty($data))
  64. {
  65. $data = $this->getItem();
  66. }
  67. return $data;
  68. }
  69. /**
  70. * Method to get a single record.
  71. *
  72. * @param string $pk The key name.
  73. *
  74. * @return mixed Object on success, false otherwise.
  75. *
  76. * @since 2.5
  77. */
  78. public function getItem($pk = null)
  79. {
  80. require_once JPATH_COMPONENT.'/helpers/languages.php';
  81. $pk = (!empty($pk)) ? $pk : JRequest::getCmd('id');
  82. $filename = constant('JPATH_'.strtoupper($this->getState('filter.client'))) . '/language/overrides/' . $this->getState('filter.language', 'en-GB').'.override.ini';
  83. $strings = LanguagesHelper::parseFile($filename);
  84. $result = new stdClass();
  85. $result->key = '';
  86. $result->override = '';
  87. if (isset($strings[$pk]))
  88. {
  89. $result->key = $pk;
  90. $result->override = $strings[$pk];
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Method to save the form data.
  96. *
  97. * @param array $data The form data.
  98. * @param boolean $opposite_client Indicates whether the override should not be created for the current client
  99. *
  100. * @return boolean True on success, false otherwise.
  101. *
  102. * @since 2.5
  103. */
  104. public function save($data, $opposite_client = false)
  105. {
  106. $app = JFactory::getApplication();
  107. require_once JPATH_COMPONENT.'/helpers/languages.php';
  108. $client = $app->getUserState('com_languages.overrides.filter.client', 0);
  109. $language = $app->getUserState('com_languages.overrides.filter.language', 'en-GB');
  110. // If the override should be created for both
  111. if($opposite_client)
  112. {
  113. $client = 1 - $client;
  114. }
  115. $client = $client ? 'administrator' : 'site';
  116. // Parse the override.ini file in oder to get the keys and strings
  117. $filename = constant('JPATH_'.strtoupper($client)) . '/language/overrides/' . $language . '.override.ini';
  118. $strings = LanguagesHelper::parseFile($filename);
  119. if (isset($strings[$data['id']]))
  120. {
  121. // If an existent string was edited check whether
  122. // the name of the constant is still the same
  123. if ($data['key'] == $data['id'])
  124. {
  125. // If yes, simply override it
  126. $strings[$data['key']] = $data['override'];
  127. }
  128. else
  129. {
  130. // If no, delete the old string and prepend the new one
  131. unset($strings[$data['id']]);
  132. $strings = array($data['key'] => $data['override']) + $strings;
  133. }
  134. }
  135. else
  136. {
  137. // If it is a new override simply prepend it
  138. $strings = array($data['key'] => $data['override']) + $strings;
  139. }
  140. foreach ($strings as $key => $string) {
  141. $strings[$key] = str_replace('"', '"_QQ_"', $string);
  142. }
  143. // Write override.ini file with the strings
  144. $registry = new JRegistry();
  145. $registry->loadObject($strings);
  146. if (!JFile::write($filename, $registry->toString('INI')))
  147. {
  148. return false;
  149. }
  150. // If the override should be stored for both clients save
  151. // it also for the other one and prevent endless recursion
  152. if(isset($data['both']) && $data['both'] && !$opposite_client)
  153. {
  154. return $this->save($data, true);
  155. }
  156. return true;
  157. }
  158. /**
  159. * Method to auto-populate the model state.
  160. *
  161. * Note. Calling getState in this method will result in recursion.
  162. *
  163. * @return void
  164. *
  165. * @since 2.5
  166. */
  167. protected function populateState()
  168. {
  169. $app = JFactory::getApplication();
  170. $client = $app->getUserStateFromRequest('com_languages.overrides.filter.client', 'filter_client', 0, 'int') ? 'administrator' : 'site';
  171. $this->setState('filter.client', $client);
  172. $language = $app->getUserStateFromRequest('com_languages.overrides.filter.language', 'filter_language', 'en-GB', 'cmd');
  173. $this->setState('filter.language', $language);
  174. }
  175. }