]> git.mxchange.org Git - friendica.git/blob - src/Content/Conversation/Repository/Channel.php
Show network elements in the channel widget
[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 EntityTimeline;
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 EntityTimeline
45          * @throws \Friendica\Network\HTTPException\NotFoundException
46          */
47         public function selectById(int $id, int $uid): EntityTimeline
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          * Fetch all user channels
66          *
67          * @param integer $uid
68          * @return BaseCollection
69          */
70         public function selectByUid(int $uid): BaseCollection
71         {
72                 return $this->_select(['uid' => $uid]);
73         }
74
75         public function save(EntityTimeline $Channel): EntityTimeline
76         {
77                 $fields = [
78                         'label'            => $Channel->label,
79                         'description'      => $Channel->description,
80                         'access-key'       => $Channel->accessKey,
81                         'uid'              => $Channel->uid,
82                         'include-tags'     => $Channel->includeTags,
83                         'exclude-tags'     => $Channel->excludeTags,
84                         'full-text-search' => $Channel->fullTextSearch,
85                         'media-type'       => $Channel->mediaType,
86                 ];
87
88                 if ($Channel->code) {
89                         $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
90                 } else {
91                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
92
93                         $newChannelId = $this->db->lastInsertId();
94
95                         $Channel = $this->selectById($newChannelId, $Channel->uid);
96                 }
97
98                 return $Channel;
99         }
100 }