[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/administrator/components/com_templates/models/ -> source.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1