/** * Schedule a notification. * * @param PushNotifications_NotificationModel $notification */ public function scheduleNotification(PushNotifications_NotificationModel $notification) { // Get schedule $schedule = $notification->schedule; // Initialize the cronjob repository $adapter = new Cronjob_AdapterModel(); $repository = new Cronjob_RepositoryModel($adapter); // Check if cronjob exists $results = $repository->findJobByRegex('/^' . $notification->id . '$/'); $cronjob = isset($results[0]) ? $results[0] : new CronjobModel(); // Set up cronjob $cronjob->minutes = $schedule->format('i'); $cronjob->hours = $schedule->format('H'); $cronjob->dayOfMonth = $schedule->format('d'); $cronjob->months = $schedule->format('m'); $cronjob->taskCommandLine = CRAFT_APP_PATH . 'etc/console/yiic pushnotifications send ' . $notification->id; $cronjob->comments = $notification->id; // Now save the cronjob if (!isset($results[0])) { $repository->addJob($cronjob); } $repository->persist(); }
/** * Deletes an notification. */ public function actionDeleteNotification() { $this->requirePostRequest(); // Get notification id $notificationId = craft()->request->getRequiredPost('notificationId'); // Check if we're using the cronjob manager if (craft()->plugins->getPlugin('cronjob')) { // Remove cronjob $repository = new Cronjob_RepositoryModel(new Cronjob_AdapterModel()); $results = $repository->findJobByRegex('/^' . $notificationId . '$/'); if (isset($results[0])) { $repository->removeJob($results[0]); $repository->persist(); } } // Delete the element if (craft()->elements->deleteElementById($notificationId)) { craft()->userSession->setNotice(Craft::t('Notification deleted.')); $this->redirectToPostedUrl(); } else { craft()->userSession->setError(Craft::t('Couldn’t delete notification.')); } }