forked from contao/core
/
PageTree.php
201 lines (167 loc) · 5.13 KB
/
PageTree.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
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2016 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao;
/**
* Provide methods to handle input field "page tree".
*
* @property string $orderField
* @property boolean $multiple
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class PageTree extends \Widget
{
/**
* Submit user input
* @var boolean
*/
protected $blnSubmitInput = true;
/**
* Template
* @var string
*/
protected $strTemplate = 'be_widget';
/**
* Order ID
* @var string
*/
protected $strOrderId;
/**
* Order name
* @var string
*/
protected $strOrderName;
/**
* Load the database object
*
* @param array $arrAttributes
*/
public function __construct($arrAttributes=null)
{
$this->import('Database');
parent::__construct($arrAttributes);
// Prepare the order field
if ($this->orderField != '')
{
$this->strOrderId = $this->orderField . str_replace($this->strField, '', $this->strId);
$this->strOrderName = $this->orderField . str_replace($this->strField, '', $this->strName);
// Retrieve the order value
$objRow = $this->Database->prepare("SELECT ".\Database::quoteIdentifier($this->orderField)." FROM {$this->strTable} WHERE id=?")
->limit(1)
->execute($this->activeRecord->id);
$tmp = deserialize($objRow->{$this->orderField});
$this->{$this->orderField} = (!empty($tmp) && is_array($tmp)) ? array_filter($tmp) : array();
}
}
/**
* Return an array if the "multiple" attribute is set
*
* @param mixed $varInput
*
* @return mixed
*/
protected function validator($varInput)
{
// Store the order value
if ($this->orderField != '')
{
$arrNew = explode(',', \Input::post($this->strOrderName));
// Only proceed if the value has changed
if ($arrNew !== $this->{$this->orderField})
{
$this->Database->prepare("UPDATE {$this->strTable} SET tstamp=?, {$this->orderField}=? WHERE id=?")
->execute(time(), serialize($arrNew), $this->activeRecord->id);
$this->objDca->createNewVersion = true; // see #6285
}
}
// Return the value as usual
if ($varInput == '')
{
if ($this->mandatory)
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['mandatory'], $this->strLabel));
}
return '';
}
elseif (strpos($varInput, ',') === false)
{
return $this->multiple ? array(intval($varInput)) : intval($varInput);
}
else
{
$arrValue = array_map('intval', array_filter(explode(',', $varInput)));
return $this->multiple ? $arrValue : $arrValue[0];
}
}
/**
* Generate the widget and return it as string
*
* @return string
*/
public function generate()
{
$arrSet = array();
$arrValues = array();
$blnHasOrder = ($this->orderField != '' && is_array($this->{$this->orderField}));
if (!empty($this->varValue)) // Can be an array
{
$objPages = \PageModel::findMultipleByIds((array)$this->varValue);
if ($objPages !== null)
{
while ($objPages->next())
{
$arrSet[] = $objPages->id;
$arrValues[$objPages->id] = \Image::getHtml($this->getPageStatusIcon($objPages)) . ' ' . $objPages->title . ' (' . $objPages->alias . \Config::get('urlSuffix') . ')';
}
}
// Apply a custom sort order
if ($blnHasOrder)
{
$arrNew = array();
foreach ((array) $this->{$this->orderField} as $i)
{
if (isset($arrValues[$i]))
{
$arrNew[$i] = $arrValues[$i];
unset($arrValues[$i]);
}
}
if (!empty($arrValues))
{
foreach ($arrValues as $k=>$v)
{
$arrNew[$k] = $v;
}
}
$arrValues = $arrNew;
unset($arrNew);
}
}
// Load the fonts for the drag hint (see #4838)
\Config::set('loadGoogleFonts', true);
$return = '<input type="hidden" name="'.$this->strName.'" id="ctrl_'.$this->strId.'" value="'.implode(',', $arrSet).'">' . ($blnHasOrder ? '
<input type="hidden" name="'.$this->strOrderName.'" id="ctrl_'.$this->strOrderId.'" value="'.$this->{$this->orderField}.'">' : '') . '
<div class="selector_container">' . (($blnHasOrder && count($arrValues) > 1) ? '
<p class="sort_hint">' . $GLOBALS['TL_LANG']['MSC']['dragItemsHint'] . '</p>' : '') . '
<ul id="sort_'.$this->strId.'" class="'.($blnHasOrder ? 'sortable' : '').'">';
foreach ($arrValues as $k=>$v)
{
$return .= '<li data-id="'.$k.'">'.$v.'</li>';
}
$return .= '</ul>
<p><a href="contao/page.php?do='.\Input::get('do').'&table='.$this->strTable.'&field='.$this->strField.'&act=show&id='.$this->activeRecord->id.'&value='.implode(',', $arrSet).'&rt='.REQUEST_TOKEN.'" class="tl_submit" onclick="Backend.getScrollOffset();Backend.openModalSelector({\'width\':768,\'title\':\''.specialchars(str_replace("'", "\\'", $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['label'][0])).'\',\'url\':this.href,\'id\':\''.$this->strId.'\'});return false">'.$GLOBALS['TL_LANG']['MSC']['changeSelection'].'</a></p>' . ($blnHasOrder ? '
<script>Backend.makeMultiSrcSortable("sort_'.$this->strId.'", "ctrl_'.$this->strOrderId.'")</script>' : '') . '
</div>';
if (!\Environment::get('isAjaxRequest'))
{
$return = '<div>' . $return . '</div>';
}
return $return;
}
}