]> git.mxchange.org Git - friendica.git/blob - src/Repository/Notify.php
Merge pull request #8163 from MrPetovan/task/7817-custom-fields-part-3
[friendica.git] / src / Repository / Notify.php
1 <?php
2
3 namespace Friendica\Repository;
4
5 use Exception;
6 use Friendica\BaseRepository;
7 use Friendica\Core\Hook;
8 use Friendica\Model;
9 use Friendica\Collection;
10 use Friendica\Network\HTTPException\InternalServerErrorException;
11 use Friendica\Network\HTTPException\NotFoundException;
12 use Friendica\Util\DateTimeFormat;
13
14 class Notify extends BaseRepository
15 {
16         protected static $table_name = 'notify';
17
18         protected static $model_class = Model\Notify::class;
19
20         protected static $collection_class = Collection\Notifies::class;
21
22         /**
23          * {@inheritDoc}
24          *
25          * @return Model\Notify
26          */
27         protected function create(array $data)
28         {
29                 return new Model\Notify($this->dba, $this->logger, $this, $data);
30         }
31
32         /**
33          * {@inheritDoc}
34          *
35          * @return Collection\Notifies
36          */
37         public function select(array $condition = [], array $params = [])
38         {
39                 $params['order'] = $params['order'] ?? ['date' => 'DESC'];
40
41                 return parent::select($condition, $params);
42         }
43
44         /**
45          * {@inheritDoc}
46          *
47          * @return Model\Notify
48          * @throws NotFoundException
49          */
50         public function getByID(int $id)
51         {
52                 return $this->selectFirst(['id' => $id, 'uid' => local_user()]);
53         }
54
55         /**
56          * Set seen state of notifications of the local_user()
57          *
58          * @param bool         $seen   optional true or false. default true
59          * @param Model\Notify $notify optional a notify, which should be set seen (including his parents)
60          *
61          * @return bool true on success, false on error
62          *
63          * @throws Exception
64          */
65         public function setSeen(bool $seen = true, Model\Notify $notify = null)
66         {
67                 if (empty($notify)) {
68                         $conditions = ['uid' => local_user()];
69                 } else {
70                         $conditions = ['(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
71                                 $notify->link,
72                                 $notify->parent,
73                                 $notify->otype,
74                                 local_user()];
75                 }
76
77                 return $this->dba->update('notify', ['seen' => $seen], $conditions);
78         }
79
80         /**
81          * @param array $fields
82          *
83          * @return Model\Notify|false
84          *
85          * @throws InternalServerErrorException
86          * @throws Exception
87          */
88         public function insert(array $fields)
89         {
90                 $fields['date'] = DateTimeFormat::utcNow();
91
92                 Hook::callAll('enotify_store', $fields);
93
94                 if (empty($fields)) {
95                         $this->logger->debug('Abort adding notification entry');
96                         return false;
97                 }
98
99                 $this->logger->debug('adding notification entry', ['fields' => $fields]);
100
101                 return parent::insert($fields);
102         }
103 }