/**
  * Saves an notification.
  *
  * @param PushNotifications_NotificationModel $notification
  *
  * @throws \Exception
  *
  * @return bool
  */
 public function saveNotification(PushNotifications_NotificationModel $notification)
 {
     $isNewNotification = !$notification->id;
     // Notification data
     if (!$isNewNotification) {
         $notificationRecord = PushNotifications_NotificationRecord::model()->findById($notification->id);
         if (!$notificationRecord) {
             throw new Exception(Craft::t('No notification exists with the ID “{id}”', array('id' => $notification->id)));
         }
     } else {
         $notificationRecord = new PushNotifications_NotificationRecord();
     }
     $notificationRecord->appId = $notification->appId;
     $notificationRecord->title = $notification->title;
     $notificationRecord->body = $notification->body;
     $notificationRecord->command = $notification->command;
     $notificationRecord->schedule = $notification->schedule ? $notification->schedule : $notificationRecord->schedule;
     $notificationRecord->validate();
     $notification->addErrors($notificationRecord->getErrors());
     if (!$notification->hasErrors()) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             // Fire an 'onBeforeSaveNotification' event
             $this->onBeforeSaveNotification(new Event($this, array('notification' => $notification, 'isNewNotification' => $isNewNotification)));
             if (craft()->elements->saveElement($notification)) {
                 // Now that we have an element ID, save it on the other stuff
                 if ($isNewNotification) {
                     $notificationRecord->id = $notification->id;
                 }
                 $notificationRecord->save(false);
                 // Fire an 'onSaveNotification' event
                 $this->onSaveNotification(new Event($this, array('notification' => $notification, 'isNewNotification' => $isNewNotification)));
                 // Check if we're using the cronjob plugin and have a schedule
                 if (craft()->plugins->getPlugin('cronjob') && $notification->schedule) {
                     // Schedule the notification
                     craft()->pushNotifications_push->scheduleNotification($notification);
                 } else {
                     // Send the notification
                     craft()->pushNotifications_push->sendNotification($notification);
                 }
                 // Now do the transaction
                 if ($transaction !== null) {
                     $transaction->commit();
                 }
                 return true;
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
     }
     return false;
 }
 /**
  * Populates an element model based on a query result.
  *
  * @param array $row
  *
  * @return array
  */
 public function populateElementModel($row)
 {
     return PushNotifications_NotificationModel::populateModel($row);
 }