1 <?php
2
3 4 5 6 7
8 class rex_article_slice_history
9 {
10 public static function getTable()
11 {
12 return rex::getTablePrefix() . 'article_slice_history';
13 }
14
15 public static function makeSnapshot($article_id, $clang_id, $history_type, $revision = 0)
16 {
17 self::checkTables();
18
19 $slices = rex_sql::factory()->getArray(
20 'select * from ' . rex::getTable('article_slice') . ' where article_id=? and clang_id=? and revision=?',
21 [
22 $article_id,
23 $clang_id,
24 $revision,
25 ]
26 );
27
28 $history_date = date('Y-m-d H:i:s');
29
30 foreach ($slices as $slice) {
31 $sql = rex_sql::factory();
32 $sql->setTable(self::getTable());
33 foreach ($slice as $k => $v) {
34 if ($k == 'id') {
35 $sql->setValue('slice_id', $v);
36 } else {
37 $sql->setValue($k, $v);
38 }
39 }
40 $sql->setValue('history_type', $history_type);
41 $sql->setValue('history_date', $history_date);
42 $sql->setValue('history_user', rex::getUser()->getValue('login'));
43 $sql->insert();
44 }
45 }
46
47 public static function getSnapshots($article_id, $clang_id, $revision = 0)
48 {
49 return rex_sql::factory()->getArray(
50 'select distinct history_date, history_type, history_user from ' . self::getTable() . ' where article_id=? and clang_id=? and revision=? order by history_date desc',
51 [$article_id, $clang_id, $revision]
52 );
53 }
54
55 public static function restoreSnapshot($history_date, $article_id, $clang_id, $revision = 0)
56 {
57 self::checkTables();
58
59 $sql = rex_sql::factory();
60 $slices = $sql->getArray('select id from ' . self::getTable() . ' where article_id=? and clang_id=? and revision=? and history_date=?', [$article_id, $clang_id, $revision, $history_date]);
61
62 if (count($slices) == 0) {
63 return false;
64 }
65
66 self::makeSnapshot($article_id, $clang_id, 'version set ' . $history_date, $revision);
67
68 $article_slices_table = rex_sql_table::get(rex::getTable('article_slice'));
69
70 $sql = rex_sql::factory();
71 $sql->setQuery('delete from ' . rex::getTable('article_slice') . ' where article_id=? and clang_id=? and revision=?', [$article_id, $clang_id, $revision]);
72
73 $slices = rex_sql::factory();
74 $slices = $slices->getArray('select * from ' . self::getTable() . ' where article_id=? and clang_id=? and revision=? and history_date=?', [$article_id, $clang_id, $revision, $history_date]);
75
76 foreach ($slices as $slice) {
77 $sql = rex_sql::factory();
78 $sql->setTable(rex::getTable('article_slice'));
79
80 $ignoreFields = ['id', 'slice_id', 'history_date', 'history_type', 'history_user'];
81 foreach ($article_slices_table->getColumns() as $column) {
82 $columnName = $column->getName();
83 if (!in_array($columnName, $ignoreFields)) {
84 $sql->setValue($columnName, $slice[$columnName]);
85 }
86 }
87
88 $sql->insert();
89 }
90 rex_article_cache::delete($article_id, $clang_id);
91 return true;
92 }
93
94 public static function clearAllHistory()
95 {
96 rex_sql::factory()->setQuery('delete from ' . self::getTable());
97 }
98
99 public static function checkTables()
100 {
101 $slices_table = rex_sql_table::get(rex::getTable('article_slice'));
102 $history_table = rex_sql_table::get(self::getTable());
103
104 foreach ($slices_table->getColumns() as $column) {
105 if (strtolower($column->getName()) != 'id') {
106 $history_table->ensureColumn($column);
107 }
108 }
109
110 $history_table->alter();
111 }
112 }
113