[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_content/src/Model/ -> FormModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_content
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Content\Site\Model;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Helper\TagsHelper;
  16  use Joomla\CMS\Language\Associations;
  17  use Joomla\CMS\Language\Multilanguage;
  18  use Joomla\CMS\Object\CMSObject;
  19  use Joomla\CMS\Table\Table;
  20  use Joomla\Database\ParameterType;
  21  use Joomla\Registry\Registry;
  22  use Joomla\Utilities\ArrayHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Content Component Article Model
  30   *
  31   * @since  1.5
  32   */
  33  class FormModel extends \Joomla\Component\Content\Administrator\Model\ArticleModel
  34  {
  35      /**
  36       * Model typeAlias string. Used for version history.
  37       *
  38       * @var        string
  39       */
  40      public $typeAlias = 'com_content.article';
  41  
  42      /**
  43       * Method to auto-populate the model state.
  44       *
  45       * Note. Calling getState in this method will result in recursion.
  46       *
  47       * @return  void
  48       *
  49       * @since   1.6
  50       */
  51      protected function populateState()
  52      {
  53          $app = Factory::getApplication();
  54  
  55          // Load the parameters.
  56          $params = $app->getParams();
  57          $this->setState('params', $params);
  58  
  59          if ($params && $params->get('enable_category') == 1 && $params->get('catid')) {
  60              $catId = $params->get('catid');
  61          } else {
  62              $catId = 0;
  63          }
  64  
  65          // Load state from the request.
  66          $pk = $app->input->getInt('a_id');
  67          $this->setState('article.id', $pk);
  68  
  69          $this->setState('article.catid', $app->input->getInt('catid', $catId));
  70  
  71          $return = $app->input->get('return', '', 'base64');
  72          $this->setState('return_page', base64_decode($return));
  73  
  74          $this->setState('layout', $app->input->getString('layout'));
  75      }
  76  
  77      /**
  78       * Method to get article data.
  79       *
  80       * @param   integer  $itemId  The id of the article.
  81       *
  82       * @return  mixed  Content item data object on success, false on failure.
  83       */
  84      public function getItem($itemId = null)
  85      {
  86          $itemId = (int) (!empty($itemId)) ? $itemId : $this->getState('article.id');
  87  
  88          // Get a row instance.
  89          $table = $this->getTable();
  90  
  91          // Attempt to load the row.
  92          $return = $table->load($itemId);
  93  
  94          // Check for a table object error.
  95          if ($return === false && $table->getError()) {
  96              $this->setError($table->getError());
  97  
  98              return false;
  99          }
 100  
 101          $properties = $table->getProperties(1);
 102          $value = ArrayHelper::toObject($properties, CMSObject::class);
 103  
 104          // Convert attrib field to Registry.
 105          $value->params = new Registry($value->attribs);
 106  
 107          // Compute selected asset permissions.
 108          $user   = Factory::getUser();
 109          $userId = $user->get('id');
 110          $asset  = 'com_content.article.' . $value->id;
 111  
 112          // Check general edit permission first.
 113          if ($user->authorise('core.edit', $asset)) {
 114              $value->params->set('access-edit', true);
 115          } elseif (!empty($userId) && $user->authorise('core.edit.own', $asset)) {
 116              // Now check if edit.own is available.
 117              // Check for a valid user and that they are the owner.
 118              if ($userId == $value->created_by) {
 119                  $value->params->set('access-edit', true);
 120              }
 121          }
 122  
 123          // Check edit state permission.
 124          if ($itemId) {
 125              // Existing item
 126              $value->params->set('access-change', $user->authorise('core.edit.state', $asset));
 127          } else {
 128              // New item.
 129              $catId = (int) $this->getState('article.catid');
 130  
 131              if ($catId) {
 132                  $value->params->set('access-change', $user->authorise('core.edit.state', 'com_content.category.' . $catId));
 133                  $value->catid = $catId;
 134              } else {
 135                  $value->params->set('access-change', $user->authorise('core.edit.state', 'com_content'));
 136              }
 137          }
 138  
 139          $value->articletext = $value->introtext;
 140  
 141          if (!empty($value->fulltext)) {
 142              $value->articletext .= '<hr id="system-readmore">' . $value->fulltext;
 143          }
 144  
 145          // Convert the metadata field to an array.
 146          $registry = new Registry($value->metadata);
 147          $value->metadata = $registry->toArray();
 148  
 149          if ($itemId) {
 150              $value->tags = new TagsHelper();
 151              $value->tags->getTagIds($value->id, 'com_content.article');
 152              $value->metadata['tags'] = $value->tags;
 153          }
 154  
 155          return $value;
 156      }
 157  
 158      /**
 159       * Get the return URL.
 160       *
 161       * @return  string  The return URL.
 162       *
 163       * @since   1.6
 164       */
 165      public function getReturnPage()
 166      {
 167          return base64_encode($this->getState('return_page', ''));
 168      }
 169  
 170      /**
 171       * Method to save the form data.
 172       *
 173       * @param   array  $data  The form data.
 174       *
 175       * @return  boolean  True on success.
 176       *
 177       * @since   3.2
 178       */
 179      public function save($data)
 180      {
 181          // Associations are not edited in frontend ATM so we have to inherit them
 182          if (
 183              Associations::isEnabled() && !empty($data['id'])
 184              && $associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $data['id'])
 185          ) {
 186              foreach ($associations as $tag => $associated) {
 187                  $associations[$tag] = (int) $associated->id;
 188              }
 189  
 190              $data['associations'] = $associations;
 191          }
 192  
 193          if (!Multilanguage::isEnabled()) {
 194              $data['language'] = '*';
 195          }
 196  
 197          return parent::save($data);
 198      }
 199  
 200      /**
 201       * Method to get the record form.
 202       *
 203       * @param   array    $data      Data for the form.
 204       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 205       *
 206       * @return  Form|boolean  A Form object on success, false on failure
 207       *
 208       * @since   1.6
 209       */
 210      public function getForm($data = [], $loadData = true)
 211      {
 212          $form = parent::getForm($data, $loadData);
 213  
 214          if (empty($form)) {
 215              return false;
 216          }
 217  
 218          $app  = Factory::getApplication();
 219          $user = $app->getIdentity();
 220  
 221          // On edit article, we get ID of article from article.id state, but on save, we use data from input
 222          $id = (int) $this->getState('article.id', $app->input->getInt('a_id'));
 223  
 224          // Existing record. We can't edit the category in frontend if not edit.state.
 225          if ($id > 0 && !$user->authorise('core.edit.state', 'com_content.article.' . $id)) {
 226              $form->setFieldAttribute('catid', 'readonly', 'true');
 227              $form->setFieldAttribute('catid', 'required', 'false');
 228              $form->setFieldAttribute('catid', 'filter', 'unset');
 229          }
 230  
 231          // Prevent messing with article language and category when editing existing article with associations
 232          if ($this->getState('article.id') && Associations::isEnabled()) {
 233              $associations = Associations::getAssociations('com_content', '#__content', 'com_content.item', $id);
 234  
 235              // Make fields read only
 236              if (!empty($associations)) {
 237                  $form->setFieldAttribute('language', 'readonly', 'true');
 238                  $form->setFieldAttribute('catid', 'readonly', 'true');
 239                  $form->setFieldAttribute('language', 'filter', 'unset');
 240                  $form->setFieldAttribute('catid', 'filter', 'unset');
 241              }
 242          }
 243  
 244          return $form;
 245      }
 246  
 247      /**
 248       * Allows preprocessing of the JForm object.
 249       *
 250       * @param   Form    $form   The form object
 251       * @param   array   $data   The data to be merged into the form object
 252       * @param   string  $group  The plugin group to be executed
 253       *
 254       * @return  void
 255       *
 256       * @since   3.7.0
 257       */
 258      protected function preprocessForm(Form $form, $data, $group = 'content')
 259      {
 260          $params = $this->getState()->get('params');
 261  
 262          if ($params && $params->get('enable_category') == 1 && $params->get('catid')) {
 263              $form->setFieldAttribute('catid', 'default', $params->get('catid'));
 264              $form->setFieldAttribute('catid', 'readonly', 'true');
 265  
 266              if (Multilanguage::isEnabled()) {
 267                  $categoryId = (int) $params->get('catid');
 268  
 269                  $db    = $this->getDatabase();
 270                  $query = $db->getQuery(true)
 271                      ->select($db->quoteName('language'))
 272                      ->from($db->quoteName('#__categories'))
 273                      ->where($db->quoteName('id') . ' = :categoryId')
 274                      ->bind(':categoryId', $categoryId, ParameterType::INTEGER);
 275                  $db->setQuery($query);
 276  
 277                  $result = $db->loadResult();
 278  
 279                  if ($result != '*') {
 280                      $form->setFieldAttribute('language', 'readonly', 'true');
 281                      $form->setFieldAttribute('language', 'default', $result);
 282                  }
 283              }
 284          }
 285  
 286          if (!Multilanguage::isEnabled()) {
 287              $form->setFieldAttribute('language', 'type', 'hidden');
 288              $form->setFieldAttribute('language', 'default', '*');
 289          }
 290  
 291          parent::preprocessForm($form, $data, $group);
 292      }
 293  
 294      /**
 295       * Method to get a table object, load it if necessary.
 296       *
 297       * @param   string  $name     The table name. Optional.
 298       * @param   string  $prefix   The class prefix. Optional.
 299       * @param   array   $options  Configuration array for model. Optional.
 300       *
 301       * @return  Table  A Table object
 302       *
 303       * @since   4.0.0
 304       * @throws  \Exception
 305       */
 306      public function getTable($name = 'Article', $prefix = 'Administrator', $options = array())
 307      {
 308          return parent::getTable($name, $prefix, $options);
 309      }
 310  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer