1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class JTableContenthistory extends JTable
18 {
19 20 21 22 23 24 25
26 public $ignoreChanges = array();
27
28 29 30 31 32 33 34 35
36 public $convertToInt = array();
37
38 39 40 41 42 43 44
45 public function __construct($db)
46 {
47 parent::__construct('#__ucm_history', 'version_id', $db);
48 $this->ignoreChanges = array(
49 'modified_by',
50 'modified_user_id',
51 'modified',
52 'modified_time',
53 'checked_out',
54 'checked_out_time',
55 'version',
56 'hits',
57 'path',
58 );
59 $this->convertToInt = array('publish_up', 'publish_down', 'ordering', 'featured');
60 }
61
62 63 64 65 66 67 68 69 70
71 public function store($updateNulls = false)
72 {
73 $this->set('character_count', strlen($this->get('version_data')));
74 $typeTable = JTable::getInstance('Contenttype');
75 $typeTable->load($this->ucm_type_id);
76
77 if (!isset($this->sha1_hash))
78 {
79 $this->set('sha1_hash', $this->getSha1($this->get('version_data'), $typeTable));
80 }
81
82
83 if ($this->get('keep_forever') === null)
84 {
85 $this->set('editor_user_id', JFactory::getUser()->id);
86 $this->set('save_date', JFactory::getDate()->toSql());
87 }
88
89 return parent::store($updateNulls);
90 }
91
92 93 94 95 96 97 98 99 100 101 102
103 public function getSha1($jsonData, JTableContenttype $typeTable)
104 {
105 $object = is_object($jsonData) ? $jsonData : json_decode($jsonData);
106
107 if (isset($typeTable->content_history_options) && is_object(json_decode($typeTable->content_history_options)))
108 {
109 $options = json_decode($typeTable->content_history_options);
110 $this->ignoreChanges = isset($options->ignoreChanges) ? $options->ignoreChanges : $this->ignoreChanges;
111 $this->convertToInt = isset($options->convertToInt) ? $options->convertToInt : $this->convertToInt;
112 }
113
114 foreach ($this->ignoreChanges as $remove)
115 {
116 if (property_exists($object, $remove))
117 {
118 unset($object->$remove);
119 }
120 }
121
122
123 foreach ($object as $name => $value)
124 {
125 if (is_object($value))
126 {
127
128 foreach ($value as $subName => $subValue)
129 {
130 $object->$subName = is_int($subValue) || is_bool($subValue) || $subValue === null ? (string) $subValue : $subValue;
131 }
132 }
133 else
134 {
135 $object->$name = is_int($value) || is_bool($value) || $value === null ? (string) $value : $value;
136 }
137 }
138
139
140 foreach ($this->convertToInt as $convert)
141 {
142 if (isset($object->$convert))
143 {
144 $object->$convert = (int) $object->$convert;
145 }
146 }
147
148 if (isset($object->review_time))
149 {
150 $object->review_time = (int) $object->review_time;
151 }
152
153 return sha1(json_encode($object));
154 }
155
156 157 158 159 160 161 162 163
164 public function getHashMatch()
165 {
166 $db = $this->_db;
167 $query = $db->getQuery(true);
168 $query->select('*')
169 ->from($db->quoteName('#__ucm_history'))
170 ->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
171 ->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
172 ->where($db->quoteName('sha1_hash') . ' = ' . $db->quote($this->get('sha1_hash')));
173 $db->setQuery($query, 0, 1);
174
175 return $db->loadObject();
176 }
177
178 179 180 181 182 183 184 185 186
187 public function deleteOldVersions($maxVersions)
188 {
189 $result = true;
190
191
192 $db = $this->_db;
193 $query = $db->getQuery(true);
194 $query->select($db->quoteName('version_id'))
195 ->from($db->quoteName('#__ucm_history'))
196 ->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
197 ->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
198 ->where($db->quoteName('keep_forever') . ' != 1')
199 ->order($db->quoteName('save_date') . ' DESC ');
200 $db->setQuery($query, 0, (int) $maxVersions);
201 $idsToSave = $db->loadColumn(0);
202
203
204 if (count($idsToSave) === (int) $maxVersions)
205 {
206
207 $query = $db->getQuery(true);
208 $query->delete($db->quoteName('#__ucm_history'))
209 ->where($db->quoteName('ucm_item_id') . ' = ' . (int) $this->get('ucm_item_id'))
210 ->where($db->quoteName('ucm_type_id') . ' = ' . (int) $this->get('ucm_type_id'))
211 ->where($db->quoteName('version_id') . ' NOT IN (' . implode(',', $idsToSave) . ')')
212 ->where($db->quoteName('keep_forever') . ' != 1');
213 $db->setQuery($query);
214 $result = (boolean) $db->execute();
215 }
216
217 return $result;
218 }
219 }
220