]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Fix mods/README.md format
[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 require_once 'boot.php';
14 require_once 'include/text.php';
15
16 /**
17  * A list of threads
18  *
19  * We should think about making this a SPL Iterator
20  */
21 class Thread extends BaseObject
22 {
23         private $parents = [];
24         private $mode = null;
25         private $writable = false;
26         private $profile_owner = 0;
27         private $preview = false;
28
29         /**
30          * Constructor
31          *
32          * @param string  $mode    The mode
33          * @param boolean $preview Are we in the preview mode?
34          * @param boolean $writable Override the writable check
35          */
36         public function __construct($mode, $preview, $writable = false)
37         {
38                 $this->setMode($mode, $writable);
39                 $this->preview = $preview;
40         }
41
42         /**
43          * Set the mode we'll be displayed on
44          *
45          * @param string $mode The mode to set
46          * @param boolean $writable Override the writable check
47          *
48          * @return void
49          */
50         private function setMode($mode, $writable)
51         {
52                 if ($this->getMode() == $mode) {
53                         return;
54                 }
55
56                 $a = self::getApp();
57
58                 switch ($mode) {
59                         case 'network':
60                         case 'notes':
61                                 $this->profile_owner = local_user();
62                                 $this->writable = true;
63                                 break;
64                         case 'profile':
65                                 $this->profile_owner = $a->profile['profile_uid'];
66                                 $this->writable = Security::canWriteToUserWall($this->profile_owner);
67                                 break;
68                         case 'display':
69                                 $this->profile_owner = $a->profile['uid'];
70                                 $this->writable = Security::canWriteToUserWall($this->profile_owner) || $writable;
71                                 break;
72                         case 'community':
73                                 $this->profile_owner = 0;
74                                 $this->writable = $writable;
75                                 break;
76                         case 'contacts':
77                                 $this->profile_owner = 0;
78                                 $this->writable = $writable;
79                                 break;
80                         default:
81                                 Logger::log('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', Logger::DEBUG);
82                                 return false;
83                                 break;
84                 }
85                 $this->mode = $mode;
86         }
87
88         /**
89          * Get mode
90          *
91          * @return string
92          */
93         public function getMode()
94         {
95                 return $this->mode;
96         }
97
98         /**
99          * Check if page is writable
100          *
101          * @return boolean
102          */
103         public function isWritable()
104         {
105                 return $this->writable;
106         }
107
108         /**
109          * Check if page is a preview
110          *
111          * @return boolean
112          */
113         public function isPreview()
114         {
115                 return $this->preview;
116         }
117
118         /**
119          * Get profile owner
120          *
121          * @return integer
122          */
123         public function getProfileOwner()
124         {
125                 return $this->profile_owner;
126         }
127
128         /**
129          * Add a thread to the conversation
130          *
131          * @param object $item The item to insert
132          *
133          * @return mixed The inserted item on success
134          *               false on failure
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          */
179         public function getTemplateData($conv_responses)
180         {
181                 $a = self::getApp();
182                 $result = [];
183                 $i = 0;
184
185                 foreach ($this->parents as $item) {
186                         if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
187                                 continue;
188                         }
189
190                         $item_data = $item->getTemplateData($conv_responses);
191
192                         if (!$item_data) {
193                                 Logger::log('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', Logger::DEBUG);
194                                 return false;
195                         }
196                         $result[] = $item_data;
197                 }
198
199                 return $result;
200         }
201
202         /**
203          * Get a thread based on its item id
204          *
205          * @param integer $id Item id
206          *
207          * @return mixed The found item on success
208          *               false on failure
209          */
210         private function getParent($id)
211         {
212                 foreach ($this->parents as $item) {
213                         if ($item->getId() == $id) {
214                                 return $item;
215                         }
216                 }
217
218                 return false;
219         }
220 }