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