Example #1
0
 /**
  * Sends a theme notification to the Factory.
  *
  * This is going to contact the Factory, so it qualifies as a third-party
  * call, therefore calling it during a normal page load is not advisable. A
  * possibly safer solution could be executing this via a menu callback called
  * through an asynchronous JavaScript call.
  *
  * If the request does not succeed (and $store_failed_notification is truthy),
  * the notification will be stored so that we may try again later when cron
  * runs.
  *
  * @param string $scope
  *   The scope. Either "theme", "site", "group", or "global".
  * @param string $event
  *   The type of theme event that occurred. Either "create", "modify", or
  *   "delete".
  * @param int $nid
  *   The node ID associated with the scope. Only required for "group" scope
  *   notifications. If empty, it will be filled in automatically for "theme"
  *   and "site" scope notifications.
  * @param string $theme
  *   The system name of the theme the event relates to. Only relevant for
  *   "theme" scope notifications.
  * @param int $timestamp
  *   The timestamp when the notification was created.
  * @param bool $store_failed_notification
  *   Optional variable to disable storing a notification when the sending
  *   fails. Should be only used in case of notifications which have been
  *   already added to the pending notification table.
  *
  * @return array
  *   The message response body and code.
  */
 public function sendNotification($scope, $event, $nid = NULL, $theme = NULL, $timestamp = NULL, $store_failed_notification = TRUE)
 {
     if (!$this->isEnabled()) {
         return array('code' => 500, 'data' => array('message' => t('The theme change notification feature is not enabled.')));
     }
     try {
         if (empty($nid) && in_array($scope, array('theme', 'site'))) {
             $site = acsf_get_acsf_site();
             $nid = $site->site_id;
         }
         $parameters = array('scope' => $scope, 'event' => $event, 'nid' => $nid);
         if ($theme) {
             $parameters['theme'] = $theme;
         }
         if ($timestamp) {
             $parameters['timestamp'] = $timestamp;
         }
         $message = new AcsfMessageRest('POST', 'site-api/v1/theme/notification', $parameters);
         $message->send();
         $response = array('code' => $message->getResponseCode(), 'data' => $message->getResponseBody());
     } catch (\Exception $e) {
         $error_message = t('AcsfThemeNotify failed with error: @message.', array('@message' => $e->getMessage()));
         syslog(LOG_ERR, $error_message);
         // Send a log message to the Factory.
         $acsf_log = new AcsfLog();
         $acsf_log->log('theme_notify', $error_message, LOG_ERR);
         $response = array('code' => 500, 'data' => array('message' => $error_message));
     }
     if ($store_failed_notification && $response['code'] !== 200) {
         $this->addNotification($event, $theme);
     }
     return $response;
 }