]> git.mxchange.org Git - friendica.git/blob - src/Content/Conversation/Repository/Channel.php
Channel frontend added
[friendica.git] / src / Content / Conversation / Repository / Channel.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content\Conversation\Repository;
23
24 use Friendica\BaseCollection;
25 use Friendica\Content\Conversation\Entity\Timeline as TimelineEntity;
26 use Friendica\Content\Conversation\Factory\Timeline;
27 use Friendica\Database\Database;
28 use Psr\Log\LoggerInterface;
29
30 class Channel extends \Friendica\BaseRepository
31 {
32         protected static $table_name = 'channel';
33
34         public function __construct(Database $database, LoggerInterface $logger, Timeline $factory)
35         {
36                 parent::__construct($database, $logger, $factory);
37         }
38
39         /**
40          * Fetch a single user channel
41          *
42          * @param int $id
43          * @param int $uid
44          * @return TimelineEntity
45          * @throws \Friendica\Network\HTTPException\NotFoundException
46          */
47         public function selectById(int $id, int $uid): TimelineEntity
48         {
49                 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
50         }
51
52         /**
53          * Checks if the provided channel id exists for this user
54          *
55          * @param integer $id
56          * @param integer $uid
57          * @return boolean
58          */
59         public function existsById(int $id, int $uid): bool
60         {
61                 return $this->exists(['id' => $id, 'uid' => $uid]);
62         }
63
64         /**
65          * Delete the given channel
66          *
67          * @param integer $id
68          * @param integer $uid
69          * @return boolean
70          */
71         public function deleteById(int $id, int $uid): bool
72         {
73                 return $this->db->delete('channel', ['id' => $id, 'uid' => $uid]);
74         }
75
76         /**
77          * Fetch all user channels
78          *
79          * @param integer $uid
80          * @return BaseCollection
81          */
82         public function selectByUid(int $uid): BaseCollection
83         {
84                 return $this->_select(['uid' => $uid]);
85         }
86
87         public function save(TimelineEntity $Channel): TimelineEntity
88         {
89                 $fields = [
90                         'label'            => $Channel->label,
91                         'description'      => $Channel->description,
92                         'access-key'       => $Channel->accessKey,
93                         'uid'              => $Channel->uid,
94                         'include-tags'     => $Channel->includeTags,
95                         'exclude-tags'     => $Channel->excludeTags,
96                         'full-text-search' => $Channel->fullTextSearch,
97                         'media-type'       => $Channel->mediaType,
98                 ];
99
100                 if ($Channel->code) {
101                         $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
102                 } else {
103                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
104
105                         $newChannelId = $this->db->lastInsertId();
106
107                         $Channel = $this->selectById($newChannelId, $Channel->uid);
108                 }
109
110                 return $Channel;
111         }
112 }