Skip to content

Commit e0390f0

Browse files
committed
Fix multiple editing in the backend without including the field for the CSS class. Additional security mechanisms after deleting style groups - classes remain included.
1 parent 82bc52f commit e0390f0

File tree

13 files changed

+228
-23
lines changed

13 files changed

+228
-23
lines changed

src/Resources/contao/classes/StyleManager.php

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
class StyleManager
1111
{
1212
/**
13-
* Clear StyleManager classes from cssClass field
13+
* Valid CSS-Class fields in tables
14+
* @var array
15+
*/
16+
public $validCssClassFields = array('cssID', 'cssClass');
17+
18+
/**
19+
* Clear StyleManager classes from css class field
1420
*
1521
* @param mixed $varValue
1622
* @param DataContainer $dc
@@ -27,6 +33,9 @@ public function clearStyleManager($varValue, $dc)
2733

2834
$arrValues = \StringUtil::deserialize($dc->activeRecord->styleManager, true);
2935

36+
// remove non-exiting values
37+
$this->cleanupClasses($arrValues, $dc->table);
38+
3039
if(count($arrValues))
3140
{
3241
$varValue = str_replace($arrValues, '', $varValue);
@@ -68,4 +77,154 @@ public function updateStyleManager($varValue, $dc)
6877

6978
return $varValue;
7079
}
71-
}
80+
81+
/**
82+
* Reset all StyleManager classes from css class field
83+
*
84+
* @param mixed $varValue
85+
* @param DataContainer $dc
86+
* @param $strTable
87+
*
88+
* @return mixed
89+
*/
90+
private function resetStyleManagerClasses($varValue, $dc, $strTable)
91+
{
92+
if($dc->field === 'cssID')
93+
{
94+
$cssID = \StringUtil::deserialize($varValue, true);
95+
$varValue = $cssID[1];
96+
}
97+
98+
$objStyles = StyleManagerModel::findByTable($strTable);
99+
$arrStyles = array();
100+
$varValue = ' ' . $varValue . ' ';
101+
102+
if($objStyles !== null)
103+
{
104+
while($objStyles->next())
105+
{
106+
$arrGroup = \StringUtil::deserialize($objStyles->cssClasses, true);
107+
108+
foreach ($arrGroup as $opts)
109+
{
110+
$arrStyles[] = ' ' . $opts['key'] . ' ';
111+
}
112+
}
113+
114+
$arrStyles = array_filter($arrStyles);
115+
}
116+
117+
if(count($arrStyles))
118+
{
119+
$varValue = str_replace($arrStyles, ' ', $varValue);
120+
$varValue = trim(preg_replace('#\s+#', ' ', $varValue));
121+
}
122+
123+
if($dc->field === 'cssID')
124+
{
125+
$varValue = serialize(array($cssID[0], $varValue));
126+
}
127+
128+
return $varValue;
129+
}
130+
131+
/**
132+
* Update classes on multi edit
133+
*
134+
* @param mixed $varValue
135+
* @param DataContainer $dc
136+
*
137+
* @return mixed
138+
*/
139+
public function updateOnMultiEdit($varValue, $dc)
140+
{
141+
if (\Input::get('act') === 'editAll')
142+
{
143+
if($field = $this->getClassFieldNameByTable($dc->table))
144+
{
145+
$stdClass = $dc;
146+
$stdClass->field = $field;
147+
$stdClass->activeRecord->styleManager = $varValue;
148+
149+
// Get new value
150+
$value = $this->resetStyleManagerClasses($dc->activeRecord->{$field}, $stdClass, $dc->table);
151+
$value = $this->updateStyleManager($value, $stdClass);
152+
$value = $field === 'cssID' ? serialize($value) : $value;
153+
154+
// Update css class field
155+
$dc->Database->prepare('UPDATE ' . $dc->table . ' SET ' . $field . '=? WHERE id=?')
156+
->execute($value, $dc->activeRecord->id);
157+
}
158+
}
159+
160+
return $varValue;
161+
}
162+
163+
/**
164+
* Return the field name of css classes by table
165+
*
166+
* @param $strTable
167+
*
168+
* @return mixed
169+
*/
170+
public function getClassFieldNameByTable($strTable)
171+
{
172+
\Backend::loadDataContainer($strTable);
173+
174+
foreach ($this->validCssClassFields as $field)
175+
{
176+
if(isset($GLOBALS['TL_DCA'][ $strTable ]['fields'][ $field ]))
177+
{
178+
return $field;
179+
}
180+
}
181+
182+
return false;
183+
}
184+
185+
/**
186+
* Checks the passed array and removes non-existent values
187+
*
188+
* @param $arrValues
189+
* @param $strTable
190+
*/
191+
public function cleanupClasses(&$arrValues, $strTable)
192+
{
193+
if(is_array($arrValues))
194+
{
195+
$objStyles = StyleManagerModel::findByTable($strTable);
196+
197+
if($objStyles !== null)
198+
{
199+
$arrExistingKeys = array();
200+
$arrExistingValues = array();
201+
202+
while($objStyles->next())
203+
{
204+
$arrExistingKeys[] = $objStyles->alias;
205+
206+
$arrGroup = \StringUtil::deserialize($objStyles->cssClasses, true);
207+
208+
foreach ($arrGroup as $opts)
209+
{
210+
$arrExistingValues[] = $opts['key'];
211+
}
212+
}
213+
214+
foreach ($arrValues as $key => $value)
215+
{
216+
if(!in_array($key, $arrExistingKeys))
217+
{
218+
unset($arrValues[$key]);
219+
continue;
220+
}
221+
222+
if(!in_array($value, $arrExistingValues))
223+
{
224+
unset($arrValues[$key]);
225+
}
226+
}
227+
}
228+
}
229+
}
230+
}

src/Resources/contao/dca/tl_article.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
'exclude' => true,
1919
'inputType' => 'stylemanager',
2020
'eval' => array('tl_class'=>'clr stylemanager'),
21-
'sql' => "blob NULL"
21+
'sql' => "blob NULL",
22+
'save_callback' => array(
23+
array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateOnMultiEdit')
24+
)
2225
);
2326

2427
$GLOBALS['TL_DCA']['tl_article']['fields']['cssID']['load_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'clearStyleManager');
25-
$GLOBALS['TL_DCA']['tl_article']['fields']['cssID']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');
28+
$GLOBALS['TL_DCA']['tl_article']['fields']['cssID']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');

src/Resources/contao/dca/tl_content.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
// Extend fields
2323
$GLOBALS['TL_DCA']['tl_content']['fields']['styleManager'] = array
2424
(
25-
'label' => &$GLOBALS['TL_LANG']['tl_article']['styleManager'],
25+
'label' => &$GLOBALS['TL_LANG']['tl_content']['styleManager'],
2626
'exclude' => true,
2727
'inputType' => 'stylemanager',
2828
'eval' => array('tl_class'=>'clr stylemanager'),
29-
'sql' => "blob NULL"
29+
'sql' => "blob NULL",
30+
'save_callback' => array(
31+
array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateOnMultiEdit')
32+
)
3033
);
3134

3235
$GLOBALS['TL_DCA']['tl_content']['fields']['cssID']['load_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'clearStyleManager');
33-
$GLOBALS['TL_DCA']['tl_content']['fields']['cssID']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');
36+
$GLOBALS['TL_DCA']['tl_content']['fields']['cssID']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');

src/Resources/contao/dca/tl_page.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
'exclude' => true,
1919
'inputType' => 'stylemanager',
2020
'eval' => array('tl_class'=>'clr stylemanager'),
21-
'sql' => "blob NULL"
21+
'sql' => "blob NULL",
22+
'save_callback' => array(
23+
array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateOnMultiEdit')
24+
)
2225
);
2326

2427
$GLOBALS['TL_DCA']['tl_page']['fields']['cssClass']['load_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'clearStyleManager');
25-
$GLOBALS['TL_DCA']['tl_page']['fields']['cssClass']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');
28+
$GLOBALS['TL_DCA']['tl_page']['fields']['cssClass']['save_callback'][] = array('\\Oveleon\\ContaoComponentStyleManager\\StyleManager', 'updateStyleManager');

src/Resources/contao/languages/de/tl_article.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<source>Style Manager</source>
77
<target>Style Manager</target>
88
</trans-unit>
9+
10+
<trans-unit id="tl_article.styleManager.0">
11+
<source>Style Manager</source>
12+
<target>Style Manager</target>
13+
</trans-unit>
914
</body>
1015
</file>
11-
</xliff>
16+
</xliff>

src/Resources/contao/languages/de/tl_content.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<source>Style Manager</source>
77
<target>Style Manager</target>
88
</trans-unit>
9+
10+
<trans-unit id="tl_content.styleManager.0">
11+
<source>Style Manager</source>
12+
<target>Style Manager</target>
13+
</trans-unit>
914
</body>
1015
</file>
11-
</xliff>
16+
</xliff>

src/Resources/contao/languages/de/tl_page.xlf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<source>Style Manager</source>
77
<target>Style Manager</target>
88
</trans-unit>
9+
10+
<trans-unit id="tl_page.styleManager.0">
11+
<source>Style Manager</source>
12+
<target>Style Manager</target>
13+
</trans-unit>
914
</body>
1015
</file>
11-
</xliff>
16+
</xliff>

src/Resources/contao/languages/en/tl_article.xlf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<trans-unit id="tl_article.style_manager_legend">
66
<source>Style Manager</source>
77
</trans-unit>
8+
9+
<trans-unit id="tl_article.styleManager.0">
10+
<source>Style Manager</source>
11+
</trans-unit>
812
</body>
913
</file>
10-
</xliff>
14+
</xliff>

src/Resources/contao/languages/en/tl_content.xlf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<trans-unit id="tl_content.style_manager_legend">
66
<source>Style Manager</source>
77
</trans-unit>
8+
9+
<trans-unit id="tl_content.styleManager.0">
10+
<source>Style Manager</source>
11+
</trans-unit>
812
</body>
913
</file>
10-
</xliff>
14+
</xliff>

src/Resources/contao/languages/en/tl_page.xlf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<trans-unit id="tl_page.style_manager_legend">
66
<source>Style Manager</source>
77
</trans-unit>
8+
9+
<trans-unit id="tl_page.styleManager.0">
10+
<source>Style Manager</source>
11+
</trans-unit>
812
</body>
913
</file>
10-
</xliff>
14+
</xliff>

src/Resources/public/stylemanager.css

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/public/stylemanager.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/public/stylemanager.scss

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1+
#pal_style_manager_legend{
2+
3+
.widget {
4+
5+
&.stylemanager {
6+
7+
> h3{
8+
display: none;
9+
}
10+
}
11+
}
12+
}
13+
114
.widget{
215

316
&.stylemanager{
417
margin-left: 15px;
518
margin-right: 15px;
619
clear: both;
720

8-
> h3{
9-
display: none;
10-
}
11-
1221
fieldset{
1322
position: relative;
1423
border: 1px solid #e6e6e8;

0 commit comments

Comments
 (0)