]> git.mxchange.org Git - friendica.git/blob - src/Object/Thread.php
Remove unused code
[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\Util\Security;
11
12 /**
13  * A list of threads
14  *
15  * We should think about making this a SPL Iterator
16  */
17 class Thread extends BaseObject
18 {
19         private $parents = [];
20         private $mode = null;
21         private $writable = false;
22         private $profile_owner = 0;
23         private $preview = false;
24
25         /**
26          * Constructor
27          *
28          * @param string  $mode     The mode
29          * @param boolean $preview  Are we in the preview mode?
30          * @param boolean $writable Override the writable check
31          * @throws \Exception
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          * @throws \Exception
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 = Security::canWriteToUserWall($this->profile_owner);
65                                 break;
66                         case 'display':
67                                 $this->profile_owner = $a->profile['uid'];
68                                 $this->writable = Security::canWriteToUserWall($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::log('[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 Post $item The item to insert
130          *
131          * @return mixed The inserted item on success
132          *               false on failure
133          * @throws \Exception
134          */
135         public function addParent(Post $item)
136         {
137                 $item_id = $item->getId();
138
139                 if (!$item_id) {
140                         Logger::log('[ERROR] Conversation::addThread : Item has no ID!!', Logger::DEBUG);
141                         return false;
142                 }
143
144                 if ($this->getParent($item->getId())) {
145                         Logger::log('[WARN] Conversation::addThread : Thread already exists ('. $item->getId() .').', Logger::DEBUG);
146                         return false;
147                 }
148
149                 /*
150                  * Only add will be displayed
151                  */
152                 if ($item->getDataValue('network') === Protocol::MAIL && local_user() != $item->getDataValue('uid')) {
153                         Logger::log('[WARN] Conversation::addThread : Thread is a mail ('. $item->getId() .').', Logger::DEBUG);
154                         return false;
155                 }
156
157                 if ($item->getDataValue('verb') === ACTIVITY_LIKE || $item->getDataValue('verb') === ACTIVITY_DISLIKE) {
158                         Logger::log('[WARN] Conversation::addThread : Thread is a (dis)like ('. $item->getId() .').', Logger::DEBUG);
159                         return false;
160                 }
161
162                 $item->setThread($this);
163                 $this->parents[] = $item;
164
165                 return end($this->parents);
166         }
167
168         /**
169          * Get data in a form usable by a conversation template
170          *
171          * We should find a way to avoid using those arguments (at least most of them)
172          *
173          * @param object $conv_responses data
174          *
175          * @return mixed The data requested on success
176          *               false on failure
177          * @throws \Exception
178          */
179         public function getTemplateData($conv_responses)
180         {
181                 $result = [];
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::log('[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 }