|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Creates and sends notifications to user. |
| 4 | + * |
| 5 | + * @copyright 2009-2019 Vanilla Forums Inc. |
| 6 | + * @license GPL-2.0-only |
| 7 | + * @package Dashboard |
| 8 | + * @since 2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +use Vanilla\Formatting\Formats\HtmlFormat; |
| 12 | + |
| 13 | +/** |
| 14 | + * Handle /notifications endpoint. |
| 15 | + */ |
| 16 | +class NotificationsController extends Gdn_Controller { |
| 17 | + |
| 18 | + /** |
| 19 | + * CSS, JS and module includes. |
| 20 | + */ |
| 21 | + public function initialize() { |
| 22 | + $this->Head = new HeadModule($this); |
| 23 | + $this->addJsFile('jquery.js'); |
| 24 | + $this->addJsFile('jquery.form.js'); |
| 25 | + $this->addJsFile('jquery.popup.js'); |
| 26 | + $this->addJsFile('jquery.gardenhandleajaxform.js'); |
| 27 | + $this->addJsFile('global.js'); |
| 28 | + $this->addCssFile('style.css'); |
| 29 | + $this->addCssFile('vanillicon.css', 'static'); |
| 30 | + $this->addModule('GuestModule'); |
| 31 | + parent::initialize(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Adds inform messages to response for inclusion in pages dynamically. |
| 36 | + * |
| 37 | + * @since 2.0.18 |
| 38 | + * @access public |
| 39 | + */ |
| 40 | + public function inform() { |
| 41 | + $this->deliveryType(DELIVERY_TYPE_BOOL); |
| 42 | + $this->deliveryMethod(DELIVERY_METHOD_JSON); |
| 43 | + |
| 44 | + // Retrieve all notifications and inform them. |
| 45 | + NotificationsController::informNotifications($this); |
| 46 | + $this->fireEvent('BeforeInformNotifications'); |
| 47 | + |
| 48 | + $this->render(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Grabs all new notifications and adds them to the sender's inform queue. |
| 53 | + * |
| 54 | + * This method gets called by dashboard's hooks file to display new |
| 55 | + * notifications on every pageload. |
| 56 | + * |
| 57 | + * @since 2.0.18 |
| 58 | + * @access public |
| 59 | + * |
| 60 | + * @param Gdn_Controller $sender The object calling this method. |
| 61 | + */ |
| 62 | + public static function informNotifications($sender) { |
| 63 | + $session = Gdn::session(); |
| 64 | + if (!$session->isValid()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $activityModel = new ActivityModel(); |
| 69 | + // Get five pending notifications. |
| 70 | + $where = [ |
| 71 | + 'NotifyUserID' => Gdn::session()->UserID, |
| 72 | + 'Notified' => ActivityModel::SENT_PENDING]; |
| 73 | + |
| 74 | + // If we're in the middle of a visit only get very recent notifications. |
| 75 | + $where['DateUpdated >'] = Gdn_Format::toDateTime(strtotime('-5 minutes')); |
| 76 | + |
| 77 | + $activities = $activityModel->getWhere($where, '', '', 5, 0)->resultArray(); |
| 78 | + |
| 79 | + $activityIDs = array_column($activities, 'ActivityID'); |
| 80 | + $activityModel->setNotified($activityIDs); |
| 81 | + |
| 82 | + $sender->EventArguments['Activities'] = &$activities; |
| 83 | + $sender->fireEvent('InformNotifications'); |
| 84 | + |
| 85 | + foreach ($activities as $activity) { |
| 86 | + if ($activity['Photo']) { |
| 87 | + $userPhoto = anchor( |
| 88 | + img($activity['Photo'], ['class' => 'ProfilePhotoMedium']), |
| 89 | + $activity['Url'], |
| 90 | + 'Icon' |
| 91 | + ); |
| 92 | + } else { |
| 93 | + $userPhoto = ''; |
| 94 | + } |
| 95 | + |
| 96 | + $excerpt = ''; |
| 97 | + $story = $activity['Story'] ?? null; |
| 98 | + $format = $activity['Format'] ?? HtmlFormat::FORMAT_KEY; |
| 99 | + $excerpt = htmlspecialchars($story ? Gdn::formatService()->renderExcerpt($story, $format) : $excerpt); |
| 100 | + $activityClass = ' Activity-'.$activity['ActivityType']; |
| 101 | + |
| 102 | + // FIX: https://github.com/topcoder-platform/forums/issues/506 |
| 103 | + $sender->informMessage( |
| 104 | + $userPhoto |
| 105 | + .wrap($activity['Headline'], 'div', ['class' => 'Title']) |
| 106 | + //.wrap($excerpt, 'div', ['class' => 'Excerpt']) |
| 107 | + ,'Dismissable AutoDismiss'.$activityClass.($userPhoto == '' ? '' : ' HasIcon') |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments