]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Stopped using deprecated constants NETWORK_* (#5537)
[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
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                         default:
75                                 logger('[ERROR] Conversation::setMode : Unhandled mode ('. $mode .').', LOGGER_DEBUG);
76                                 return false;
77                                 break;
78                 }
79                 $this->mode = $mode;
80         }
81
82         /**
83          * Get mode
84          *
85          * @return string
86          */
87         public function getMode()
88         {
89                 return $this->mode;
90         }
91
92         /**
93          * Check if page is writable
94          *
95          * @return boolean
96          */
97         public function isWritable()
98         {
99                 return $this->writable;
100         }
101
102         /**
103          * Check if page is a preview
104          *
105          * @return boolean
106          */
107         public function isPreview()
108         {
109                 return $this->preview;
110         }
111
112         /**
113          * Get profile owner
114          *
115          * @return integer
116          */
117         public function getProfileOwner()
118         {
119                 return $this->profile_owner;
120         }
121
122         /**
123          * Add a thread to the conversation
124          *
125          * @param object $item The item to insert
126          *
127          * @return mixed The inserted item on success
128          *               false on failure
129          */
130         public function addParent(Post $item)
131         {
132                 $item_id = $item->getId();
133
134                 if (!$item_id) {
135                         logger('[ERROR] Conversation::addThread : Item has no ID!!', LOGGER_DEBUG);
136                         return false;
137                 }
138
139                 if ($this->getParent($item->getId())) {
140                         logger('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', LOGGER_DEBUG);
141                         return false;
142                 }
143
144                 /*
145                  * Only add will be displayed
146                  */
147                 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
148                         logger('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', LOGGER_DEBUG);
149                         return false;
150                 }
151
152                 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
153                         logger('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', LOGGER_DEBUG);
154                         return false;
155                 }
156
157                 $item->setThread($this);
158                 $this->parents[] = $item;
159
160                 return end($this->parents);
161         }
162
163         /**
164          * Get data in a form usable by a conversation template
165          *
166          * We should find a way to avoid using those arguments (at least most of them)
167          *
168          * @param object $conv_responses data
169          *
170          * @return mixed The data requested on success
171          *               false on failure
172          */
173         public function getTemplateData($conv_responses)
174         {
175                 $a = self::getApp();
176                 $result = [];
177                 $i = 0;
178
179                 foreach ($this->parents as $item) {
180                         if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
181                                 continue;
182                         }
183
184                         $item_data = $item->getTemplateData($conv_responses);
185
186                         if (!$item_data) {
187                                 logger('[ERROR] Conversation::getTemplateData : Failed to get item template data ('. $item->getId() .').', LOGGER_DEBUG);
188                                 return false;
189                         }
190                         $result[] = $item_data;
191                 }
192
193                 return $result;
194         }
195
196         /**
197          * Get a thread based on its item id
198          *
199          * @param integer $id Item id
200          *
201          * @return mixed The found item on success
202          *               false on failure
203          */
204         private function getParent($id)
205         {
206                 foreach ($this->parents as $item) {
207                         if ($item->getId() == $id) {
208                                 return $item;
209                         }
210                 }
211
212                 return false;
213         }
214 }