示例#1
0
文件: Log.php 项目: hubzero/framework
 /**
  * Create an activity log entry.
  *
  * @param   mixed    $data
  * @param   array    $recipients
  * @return  boolean
  */
 public static function log($data = array(), $recipients = array())
 {
     if (is_object($data)) {
         $data = (array) $data;
     }
     if (is_string($data)) {
         $data = array('description' => $data);
         $data['action'] = 'create';
         if (substr(strtolower($data['description']), 0, 6) == 'update') {
             $data['action'] = 'update';
         }
         if (substr(strtolower($data['description']), 0, 6) == 'delete') {
             $data['action'] = 'delete';
         }
     }
     try {
         $activity = self::blank()->set($data);
         if (!$activity->save()) {
             return false;
         }
         // Get everyone subscribed
         $subscriptions = Subscription::all()->whereEquals('scope', $activity->get('scope'))->whereEquals('scope_id', $activity->get('scope_id'))->rows();
         foreach ($subscriptions as $subscription) {
             $recipients[] = array('scope' => 'user', 'scope_id' => $subscription->user_id);
         }
         $sent = array();
         // Do we have any recipients?
         foreach ($recipients as $receiver) {
             // Default to type 'user'
             if (!is_array($receiver)) {
                 $receiver = array('scope' => 'user', 'scope_id' => $receiver);
             }
             // Make sure we have expected data
             if (!isset($receiver['scope']) || !isset($receiver['scope_id'])) {
                 $receiver = array_values($receiver);
                 $receiver['scope'] = $receiver[0];
                 $receiver['scope_id'] = $receiver[1];
             }
             $key = $receiver['scope'] . '.' . $receiver['scope_id'];
             // No duplicate sendings
             if (in_array($key, $sent)) {
                 continue;
             }
             // Create a recipient object that ties a user to an activity
             $recipient = Recipient::blank()->set(['scope' => $receiver['scope'], 'scope_id' => $receiver['scope_id'], 'log_id' => $activity->get('id'), 'state' => 1]);
             $recipient->save();
             $sent[] = $key;
         }
     } catch (Exception $e) {
         return false;
     }
     return true;
 }
示例#2
0
 /**
  * Subscribe
  *
  * @return  string  HTML
  */
 protected function subscribeAction()
 {
     // Login required
     if (User::isGuest() || !$this->resource->id) {
         App::redirect(Route::url($this->link));
     }
     // Load a subscription, if it exists
     $watch = \Hubzero\Activity\Subscription::oneByScope($this->resource->id, 'resource', User::get('id'));
     // Unsubscribing
     if ($this->action == 'unsubscribe') {
         $msg = Lang::txt('PLG_RESOURCES_WATCH_SUCCESS_UNSUBSCRIBED');
         if ($watch->get('id')) {
             if (!$watch->destroy()) {
                 $this->setError($watch->getError());
             }
         }
     }
     // Subscribing
     if ($this->action == 'subscribe') {
         $msg = Lang::txt('PLG_RESOURCES_WATCH_SUCCESS_SUBSCRIBED');
         if (!$watch->get('id')) {
             $watch->set('scope_id', $this->resource->id);
             $watch->set('scope', 'resource');
             $watch->set('user_id', User::get('id'));
             if (!$watch->save()) {
                 $this->setError($watch->getError());
             }
         }
     }
     $url = Route::url('index.php?option=com_resources&' . ($this->resource->alias ? 'alias=' . $this->resource->alias : 'id=' . $this->resource->id));
     // Log the activity
     Event::trigger('system.logActivity', ['activity' => ['action' => $this->action . 'd', 'scope' => 'resource', 'scope_id' => $this->resource->id, 'description' => Lang::txt('PLG_RESOURCES_WATCH_' . strtoupper($this->action) . 'D', '<a href="' . $url . '">' . $this->resource->title . '</a>'), 'details' => array('title' => $this->resource->title, 'url' => $url)], 'recipients' => [User::get('id')]]);
     // Redirect
     App::redirect(Route::url($this->link), $this->getError() ? $this->getError() : $msg, $this->getError() ? 'error' : null);
 }