public function main() { $tableFields = array_keys($this->Notification->getColumnTypes()); $timezone = date_default_timezone_get(); $date = new DateTime(); if ($timezone != 'UTC' || $timezone != 'GMT') { $date->setTimezone(new DateTimeZone('UTC')); } $params = ['conditions' => ['AND' => ['Notification.sent' => false, 'Notification.errors' => null]]]; if (in_array('send_on', $tableFields)) { $params['conditions']['AND']['OR'] = ['Notification.send_on' => null, "CONVERT_TZ(Notification.send_on, Notification.timezone, 'UTC') <" => $date->format('Y-m-d H:i:s')]; } $notifications = $this->Notification->find('all', $params); foreach ($notifications as $notification) { $response = NotificationUtility::notify($notification['Notification']); if ($response === true) { $this->Notification->id = $notification['Notification']['id']; $this->Notification->saveField('sent', true); if (in_array('sent_on', $tableFields)) { $this->Notification->saveField('sent_on', date('Y-m-d H:i:s')); } $this->out($notification['Notification']['type'] . ' sent!'); } else { $this->Notification->id = $notification['Notification']['id']; $this->Notification->saveField('errors', json_encode($response)); $this->out($notification['Notification']['type'] . ' error! ' . json_encode($response)); } } }
public static function notify($notification) { $data = json_decode($notification['data']); $notify = []; try { $property = static::getProperty($notification); } catch (Exception $e) { if (!empty($data->to)) { $property = $data->to; } else { return 'Could not get property for notification: ' . $e->getMessage(); } } if (empty($property) && !empty($data->to)) { $property = $data->to; } elseif (empty($property)) { return false; } if (!empty($notification['condition'])) { $test = static::checkConditions($notification); if ($test !== true) { return 'Conditions not met: ' . $test; } } switch ($notification['type']) { case 'EMAIL': $notify['to'] = explode(',', $property); $notify = array_merge($notify, json_decode(json_encode($data), true)); if (empty($notify['emailFormat']) && !empty($notify['format'])) { $notify['emailFormat'] = $notify['format']; } if (empty($notify['viewVars']) && !empty($notify['vars'])) { $notify['viewVars'] = $notify['vars']; } return NotificationUtility::email($notify); break; case 'SMS': $notify['to'] = $property; $notify['notification'] = $data->notification; return NotificationUtility::sms($notify); break; } return true; }