/administrator/components/com_contenthistory/src/Model/PreviewModel.php

https://github.com/joomla/joomla-cms · PHP · 146 lines · 75 code · 19 blank · 52 comment · 10 complexity · 4f98ec54060542acada5935fcab335af MD5 · raw file

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