1212
1313namespace SemanticHTML5 \Backend ;
1414
15+ use Contao \BackendTemplate ;
16+ use Contao \DataContainer ;
17+ use Contao \System ;
18+ use Contao \Database ;
19+ use Contao \Input ;
20+
1521/**
1622 * Generall class to handle all backend callbacks
1723 */
@@ -31,7 +37,7 @@ class Callbacks
3137 * Array tempData
3238 */
3339 private static $ tempData = [];
34-
40+
3541 /**
3642 * Prevent direct instantiation (Singleton)
3743 */
@@ -55,18 +61,18 @@ public static function getInstance()
5561
5662 /**
5763 * Adds or updates the corresponding star or end tag
58- * @param \ DataContainer $dc
64+ * @param DataContainer $dc
5965 */
60- public static function onsubmitCallback (\ DataContainer $ dc )
61- {
66+ public static function onsubmitCallback (DataContainer $ dc )
67+ {
6268 //if this is not a html5 element, do nothing
6369 if (in_array ($ dc ->activeRecord ->type , array ('sHtml5Start ' , 'sHtml5End ' ))) {
6470
65- $ item = $ dc ->activeRecord ;
6671 $ util = new TagUtils ($ dc ->table );
72+ $ item = $ util ->getCurrentRecord ($ dc ->activeRecord ->id );
6773
6874 //correct the sh5_pid if needed
69- if ($ item ->type == 'sHtml5Start ' &&
75+ if ($ item ->type == 'sHtml5Start ' &&
7076 $ item ->id != $ item ->sh5_pid ) {
7177 $ item = $ util ->updateTag ($ item ->id , array ('sh5_pid ' => $ item ->id ));
7278 }
@@ -78,46 +84,47 @@ public static function onsubmitCallback(\DataContainer $dc)
7884
7985 /**
8086 * Deletes the corresponding html5 tag
81- *
82- * @param \ DataContainer $dc
87+ *
88+ * @param DataContainer $dc
8389 * @param int $id
8490 */
85- public static function ondeleteCallback (\ DataContainer $ dc , $ id )
91+ public static function ondeleteCallback (DataContainer $ dc , $ id )
8692 {
8793 //if this is not a html5 element, do nothing
8894 if (in_array ($ dc ->activeRecord ->type , array ('sHtml5Start ' , 'sHtml5End ' ))) {
8995 $ util = new TagUtils ($ dc ->table );
90- $ util ->deleteCorrespondingTag ($ dc ->activeRecord );
96+ $ item = $ util ->getCurrentRecord ($ dc ->activeRecord ->id );
97+ $ util ->deleteCorrespondingTag ($ item );
9198 }
9299 }
93100
94101 /**
95- * This methods corrects the hml5-elements after using the copy function of
102+ * This methods corrects the hml5-elements after using the copy function of
96103 * the tl_page table
97- *
104+ *
98105 * @param type $id
99- * @param \ DataContainer $dc
106+ * @param DataContainer $dc
100107 */
101- public static function oncopyPageCallback ($ id , \ DataContainer $ dc )
108+ public static function oncopyPageCallback ($ id , DataContainer $ dc )
102109 {
103110
104111 $ pages = array ($ id );
105112
106113 //fetch the child pages, if needed
107- if (\ Input::get ('childs ' )) {
108- $ pages = array_merge ($ pages , \ Database::getInstance ()->getChildRecords ($ id , 'tl_page ' ));
109-
114+ if (Input::get ('childs ' )) {
115+ $ pages = array_merge ($ pages , Database::getInstance ()->getChildRecords ($ id , 'tl_page ' ));
116+
110117 }
111118
112119 //fetch all html5 start elemnts and update them the end elements will be corrected automatically
113- $ elements = \ Database::getInstance ()
120+ $ elements = Database::getInstance ()
114121 ->prepare (
115122 sprintf (
116123 'SELECT * FROM tl_content '
117124 . 'WHERE type = "sHtml5Start" '
118125 . 'AND pid IN '
119126 . '(SELECT id FROM tl_article WHERE pid in (%s)) ' ,
120- implode (', ' , $ pages ))
127+ implode (', ' , array_fill ( 0 , count ( $ pages), ' ? ' ) ))
121128 )->execute ($ pages );
122129
123130 //return if no elements were found
@@ -131,19 +138,19 @@ public static function oncopyPageCallback($id, \DataContainer $dc)
131138 $ util ->createOrUpdateCorresppondingTag ($ elements , true );
132139 }
133140 }
134-
141+
135142 /**
136- * This methods corrects the hml5-elements after using the copy function of
143+ * This methods corrects the hml5-elements after using the copy function of
137144 * the tl_article table
138- *
145+ *
139146 * @param type $id
140- * @param \ DataContainer $dc
147+ * @param DataContainer $dc
141148 */
142- public static function oncopyArticleCallback ($ id , \ DataContainer $ dc )
149+ public static function oncopyArticleCallback ($ id , DataContainer $ dc )
143150 {
144151
145152 //fetch all html5 start elemnts and update them the end elements will be corrected automatically
146- $ elements = \ Database::getInstance ()
153+ $ elements = Database::getInstance ()
147154 ->prepare ('SELECT * FROM tl_content WHERE type = "sHtml5Start" AND pid = ? ' )
148155 ->execute ($ id );
149156
@@ -160,18 +167,18 @@ public static function oncopyArticleCallback($id, \DataContainer $dc)
160167 }
161168
162169 /**
163- * This methods corrects the hml5-elements after using the copy function of
170+ * This methods corrects the hml5-elements after using the copy function of
164171 * the tl_content table
165- *
172+ *
166173 * @param int $id The id of the new element
167- * @param \ DataContainer $dc The datad container
174+ * @param DataContainer $dc The datad container
168175 */
169- public static function oncopyContentCallback ($ id , \ DataContainer $ dc )
176+ public static function oncopyContentCallback ($ id , DataContainer $ dc )
170177 {
171-
172- //only handle copyAll cases. If only a single element was copied the
178+
179+ //only handle copyAll cases. If only a single element was copied the
173180 //onsubmit callback will handle the correction
174- if (\ Input::get ('act ' ) == 'copyAll ' ) {
181+ if (Input::get ('act ' ) == 'copyAll ' ) {
175182
176183 $ util = new TagUtils ($ dc ->table );
177184 $ newElement = $ util ->getTag ($ id );
@@ -181,19 +188,19 @@ public static function oncopyContentCallback($id, \DataContainer $dc)
181188
182189 //save the old sh5_pid
183190 $ oldPid = $ newElement ->sh5_pid ;
184-
191+
185192 if ($ newElement ->type === 'sHtml5Start ' ) {
186193 //update the sh5_pid
187194 $ newElement = $ util ->updateTag ($ id , array ('sh5_pid ' => $ id ));
188-
189- //create an end tag, just in case it was not copied
195+
196+ //create an end tag, just in case it was not copied
190197 $ correspondingId = $ util ->createOrUpdateCorresppondingTag ($ newElement );
191-
198+
192199 //Save the new id if available
193200 if ($ correspondingId !== null ) {
194201 self ::$ tempData [$ oldPid ]['end ' ] = $ correspondingId ;
195202 }
196-
203+
197204 //also save the new start tag
198205 self ::$ tempData [$ oldPid ]['start ' ] = $ newElement ->id ;
199206
@@ -202,31 +209,32 @@ public static function oncopyContentCallback($id, \DataContainer $dc)
202209 if (self ::$ tempData [$ oldPid ]['end ' ] !== null ) {
203210 $ util ->deleteTag (self ::$ tempData [$ oldPid ]['end ' ]);
204211 }
205-
212+
206213 //get the new sh5_pid
207214 $ newPid = (self ::$ tempData [$ oldPid ]['start ' ]) ? self ::$ tempData [$ oldPid ]['start ' ] : $ id ;
208-
215+
209216 //update the new element and the corresponding tag
210217 $ newElement = $ util ->updateTag ($ id , array ('sh5_pid ' => $ newPid ));
211218 $ util ->createOrUpdateCorresppondingTag ($ newElement );
212- }
219+ }
213220 }
214221 }
215222 }
216223
217224 /**
218225 * Callback function to add the JS for colorization the the markup
219- *
226+ *
220227 * @param type $objRow
221228 * @param type $strBuffer
222229 * @param type $objElement
223230 * @return String
224231 */
225232 public static function addColorizeJs ($ objRow , $ strBuffer , $ objElement )
226233 {
227- // if the element is no type of semantic html5 or the element ist not
234+ // if the element is no type of semantic html5 or the element ist not
228235 // renderen in the backend, do nothing
229- if (TL_MODE == 'BE ' && ($ objRow ->type == 'sHtml5Start ' || $ objRow ->type == 'sHtml5End ' )) {
236+ $ request = System::getContainer ()->get ('request_stack ' )->getCurrentRequest ();
237+ if ($ request && System::getContainer ()->get ('contao.routing.scope_matcher ' )->isBackendRequest ($ request ) && ($ objRow ->type == 'sHtml5Start ' || $ objRow ->type == 'sHtml5End ' )) {
230238 //get the color of the parent start-tag or rotate the color
231239 if ($ objRow ->type == 'sHtml5End ' ) {
232240 $ color = self ::$ elementColors [$ objRow ->sh5_pid ] ?? null ;
@@ -235,7 +243,7 @@ public static function addColorizeJs($objRow, $strBuffer, $objElement)
235243 self ::$ elementColors [$ objRow ->id ] = $ color ;
236244 }
237245
238- $ template = new \ BackendTemplate ('be_semantic_html5_colorizejs ' );
246+ $ template = new BackendTemplate ('be_semantic_html5_colorizejs ' );
239247 $ template ->id = $ objRow ->id ;
240248 $ template ->color = $ color ;
241249
@@ -247,11 +255,11 @@ public static function addColorizeJs($objRow, $strBuffer, $objElement)
247255
248256 /**
249257 * Returns all valid html5 tag for the given datacontainer
250- *
251- * @param \ DataContainer $dc
258+ *
259+ * @param DataContainer $dc
252260 * @return array The array with the valid html5 tags
253261 */
254- public function getHtml5Tags (\ DataContainer $ dc ) {
262+ public function getHtml5Tags (DataContainer $ dc ) {
255263
256264 return $ GLOBALS ['TL_HTML5 ' ]['tags ' ][$ dc ->table ];
257265 }
0 commit comments