]> git.mxchange.org Git - friendica.git/blob - src/Content/Conversation/Repository/UserDefinedChannel.php
User Repository\UserDefinedChannel->selectByUid instead of Factory\UserDefinedChannel...
[friendica.git] / src / Content / Conversation / Repository / UserDefinedChannel.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\Collection\UserDefinedChannels;
26 use Friendica\Content\Conversation\Entity;
27 use Friendica\Content\Conversation\Factory;
28 use Friendica\Database\Database;
29 use Psr\Log\LoggerInterface;
30
31 class UserDefinedChannel extends \Friendica\BaseRepository
32 {
33         protected static $table_name = 'channel';
34
35         public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory)
36         {
37                 parent::__construct($database, $logger, $factory);
38         }
39
40         /**
41          * @param array $condition
42          * @param array $params
43          * @return UserDefinedChannels
44          * @throws \Exception
45          */
46         protected function _select(array $condition, array $params = []): BaseCollection
47         {
48                 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
49
50                 $Entities = new UserDefinedChannels();
51                 foreach ($rows as $fields) {
52                         $Entities[] = $this->factory->createFromTableRow($fields);
53                 }
54
55                 return $Entities;
56         }
57
58         /**
59          * Fetch a single user channel
60          *
61          * @param int $id  The id of the user defined channel
62          * @param int $uid The user that this channel belongs to. (Not part of the primary key)
63          * @return Entity\UserDefinedChannel
64          * @throws \Friendica\Network\HTTPException\NotFoundException
65          */
66         public function selectById(int $id, int $uid): Entity\UserDefinedChannel
67         {
68                 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
69         }
70
71         /**
72          * Checks if the provided channel id exists for this user
73          *
74          * @param integer $id
75          * @param integer $uid
76          * @return boolean
77          */
78         public function existsById(int $id, int $uid): bool
79         {
80                 return $this->exists(['id' => $id, 'uid' => $uid]);
81         }
82
83         /**
84          * Delete the given channel
85          *
86          * @param integer $id
87          * @param integer $uid
88          * @return boolean
89          */
90         public function deleteById(int $id, int $uid): bool
91         {
92                 return $this->db->delete('channel', ['id' => $id, 'uid' => $uid]);
93         }
94
95         /**
96          * Fetch all user channels
97          *
98          * @param integer $uid
99          * @return UserDefinedChannels
100          * @throws \Exception
101          */
102         public function selectByUid(int $uid): UserDefinedChannels
103         {
104                 return $this->_select(['uid' => $uid]);
105         }
106
107         public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
108         {
109                 $fields = [
110                         'label'            => $Channel->label,
111                         'description'      => $Channel->description,
112                         'access-key'       => $Channel->accessKey,
113                         'uid'              => $Channel->uid,
114                         'circle'           => $Channel->circle,
115                         'include-tags'     => $Channel->includeTags,
116                         'exclude-tags'     => $Channel->excludeTags,
117                         'full-text-search' => $Channel->fullTextSearch,
118                         'media-type'       => $Channel->mediaType,
119                 ];
120
121                 if ($Channel->code) {
122                         $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
123                 } else {
124                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
125
126                         $newChannelId = $this->db->lastInsertId();
127
128                         $Channel = $this->selectById($newChannelId, $Channel->uid);
129                 }
130
131                 return $Channel;
132         }
133 }