]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Fix PHPDoc comments project-wide
[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\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Object\Post;
11 use Friendica\Util\Security;
12
13 /**
14  * A list of threads
15  *
16  * We should think about making this a SPL Iterator
17  */
18 class Thread extends BaseObject
19 {
20         private $parents = [];
21         private $mode = null;
22         private $writable = false;
23         private $profile_owner = 0;
24         private $preview = false;
25
26         /**
27          * Constructor
28          *
29          * @param string  $mode     The mode
30          * @param boolean $preview  Are we in the preview mode?
31          * @param boolean $writable Override the writable check
32          * @throws \Exception
33          */
34         public function __construct($mode, $preview, $writable = false)
35         {
36                 $this->setMode($mode, $writable);
37                 $this->preview = $preview;
38         }
39
40         /**
41          * Set the mode we'll be displayed on
42          *
43          * @param string  $mode     The mode to set
44          * @param boolean $writable Override the writable check
45          *
46          * @return void
47          * @throws \Exception
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::log('[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 Post $item The item to insert
131          *
132          * @return mixed The inserted item on success
133          *               false on failure
134          * @throws \Exception
135          */
136         public function addParent(Post $item)
137         {
138                 $item_id = $item->getId();
139
140                 if (!$item_id) {
141                         Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
142                         return false;
143                 }
144
145                 if ($this->getParent($item->getId())) {
146                         Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
147                         return false;
148                 }
149
150                 /*
151                  * Only add will be displayed
152                  */
153                 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
154                         Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
155                         return false;
156                 }
157
158                 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
159                         Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
160                         return false;
161                 }
162
163                 $item->setThread($this);
164                 $this->parents[] = $item;
165
166                 return end($this->parents);
167         }
168
169         /**
170          * Get data in a form usable by a conversation template
171          *
172          * We should find a way to avoid using those arguments (at least most of them)
173          *
174          * @param object $conv_responses data
175          *
176          * @return mixed The data requested on success
177          *               false on failure
178          * @throws \Exception
179          */
180         public function getTemplateData($conv_responses)
181         {
182                 $a = self::getApp();
183                 $result = [];
184                 $i = 0;
185
186                 foreach ($this->parents as $item) {
187                         if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
188                                 continue;
189                         }
190
191                         $item_data = $item->getTemplateData($conv_responses);
192
193                         if (!$item_data) {
194                                 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
195                                 return false;
196                         }
197                         $result[] = $item_data;
198                 }
199
200                 return $result;
201         }
202
203         /**
204          * Get a thread based on its item id
205          *
206          * @param integer $id Item id
207          *
208          * @return mixed The found item on success
209          *               false on failure
210          */
211         private function getParent($id)
212         {
213                 foreach ($this->parents as $item) {
214                         if ($item->getId() == $id) {
215                                 return $item;
216                         }
217                 }
218
219                 return false;
220         }
221 }