[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_tags/src/Model/ -> TagModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_tags
   6   *
   7   * @copyright   (C) 2013 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\Tags\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Date\Date;
  15  use Joomla\CMS\Factory;
  16  use Joomla\CMS\MVC\Model\AdminModel;
  17  use Joomla\CMS\Plugin\PluginHelper;
  18  use Joomla\CMS\Versioning\VersionableModelTrait;
  19  use Joomla\Registry\Registry;
  20  use Joomla\String\StringHelper;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Tags Component Tag Model
  28   *
  29   * @since  3.1
  30   */
  31  class TagModel extends AdminModel
  32  {
  33      use VersionableModelTrait;
  34  
  35      /**
  36       * @var    string  The prefix to use with controller messages.
  37       * @since  3.1
  38       */
  39      protected $text_prefix = 'COM_TAGS';
  40  
  41      /**
  42       * @var    string  The type alias for this content type.
  43       * @since  3.2
  44       */
  45      public $typeAlias = 'com_tags.tag';
  46  
  47      /**
  48       * Allowed batch commands
  49       *
  50       * @var    array
  51       * @since  3.7.0
  52       */
  53      protected $batch_commands = array(
  54          'assetgroup_id' => 'batchAccess',
  55          'language_id' => 'batchLanguage',
  56      );
  57  
  58      /**
  59       * Method to test whether a record can be deleted.
  60       *
  61       * @param   object  $record  A record object.
  62       *
  63       * @return  boolean  True if allowed to delete the record. Defaults to the permission set in the component.
  64       *
  65       * @since   3.1
  66       */
  67      protected function canDelete($record)
  68      {
  69          if (empty($record->id) || $record->published != -2) {
  70              return false;
  71          }
  72  
  73          return parent::canDelete($record);
  74      }
  75  
  76      /**
  77       * Auto-populate the model state.
  78       *
  79       * @note Calling getState in this method will result in recursion.
  80       *
  81       * @return  void
  82       *
  83       * @since   3.1
  84       */
  85      protected function populateState()
  86      {
  87          $app = Factory::getApplication();
  88  
  89          $parentId = $app->input->getInt('parent_id');
  90          $this->setState('tag.parent_id', $parentId);
  91  
  92          // Load the User state.
  93          $pk = $app->input->getInt('id');
  94          $this->setState($this->getName() . '.id', $pk);
  95  
  96          // Load the parameters.
  97          $params = ComponentHelper::getParams('com_tags');
  98          $this->setState('params', $params);
  99      }
 100  
 101      /**
 102       * Method to get a tag.
 103       *
 104       * @param   integer  $pk  An optional id of the object to get, otherwise the id from the model state is used.
 105       *
 106       * @return  mixed  Tag data object on success, false on failure.
 107       *
 108       * @since   3.1
 109       */
 110      public function getItem($pk = null)
 111      {
 112          if ($result = parent::getItem($pk)) {
 113              // Prime required properties.
 114              if (empty($result->id)) {
 115                  $result->parent_id = $this->getState('tag.parent_id');
 116              }
 117  
 118              // Convert the metadata field to an array.
 119              $registry = new Registry($result->metadata);
 120              $result->metadata = $registry->toArray();
 121  
 122              // Convert the images field to an array.
 123              $registry = new Registry($result->images);
 124              $result->images = $registry->toArray();
 125  
 126              // Convert the urls field to an array.
 127              $registry = new Registry($result->urls);
 128              $result->urls = $registry->toArray();
 129  
 130              // Convert the modified date to local user time for display in the form.
 131              $tz = new \DateTimeZone(Factory::getApplication()->get('offset'));
 132  
 133              if ((int) $result->modified_time) {
 134                  $date = new Date($result->modified_time);
 135                  $date->setTimezone($tz);
 136                  $result->modified_time = $date->toSql(true);
 137              } else {
 138                  $result->modified_time = null;
 139              }
 140          }
 141  
 142          return $result;
 143      }
 144  
 145      /**
 146       * Method to get the row form.
 147       *
 148       * @param   array    $data      Data for the form.
 149       * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
 150       *
 151       * @return  bool|\Joomla\CMS\Form\Form  A Form object on success, false on failure
 152       *
 153       * @since   3.1
 154       */
 155      public function getForm($data = array(), $loadData = true)
 156      {
 157          $jinput = Factory::getApplication()->input;
 158  
 159          // Get the form.
 160          $form = $this->loadForm('com_tags.tag', 'tag', array('control' => 'jform', 'load_data' => $loadData));
 161  
 162          if (empty($form)) {
 163              return false;
 164          }
 165  
 166          $user = Factory::getUser();
 167  
 168          if (!$user->authorise('core.edit.state', 'com_tags' . $jinput->get('id'))) {
 169              // Disable fields for display.
 170              $form->setFieldAttribute('ordering', 'disabled', 'true');
 171              $form->setFieldAttribute('published', 'disabled', 'true');
 172  
 173              // Disable fields while saving.
 174              // The controller has already verified this is a record you can edit.
 175              $form->setFieldAttribute('ordering', 'filter', 'unset');
 176              $form->setFieldAttribute('published', 'filter', 'unset');
 177          }
 178  
 179          return $form;
 180      }
 181  
 182      /**
 183       * Method to get the data that should be injected in the form.
 184       *
 185       * @return  mixed  The data for the form.
 186       *
 187       * @since   3.1
 188       */
 189      protected function loadFormData()
 190      {
 191          // Check the session for previously entered form data.
 192          $data = Factory::getApplication()->getUserState('com_tags.edit.tag.data', array());
 193  
 194          if (empty($data)) {
 195              $data = $this->getItem();
 196          }
 197  
 198          $this->preprocessData('com_tags.tag', $data);
 199  
 200          return $data;
 201      }
 202  
 203      /**
 204       * Method to save the form data.
 205       *
 206       * @param   array  $data  The form data.
 207       *
 208       * @return  boolean  True on success.
 209       *
 210       * @since   3.1
 211       */
 212      public function save($data)
 213      {
 214          /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 215          $table      = $this->getTable();
 216          $input      = Factory::getApplication()->input;
 217          $pk         = (!empty($data['id'])) ? $data['id'] : (int) $this->getState($this->getName() . '.id');
 218          $isNew      = true;
 219          $context    = $this->option . '.' . $this->name;
 220  
 221          // Include the plugins for the save events.
 222          PluginHelper::importPlugin($this->events_map['save']);
 223  
 224          try {
 225              // Load the row if saving an existing tag.
 226              if ($pk > 0) {
 227                  $table->load($pk);
 228                  $isNew = false;
 229              }
 230  
 231              // Set the new parent id if parent id not matched OR while New/Save as Copy .
 232              if ($table->parent_id != $data['parent_id'] || $data['id'] == 0) {
 233                  $table->setLocation($data['parent_id'], 'last-child');
 234              }
 235  
 236              // Alter the title for save as copy
 237              if ($input->get('task') == 'save2copy') {
 238                  $origTable = $this->getTable();
 239                  $origTable->load($input->getInt('id'));
 240  
 241                  if ($data['title'] == $origTable->title) {
 242                      list($title, $alias) = $this->generateNewTitle($data['parent_id'], $data['alias'], $data['title']);
 243                      $data['title'] = $title;
 244                      $data['alias'] = $alias;
 245                  } elseif ($data['alias'] == $origTable->alias) {
 246                      $data['alias'] = '';
 247                  }
 248  
 249                  $data['published'] = 0;
 250              }
 251  
 252              // Bind the data.
 253              if (!$table->bind($data)) {
 254                  $this->setError($table->getError());
 255  
 256                  return false;
 257              }
 258  
 259              // Prepare the row for saving
 260              $this->prepareTable($table);
 261  
 262              // Check the data.
 263              if (!$table->check()) {
 264                  $this->setError($table->getError());
 265  
 266                  return false;
 267              }
 268  
 269              // Trigger the before save event.
 270              $result = Factory::getApplication()->triggerEvent($this->event_before_save, array($context, $table, $isNew, $data));
 271  
 272              if (in_array(false, $result, true)) {
 273                  $this->setError($table->getError());
 274  
 275                  return false;
 276              }
 277  
 278              // Store the data.
 279              if (!$table->store()) {
 280                  $this->setError($table->getError());
 281  
 282                  return false;
 283              }
 284  
 285              // Trigger the after save event.
 286              Factory::getApplication()->triggerEvent($this->event_after_save, array($context, $table, $isNew));
 287  
 288              // Rebuild the path for the tag:
 289              if (!$table->rebuildPath($table->id)) {
 290                  $this->setError($table->getError());
 291  
 292                  return false;
 293              }
 294  
 295              // Rebuild the paths of the tag's children:
 296              if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path)) {
 297                  $this->setError($table->getError());
 298  
 299                  return false;
 300              }
 301          } catch (\Exception $e) {
 302              $this->setError($e->getMessage());
 303  
 304              return false;
 305          }
 306  
 307          $this->setState($this->getName() . '.id', $table->id);
 308          $this->setState($this->getName() . '.new', $isNew);
 309  
 310          // Clear the cache
 311          $this->cleanCache();
 312  
 313          return true;
 314      }
 315  
 316      /**
 317       * Prepare and sanitise the table data prior to saving.
 318       *
 319       * @param   \Joomla\CMS\Table\Table  $table  A Table object.
 320       *
 321       * @return  void
 322       *
 323       * @since   1.6
 324       */
 325      protected function prepareTable($table)
 326      {
 327          // Increment the content version number.
 328          $table->version++;
 329      }
 330  
 331      /**
 332       * Method rebuild the entire nested set tree.
 333       *
 334       * @return  boolean  False on failure or error, true otherwise.
 335       *
 336       * @since   3.1
 337       */
 338      public function rebuild()
 339      {
 340          // Get an instance of the table object.
 341          /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 342  
 343          $table = $this->getTable();
 344  
 345          if (!$table->rebuild()) {
 346              $this->setError($table->getError());
 347  
 348              return false;
 349          }
 350  
 351          // Clear the cache
 352          $this->cleanCache();
 353  
 354          return true;
 355      }
 356  
 357      /**
 358       * Method to save the reordered nested set tree.
 359       * First we save the new order values in the lft values of the changed ids.
 360       * Then we invoke the table rebuild to implement the new ordering.
 361       *
 362       * @param   array    $idArray   An array of primary key ids.
 363       * @param   integer  $lftArray  The lft value
 364       *
 365       * @return  boolean  False on failure or error, True otherwise
 366       *
 367       * @since   3.1
 368       */
 369      public function saveorder($idArray = null, $lftArray = null)
 370      {
 371          // Get an instance of the table object.
 372          /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 373  
 374          $table = $this->getTable();
 375  
 376          if (!$table->saveorder($idArray, $lftArray)) {
 377              $this->setError($table->getError());
 378  
 379              return false;
 380          }
 381  
 382          // Clear the cache
 383          $this->cleanCache();
 384  
 385          return true;
 386      }
 387  
 388      /**
 389       * Method to change the title & alias.
 390       *
 391       * @param   integer  $parentId  The id of the parent.
 392       * @param   string   $alias     The alias.
 393       * @param   string   $title     The title.
 394       *
 395       * @return  array  Contains the modified title and alias.
 396       *
 397       * @since   3.1
 398       */
 399      protected function generateNewTitle($parentId, $alias, $title)
 400      {
 401          // Alter the title & alias
 402          /** @var \Joomla\Component\Tags\Administrator\Table\TagTable $table */
 403  
 404          $table = $this->getTable();
 405  
 406          while ($table->load(array('alias' => $alias, 'parent_id' => $parentId))) {
 407              $title = ($table->title != $title) ? $title : StringHelper::increment($title);
 408              $alias = StringHelper::increment($alias, 'dash');
 409          }
 410  
 411          return array($title, $alias);
 412      }
 413  }


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