compare.php
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* @package Joomla.Administrator
* @subpackage com_contenthistory
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JLoader::register('ContenthistoryHelper', JPATH_ADMINISTRATOR . '/components/com_contenthistory/helpers/contenthistory.php');
/**
* Methods supporting a list of contenthistory records.
*
* @since 3.2
*/
class ContenthistoryModelCompare extends JModelItem
{
/**
* Method to get a version history row.
*
* @return array|boolean On success, array of populated tables. False on failure.
*
* @since 3.2
*/
public function getItems()
{
$input = JFactory::getApplication()->input;
/** @var JTableContenthistory $table1 */
$table1 = JTable::getInstance('Contenthistory');
/** @var JTableContenthistory $table2 */
$table2 = JTable::getInstance('Contenthistory');
$id1 = $input->getInt('id1');
$id2 = $input->getInt('id2');
$result = array();
if ($table1->load($id1) && $table2->load($id2))
{
// Get the first history record's content type record so we can check ACL
/** @var JTableContenttype $contentTypeTable */
$contentTypeTable = JTable::getInstance('Contenttype');
$ucmTypeId = $table1->ucm_type_id;
if (!$contentTypeTable->load($ucmTypeId))
{
// Assume a failure to load the content type means broken data, abort mission
return false;
}
$user = JFactory::getUser();
// Access check
if ($user->authorise('core.edit', $contentTypeTable->type_alias . '.' . (int) $table1->ucm_item_id) || $this->canEdit($table1))
{
$return = true;
}
else
{
$this->setError(JText::_('JERROR_ALERTNOAUTHOR'));
return false;
}
// All's well, process the records
if ($return == true)
{
foreach (array($table1, $table2) as $table)
{
$object = new stdClass;
$object->data = ContenthistoryHelper::prepareData($table);
$object->version_note = $table->version_note;
// Let's use custom calendars when present
$object->save_date = JHtml::_('date', $table->save_date, JText::_('DATE_FORMAT_LC6'));
$dateProperties = array (
'modified_time',
'created_time',
'modified',
'created',
'checked_out_time',
'publish_up',
'publish_down',
);
foreach ($dateProperties as $dateProperty)
{
if (array_key_exists($dateProperty, $object->data) && $object->data->$dateProperty->value != '0000-00-00 00:00:00')
{
$object->data->$dateProperty->value = JHtml::_('date', $object->data->$dateProperty->value, JText::_('DATE_FORMAT_LC6'));
}
}
$result[] = $object;
}
return $result;
}
}
return false;
}
/**
* Method to test whether a record is editable
*
* @param JTableContenthistory $record A JTable object.
*
* @return boolean True if allowed to edit the record. Defaults to the permission set in the component.
*
* @since 3.6
*/
protected function canEdit($record)
{
$result = false;
if (!empty($record->ucm_type_id))
{
// Check that the type id matches the type alias
$typeAlias = JFactory::getApplication()->input->get('type_alias');
/** @var JTableContenttype $contentTypeTable */
$contentTypeTable = JTable::getInstance('Contenttype', 'JTable');
if ($contentTypeTable->getTypeId($typeAlias) == $record->ucm_type_id)
{
/**
* Make sure user has edit privileges for this content item. Note that we use edit permissions
* for the content item, not delete permissions for the content history row.
*/
$user = JFactory::getUser();
$result = $user->authorise('core.edit', $typeAlias . '.' . (int) $record->ucm_item_id);
}
// Finally try session (this catches edit.own case too)
if (!$result)
{
$contentTypeTable->load($record->ucm_type_id);
$typeEditables = (array) JFactory::getApplication()->getUserState(str_replace('.', '.edit.', $contentTypeTable->type_alias) . '.id');
$result = in_array((int) $record->ucm_item_id, $typeEditables);
}
}
return $result;
}
}