[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_contenthistory/src/Model/ -> CompareModel.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\ListModel;
  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 CompareModel extends ListModel
  33  {
  34      /**
  35       * Method to get a version history row.
  36       *
  37       * @return  array|boolean    On success, array of populated tables. False on failure.
  38       *
  39       * @since   3.2
  40       *
  41       * @throws  NotAllowed   Thrown if not authorised to edit an item
  42       */
  43      public function getItems()
  44      {
  45          $input = Factory::getApplication()->input;
  46  
  47          /** @var ContentHistory $table1 */
  48          $table1 = $this->getTable('ContentHistory');
  49  
  50          /** @var ContentHistory $table2 */
  51          $table2 = $this->getTable('ContentHistory');
  52  
  53          $id1 = $input->getInt('id1');
  54          $id2 = $input->getInt('id2');
  55  
  56          if (!$id1 || \is_array($id1) || !$id2 || \is_array($id2)) {
  57              $this->setError(Text::_('COM_CONTENTHISTORY_ERROR_INVALID_ID'));
  58  
  59              return false;
  60          }
  61  
  62          $result = array();
  63  
  64          if (!$table1->load($id1) || !$table2->load($id2)) {
  65              $this->setError(Text::_('COM_CONTENTHISTORY_ERROR_VERSION_NOT_FOUND'));
  66  
  67              // Assume a failure to load the content means broken data, abort mission
  68              return false;
  69          }
  70  
  71          // Get the first history record's content type record so we can check ACL
  72          /** @var ContentType $contentTypeTable */
  73          $contentTypeTable = $this->getTable('ContentType');
  74          $typeAlias        = explode('.', $table1->item_id);
  75          array_pop($typeAlias);
  76          $typeAlias        = implode('.', $typeAlias);
  77  
  78          if (!$contentTypeTable->load(array('type_alias' => $typeAlias))) {
  79              $this->setError(Text::_('COM_CONTENTHISTORY_ERROR_FAILED_LOADING_CONTENT_TYPE'));
  80  
  81              // Assume a failure to load the content type means broken data, abort mission
  82              return false;
  83          }
  84  
  85          $user = Factory::getUser();
  86  
  87          // Access check
  88          if (!$user->authorise('core.edit', $table1->item_id) && !$this->canEdit($table1)) {
  89              throw new NotAllowed(Text::_('JERROR_ALERTNOAUTHOR'), 403);
  90          }
  91  
  92          $nullDate = $this->getDatabase()->getNullDate();
  93  
  94          foreach (array($table1, $table2) as $table) {
  95              $object = new \stdClass();
  96              $object->data = ContenthistoryHelper::prepareData($table);
  97              $object->version_note = $table->version_note;
  98  
  99              // Let's use custom calendars when present
 100              $object->save_date = HTMLHelper::_('date', $table->save_date, Text::_('DATE_FORMAT_LC6'));
 101  
 102              $dateProperties = array (
 103                  'modified_time',
 104                  'created_time',
 105                  'modified',
 106                  'created',
 107                  'checked_out_time',
 108                  'publish_up',
 109                  'publish_down',
 110              );
 111  
 112              foreach ($dateProperties as $dateProperty) {
 113                  if (
 114                      property_exists($object->data, $dateProperty)
 115                      && $object->data->$dateProperty->value !== null
 116                      && $object->data->$dateProperty->value !== $nullDate
 117                  ) {
 118                      $object->data->$dateProperty->value = HTMLHelper::_(
 119                          'date',
 120                          $object->data->$dateProperty->value,
 121                          Text::_('DATE_FORMAT_LC6')
 122                      );
 123                  }
 124              }
 125  
 126              $result[] = $object;
 127          }
 128  
 129          return $result;
 130      }
 131  
 132      /**
 133       * Method to get a table object, load it if necessary.
 134       *
 135       * @param   string  $type    The table name. Optional.
 136       * @param   string  $prefix  The class prefix. Optional.
 137       * @param   array   $config  Configuration array for model. Optional.
 138       *
 139       * @return  Table   A Table object
 140       *
 141       * @since   3.2
 142       */
 143      public function getTable($type = 'Contenthistory', $prefix = 'Joomla\\CMS\\Table\\', $config = array())
 144      {
 145          return Table::getInstance($type, $prefix, $config);
 146      }
 147  
 148      /**
 149       * Method to test whether a record is editable
 150       *
 151       * @param   ContentHistory  $record  A Table object.
 152       *
 153       * @return  boolean  True if allowed to edit the record. Defaults to the permission set in the component.
 154       *
 155       * @since   3.6
 156       */
 157      protected function canEdit($record)
 158      {
 159          $result = false;
 160  
 161          if (!empty($record->item_id)) {
 162              /**
 163               * Make sure user has edit privileges for this content item. Note that we use edit permissions
 164               * for the content item, not delete permissions for the content history row.
 165               */
 166              $user   = Factory::getUser();
 167              $result = $user->authorise('core.edit', $record->item_id);
 168  
 169              // Finally try session (this catches edit.own case too)
 170              if (!$result) {
 171                  /** @var ContentType $contentTypeTable */
 172                  $contentTypeTable = $this->getTable('ContentType');
 173  
 174                  $typeAlias        = explode('.', $record->item_id);
 175                  $id = array_pop($typeAlias);
 176                  $typeAlias        = implode('.', $typeAlias);
 177                  $contentTypeTable->load(array('type_alias' => $typeAlias));
 178                  $typeEditables = (array) Factory::getApplication()->getUserState(str_replace('.', '.edit.', $contentTypeTable->type_alias) . '.id');
 179                  $result = in_array((int) $id, $typeEditables);
 180              }
 181          }
 182  
 183          return $result;
 184      }
 185  }


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