]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Merge pull request #6024 from MrPetovan/bug/fix-disabling-addons
[friendica.git] / src / Object / Thread.php
1 <?php
2 /**
3  * @file src/Object/Thread.php
4  */
5 namespace Friendica\Object;
6
7 use Friendica\BaseObject;
8 use Friendica\Core\Protocol;
9 use Friendica\Object\Post;
10 use Friendica\Util\Security;
11
12 require_once 'boot.php';
13 require_once 'include/text.php';
14
15 /**
16  * A list of threads
17  *
18  * We should think about making this a SPL Iterator
19  */
20 class Thread extends BaseObject
21 {
22         private $parents = [];
23         private $mode = null;
24         private $writable = false;
25         private $profile_owner = 0;
26         private $preview = false;
27
28         /**
29          * Constructor
30          *
31          * @param string  $mode    The mode
32          * @param boolean $preview Are we in the preview mode?
33          * @param boolean $writable Override the writable check
34          */
35         public function __construct($mode, $preview, $writable = false)
36         {
37                 $this->setMode($mode, $writable);
38                 $this->preview = $preview;
39         }
40
41         /**
42          * Set the mode we'll be displayed on
43          *
44          * @param string $mode The mode to set
45          * @param boolean $writable Override the writable check
46          *
47          * @return void
48          */
49         private function setMode($mode, $writable)
50         {
51                 if ($this->getMode() == $mode) {
52                         return;
53                 }
54
55                 $a = self::getApp();
56
57                 switch ($mode) {
58                         case 'network':
59                         case 'notes':
60                                 $this->profile_owner = local_user();
61                                 $this->writable = true;
62                                 break;
63                         case 'profile':
64                                 $this->profile_owner = $a->profile['profile_uid'];
65                                 $this->writable = Security::canWriteToUserWall($this->profile_owner);
66                                 break;
67                         case 'display':
68                                 $this->profile_owner = $a->profile['uid'];
69                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
70                                 break;
71                         case 'community':
72                                 $this->profile_owner = 0;
73                                 $this->writable = $writable;
74                                 break;
75                         case 'contacts':
76                                 $this->profile_owner = 0;
77                                 $this->writable = $writable;
78                                 break;
79                         default:
80                                 logger('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
81                                 return false;
82                                 break;
83                 }
84                 $this->mode = $mode;
85         }
86
87         /**
88          * Get mode
89          *
90          * @return string
91          */
92         public function getMode()
93         {
94                 return $this->mode;
95         }
96
97         /**
98          * Check if page is writable
99          *
100          * @return boolean
101          */
102         public function isWritable()
103         {
104                 return $this->writable;
105         }
106
107         /**
108          * Check if page is a preview
109          *
110          * @return boolean
111          */
112         public function isPreview()
113         {
114                 return $this->preview;
115         }
116
117         /**
118          * Get profile owner
119          *
120          * @return integer
121          */
122         public function getProfileOwner()
123         {
124                 return $this->profile_owner;
125         }
126
127         /**
128          * Add a thread to the conversation
129          *
130          * @param object $item The item to insert
131          *
132          * @return mixed The inserted item on success
133          *               false on failure
134          */
135         public function addParent(Post $item)
136         {
137                 $item_id = $item->getId();
138
139                 if (!$item_id) {
140                         logger('[ERROR] Conversation::addThread : Item has no ID!!', LOGGER_DEBUG);
141                         return false;
142                 }
143
144                 if ($this->getParent($item->getId())) {
145                         logger('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', LOGGER_DEBUG);
146                         return false;
147                 }
148
149                 /*
150                  * Only add will be displayed
151                  */
152                 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
153                         logger('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', LOGGER_DEBUG);
154                         return false;
155                 }
156
157                 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
158                         logger('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', LOGGER_DEBUG);
159                         return false;
160                 }
161
162                 $item->setThread($this);
163                 $this->parents[] = $item;
164
165                 return end($this->parents);
166         }
167
168         /**
169          * Get data in a form usable by a conversation template
170          *
171          * We should find a way to avoid using those arguments (at least most of them)
172          *
173          * @param object $conv_responses data
174          *
175          * @return mixed The data requested on success
176          *               false on failure
177          */
178         public function getTemplateData($conv_responses)
179         {
180                 $a = self::getApp();
181                 $result = [];
182                 $i = 0;
183
184                 foreach ($this->parents as $item) {
185                         if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
186                                 continue;
187                         }
188
189                         $item_data = $item->getTemplateData($conv_responses);
190
191                         if (!$item_data) {
192                                 logger('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', LOGGER_DEBUG);
193                                 return false;
194                         }
195                         $result[] = $item_data;
196                 }
197
198                 return $result;
199         }
200
201         /**
202          * Get a thread based on its item id
203          *
204          * @param integer $id Item id
205          *
206          * @return mixed The found item on success
207          *               false on failure
208          */
209         private function getParent($id)
210         {
211                 foreach ($this->parents as $item) {
212                         if ($item->getId() == $id) {
213                                 return $item;
214                         }
215                 }
216
217                 return false;
218         }
219 }