[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_contenthistory/src/Model/ -> PreviewModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_contenthistory
   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\Contenthistory\Administrator\Model;
  12  
  13  use Joomla\CMS\Access\Exception\NotAllowed;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\HTML\HTMLHelper;
  16  use Joomla\CMS\Language\Text;
  17  use Joomla\CMS\MVC\Model\ItemModel;
  18  use Joomla\CMS\Table\ContentHistory;
  19  use Joomla\CMS\Table\ContentType;
  20  use Joomla\CMS\Table\Table;
  21  use Joomla\Component\Contenthistory\Administrator\Helper\ContenthistoryHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Methods supporting a list of contenthistory records.
  29   *
  30   * @since  3.2
  31   */
  32  class PreviewModel extends ItemModel
  33  {
  34      /**
  35       * Method to get a version history row.
  36       *
  37       * @param   integer  $pk  The id of the item
  38       *
  39       * @return  \stdClass|boolean    On success, standard object with row data. False on failure.
  40       *
  41       * @since   3.2
  42       *
  43       * @throws  NotAllowed   Thrown if not authorised to edit an item
  44       */
  45      public function getItem($pk = null)
  46      {
  47          /** @var ContentHistory $table */
  48          $table = $this->getTable('ContentHistory');
  49          $versionId = Factory::getApplication()->input->getInt('version_id');
  50  
  51          if (!$versionId || \is_array($versionId) || !$table->load($versionId)) {
  52              return false;
  53          }
  54  
  55          $user = Factory::getUser();
  56  
  57          // Access check
  58          if (!$user->authorise('core.edit', $table->item_id) && !$this->canEdit($table)) {
  59              throw new NotAllowed(Text::_('JERROR_ALERTNOAUTHOR'), 403);
  60          }
  61  
  62          $result = new \stdClass();
  63          $result->version_note = $table->version_note;
  64          $result->data = ContenthistoryHelper::prepareData($table);
  65  
  66          // Let's use custom calendars when present
  67          $result->save_date = HTMLHelper::_('date', $table->save_date, Text::_('DATE_FORMAT_LC6'));
  68  
  69          $dateProperties = array (
  70              'modified_time',
  71              'created_time',
  72              'modified',
  73              'created',
  74              'checked_out_time',
  75              'publish_up',
  76              'publish_down',
  77          );
  78  
  79          $nullDate = $this->getDatabase()->getNullDate();
  80  
  81          foreach ($dateProperties as $dateProperty) {
  82              if (
  83                  property_exists($result->data, $dateProperty)
  84                  && $result->data->$dateProperty->value !== null
  85                  && $result->data->$dateProperty->value !== $nullDate
  86              ) {
  87                  $result->data->$dateProperty->value = HTMLHelper::_(
  88                      'date',
  89                      $result->data->$dateProperty->value,
  90                      Text::_('DATE_FORMAT_LC6')
  91                  );
  92              }
  93          }
  94  
  95          return $result;
  96      }
  97  
  98      /**
  99       * Method to get a table object, load it if necessary.
 100       *
 101       * @param   string  $type    The table name. Optional.
 102       * @param   string  $prefix  The class prefix. Optional.
 103       * @param   array   $config  Configuration array for model. Optional.
 104       *
 105       * @return  Table   A Table object
 106       *
 107       * @since   3.2
 108       */
 109      public function getTable($type = 'ContentHistory', $prefix = 'Joomla\\CMS\\Table\\', $config = array())
 110      {
 111          return Table::getInstance($type, $prefix, $config);
 112      }
 113  
 114      /**
 115       * Method to test whether a record is editable
 116       *
 117       * @param   ContentHistory  $record  A Table object.
 118       *
 119       * @return  boolean  True if allowed to edit the record. Defaults to the permission set in the component.
 120       *
 121       * @since   3.6
 122       */
 123      protected function canEdit($record)
 124      {
 125          $result = false;
 126  
 127          if (!empty($record->item_id)) {
 128              /**
 129               * Make sure user has edit privileges for this content item. Note that we use edit permissions
 130               * for the content item, not delete permissions for the content history row.
 131               */
 132              $user   = Factory::getUser();
 133              $result = $user->authorise('core.edit', $record->item_id);
 134  
 135              // Finally try session (this catches edit.own case too)
 136              if (!$result) {
 137                  /** @var ContentType $contentTypeTable */
 138                  $contentTypeTable = $this->getTable('ContentType');
 139  
 140                  $typeAlias        = explode('.', $record->item_id);
 141                  $id = array_pop($typeAlias);
 142                  $typeAlias        = implode('.', $typeAlias);
 143                  $typeEditables = (array) Factory::getApplication()->getUserState(str_replace('.', '.edit.', $contentTypeTable->type_alias) . '.id');
 144                  $result = in_array((int) $id, $typeEditables);
 145              }
 146          }
 147  
 148          return $result;
 149      }
 150  }


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