PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_templates/models/source.php

https://bitbucket.org/gnomeontherun/square-one
PHP | 240 lines | 127 code | 37 blank | 76 comment | 19 complexity | 84fbd04a1dbd730fa97ce5c5efbf9616 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Administrator
  5. * @subpackage com_templates
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.modelform');
  12. /**
  13. * @package Joomla.Administrator
  14. * @subpackage com_templates
  15. * @since 1.5
  16. */
  17. class TemplatesModelSource extends JModelForm
  18. {
  19. /**
  20. * Cache for the template information.
  21. *
  22. * @var object
  23. */
  24. private $_template = null;
  25. /**
  26. * Method to auto-populate the model state.
  27. *
  28. * Note. Calling getState in this method will result in recursion.
  29. *
  30. * @since 1.6
  31. */
  32. protected function populateState()
  33. {
  34. jimport('joomla.filesystem.file');
  35. $app = JFactory::getApplication('administrator');
  36. // Load the User state.
  37. $id = $app->getUserState('com_templates.edit.source.id');
  38. // Parse the template id out of the compound reference.
  39. $temp = explode(':', base64_decode($id));
  40. $this->setState('extension.id', (int) array_shift($temp));
  41. $fileName = array_shift($temp);
  42. $this->setState('filename', $fileName);
  43. // Save the syntax for later use
  44. $app->setUserState('editor.source.syntax', JFile::getExt($fileName));
  45. // Load the parameters.
  46. $params = JComponentHelper::getParams('com_templates');
  47. $this->setState('params', $params);
  48. }
  49. /**
  50. * Method to get the record form.
  51. *
  52. * @param array $data Data for the form.
  53. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  54. * @return JForm A JForm object on success, false on failure
  55. * @since 1.6
  56. */
  57. public function getForm($data = array(), $loadData = true)
  58. {
  59. // Initialise variables.
  60. $app = JFactory::getApplication();
  61. // Codemirror or Editor None should be enabled
  62. $db = JFactory::getDBO();
  63. $query = $db->getQuery(true);
  64. $query->select('COUNT(*)');
  65. $query->from('#__extensions as a');
  66. $query->where('(a.name ='.$db->quote('plg_editors_codemirror').' AND a.enabled = 1) OR (a.name ='.$db->quote('plg_editors_none').' AND a.enabled = 1)');
  67. $db->setQuery($query);
  68. $state = $db->loadResult();
  69. if ((int)$state < 1 ) {
  70. $app->enqueueMessage(JText::_('COM_TEMPLATES_ERROR_EDITOR_DISABLED'), 'warning');
  71. }
  72. // Get the form.
  73. $form = $this->loadForm('com_templates.source', 'source', array('control' => 'jform', 'load_data' => $loadData));
  74. if (empty($form)) {
  75. return false;
  76. }
  77. return $form;
  78. }
  79. /**
  80. * Method to get the data that should be injected in the form.
  81. *
  82. * @return mixed The data for the form.
  83. * @since 1.6
  84. */
  85. protected function loadFormData()
  86. {
  87. // Check the session for previously entered form data.
  88. $data = JFactory::getApplication()->getUserState('com_templates.edit.source.data', array());
  89. if (empty($data)) {
  90. $data = $this->getSource();
  91. }
  92. return $data;
  93. }
  94. /**
  95. * Method to get a single record.
  96. *
  97. * @return mixed Object on success, false on failure.
  98. * @since 1.6
  99. */
  100. public function &getSource()
  101. {
  102. $item = new stdClass;
  103. if (!$this->_template) {
  104. $this->getTemplate();
  105. }
  106. if ($this->_template) {
  107. $fileName = $this->getState('filename');
  108. $client = JApplicationHelper::getClientInfo($this->_template->client_id);
  109. $filePath = JPath::clean($client->path.'/templates/'.$this->_template->element.'/'.$fileName);
  110. if (file_exists($filePath)) {
  111. jimport('joomla.filesystem.file');
  112. $item->extension_id = $this->getState('extension.id');
  113. $item->filename = $this->getState('filename');
  114. $item->source = JFile::read($filePath);
  115. } else {
  116. $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_FOUND'));
  117. }
  118. }
  119. return $item;
  120. }
  121. /**
  122. * Method to get the template information.
  123. *
  124. * @return mixed Object if successful, false if not and internal error is set.
  125. * @since 1.6
  126. */
  127. public function &getTemplate()
  128. {
  129. // Initialise variables.
  130. $pk = $this->getState('extension.id');
  131. $db = $this->getDbo();
  132. $result = false;
  133. // Get the template information.
  134. $db->setQuery(
  135. 'SELECT extension_id, client_id, element' .
  136. ' FROM #__extensions' .
  137. ' WHERE extension_id = '.(int) $pk.
  138. ' AND type = '.$db->quote('template')
  139. );
  140. $result = $db->loadObject();
  141. if (empty($result)) {
  142. if ($error = $db->getErrorMsg()) {
  143. $this->setError($error);
  144. }
  145. else {
  146. $this->setError(JText::_('COM_TEMPLATES_ERROR_EXTENSION_RECORD_NOT_FOUND'));
  147. }
  148. $this->_template = false;
  149. } else {
  150. $this->_template = $result;
  151. }
  152. return $this->_template;
  153. }
  154. /**
  155. * Method to store the source file contents.
  156. *
  157. * @param array The souce data to save.
  158. *
  159. * @return boolean True on success, false otherwise and internal error set.
  160. * @since 1.6
  161. */
  162. public function save($data)
  163. {
  164. jimport('joomla.filesystem.file');
  165. // Get the template.
  166. $template = $this->getTemplate();
  167. if (empty($template)) {
  168. return false;
  169. }
  170. $dispatcher = JDispatcher::getInstance();
  171. $fileName = $this->getState('filename');
  172. $client = JApplicationHelper::getClientInfo($template->client_id);
  173. $filePath = JPath::clean($client->path.'/templates/'.$template->element.'/'.$fileName);
  174. // Include the extension plugins for the save events.
  175. JPluginHelper::importPlugin('extension');
  176. // Set FTP credentials, if given.
  177. JClientHelper::setCredentialsFromRequest('ftp');
  178. $ftp = JClientHelper::getCredentials('ftp');
  179. // Try to make the template file writeable.
  180. if (!$ftp['enabled'] && JPath::isOwner($filePath) && !JPath::setPermissions($filePath, '0644')) {
  181. $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_WRITABLE'));
  182. return false;
  183. }
  184. // Trigger the onExtensionBeforeSave event.
  185. $result = $dispatcher->trigger('onExtensionBeforeSave', array('com_templates.source', &$data, false));
  186. if (in_array(false, $result, true)) {
  187. $this->setError($table->getError());
  188. return false;
  189. }
  190. $return = JFile::write($filePath, $data['source']);
  191. // Try to make the template file unwriteable.
  192. if (!$ftp['enabled'] && JPath::isOwner($filePath) && !JPath::setPermissions($filePath, '0444')) {
  193. $this->setError(JText::_('COM_TEMPLATES_ERROR_SOURCE_FILE_NOT_UNWRITABLE'));
  194. return false;
  195. } elseif (!$return) {
  196. $this->setError(JText::sprintf('COM_TEMPLATES_ERROR_FAILED_TO_SAVE_FILENAME', $fileName));
  197. return false;
  198. }
  199. // Trigger the onExtensionAfterSave event.
  200. $dispatcher->trigger('onExtensionAfterSave', array('com_templates.source', &$table, false));
  201. return true;
  202. }
  203. }