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