1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16 17
18 class JHelperContenthistory extends JHelper
19 {
20 21 22 23 24 25
26 public $typeAlias = null;
27
28 29 30 31 32 33 34
35 public function __construct($typeAlias = null)
36 {
37 $this->typeAlias = $typeAlias;
38 }
39
40 41 42 43 44 45 46 47 48
49 public function deleteHistory($table)
50 {
51 $key = $table->getKeyName();
52 $id = $table->$key;
53 $typeTable = JTable::getInstance('Contenttype', 'JTable');
54 $typeId = $typeTable->getTypeId($this->typeAlias);
55 $db = JFactory::getDbo();
56 $query = $db->getQuery(true);
57 $query->delete($db->quoteName('#__ucm_history'))
58 ->where($db->quoteName('ucm_item_id') . ' = ' . (int) $id)
59 ->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId);
60 $db->setQuery($query);
61
62 return $db->execute();
63 }
64
65 66 67 68 69 70 71 72 73 74
75 public function getHistory($typeId, $id)
76 {
77 $db = JFactory::getDbo();
78 $query = $db->getQuery(true);
79 $query->select($db->quoteName('h.version_note') . ',' . $db->quoteName('h.save_date') . ',' . $db->quoteName('u.name'))
80 ->from($db->quoteName('#__ucm_history') . ' AS h ')
81 ->leftJoin($db->quoteName('#__users') . ' AS u ON ' . $db->quoteName('u.id') . ' = ' . $db->quoteName('h.editor_user_id'))
82 ->where($db->quoteName('ucm_item_id') . ' = ' . $db->quote($id))
83 ->where($db->quoteName('ucm_type_id') . ' = ' . (int) $typeId)
84 ->order($db->quoteName('save_date') . ' DESC ');
85 $db->setQuery($query);
86
87 return $db->loadObjectList();
88 }
89
90 91 92 93 94 95 96 97 98
99 public function store($table)
100 {
101 $dataObject = $this->getDataObject($table);
102 $historyTable = JTable::getInstance('Contenthistory', 'JTable');
103 $typeTable = JTable::getInstance('Contenttype', 'JTable');
104 $typeTable->load(array('type_alias' => $this->typeAlias));
105 $historyTable->set('ucm_type_id', $typeTable->type_id);
106
107 $key = $table->getKeyName();
108 $historyTable->set('ucm_item_id', $table->$key);
109
110
111 if (!$historyTable->ucm_item_id)
112 {
113 return true;
114 }
115
116 $historyTable->set('version_data', json_encode($dataObject));
117 $input = JFactory::getApplication()->input;
118 $data = $input->get('jform', array(), 'array');
119 $versionName = false;
120
121 if (isset($data['version_note']))
122 {
123 $versionName = JFilterInput::getInstance()->clean($data['version_note'], 'string');
124 $historyTable->set('version_note', $versionName);
125 }
126
127
128 $historyTable->set('sha1_hash', $historyTable->getSha1($dataObject, $typeTable));
129
130 if ($historyRow = $historyTable->getHashMatch())
131 {
132 if (!$versionName || ($historyRow->version_note === $versionName))
133 {
134 return true;
135 }
136 else
137 {
138
139 $historyTable->set('version_id', $historyRow->version_id);
140 }
141 }
142
143 $result = $historyTable->store();
144
145
146 $aliasParts = explode('.', $this->typeAlias);
147
148 $context = isset($aliasParts[1]) ? $aliasParts[1] : '';
149
150 $maxVersionsContext = JComponentHelper::getParams($aliasParts[0])->get('history_limit' . '_' . $context, 0);
151
152 if ($maxVersionsContext)
153 {
154 $historyTable->deleteOldVersions($maxVersionsContext);
155 }
156 elseif ($maxVersions = JComponentHelper::getParams($aliasParts[0])->get('history_limit', 0))
157 {
158 $historyTable->deleteOldVersions($maxVersions);
159 }
160
161 return $result;
162 }
163 }
164