]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/User.php
Merge pull request #12305 from MrPetovan/bug/notices
[friendica.git] / src / Model / Post / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Model\Post;
23
24 use Friendica\Database\DBA;
25 use \BadMethodCallException;
26 use Friendica\Database\Database;
27 use Friendica\DI;
28
29 class User
30 {
31         /**
32          * Insert a new post user entry
33          *
34          * @param integer $uri_id
35          * @param integer $uid
36          * @param array   $fields
37          * @return int    ID of inserted post-user
38          * @throws \Exception
39          */
40         public static function insert(int $uri_id, int $uid, array $data = [])
41         {
42                 if (empty($uri_id)) {
43                         throw new BadMethodCallException('Empty URI_id');
44                 }
45
46                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
47
48                 // Additionally assign the key fields
49                 $fields['uri-id'] = $uri_id;
50                 $fields['uid'] = $uid;
51
52                 // Public posts are always seen
53                 if ($uid == 0) {
54                         $fields['unseen'] = false;
55                 }
56
57                 // Does the entry already exist?
58                 if (DBA::exists('post-user', ['uri-id' => $uri_id, 'uid' => $uid])) {
59                         $postuser = DBA::selectFirst('post-user', [], ['uri-id' => $uri_id, 'uid' => $uid]);
60
61                         // We quit here, when there are obvious differences
62                         foreach (['created', 'owner-id', 'author-id', 'vid', 'network', 'private', 'wall', 'origin'] as $key) {
63                                 if ($fields[$key] != $postuser[$key]) {
64                                         return 0;
65                                 }
66                         }
67                         
68                         $update = [];
69                         foreach (['gravity', 'parent-uri-id', 'thr-parent-id'] as $key) {
70                                 if ($fields[$key] != $postuser[$key]) {
71                                         $update[$key] = $fields[$key];
72                                 }
73                         }
74
75                         // When the parents changed, we apply these changes to the existing entry
76                         if (!empty($update)) {
77                                 DBA::update('post-user', $update, ['id' => $postuser['id']]);
78                                 return $postuser['id'];
79                         } else {
80                                 return 0;
81                         }
82                 }
83
84                 if (!DBA::insert('post-user', $fields, Database::INSERT_IGNORE)) {
85                         return 0;
86                 }
87
88                 return DBA::lastInsertId();
89         }
90
91         /**
92          * Update a post user entry
93          *
94          * @param integer $uri_id
95          * @param integer $uid
96          * @param array   $data
97          * @param bool    $insert_if_missing
98          * @return bool
99          * @throws \Exception
100          */
101         public static function update(int $uri_id, int $uid, array $data = [], bool $insert_if_missing = false)
102         {
103                 if (empty($uri_id)) {
104                         throw new BadMethodCallException('Empty URI_id');
105                 }
106
107                 $fields = DI::dbaDefinition()->truncateFieldsForTable('post-user', $data);
108
109                 // Remove the key fields
110                 unset($fields['uri-id']);
111                 unset($fields['uid']);
112
113                 if (empty($fields)) {
114                         return true;
115                 }
116
117                 return DBA::update('post-user', $fields, ['uri-id' => $uri_id, 'uid' => $uid], $insert_if_missing ? true : []);
118         }
119
120         /**
121          * Delete a row from the post-user table
122          *
123          * @param array        $conditions Field condition(s)
124          * @param array        $options
125          *                           - cascade: If true we delete records in other tables that depend on the one we're deleting through
126          *                           relations (default: true)
127          *
128          * @return boolean was the delete successful?
129          * @throws \Exception
130          */
131         public static function delete(array $conditions, array $options = [])
132         {
133                 return DBA::delete('post-user', $conditions, $options);
134         }
135 }