Browse code

Slot Assistant alpha release

Benjamin Roth authored on19/07/2024 11:54:43
Showing2 changed files
... ...
@@ -7,6 +7,9 @@
7 7
 {% endblock %}
8 8
 
9 9
 {% block main %}
10
+    <div class="tl_message">
11
+        {{ messages|raw }}
12
+    </div>
10 13
     <div id="tl_buttons">
11 14
         <a href="#" class="header_back" title="" accesskey="b" onclick="Backend.getScrollOffset()">Zurück</a>
12 15
     </div>
... ...
@@ -26,6 +26,7 @@ use Contao\FileTree;
26 26
 use Contao\FrontendUser;
27 27
 use Contao\Image;
28 28
 use Contao\Input;
29
+use Contao\Message;
29 30
 use Contao\SelectMenu;
30 31
 use Contao\StringUtil;
31 32
 use Contao\System;
... ...
@@ -103,6 +104,7 @@ class WeinanlieferungSlotAssistantController extends AbstractController
103 104
         }
104 105
 
105 106
         $arrData['fields'] = $this->getWidgets();
107
+        $arrData['messages'] = Message::generateUnwrapped() . Backend::getSystemMessages();
106 108
 
107 109
         // Handle ajax request post actions
108 110
         if ($_POST && Environment::get('isAjaxRequest'))
... ...
@@ -424,6 +426,8 @@ class WeinanlieferungSlotAssistantController extends AbstractController
424 426
             $StartDate = new Date($timeframe['date_start'], Date::getNumericDatimFormat());
425 427
             $EndDate = new Date($timeframe['date_end'], Date::getNumericDatimFormat());
426 428
 
429
+            $now = time();
430
+            $intNewSlots = 0;
427 431
             $intStart  = $StartDate->tstamp;
428 432
             $intEnd  = $EndDate->tstamp;
429 433
             $intBuffer = intval(Input::post('buffer')) * 60;
... ...
@@ -433,7 +437,7 @@ class WeinanlieferungSlotAssistantController extends AbstractController
433 437
             $arrEndTimes = [];
434 438
 
435 439
             // Get existing slots in the timeframe
436
-            $ExistingSlots = $this->db->prepare("SELECT id, date, time as 'start', time+duration as 'end', duration FROM tl_vr_wa_slot WHERE pid = ? AND (time >= ? OR time+$intDuration+$intBuffer >= ?) AND (time <= ? OR time+$intDuration+$intBuffer <= ?) ORDER BY time")
440
+            $ExistingSlots = $this->db->prepare("SELECT id, date, time as 'start', (time+duration*60) as 'end', duration FROM tl_vr_wa_slot WHERE pid = ? AND (time >= ? OR time+$intDuration+$intBuffer >= ?) AND (time <= ? OR time+$intDuration+$intBuffer <= ?) ORDER BY time")
437 441
                                       ->executeQuery([$Standort->id, $intStart, $intStart, $intEnd, $intEnd]);
438 442
 
439 443
             if ($ExistingSlots->rowCount())
... ...
@@ -449,20 +453,70 @@ class WeinanlieferungSlotAssistantController extends AbstractController
449 453
                 }
450 454
             }
451 455
 
456
+            // Create all possible timeslots for given timeframe
452 457
             $intTime = $intStart;
453 458
             while ($intTime < $intEnd-$intDuration)
454 459
             {
455
-                dump(Date::parse(Date::getNumericDatimFormat(),$intTime));
456
-                $intTime = $intTime + $intDuration + $intBuffer;
460
+                $arrConflicts = [];
457 461
 
458
-                /** Todo:
459
-                 * Check time against start and stop times and take buffer into account.
460
-                 * If theres a conflict, determine the next possible start time and check again.
461
-                 */
462
-            }
462
+                foreach ($arrStartTimes as $check_start)
463
+                {
464
+                    $checkslot = $arrTimes[$check_start];
465
+                    if (
466
+                        (
467
+                            $intTime >= $checkslot['start']
468
+                            && $intTime <= $checkslot['end']
469
+                        )
470
+                        || (
471
+                            $intTime + $intDuration + $intBuffer > $checkslot['start']
472
+                            && $intTime + $intDuration + $intBuffer < $checkslot['end']
473
+                        )
474
+                    ) {
475
+//                        dump('Konflikt Start: ' . Date::parse(Date::getNumericDatimFormat(),$checkslot['start']));
476
+//                        dump('Konflikt Ende: ' . Date::parse(Date::getNumericDatimFormat(),$checkslot['end']));
477
+
478
+                        $arrConflicts[] = $checkslot['end'];
479
+                    }
480
+                }
463 481
 
464
-            //$ExistingSlots = $this->db->prepare("SELECT id, date, time, duration FROM tl_vr_wa_slot WHERE pid = ? AND (time >= ? OR time+duration*60+$intBuffer >= ?) AND (time <= ? OR time+duration*60+$intBuffer <= ?)")
482
+                if (\count($arrConflicts) > 0)
483
+                {
484
+                    $intTime = max($arrConflicts) + $intBuffer;
485
+                    continue;
486
+                }
465 487
 
488
+//                dump(Date::parse(Date::getNumericDatimFormat(),$intTime));
489
+
490
+                $NewDateTime = new Date($intTime);
491
+                $NewSlot = new WeinanlieferungSlotsModel();
492
+                $NewSlot->setRow([
493
+                    'pid' => $SlotType->pid,
494
+                    'tstamp' => $now,
495
+                    'date' => $NewDateTime->dayBegin,
496
+                    'time' => $intTime,
497
+                    'duration' => $intDuration/3600,
498
+                    'behaelter' => $SlotType->containers,
499
+                    'anmerkungen' => Input::post('annotation'),
500
+                    'buchbar_ab' => (int) $intTime-intval(Input::post('bookableFrom'))*3600,
501
+                    'buchbar_bis' => (int) $intTime-intval(Input::post('bookableTill'))*3600,
502
+                    'published' => '1'
503
+                ]);
504
+
505
+                if (Input::post('enclosure'))
506
+                {
507
+                    $NewSlot->addEnclosure = '1';
508
+                    $NewSlot->enclosure = explode(',',Input::post('enclosure'));
509
+                }
510
+
511
+                $NewSlot->save();
512
+                $intNewSlots++;
513
+
514
+                $intTime = $intTime + $intDuration + $intBuffer;
515
+
516
+            }
466 517
         }
518
+
519
+        Message::addConfirmation(sprintf('Es wurden %s neue Zeitslots angelegt.',$intNewSlots));
520
+//        Backend::reload();
467 521
     }
468 522
 }