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