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