]> git.mxchange.org Git - friendica.git/blob - object/Conversation.php
New objects: Conversation and Item
[friendica.git] / object / Conversation.php
1 <?php
2 if(class_exists('Conversation'))
3         return;
4
5 require_once('object/Item.php');
6 require_once('include/text.php');
7
8 /**
9  * A list of threads
10  *
11  * We should think about making this a SPL Iterator
12  */
13 class Conversation {
14         private $threads = array();
15
16         /**
17          * Add a thread to the conversation
18          *
19          * Returns:
20          *              _ The inserted item on success
21          *              _ false on failure
22          */
23         public function add_thread($item) {
24                 $item_id = $item->get_id();
25                 if(!$item_id) {
26                         logger('[ERROR] Conversation::add_thread : Item has no ID!!', LOGGER_DEBUG);
27                         return false;
28                 }
29                 if($this->get_thread($item->get_id())) {
30                         logger('[WARN] Conversation::add_thread : Thread already exists ('. $item->get_id() .').', LOGGER_DEBUG);
31                         return false;
32                 }
33                 $this->threads[] = $item;
34                 return end($this->threads);
35         }
36
37         /**
38          * Get data in a form usable by a conversation template
39          *
40          * We should find a way to avoid using those arguments (at least most of them)
41          *
42          * Returns:
43          *              _ The data requested on success
44          *              _ false on failure
45          */
46         public function get_template_data($a, $cmnt_tpl, $page_writeable, $mode, $profile_owner) {
47                 $result = array();
48
49                 /*foreach($this->threads as $item) {
50                         $item_data = $item->get_template_data();
51                         if(!$item_data) {
52                                 logger('[ERROR] Conversation::get_template_data : Failed to get item template data ('. $item->get_id() .').', LOGGER_DEBUG);
53                                 return false;
54                         }
55                         $result[] = $item_data;
56                 }*/
57
58                 /*
59                  * This should be moved on the item object;
60                  */
61                 $threads = array();
62                 foreach($this->threads as $item) {
63                         $threads[] = $item->get_data();
64                 }
65                 return $this->prepare_threads_body($a, $threads, $cmnt_tpl, $page_writeable, $mode, $profile_owner);
66
67                 return $result;
68         }
69
70         /**
71          * Get a thread based on its item id
72          *
73          * Returns:
74          *              _ The found item on success
75          *              _ false on failure
76          */
77         private function get_thread($id) {
78         foreach($this->threads as $item) {
79                 if($item->get_id() == $id)
80                         return $item;
81         }
82
83         return false;
84         }
85
86         /**
87          * Recursively prepare a thread for HTML
88          */
89
90         private function prepare_threads_body($a, $items, $cmnt_tpl, $page_writeable, $mode, $profile_owner, $thread_level=1) {
91                 $result = array();
92
93                 $wall_template = 'wall_thread.tpl';
94                 $wallwall_template = 'wallwall_thread.tpl';
95                 $items_seen = 0;
96                 $nb_items = count($items);
97                 
98                 $total_children = $nb_items;
99                 
100                 foreach($items as $item) {
101                         // prevent private email reply to public conversation from leaking.
102                         if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
103                                 // Don't count it as a visible item
104                                 $nb_items--;
105                                 continue;
106                         }
107                         
108                         $items_seen++;
109                         
110                         $alike = array();
111                         $dlike = array();
112                         $comment = '';
113                         $template = $wall_template;
114                         $commentww = '';
115                         $sparkle = '';
116                         $owner_url = $owner_photo = $owner_name = '';
117                         $buttons = '';
118                         $dropping = false;
119                         $star = false;
120                         $isstarred = "unstarred";
121                         $photo = $item['photo'];
122                         $thumb = $item['thumb'];
123                         $indent = '';
124                         $osparkle = '';
125                         $lastcollapsed = false;
126                         $firstcollapsed = false;
127                         $total_children += count_descendants($item);
128
129                         $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
130                         $item_writeable = (($item['writable'] || $item['self']) ? true : false);
131                         $show_comment_box = ((($page_writeable) && ($item_writeable)) ? true : false);
132                         $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
133                                 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
134                                 ? t('Private Message')
135                                 : false);
136                         $redirect_url = $a->get_baseurl($ssl_state) . '/redir/' . $item['cid'] ;
137                         $shareable = ((($profile_owner == local_user()) && ($item['private'] != 1)) ? true : false);
138                         if(local_user() && link_compare($a->contact['url'],$item['author-link']))
139                                 $edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
140                         else
141                                 $edpost = false;
142                         if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
143                                 $dropping = true;
144
145                         $drop = array(
146                                 'dropping' => $dropping,
147                                 'select' => t('Select'), 
148                                 'delete' => t('Delete'),
149                         );
150                         
151                         $filer = (($profile_owner == local_user()) ? t("save to folder") : false);
152
153                         $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
154                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
155                         if($item['author-link'] && (! $item['author-name']))
156                                 $profile_name = $item['author-link'];
157
158                         $sp = false;
159                         $profile_link = best_link_url($item,$sp);
160                         if($profile_link === 'mailbox')
161                                 $profile_link = '';
162                         if($sp)
163                                 $sparkle = ' sparkle';
164                         else
165                                 $profile_link = zrl($profile_link);                                     
166
167                         $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
168                         if(($normalised != 'mailbox') && (x($a->contacts,$normalised)))
169                                 $profile_avatar = $a->contacts[$normalised]['thumb'];
170                         else
171                                 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $a->get_cached_avatar_image($thumb));
172
173                         $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
174                         call_hooks('render_location',$locate);
175                         $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_google($locate));
176
177                         $tags=array();
178                         foreach(explode(',',$item['tag']) as $tag){
179                                 $tag = trim($tag);
180                                 if ($tag!="") $tags[] = bbcode($tag);
181                         }
182                         
183                         like_puller($a,$item,$alike,'like');
184                         like_puller($a,$item,$dlike,'dislike');
185
186                         $like    = ((x($alike,$item['uri'])) ? format_like($alike[$item['uri']],$alike[$item['uri'] . '-l'],'like',$item['uri']) : '');
187                         $dislike = ((x($dlike,$item['uri'])) ? format_like($dlike[$item['uri']],$dlike[$item['uri'] . '-l'],'dislike',$item['uri']) : '');
188
189                         if($toplevelpost) {
190                                 if((! $item['self']) && ($mode !== 'profile')) {
191                                         if($item['wall']) {
192
193                                                 // On the network page, I am the owner. On the display page it will be the profile owner.
194                                                 // This will have been stored in $a->page_contact by our calling page.
195                                                 // Put this person as the wall owner of the wall-to-wall notice.
196
197                                                 $owner_url = zrl($a->page_contact['url']);
198                                                 $owner_photo = $a->page_contact['thumb'];
199                                                 $owner_name = $a->page_contact['name'];
200                                                 $template = $wallwall_template;
201                                                 $commentww = 'ww';      
202                                         }
203                                 }
204                                 else if($item['owner-link']) {
205
206                                         $owner_linkmatch = (($item['owner-link']) && link_compare($item['owner-link'],$item['author-link']));
207                                         $alias_linkmatch = (($item['alias']) && link_compare($item['alias'],$item['author-link']));
208                                         $owner_namematch = (($item['owner-name']) && $item['owner-name'] == $item['author-name']);
209                                         if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
210
211                                                 // The author url doesn't match the owner (typically the contact)
212                                                 // and also doesn't match the contact alias. 
213                                                 // The name match is a hack to catch several weird cases where URLs are 
214                                                 // all over the park. It can be tricked, but this prevents you from
215                                                 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
216                                                 // well that it's the same Bob Smith. 
217
218                                                 // But it could be somebody else with the same name. It just isn't highly likely. 
219                                                 
220
221                                                 $owner_url = $item['owner-link'];
222                                                 $owner_photo = $item['owner-avatar'];
223                                                 $owner_name = $item['owner-name'];
224                                                 $template = $wallwall_template;
225                                                 $commentww = 'ww';
226                                                 // If it is our contact, use a friendly redirect link
227                                                 if((link_compare($item['owner-link'],$item['url'])) 
228                                                         && ($item['network'] === NETWORK_DFRN)) {
229                                                         $owner_url = $redirect_url;
230                                                         $osparkle = ' sparkle';
231                                                 }
232                                                 else
233                                                         $owner_url = zrl($owner_url);
234                                         }
235                                 }
236                                 if($profile_owner == local_user()) {
237                                         $isstarred = (($item['starred']) ? "starred" : "unstarred");
238
239                                         $star = array(
240                                                 'do' => t("add star"),
241                                                 'undo' => t("remove star"),
242                                                 'toggle' => t("toggle star status"),
243                                                 'classdo' => (($item['starred']) ? "hidden" : ""),
244                                                 'classundo' => (($item['starred']) ? "" : "hidden"),
245                                                 'starred' =>  t('starred'),
246                                                 'tagger' => t("add tag"),
247                                                 'classtagger' => "",
248                                         );
249                                 }
250                         } else {
251                                 $indent = 'comment';
252                                 // Collapse comments
253                                 if(($nb_items > 2) || ($thread_level > 2)) {
254                                         if($items_seen == 1) {
255                                                 $firstcollapsed = true;
256                                         }
257                                         if($thread_level > 2) {
258                                                 if($items_seen == $nb_items)
259                                                         $lastcollapsed = true;
260                                         }
261                                         else if($items_seen == ($nb_items - 2)) {
262                                                 $lastcollapsed = true;
263                                         }
264                                 }
265                         }
266
267                         if($page_writeable) {
268                                 $buttons = array(
269                                         'like' => array( t("I like this \x28toggle\x29"), t("like")),
270                                         'dislike' => array( t("I don't like this \x28toggle\x29"), t("dislike")),
271                                 );
272                                 if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
273
274
275                                 if($show_comment_box) {
276                                         $qc = $qcomment =  null;
277
278                                         if(in_array('qcomment',$a->plugins)) {
279                                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
280                                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
281                                         }
282                                         $comment = replace_macros($cmnt_tpl,array(
283                                                 '$return_path' => '', 
284                                                 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
285                                                 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
286                                                 '$id' => $item['item_id'],
287                                                 '$parent' => $item['item_id'],
288                                                 '$qcomment' => $qcomment,
289                                                 '$profile_uid' =>  $profile_owner,
290                                                 '$mylink' => $a->contact['url'],
291                                                 '$mytitle' => t('This is you'),
292                                                 '$myphoto' => $a->contact['thumb'],
293                                                 '$comment' => t('Comment'),
294                                                 '$submit' => t('Submit'),
295                                                 '$edbold' => t('Bold'),
296                                                 '$editalic' => t('Italic'),
297                                                 '$eduline' => t('Underline'),
298                                                 '$edquote' => t('Quote'),
299                                                 '$edcode' => t('Code'),
300                                                 '$edimg' => t('Image'),
301                                                 '$edurl' => t('Link'),
302                                                 '$edvideo' => t('Video'),
303                                                 '$preview' => t('Preview'),
304                                                 '$sourceapp' => t($a->sourcename),
305                                                 '$ww' => (($mode === 'network') ? $commentww : '')
306                                         ));
307                                 }
308                         }
309
310                         if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
311                                 $indent .= ' shiny';
312
313                         localize_item($item);
314
315                         $body = prepare_body($item,true);
316
317                         $tmp_item = array(
318                                 // collapse comments in template. I don't like this much...
319                                 'comment_firstcollapsed' => $firstcollapsed,
320                                 'comment_lastcollapsed' => $lastcollapsed,
321                                 // template to use to render item (wall, walltowall, search)
322                                 'template' => $template,
323                                 
324                                 'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
325                                 'tags' => $tags,
326                                 'body' => template_escape($body),
327                                 'text' => strip_tags(template_escape($body)),
328                                 'id' => $item['item_id'],
329                                 'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
330                                 'olinktitle' => sprintf( t('View %s\'s profile @ %s'), $owner_name, ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
331                                 'to' => t('to'),
332                                 'wall' => t('Wall-to-Wall'),
333                                 'vwall' => t('via Wall-To-Wall:'),
334                                 'profile_url' => $profile_link,
335                                 'item_photo_menu' => item_photo_menu($item),
336                                 'name' => template_escape($profile_name),
337                                 'thumb' => $profile_avatar,
338                                 'osparkle' => $osparkle,
339                                 'sparkle' => $sparkle,
340                                 'title' => template_escape($item['title']),
341                                 'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
342                                 'lock' => $lock,
343                                 'location' => template_escape($location),
344                                 'indent' => $indent,
345                                 'owner_url' => $owner_url,
346                                 'owner_photo' => $owner_photo,
347                                 'owner_name' => template_escape($owner_name),
348                                 'plink' => get_plink($item),
349                                 'edpost' => $edpost,
350                                 'isstarred' => $isstarred,
351                                 'star' => $star,
352                                 'filer' => $filer,
353                                 'drop' => $drop,
354                                 'vote' => $buttons,
355                                 'like' => $like,
356                                 'dislike' => $dislike,
357                                 'comment' => $comment,
358                                 'previewing' => $previewing,
359                                 'wait' => t('Please wait'),
360                         );
361
362                         $arr = array('item' => $item, 'output' => $tmp_item);
363                         call_hooks('display_item', $arr);
364
365                         $item_result = $arr['output'];
366                         if($firstcollapsed) {
367                                 $item_result['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
368                                 $item_result['hide_text'] = t('show more');
369                         }
370
371                         $item_result['children'] = array();
372                         if(count($item['children'])) {
373                                 $item_result['children'] = prepare_threads_body($a, $item['children'], $cmnt_tpl, $page_writeable, $mode, $profile_owner, ($thread_level + 1));
374                         }
375                         $item_result['private'] = $item['private'];
376                         $item_result['toplevel'] = ($toplevelpost ? 'toplevel_item' : '');
377
378                         /*
379                          * I don't like this very much...
380                          */
381                         if(get_config('system','thread_allow')) {
382                                 $item_result['flatten'] = false;
383                                 $item_result['threaded'] = true;
384                         }
385                         else {
386                                 $item_result['flatten'] = true;
387                                 $item_result['threaded'] = false;
388                                 if(!$toplevelpost) {
389                                         $item_result['comment'] = false;
390                                 }
391                         }
392                         
393                         $result[] = $item_result;
394                 }
395
396                 return $result;
397         }
398 }
399 ?>