Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| SlideScheduleController | |
0.00% |
0 / 60 |
|
0.00% |
0 / 7 |
506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| processScheduleForDate | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getSignScheduleForDate | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| processScheduleItem | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
72 | |||
| activateSlide | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| deactivateSlide | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| refreshStoreLoop | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace BuyerKiosk\DigitalSign\Controllers; |
| 5 | |
| 6 | use BuyerKiosk\StoreController; |
| 7 | use Faker\Provider\DateTime; |
| 8 | use BuyerKiosk\Core\Controllers\BaseController; |
| 9 | |
| 10 | class SlideScheduleController { |
| 11 | |
| 12 | private $ably; |
| 13 | |
| 14 | public function __construct() { |
| 15 | $this->global_db = dbConnectByName($_ENV['DB_NAME']); |
| 16 | $this->ably = new \Ably\AblyRest($_ENV['ABLY_KEY']); |
| 17 | |
| 18 | } |
| 19 | |
| 20 | public function processScheduleForDate($date) { |
| 21 | $schedule = $this->getSignScheduleForDate($date); |
| 22 | //if there is at least one item in the schedule, process each item |
| 23 | if($schedule != false) { |
| 24 | foreach($schedule as $item) { |
| 25 | echo "Processing item: " . print_r($item, true); |
| 26 | $this->processScheduleItem($item, $date); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | |
| 31 | } |
| 32 | |
| 33 | private function getSignScheduleForDate($date) { |
| 34 | try { |
| 35 | $select = $this->global_db->prepare("SELECT * FROM digitalSignSchedule WHERE (startDate <= :date AND started IS NULL) OR (expireDate <= :date AND finished = 0)"); |
| 36 | $select->bindParam(":date", $date); |
| 37 | if($select->execute() === false) { |
| 38 | error_log("Error getting schedule: " . print_r($select->errorInfo(), true)); |
| 39 | return false; |
| 40 | } else { |
| 41 | $schedule = $select->fetchAll(\PDO::FETCH_OBJ); |
| 42 | return $schedule; |
| 43 | } |
| 44 | } catch (\PDOException $e) { |
| 45 | error_log($e->getMessage()); |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | private function processScheduleItem($item, $date) { |
| 50 | //If ($item->startDate is not NULL AND startDate <= $date AND $item->started = NULL) then we have a slide that needs to be activated today. If (startDate is null OR $item->started = 1) but ($item->expireDate is not null AND <= $date) then we have a slide that needs to be deactivated today. |
| 51 | $dateObj = new \DateTime($date); |
| 52 | if($item->startDate != NULL) { |
| 53 | $startDate = new \DateTime($item->startDate); |
| 54 | if($startDate <= $dateObj && $item->started == NULL) { |
| 55 | echo "Activating slide: " . print_r($item, true); |
| 56 | $this->activateSlide($item); |
| 57 | } |
| 58 | } |
| 59 | if($item->expireDate != NULL) { |
| 60 | $expireDate = new \DateTime($item->expireDate); |
| 61 | if($expireDate <= $dateObj && ($item->started == 1 || $item->started == NULL)) { |
| 62 | echo "Deactivating slide: " . print_r($item, true); |
| 63 | $this->deactivateSlide($item); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private function activateSlide($item) { |
| 69 | $storeController = new StoreController($item->typeNum); |
| 70 | $store = $storeController->getStore(); |
| 71 | $storeDB = dbConnectByName($store->getDbName()); |
| 72 | $update = $storeDB->prepare("UPDATE dsLoop SET scheduled = 0, startDate = NULL WHERE id = :id"); |
| 73 | $update->bindParam(":id", $item->loopID); |
| 74 | if($update->execute() === false) { |
| 75 | echo "Error activating slide: " . print_r($update->errorInfo(), true); |
| 76 | return false; |
| 77 | } |
| 78 | //Also set the started field in the global schedule to 1 |
| 79 | $updateGlobal = $this->global_db->prepare("UPDATE digitalSignSchedule SET started = 1 WHERE id = :id"); |
| 80 | $updateGlobal->bindParam(":id", $item->id); |
| 81 | if($updateGlobal->execute() === false) { |
| 82 | echo "Error updating global schedule: " . print_r($updateGlobal->errorInfo(), true); |
| 83 | return false; |
| 84 | } |
| 85 | $this->refreshStoreLoop($store); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | private function deactivateSlide($item) { |
| 90 | $storeController = new StoreController($item->typeNum); |
| 91 | $store = $storeController->getStore(); |
| 92 | $storeDB = dbConnectByName($store->getDbName()); |
| 93 | |
| 94 | //delete the item from dsLoop where ID = $item->id |
| 95 | $delete = $storeDB->prepare("DELETE FROM dsLoop WHERE id = :id"); |
| 96 | $delete->bindParam(":id", $item->loopID); |
| 97 | if($delete->execute() === false) { |
| 98 | error_log("Error deleting slide: " . print_r($delete->errorInfo(), true)); |
| 99 | return false; |
| 100 | } |
| 101 | //Also set the finished field in the global schedule to 1 |
| 102 | $updateGlobal = $this->global_db->prepare("UPDATE digitalSignSchedule SET finished = 1 WHERE id = :id"); |
| 103 | $updateGlobal->bindParam(":id", $item->id); |
| 104 | if($updateGlobal->execute() === false) { |
| 105 | error_log("Error updating global schedule: " . print_r($updateGlobal->errorInfo(), true)); |
| 106 | return false; |
| 107 | } |
| 108 | $this->refreshStoreLoop($store); |
| 109 | return true; |
| 110 | } |
| 111 | private function refreshStoreLoop($store) { |
| 112 | $channel = $this->ably->channel($store->getTypeNum()); |
| 113 | // Publish a message to the test channel |
| 114 | $channel->publish($store->getTypeNum(),array("action" => "refresh")); |
| 115 | } |
| 116 | |
| 117 | } |