-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.php
211 lines (181 loc) · 5.3 KB
/
diff.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Contao Open Source CMS
*
* Copyright (C) 2005-2012 Leo Feyer
*
* @package Core
* @link http://contao.org
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
/**
* Initialize the system
*/
define('TL_MODE', 'BE');
require_once '../../initialize.php';
/**
* Class DiffController
*
* Show the difference between two versions of a record.
* @copyright Leo Feyer 2005-2012
* @author Leo Feyer <http://contao.org>
* @package Core
*/
class DiffController extends Backend
{
/**
* Initialize the controller
*
* 1. Import the user
* 2. Call the parent constructor
* 3. Authenticate the user
* 4. Load the language files
* DO NOT CHANGE THIS ORDER!
*/
public function __construct()
{
$this->import('BackendUser', 'User');
parent::__construct();
$this->User->authenticate();
$this->loadLanguageFile('default');
// Include the PhpDiff library
require TL_ROOT . '/system/vendor/phpdiff/Diff.php';
require TL_ROOT . '/system/vendor/phpdiff/Diff/Renderer/Html/Contao.php';
}
/**
* Run the controller
*/
public function run()
{
$strBuffer = '';
$arrVersions = array();
$intTo = 0;
$intFrom = 0;
if (!\Input::get('table') || !\Input::get('id'))
{
$strBuffer = 'Please provide the table name and ID';
}
else
{
$this->strTable = \Input::get('table');
$objDraft = Netzmacht\Drafts\Model\DraftableModel::findByPK($this->strTable, \Input::get('id'));
$objModel = $objDraft->getRelated();
if ($objDraft === null || $objModel === null)
{
$strBuffer = 'There are no draft of ' . \Input::get('table') . '.id=' . \Input::get('id');
}
else
{
$intIndex = 0;
$from = $objModel->row();
$to = $objDraft->row();
$this->loadLanguageFile($this->strTable);
$this->loadDataContainer($this->strTable);
$arrFields = $GLOBALS['TL_DCA'][$this->strTable]['fields'];
// Find the changed fields and highlight the changes
foreach ($to as $k=>$v)
{
if ($from[$k] != $to[$k])
{
if (!isset($arrFields[$k]['inputType']) || $arrFields[$k]['inputType'] == 'password' || $arrFields[$k]['eval']['doNotShow'] || $arrFields[$k]['eval']['hideInput'])
{
continue;
}
// Convert serialized arrays into strings
if (is_array(($tmp = deserialize($to[$k]))) && !is_array($to[$k]))
{
$to[$k] = $this->implode($tmp);
}
if (is_array(($tmp = deserialize($from[$k]))) && !is_array($from[$k]))
{
$from[$k] = $this->implode($tmp);
}
unset($tmp);
// Convert date fields
if ($arrFields[$k]['eval']['rgxp'] == 'date')
{
$to[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['dateFormat'], $to[$k] ?: '');
$from[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['dateFormat'], $from[$k] ?: '');
}
elseif ($arrFields[$k]['eval']['rgxp'] == 'time')
{
$to[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['timeFormat'], $to[$k] ?: '');
$from[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['timeFormat'], $from[$k] ?: '');
}
elseif ($arrFields[$k]['eval']['rgxp'] == 'datim')
{
$to[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['datimFormat'], $to[$k] ?: '');
$from[$k] = $this->parseDate($GLOBALS['TL_CONFIG']['datimFormat'], $from[$k] ?: '');
}
// Convert strings into arrays
if (!is_array($to[$k]))
{
$to[$k] = explode("\n", $to[$k]);
}
if (!is_array($from[$k]))
{
$from[$k] = explode("\n", $from[$k]);
}
$objDiff = new \Diff($from[$k], $to[$k]);
$strBuffer .= $objDiff->Render(new \Diff_Renderer_Html_Contao(array('field'=>($arrFields[$k]['label'][0] ?: $k))));
}
}
}
}
// Identical versions
if ($strBuffer == '')
{
$strBuffer = '<p>'.$GLOBALS['TL_LANG']['MSC']['identicalVersions'].'</p>';
}
$GLOBALS['TL_CSS'][] = 'system/themes/'. $this->getTheme() .'/diff.css';
$this->Template = new BackendTemplate('be_drafts_diff');
// Template variables
$this->Template->content = $strBuffer;
$this->Template->versions = $arrVersions;
$this->Template->to = $intTo;
$this->Template->from = $intFrom;
$this->Template->fromLabel = 'Von';
$this->Template->toLabel = 'Zu';
$this->Template->showLabel = specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
$this->Template->table = \Input::get('table');
$this->Template->pid = intval(\Input::get('pid'));
$this->Template->theme = $this->getTheme();
$this->Template->base = \Environment::get('base');
$this->Template->language = $GLOBALS['TL_LANGUAGE'];
$this->Template->title = specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
$this->Template->charset = $GLOBALS['TL_CONFIG']['characterSet'];
$this->Template->action = ampersand(\Environment::get('request'));
$GLOBALS['TL_CONFIG']['debugMode'] = false;
$this->Template->output();
}
/**
* Implode a multi-dimensional array recursively
* @param mixed
* @return string
*/
protected function implode($var)
{
if (!is_array($var))
{
return $var;
}
elseif (!is_array(next($var)))
{
return implode(', ', $var);
}
else
{
$buffer = '';
foreach ($var as $k=>$v)
{
$buffer .= $k . ": " . $this->implode($v) . "\n";
}
return trim($buffer);
}
}
}
/**
* Instantiate the controller
*/
$objDiff = new DiffController();
$objDiff->run();