Browse code

Automatically insert end element

Benjamin Roth authored on19/06/2017 14:53:44
Showing1 changed files
... ...
@@ -17,6 +17,7 @@
17 17
  * Add callback
18 18
  */
19 19
 $GLOBALS['TL_DCA']['tl_content']['config']['onload_callback'][] = array('tl_content_eSM_slick', 'showJsLibraryHint');
20
+$GLOBALS['TL_DCA']['tl_content']['config']['onsubmit_callback'][] = array('tl_content_eSM_slick', 'insertEndElement');
20 21
 
21 22
 /**
22 23
  * Add palettes to tl_content
... ...
@@ -197,4 +198,58 @@ class tl_content_eSM_slick extends Backend
197 198
         break;
198 199
     }
199 200
   }
201
+
202
+  /**
203
+   * Automatically insert end element if start element ist about to be added
204
+   *
205
+   * @param DataContainer $dc
206
+   */
207
+  public function insertEndElement(\DataContainer $dc)
208
+  {
209
+    $activeRecord = $dc->activeRecord;
210
+    if (!$activeRecord) {
211
+      return;
212
+    }
213
+
214
+    if ($activeRecord->type === 'slickSliderStart' || $activeRecord->type === 'slickSliderSlideStart') {
215
+
216
+      // Find the next boxwrapper element
217
+      $nextElement = \Database::getInstance()
218
+        ->prepare("
219
+					SELECT type
220
+					FROM tl_content
221
+					WHERE pid = ?
222
+						AND (ptable = ? OR ptable = ?)
223
+            AND type IN ('slickSliderSlideStart', 'slickSliderSlideStop','slickSliderStart', 'slickSliderStop')
224
+						AND sorting > ?
225
+					ORDER BY sorting ASC
226
+					LIMIT 1
227
+				")
228
+        ->execute(
229
+          $activeRecord->pid,
230
+          $activeRecord->ptable ?: 'tl_article',
231
+          $activeRecord->ptable === 'tl_article' ? '' : $activeRecord->ptable,
232
+          $activeRecord->sorting
233
+        );
234
+
235
+      // Check if a stop element should be created
236
+      if (
237
+        !$nextElement->type
238
+        || ($activeRecord->type === 'slickSliderStart' && ($nextElement->type === 'slickSliderSlideStart' || $nextElement->type === 'slickSliderSlideStop' || $nextElement->type === 'slickSliderStart'))
239
+        || ($activeRecord->type === 'slickSliderSlideStart' && ($nextElement->type === 'slickSliderSlideStart' || $nextElement->type === 'slickSliderStart' || $nextElement->type === 'slickSliderStop'))
240
+      ) {
241
+        \Database::getInstance()
242
+          ->prepare('INSERT INTO tl_content %s')
243
+          ->set(array(
244
+            'pid' => $activeRecord->pid,
245
+            'ptable' => $activeRecord->ptable ?: 'tl_article',
246
+            'type' => substr($activeRecord->type, 0, -5) . 'Stop',
247
+            'sorting' => $activeRecord->sorting + 1,
248
+            'tstamp' => time(),
249
+          ))
250
+          ->execute();
251
+      }
252
+
253
+    }
254
+  }
200 255
 }
201 256
\ No newline at end of file