This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
/
DataContainer.php
533 lines (437 loc) · 16.4 KB
/
DataContainer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
/**
* Contao Open Source CMS
* Copyright (C) 2005-2011 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Leo Feyer 2005-2011
* @author Leo Feyer <http://www.contao.org>
* @package Backend
* @license LGPL
* @filesource
*/
/**
* Class DataContainer
*
* Provide methods to handle data container arrays.
* @copyright Leo Feyer 2005-2011
* @author Leo Feyer <http://www.contao.org>
* @package Controller
*/
class DataContainer extends Backend
{
/**
* Current ID
* @param integer
*/
protected $intId;
/**
* Name of the current table
* @param string
*/
protected $strTable;
/**
* Name of the current field
* @param string
*/
protected $strField;
/**
* Name attribute of the current input field
* @param string
*/
protected $strInputName;
/**
* Value of the current field
* @param mixed
*/
protected $varValue;
/**
* Name of the current palette
* @param string
*/
protected $strPalette;
/**
* WHERE clause of the database query
* @param array
*/
protected $procedure = array();
/**
* Values for the WHERE clause of the database query
* @param array
*/
protected $values = array();
/**
* Form attribute "onsubmit"
* @param array
*/
protected $onsubmit = array();
/**
* Reload the page after the form has been submitted
* @param boolean
*/
protected $noReload = false;
/**
* Active record
* @param object
*/
protected $objActiveRecord;
/**
* Return an object property
* @param string
* @return mixed
*/
public function __get($strKey)
{
switch ($strKey)
{
case 'id':
return $this->intId;
break;
case 'table':
return $this->strTable;
break;
case 'value':
return $this->varValue;
break;
case 'field':
return $this->strField;
break;
case 'inputName':
return $this->strInputName;
break;
case 'palette':
return $this->strPalette;
break;
case 'activeRecord':
return $this->objActiveRecord;
break;
default:
return null;
break;
}
}
/**
* Render a row of a box and return it as HTML string
* @return string
*/
protected function row()
{
$arrData = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField];
// Redirect if the field is excluded
if ($arrData['exclude'])
{
$this->log('Field "'.$this->strField.'" ('.$this->strTable.') was excluded from being edited', 'DataContainer row()', TL_ERROR);
$this->redirect('contao/main.php?act=error');
}
$xlabel = '';
// Toggle line wrap (textarea)
if ($arrData['inputType'] == 'textarea' && !strlen($arrData['eval']['rte']))
{
$xlabel .= ' ' . $this->generateImage('wrap.gif', $GLOBALS['TL_LANG']['MSC']['wordWrap'], 'title="' . specialchars($GLOBALS['TL_LANG']['MSC']['wordWrap']) . '" class="toggleWrap" onclick="Backend.toggleWrap(\'ctrl_'.$this->strInputName.'\');"');
}
// Add the help wizard
if ($arrData['eval']['helpwizard'])
{
$xlabel .= ' <a href="contao/help.php?table='.$this->strTable.'&field='.$this->strField.'" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['helpWizard']) . '" rel="lightbox[help 610 80%]">'.$this->generateImage('about.gif', $GLOBALS['TL_LANG']['MSC']['helpWizard'], 'style="vertical-align:text-bottom;"').'</a>';
}
// Add the popup file manager
if ($arrData['inputType'] == 'fileTree' && $this->strTable .'.'. $this->strField != 'tl_theme.templates')
{
$xlabel .= ' <a href="contao/files.php" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['fileManager']) . '" rel="lightbox[files 765 80%]">' . $this->generateImage('filemanager.gif', $GLOBALS['TL_LANG']['MSC']['fileManager'], 'style="vertical-align:text-bottom;"') . '</a>';
}
// Add table import wizard
elseif ($arrData['inputType'] == 'tableWizard')
{
$xlabel .= ' <a href="' . $this->addToUrl('key=table') . '" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['tw_import'][1]) . '" onclick="Backend.getScrollOffset();">' . $this->generateImage('tablewizard.gif', $GLOBALS['TL_LANG']['MSC']['tw_import'][0], 'style="vertical-align:text-bottom;"') . '</a>';
$xlabel .= ' ' . $this->generateImage('demagnify.gif', '', 'title="' . specialchars($GLOBALS['TL_LANG']['MSC']['tw_shrink']) . '" style="vertical-align:text-bottom; cursor:pointer;" onclick="Backend.tableWizardResize(0.9);"') . $this->generateImage('magnify.gif', '', 'title="' . specialchars($GLOBALS['TL_LANG']['MSC']['tw_expand']) . '" style="vertical-align:text-bottom; cursor:pointer;" onclick="Backend.tableWizardResize(1.1);"');
}
// Add list import wizard
elseif ($arrData['inputType'] == 'listWizard')
{
$xlabel .= ' <a href="' . $this->addToUrl('key=list') . '" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['lw_import'][1]) . '" onclick="Backend.getScrollOffset();">' . $this->generateImage('tablewizard.gif', $GLOBALS['TL_LANG']['MSC']['tw_import'][0], 'style="vertical-align:text-bottom;"') . '</a>';
}
// Input field callback
if (is_array($arrData['input_field_callback']))
{
if (!is_object($this->$arrData['input_field_callback'][0]))
{
$this->import($arrData['input_field_callback'][0]);
}
return $this->$arrData['input_field_callback'][0]->$arrData['input_field_callback'][1]($this, $xlabel);
}
// Return if the widget class does not exists
if (!isset($GLOBALS['BE_FFL'][$arrData['inputType']]))
{
return '';
}
$arrData['eval']['required'] = ($this->varValue == '' && $arrData['eval']['mandatory']) ? true : false;
$arrWidget = $this->prepareForWidget($arrData, $this->strInputName, $this->varValue, $this->strField, $this->strTable);
$objWidget = new $GLOBALS['BE_FFL'][$arrData['inputType']]($arrWidget);
$objWidget->xlabel = $xlabel;
$objWidget->currentRecord = $this->intId;
// Validate field
if ($this->Input->post('FORM_SUBMIT') == $this->strTable)
{
$paletteFields = array();
$key = ($this->Input->get('act') == 'editAll') ? 'FORM_FIELDS_' . $this->intId : 'FORM_FIELDS';
// Calculate the current palette
$postPaletteFields = implode(',', $this->Input->post($key));
$postPaletteFields = array_unique(trimsplit('[,;]', $postPaletteFields));
$newPaletteFields = trimsplit('[,;]', $this->getPalette());
if ($this->Input->get('act') == 'editAll')
{
foreach ($newPaletteFields as $k=>$v)
{
$newPaletteFields[$k] = $v . '_' . $this->intId;
}
if ($this->User->isAdmin)
{
$newPaletteFields['pid'] = 'pid_' . $this->intId;
$newPaletteFields['sorting'] = 'sorting_' . $this->intId;
}
}
$paletteFields = array_intersect($postPaletteFields, $newPaletteFields);
if (in_array($this->strInputName, $paletteFields) || $this->Input->get('act') == 'overrideAll')
{
$objWidget->validate();
if ($objWidget->hasErrors())
{
$this->noReload = true;
}
elseif ($objWidget->submitInput())
{
$varValue = $objWidget->value;
// Sort array by key (fix for JavaScript wizards)
if (is_array($varValue))
{
ksort($varValue);
$varValue = serialize($varValue);
}
// Save the current value
try
{
$this->save($varValue);
}
catch (Exception $e)
{
$this->noReload = true;
$objWidget->addError($e->getMessage());
$this->blnCreateNewRecord = false;
}
}
}
}
$wizard = '';
$datepicker = '';
// Datepicker
if ($arrData['eval']['datepicker'])
{
$datepicker = '
<script>
window.addEvent(\'domready\', function() { ' . sprintf($arrData['eval']['datepicker'], 'ctrl_' . $objWidget->id) . ' });
</script>';
}
// Add custom wizard
if (is_array($arrData['wizard']))
{
foreach ($arrData['wizard'] as $callback)
{
$this->import($callback[0]);
$wizard .= $this->$callback[0]->$callback[1]($this);
}
}
$objWidget->wizard = $wizard;
// Set correct form enctype
if ($objWidget instanceof uploadable)
{
$this->blnUploadable = true;
}
// Mark floated single checkboxes
if ($arrData['inputType'] == 'checkbox' && !$arrData['eval']['multiple'] && strpos($arrData['eval']['tl_class'], 'w50') !== false)
{
$arrData['eval']['tl_class'] .= ' cbx';
}
// No 2-column layout in "edit all" mode
if ($this->Input->get('act') == 'editAll' || $this->Input->get('act') == 'overrideAll')
{
$arrData['eval']['tl_class'] = str_replace(array('w50', 'clr', 'wizard', 'long', 'm12', 'cbx'), '', $arrData['eval']['tl_class']);
}
$updateMode = '';
// Handle multi-select fields in "override all" mode
if ($this->Input->get('act') == 'overrideAll' && ($arrData['inputType'] == 'checkbox' || $arrData['inputType'] == 'checkboxWizard') && $arrData['eval']['multiple'])
{
$updateMode = '
</div>
<div>
<fieldset class="tl_radio_container">
<legend>' . $GLOBALS['TL_LANG']['MSC']['updateMode'] . '</legend>
<input type="radio" name="'.$this->strInputName.'_update" id="opt_'.$this->strInputName.'_update_1" class="tl_radio" value="add" onfocus="Backend.getScrollOffset();"> <label for="opt_'.$this->strInputName.'_update_1">' . $GLOBALS['TL_LANG']['MSC']['updateAdd'] . '</label><br>
<input type="radio" name="'.$this->strInputName.'_update" id="opt_'.$this->strInputName.'_update_2" class="tl_radio" value="remove" onfocus="Backend.getScrollOffset();"> <label for="opt_'.$this->strInputName.'_update_2">' . $GLOBALS['TL_LANG']['MSC']['updateRemove'] . '</label><br>
<input type="radio" name="'.$this->strInputName.'_update" id="opt_'.$this->strInputName.'_update_0" class="tl_radio" value="replace" checked="checked" onfocus="Backend.getScrollOffset();"> <label for="opt_'.$this->strInputName.'_update_0">' . $GLOBALS['TL_LANG']['MSC']['updateReplace'] . '</label>
</fieldset>';
}
return '
<div' . ($arrData['eval']['tl_class'] ? ' class="' . $arrData['eval']['tl_class'] . '"' : '') . '>' . $objWidget->parse() . $datepicker . $updateMode . (($GLOBALS['TL_CONFIG']['oldBeTheme'] || !$objWidget->hasErrors()) ? $this->help() : '') . '
</div>';
}
/**
* Return the field explanation as HTML string
* @return string
*/
public function help()
{
$return = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['label'][1];
if (!$GLOBALS['TL_CONFIG']['showHelp'] || $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['inputType'] == 'password' || !strlen($return))
{
return '';
}
return '
<p class="tl_help' . (!$GLOBALS['TL_CONFIG']['oldBeTheme'] ? ' tl_tip' : '') . '">'.$return.'</p>';
}
/**
* Generate possible palette names from an array by taking the first value and either adding or not adding the following values
* @param array
* @return array
*/
protected function combiner($names)
{
$return = array('');
for ($i=0; $i<count($names); $i++)
{
$buffer = array();
$return = array_reverse($return);
foreach ($return as $k=>$v)
{
$buffer[] = ($k%2 == 0) ? $v : $v.$names[$i];
$buffer[] = ($k%2 == 0) ? $v.$names[$i] : $v;
}
$return = $buffer;
}
return array_filter($return);
}
/**
* Return a query string that switches into edit mode
* @param integer
* @return string
*/
protected function switchToEdit($id)
{
$arrKeys = array();
$arrUnset = array('act', 'id', 'table');
foreach (array_keys($_GET) as $strKey)
{
if (!in_array($strKey, $arrUnset))
{
$arrKeys[$strKey] = $strKey . '=' . $this->Input->get($strKey);
}
}
$strUrl = $this->Environment->script . '?' . implode('&', $arrKeys);
$glue = count($arrKeys) ? '&' : '';
return $strUrl . $glue . ($this->Input->get('table') ? 'table='.$this->Input->get('table').'&' : '').'act=edit&id='.$id;
}
/**
* Compile buttons from the table configuration array and return them as HTML
* @param array
* @param string
* @param array
* @param boolean
* @param array
* @param int
* @param int
* @return string
*/
protected function generateButtons($arrRow, $strTable, $arrRootIds=array(), $blnCircularReference=false, $arrChildRecordIds=null, $strPrevious=null, $strNext=null)
{
if (!count($GLOBALS['TL_DCA'][$strTable]['list']['operations']))
{
return '';
}
$return = '';
foreach ($GLOBALS['TL_DCA'][$strTable]['list']['operations'] as $k=>$v)
{
$v = is_array($v) ? $v : array($v);
$label = strlen($v['label'][0]) ? $v['label'][0] : $k;
$title = sprintf((strlen($v['label'][1]) ? $v['label'][1] : $k), $arrRow['id']);
$attributes = strlen($v['attributes']) ? ' ' . ltrim(sprintf($v['attributes'], $arrRow['id'], $arrRow['id'])) : '';
// Call a custom function instead of using the default button
if (is_array($v['button_callback']))
{
$this->import($v['button_callback'][0]);
$return .= $this->$v['button_callback'][0]->$v['button_callback'][1]($arrRow, $v['href'], $label, $title, $v['icon'], $attributes, $strTable, $arrRootIds, $arrChildRecordIds, $blnCircularReference, $strPrevious, $strNext);
continue;
}
// Generate all buttons except "move up" and "move down" buttons
if ($k != 'move' && $v != 'move')
{
$return .= '<a href="'.$this->addToUrl($v['href'].'&id='.$arrRow['id']).'" title="'.specialchars($title).'"'.$attributes.'>'.$this->generateImage($v['icon'], $label).'</a> ';
continue;
}
$arrDirections = array('up', 'down');
$arrRootIds = is_array($arrRootIds) ? $arrRootIds : array($arrRootIds);
foreach ($arrDirections as $dir)
{
$label = strlen($GLOBALS['TL_LANG'][$strTable][$dir][0]) ? $GLOBALS['TL_LANG'][$strTable][$dir][0] : $dir;
$title = strlen($GLOBALS['TL_LANG'][$strTable][$dir][1]) ? $GLOBALS['TL_LANG'][$strTable][$dir][1] : $dir;
$label = $this->generateImage($dir.'.gif', $label);
$href = strlen($v['href']) ? $v['href'] : '&act=move';
if ($dir == 'up')
{
$return .= ((is_numeric($strPrevious) && (!in_array($arrRow['id'], $arrRootIds) || !count($GLOBALS['TL_DCA'][$strTable]['list']['sorting']['root']))) ? '<a href="'.$this->addToUrl($href.'&id='.$arrRow['id']).'&sid='.intval($strPrevious).'" title="'.specialchars($title).'"'.$attributes.'>'.$label.'</a> ' : $this->generateImage('up_.gif')).' ';
continue;
}
$return .= ((is_numeric($strNext) && (!in_array($arrRow['id'], $arrRootIds) || !count($GLOBALS['TL_DCA'][$strTable]['list']['sorting']['root']))) ? '<a href="'.$this->addToUrl($href.'&id='.$arrRow['id']).'&sid='.intval($strNext).'" title="'.specialchars($title).'"'.$attributes.'>'.$label.'</a> ' : $this->generateImage('down_.gif')).' ';
}
}
return trim($return);
}
/**
* Compile global buttons from the table configuration array and return them as HTML
* @param boolean
* @return string
*/
protected function generateGlobalButtons($blnForceSeparator=false)
{
if (!is_array($GLOBALS['TL_DCA'][$this->strTable]['list']['global_operations']))
{
return '';
}
$return = '';
foreach ($GLOBALS['TL_DCA'][$this->strTable]['list']['global_operations'] as $k=>$v)
{
$v = is_array($v) ? $v : array($v);
$label = is_array($v['label']) ? $v['label'][0] : $v['label'];
$title = is_array($v['label']) ? $v['label'][1] : $v['label'];
$attributes = strlen($v['attributes']) ? ' ' . ltrim($v['attributes']) : '';
if (!strlen($label))
{
$label = $k;
}
// Call a custom function instead of using the default button
if (is_array($v['button_callback']))
{
$this->import($v['button_callback'][0]);
$return .= $this->$v['button_callback'][0]->$v['button_callback'][1]($v['href'], $label, $title, $v['icon'], $attributes, $this->strTable, $this->root);
continue;
}
$return .= '   ::   <a href="'.$this->addToUrl($v['href']).'" class="'.$v['class'].'" title="'.specialchars($title).'"'.$attributes.'>'.$label.'</a> ';
}
return ($GLOBALS['TL_DCA'][$this->strTable]['config']['closed'] && !$blnForceSeparator) ? preg_replace('/^   ::   /', '', $return) : $return;
}
}
?>