]> git.mxchange.org Git - friendica.git/blob - src/Model/Notify.php
336b82537bcc9879c1e2719bfceed6ad200ab872
[friendica.git] / src / Model / Notify.php
1 <?php
2
3 namespace Friendica\Model;
4
5 use Friendica\BaseModel;
6 use Friendica\Content\Text\BBCode;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException\InternalServerErrorException;
9 use Psr\Log\LoggerInterface;
10
11 /**
12  * Model for an entry in the notify table
13  *
14  * @property string  hash
15  * @property integer type
16  * @property string  name   Full name of the contact subject
17  * @property string  url    Profile page URL of the contact subject
18  * @property string  photo  Profile photo URL of the contact subject
19  * @property string  date   YYYY-MM-DD hh:mm:ss local server time
20  * @property string  msg
21  * @property integer uid        Owner User Id
22  * @property string  link   Notification URL
23  * @property integer iid        Item Id
24  * @property integer parent Parent Item Id
25  * @property boolean seen   Whether the notification was read or not.
26  * @property string  verb   Verb URL (@see http://activitystrea.ms)
27  * @property string  otype  Subject type (`item`, `intro` or `mail`)
28  *
29  * @property-read string name_cache Full name of the contact subject
30  * @property-read string msg_cache  Plaintext version of the notification text with a placeholder (`{0}`) for the subject contact's name.
31  */
32 class Notify extends BaseModel
33 {
34
35         /** @var \Friendica\Repository\Notify */
36         private $repo;
37
38         public function __construct(Database $dba, LoggerInterface $logger, \Friendica\Repository\Notify $repo, array $data = [])
39         {
40                 parent::__construct($dba, $logger, $data);
41
42                 $this->repo = $repo;
43
44                 $this->setNameCache();
45                 $this->setMsgCache();
46         }
47
48         /**
49          * Sets the pre-formatted name (caching)
50          */
51         private function setNameCache()
52         {
53                 try {
54                         $this->name_cache = strip_tags(BBCode::convert($this->source_name ?? ''));
55                 } catch (InternalServerErrorException $e) {
56                 }
57         }
58
59         /**
60          * Sets the pre-formatted msg (caching)
61          */
62         private function setMsgCache()
63         {
64                 try {
65                         $this->msg_cache = self::formatMessage($this->name_cache, strip_tags(BBCode::convert($this->msg)));
66                 } catch (InternalServerErrorException $e) {
67                 }
68         }
69
70         public function __set($name, $value)
71         {
72                 parent::__set($name, $value);
73
74                 if ($name == 'msg') {
75                         $this->setMsgCache();
76                 }
77
78                 if ($name == 'source_name') {
79                         $this->setNameCache();
80                 }
81         }
82
83         /**
84          * Formats a notification message with the notification author
85          *
86          * Replace the name with {0} but ensure to make that only once. The {0} is used
87          * later and prints the name in bold.
88          *
89          * @param string $name
90          * @param string $message
91          *
92          * @return string Formatted message
93          */
94         public static function formatMessage($name, $message)
95         {
96                 if ($name != '') {
97                         $pos = strpos($message, $name);
98                 } else {
99                         $pos = false;
100                 }
101
102                 if ($pos !== false) {
103                         $message = substr_replace($message, '{0}', $pos, strlen($name));
104                 }
105
106                 return $message;
107         }
108 }