Notification.php 16.8 KB
Newer Older
Luke Ward's avatar
Luke Ward committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
<?php
    namespace dtw;

    class Notification {
        private $_notification;

        public function __construct($data = null) {
            $DtW = \dtw\DtW::getInstance();

            $this->_notification = new \stdClass();
            if ($data) {
                $this->parse($data);
            }
        }

        public function __set($name, $value) {
            $this->_notification->$name = $value;
        }


        public function __isset($name) {
            return property_exists($this->_notification, $name);
        }


        public function __get($name) {
            return $this->_notification->$name;
        }

        public function markAsRead() {
            $DtW = \dtw\DtW::getInstance();

            // Mark notifications as read in DB
            $stmt = \dtw\DtW::$db->prepare('
                UPDATE user_notifications
                SET `read` = now()
                WHERE `to` = :userID AND `read` IS NULL AND `notification_id` = :notificationID
                ORDER BY `created` DESC
            ');
            $stmt->execute(array(
                ':userID' =>  $DtW->user->id,
                ':notificationID' =>  $this->id
            ));
        }

        public function delete() {
            // Check user is the recipient
        }

        public function save($email = false) {
            $DtW = \dtw\DtW::getInstance();

            // Check user is the recipient
            if (!$this->_notification->to || !ctype_digit($this->_notification->to)) {
                throw new \Exception('Invalid to');
            }

            if (!$this->_notification->type) {
                throw new \Exception('Invalid type');
            }

            // Check user allows this notification type
            $allowed = \dtw\user\Settings::getUserSetting($this->_notification->to, 'notifications.' . $this->_notification->type);
            if ($allowed === false) {
                return;
                // throw new \Exception('User has declined to recieve these notifications');
            }

            if (!$this->_notification->from) {
                $this->_notification->from = $DtW->user->id;
            }

            if ($this->_notification->type !== 'message.new') {
                // Insert into DB
                $stmt = \dtw\DtW::$db->prepare('
                    INSERT INTO user_notifications (`to`, `from`, `type`, `data`)
                    VALUES (:to, :from, :type, :data);
                ');
                $stmt->execute(array(
                    ':to' => $this->_notification->to,
                    ':from' => $this->_notification->from,
                    ':type' => $this->_notification->type,
                    ':data' => json_encode($this->_notification->data)
                ));

                $this->_notification->id = \dtw\DtW::$db->lastInsertId();
                $this->_notification->created = date('c');

                // Insert into Redis incase user is logged in
                $key = 'user:' . $this->_notification->to . ':notifications';
                \dtw\DtW::$redis->sAdd($key, json_encode($this->_notification));
                \dtw\DtW::$redis->expire($key, 900);
            }

            // Queue email
            $emails = \dtw\user\Settings::getUserSetting($this->_notification->to, 'emails.notifications');
            if ($emails || $email === true) {
                $this->parse($this->_notification);
                $message = $this->_notification->message;

101 102
                if ($message->email) {
                    \dtw\utils\Emailer::send($this->_notification->to, $message->email->subject, 'notification', $message->email);
Luke Ward's avatar
Luke Ward committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
                }
            }
        }

        private function parse($data) {
            $DtW = \dtw\DtW::getInstance();

            $this->_notification->id = $data->id;

            try {
                $this->_notification->from = \dtw\Users::getUser($data->from);
            } catch (\Exception $e) {
                // User doesn't exist
            }

            $this->_notification->unread = !$data->read;

            $this->_notification->created = strtotime($data->created);

            if (!$this->_notification->message) {
                try {
                    $this->_notification->message = $this->getMessage($data->type, $data->data);
                } catch (\Exception $e) {
                    // Piece missing
                }
            }
        }

        private function getMessage($type, $data) {
            switch ($type) {
                case 'discussion.reply': $message = $this->getMessageDiscussionReply($data); break;
                case 'discussion.mention': $message = $this->getMessageDiscussionMention($data); break;
                case 'discussion.flag': $message = $this->getMessageDiscussionFlag($data); break;
                case 'discussion.moderation': $message = $this->getMessageDiscussionModeration($data); break;
                case 'article.moderation': $message = $this->getMessageArticleModeration($data); break;
                case 'medal.awarded': $message = $this->getMessageMedalAwarded($data); break;
                case 'ticket.reply': $message = $this->getMessageTicketReply($data); break;
                case 'ticket.status': $message = $this->getMessageTicketStatus($data); break;
                case 'message.new': $message = $this->getMessageMessageNew($data); break;
                case 'profile.follow': $message = $this->getMessageProfileFollow($data); break;
                default: $message = 'Unknown notification';
            }

            return $message;
        }

        private function getMessageDiscussionReply($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }
            
            // Get thread
            $DtW = \dtw\DtW::getInstance();
            $DtW->load('Discussions');
            try {
                $thread = $DtW->discussions->getThread($data->thread);
163
                $post = $DtW->discussions->getPost($thread->ID, $data->post);
Luke Ward's avatar
Luke Ward committed
164 165 166 167 168 169 170 171
            } catch (\Exception $e) {
                $messages = new \stdClass();
                $message = "%s posted a new mesage in a deleted thread";
                $messages->full = sprintf($message, $this->_notification->from->getLink());
                $messages->short =  "Reply in [deleted]";
                return $messages;
            }

172
            $url = $thread->permalink . '?notification=' . $this->_notification->id . '#post-' . $post->ID;
Luke Ward's avatar
Luke Ward committed
173 174 175 176 177 178 179

            $messages = new \stdClass();
            $message = "%s posted a new mesage in <a href='%s'>%s</a>";
            $messages->full = sprintf($message, $this->_notification->from->getLink(), $url, $thread->title);
            $message = "Reply in <a href='%s'>%s</a>";
            $messages->short = sprintf($message, $url, $thread->title);

180
            $messages->email = new \stdClass();
Luke Ward's avatar
Luke Ward committed
181
            $message = "%s posted a new mesage in \"%s\"";
182 183 184 185
            $messages->email->subject = sprintf($message, $this->_notification->from->username, htmlspecialchars($thread->title));
            $message = "%s posted a new mesage in <a href='%s'>%s</a>";
            $messages->email->message = sprintf($message, $this->_notification->from->getLink(), $url, htmlspecialchars($thread->title));
            $messages->email->excerpt = nl2br($post->message->safe);
Luke Ward's avatar
Luke Ward committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

            return $messages;
        }

        private function getMessageDiscussionMention($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }
            
            // Get thread
            $DtW = \dtw\DtW::getInstance();
            $DtW->load('Discussions');
            try {
                $thread = $DtW->discussions->getThread($data->thread);
Luke Ward's avatar
Luke Ward committed
204
                $post = $DtW->discussions->getPost($thread->ID, $data->post);
Luke Ward's avatar
Luke Ward committed
205 206 207 208 209 210 211 212
            } catch (\Exception $e) {
                $messages = new \stdClass();
                $message = "%s mentioned you in a deleted thread";
                $messages->full = sprintf($message, $this->_notification->from->getLink());
                $messages->short =  "Mentioned in [deleted]";
                return $messages;
            }

213
            $url = $thread->permalink . '?notification=' . $this->_notification->id . '#post-' . $post->ID;
Luke Ward's avatar
Luke Ward committed
214 215 216 217 218 219 220

            $messages = new \stdClass();
            $message = "%s mentioned you in <a href='%s'>%s</a>";
            $messages->full = sprintf($message, $this->_notification->from->getLink(), $url, $thread->title);
            $message = "Mentioned in <a href='%s'>%s</a>";
            $messages->short = sprintf($message, $url, $thread->title);

221
            $messages->email = new \stdClass();
Luke Ward's avatar
Luke Ward committed
222
            $message = "%s mentioned you in \"%s\"";
223 224 225 226
            $messages->email->subject = sprintf($message, $this->_notification->from->username, htmlspecialchars($thread->title));
            $message = "%s mentioned you in <a href='%s'>%s</a>";
            $messages->email->message = sprintf($message, $this->_notification->from->getLink(), $url, htmlspecialchars($thread->title));
            $messages->email->excerpt = nl2br($post->message->safe);
Luke Ward's avatar
Luke Ward committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

            return $messages;
        }

        private function getMessageDiscussionFlag($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }

            // Get thread
            $DtW = \dtw\DtW::getInstance();
            $DtW->load('Discussions');
            try {
                $thread = $DtW->discussions->getThread($data->thread);
            } catch (\Exception $e) {
                $messages = new \stdClass();
                $message = "A flag was %s";
                $messages->full = sprintf($message, $data->status);
                $message = "Flag %s";
                $messages->short = sprintf($message, $data->status);
                return $messages;
            }

            $messages = new \stdClass();
            $message = "A flag in <a href='%s'>%s</a> was %s";
            $messages->full = sprintf($message, $url, $article->title, $data->status);
            $message = "Flag %s";
            $messages->short = sprintf($message, $data->status);

            return $messages;
        }

        private function getMessageDiscussionModeration($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }
            
            // Get thread
            $DtW = \dtw\DtW::getInstance();
            $DtW->load('Discussions');
            try {
                $thread = $DtW->discussions->getThread($data->thread);
            } catch (\Exception $e) {
                //
            }

            $url = $thread->permalink . '?notification=' . $this->_notification->id;

            $messages = new \stdClass();
            $message = "Your post in <a href='%s'>%s</a> was deleted";
            $messages->full = sprintf($message, $url, $thread->title);
            $message = "<a href='%s'>Post</a> deleted";
            $messages->short = sprintf($message, $thread->title);

            return $messages;
        }

        private function getMessageArticleModeration($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }
            
            // Get thread
            $article = \dtw\Articles::getArticle($data->article);

            $url = $article->permalink . '?notification=' . $this->_notification->id;

            $messages = new \stdClass();
            $message = "Your article <a href='%s'>%s</a> was %s";
            $messages->full = sprintf($message, $url, $article->title, $data->status);
            $message = "<a href='%s'>%s</a> %s";
            $messages->short = sprintf($message, $url, $article->title, $data->status);

312
            $messages->email = new \stdClass();
Luke Ward's avatar
Luke Ward committed
313
            $message = "Your article %s was %s";
314 315 316
            $messages->email->subject = sprintf($message, $article->title, $data->status);
            $message = "Your article <a href='%s'>%s</a> was %s";
            $messages->email->message = sprintf($message, $url, htmlspecialchars($article->title), $data->status);
Luke Ward's avatar
Luke Ward committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

            return $messages;
        }

        private function getMessageMedalAwarded($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }
            
            // Get thread
            $medal = Medals::getMedal($data->medal);

            $url = \dtw\DtW::$config->get('site.domain') . '/help/medals?notification=' . $this->_notification->id . '#' . $medal->label;

            $messages = new \stdClass();
            $message = "You have been awarded the <i class='fas fa-medal c-%s'></i> <a href='%s'>%s</a> medal";
            $messages->full = sprintf($message, $medal->colour, $url, $medal->label);
            $message = "Awarded <i class='fas fa-medal c-%s'></i> <a href='%s'>%s</a>";
            $messages->short = sprintf($message, $medal->colour, $url, $medal->label);

            return $messages;
        }

        private function getMessageTicketReply($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }

            $url = \dtw\DtW::$config->get('site.domain') . '/help/contact/' . $data->ticket . '?notification=' . $this->_notification->id;

            $messages = new \stdClass();
            $message = "A reply has been added to <a href='%s'>Ticket #%d</a>";
            $messages->full = sprintf($message, $url, $data->ticket);
            $message = "Reply to <a href='%s'>Ticket #%d</a>";
            $messages->short = sprintf($message, $url, $data->ticket);

361 362 363
            $messages->email = new \stdClass();
            $messages->email->subject = "A reply has been added to a ticket";
            $messages->email->message = $messages->full;
Luke Ward's avatar
Luke Ward committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

            return $messages;
        }

        private function getMessageTicketStatus($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }

            $url = \dtw\DtW::$config->get('site.domain') . '/help/contact/' . $data->ticket . '?notification=' . $this->_notification->id;

            $messages = new \stdClass();
            $message = "<a href='%s'>Ticket #%d</a> marked as %s";
            $messages->full = sprintf($message, $url, $data->ticket, $data->status);
            $message = "<a href='%s'>Ticket #%d</a> %s";
            $messages->short = sprintf($message, $url, $data->ticket, $data->status);

385
            $messages->email = new \stdClass();
Luke Ward's avatar
Luke Ward committed
386
            $message = "Your ticket has been marked as %s";
387 388
            $messages->email->subject = sprintf($message, $data->status);
            $messages->email->message = $messages->full;
Luke Ward's avatar
Luke Ward committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

            return $messages;
        }

        private function getMessageMessageNew($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            if (!is_object($data)) {
                $data = json_decode($data);
            }

            $url = \dtw\DtW::$config->get('site.domain') . '/message/' . $data->conversation . '?notification=' . $this->_notification->id;

            $messages = new \stdClass();
            $message = "%s sent you a new <a href='%s'>personal message</a>";
            $messages->full = sprintf($message, $this->_notification->from->getLink(), $url);
            $message = "New <a href='%s'>PM</a> from %s";
            $messages->short = sprintf($message, $url, $this->_notification->from->getLink());

410
            $messages->email = new \stdClass();
Luke Ward's avatar
Luke Ward committed
411
            $message = "%s sent you a new private message";
412 413
            $messages->email->subject = sprintf($message, $this->_notification->from->username, $thread->title);
            $messages->email->message = $messages->full;
Luke Ward's avatar
Luke Ward committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

            return $messages;
        }

        private function getMessageProfileFollow($data) {
            if (!$data) {
                $message = 'Unknown notification';
            }

            $link = $this->_notification->from->getLink(null, '?notification=' . $this->_notification->id);

            $messages = new \stdClass();
            $message = "%s started following your profile";
            $messages->full = sprintf($message, $link);
            $message = "%s followed you";
            $messages->short = sprintf($message, $link);

            return $messages;
        }

    }