]> git.mxchange.org Git - friendica.git/blob - include/conversation.php
839afc4817d02fc79531e584048c6be714a69288
[friendica.git] / include / conversation.php
1 <?php
2
3
4 function conversation(&$a, $items, $mode, $update) {
5
6         require_once('bbcode.php');
7
8         $profile_owner = 0;
9         $page_writeable      = false;
10
11         if($mode === 'network') {
12                 $profile_owner = local_user();
13                 $page_writeable = true;
14         }
15
16         if($mode === 'profile') {
17                 $profile_owner = $a->profile['profile_uid'];
18                 $page_writeable = can_write_wall($a,$profile_owner);
19         }
20
21         if($mode === 'display') {
22                 $profile_owner = $a->profile['uid'];
23                 $page_writeable = can_write_wall($a,$profile_owner);
24         }
25
26         if($update)
27                 $return_url = $_SESSION['return_url'];
28         else
29                 $return_url = $_SESSION['return_url'] = $a->cmd;
30
31
32         // find all the authors involved in remote conversations
33         // We will use a local profile photo if they are one of our contacts
34         // otherwise we have to get the photo from the item owner's site
35
36         $author_contacts = extract_item_authors($items,local_user());
37
38
39         $cmnt_tpl    = load_view_file('view/comment_item.tpl');
40         $like_tpl    = load_view_file('view/like.tpl');
41         $noshare_tpl = load_view_file('view/like_noshare.tpl');
42         $tpl         = load_view_file('view/wall_item.tpl');
43         $wallwall    = load_view_file('view/wallwall_item.tpl');
44
45         $alike = array();
46         $dlike = array();
47         
48         if(count($items)) {
49
50                 if($mode === 'network-new' || $mode === 'search') {
51
52                         // "New Item View" on network page or search page results 
53                         // - just loop through the items and format them minimally for display
54
55                         $tpl = load_view_file('view/search_item.tpl');
56                         $droptpl = load_view_file('view/wall_fake_drop.tpl');
57
58                         foreach($items as $item) {
59
60                                 $comment     = '';
61                                 $owner_url   = '';
62                                 $owner_photo = '';
63                                 $owner_name  = '';
64                                 $sparkle     = '';
65
66                                 if($mode === 'search') {
67                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
68                                                 && ($item['id'] != $item['parent']))
69                                                 continue;
70                                         $nickname = $item['nickname'];
71                                 }
72                                 else
73                                         $nickname = $a->user['nickname'];
74                         
75                                 $profile_name   = ((strlen($item['author-name']))   ? $item['author-name']   : $item['name']);
76                                 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
77                                 $profile_link   = ((strlen($item['author-link']))   ? $item['author-link']   : $item['url']);
78
79                                 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
80
81                                 if(strlen($item['author-link'])) {
82                                         if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
83                                                 $profile_link = $redirect_url;
84                                                 $sparkle = ' sparkle';
85                                         }
86                                         elseif(isset($author_contacts[$item['author-link']])) {
87                                                 $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
88                                                 $sparkle = ' sparkle';
89                                         }
90                                 }
91
92                                 $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
93                                 $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
94                                 if($coord) {
95                                         if($location)
96                                                 $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
97                                         else
98                                                 $location = '<span class="smalltext">' . $coord . '</span>';
99                                 }
100
101                                 $drop = '';
102                                 $dropping = false;
103
104                                 if((intval($item['contact-id']) && $item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
105                                         $dropping = true;
106
107                     $drop = replace_macros((($dropping)? $droptpl : $fakedrop), array('$id' => $item['id'], '$delete' => t('Delete')));
108
109
110
111                                 $drop = replace_macros($droptpl,array('$id' => $item['id']));
112                                 $lock = '<div class="wall-item-lock"></div>';
113                                 
114                                 $o .= replace_macros($tpl,array(
115                                         '$id' => $item['item_id'],
116                                         '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
117                                         '$profile_url' => $profile_link,
118                                         '$item_photo_menu' => item_photo_menu($item),
119                                         '$name' => $profile_name,
120                                         '$sparkle' => $sparkle,
121                                         '$lock' => $lock,
122                                         '$thumb' => $profile_avatar,
123                                         '$title' => $item['title'],
124                                         '$body' => smilies(bbcode($item['body'])),
125                                         '$ago' => relative_date($item['created']),
126                                         '$location' => $location,
127                                         '$indent' => '',
128                                         '$owner_url' => $owner_url,
129                                         '$owner_photo' => $owner_photo,
130                                         '$owner_name' => $owner_name,
131                                         '$drop' => $drop,
132                                         '$conv' => '<a href="' . $a->get_baseurl() . '/display/' . $nickname . '/' . $item['id'] . '">' . t('View in context') . '</a>'
133                                 ));
134
135                         }
136
137                         return $o;
138                 }
139
140
141
142
143                 // Normal View
144
145
146                 // Figure out how many comments each parent has
147                 // (Comments all have gravity of 6)
148                 // Store the result in the $comments array
149
150                 $comments = array();
151                 foreach($items as $item) {
152                         if(intval($item['gravity']) == 6) {
153                                 if(! x($comments,$item['parent']))
154                                         $comments[$item['parent']] = 1;
155                                 else
156                                         $comments[$item['parent']] += 1;
157                         }
158                 }
159
160                 // map all the like/dislike activities for each parent item 
161                 // Store these in the $alike and $dlike arrays
162
163                 foreach($items as $item) {
164                         like_puller($a,$item,$alike,'like');
165                         like_puller($a,$item,$dlike,'dislike');
166                 }
167
168                 $comments_collapsed = false;
169                 $blowhard = 0;
170                 $blowhard_count = 0;
171
172                 foreach($items as $item) {
173
174                         $comment = '';
175                         $template = $tpl;
176                         $commentww = '';
177                         $sparkle = '';
178                         $owner_url = $owner_photo = $owner_name = '';
179
180                         // We've already parsed out like/dislike for special treatment. We can ignore them now
181
182                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) 
183                                 || (activity_match($item['verb'],ACTIVITY_DISLIKE))) 
184                                 && ($item['id'] != $item['parent']))
185                                 continue;
186
187                         $toplevelpost = (($item['id'] == $item['parent']) ? true : false);
188
189
190                         // Take care of author collapsing and comment collapsing
191                         // If a single author has more than 3 consecutive top-level posts, squash the remaining ones.
192                         // If there are more than two comments, squash all but the last 2.
193
194                         if($toplevelpost) {
195
196                                 $item_writeable = (($item['writable'] || $item['self']) ? true : false);
197
198                                 if($blowhard == $item['cid'] && (! $item['self']) && ($mode != 'profile')) {
199                                         $blowhard_count ++;
200                                         if($blowhard_count == 3) {
201                                                 $o .= '<div class="icollapse-wrapper fakelink" id="icollapse-wrapper-' . $item['parent'] 
202                                                         . '" onclick="openClose(' . '\'icollapse-' . $item['parent'] . '\');" >' 
203                                                         . t('See more posts like this') . '</div>' . '<div class="icollapse" id="icollapse-' 
204                                                         . $item['parent'] . '" style="display: none;" >';
205                                         }
206                                 }
207                                 else {
208                                         $blowhard = $item['cid'];                                       
209                                         if($blowhard_count >= 3)
210                                                 $o .= '</div>';
211                                         $blowhard_count = 0;
212                                 }
213
214                                 $comments_seen = 0;
215                                 $comments_collapsed = false;
216                         }
217                         else
218                                 $comments_seen ++;
219
220
221                         $show_comment_box = ((($page_writeable) && ($item_writeable) && ($comments_seen == $comments[$item['parent']])) ? true : false);
222
223                         if(($comments[$item['parent']] > 2) && ($comments_seen <= ($comments[$item['parent']] - 2)) && ($item['gravity'] == 6)) {
224                                 if(! $comments_collapsed) {
225                                         $o .= '<div class="ccollapse-wrapper fakelink" id="ccollapse-wrapper-' . $item['parent'] 
226                                                 . '" onclick="openClose(' . '\'ccollapse-' . $item['parent'] . '\');" >' 
227                                                 . sprintf( t('See all %d comments'), $comments[$item['parent']]) . '</div>'
228                                                 . '<div class="ccollapse" id="ccollapse-' . $item['parent'] . '" style="display: none;" >';
229                                         $comments_collapsed = true;
230                                 }
231                         }
232                         if(($comments[$item['parent']] > 2) && ($comments_seen == ($comments[$item['parent']] - 1))) {
233                                 $o .= '</div>';
234                         }
235
236                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
237
238                         $lock = ((($item['private']) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
239                                 || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
240                                 ? '<div class="wall-item-lock"><img src="images/lock_icon.gif" class="lockview" alt="' . t('Private Message') . '" onclick="lockview(event,' . $item['id'] . ');" /></div>'
241                                 : '<div class="wall-item-lock"></div>');
242
243
244                         // Top-level wall post not written by the wall owner (wall-to-wall)
245                         // First figure out who owns it. 
246
247                         $osparkle = '';
248
249                         if(($toplevelpost) && (! $item['self']) && ($mode !== 'profile')) {
250
251                                 if($item['type'] === 'wall') {
252
253                                         // On the network page, I am the owner. On the display page it will be the profile owner.
254                                         // This will have been stored in $a->page_contact by our calling page.
255                                         // Put this person on the left of the wall-to-wall notice.
256
257                                         $owner_url = $a->page_contact['url'];
258                                         $owner_photo = $a->page_contact['thumb'];
259                                         $owner_name = $a->page_contact['name'];
260                                         $template = $wallwall;
261                                         $commentww = 'ww';      
262                                 }
263                                 if(($item['type'] === 'remote') && (strlen($item['owner-link'])) && ($item['owner-link'] != $item['author-link'])) {
264
265                                         // Could be anybody. 
266
267                                         $owner_url = $item['owner-link'];
268                                         $owner_photo = $item['owner-avatar'];
269                                         $owner_name = $item['owner-name'];
270                                         $template = $wallwall;
271                                         $commentww = 'ww';
272                                         // If it is our contact, use a friendly redirect link
273                                         if((link_compare($item['owner-link'],$item['url'])) 
274                                                 && ($item['network'] === 'dfrn')) {
275                                                 $owner_url = $redirect_url;
276                                                 $osparkle = ' sparkle';
277                                         }
278                                 }
279                         }
280
281
282                         $likebuttons = '';
283
284                         if($page_writeable) {
285                                 if($toplevelpost) {
286                                         $likebuttons = replace_macros((($item['private']) ? $noshare_tpl : $like_tpl),array(
287                                                 '$id' => $item['id'],
288                                                 '$likethis' => t("I like this \x28toggle\x29"),
289                                                 '$nolike' => t("I don't like this \x28toggle\x29"),
290                                                 '$share' => t('Share'),
291                                                 '$wait' => t('Please wait') 
292                                         ));
293                                 }
294
295                                 if(($show_comment_box) || (($show_comment_box == false) && ($item['last-child']))) {
296                                         $comment = replace_macros($cmnt_tpl,array(
297                                                 '$return_path' => '', 
298                                                 '$jsreload' => (($mode === 'display') ? $_SESSION['return_url'] : ''),
299                                                 '$type' => (($mode === 'profile') ? 'wall-comment' : 'net-comment'),
300                                                 '$id' => $item['item_id'],
301                                                 '$parent' => $item['parent'],
302                                                 '$profile_uid' =>  $profile_owner,
303                                                 '$mylink' => $a->contact['url'],
304                                                 '$mytitle' => t('This is you'),
305                                                 '$myphoto' => $a->contact['thumb'],
306                                                 '$comment' => t('Comment'),
307                                                 '$submit' => t('Submit'),
308                                                 '$ww' => (($mode === 'network') ? $commentww : '')
309                                         ));
310                                 }
311                         }
312
313                         $edpost = ((($profile_owner == local_user()) && ($toplevelpost) && (intval($item['wall']) == 1))
314                                         ? '<a class="editpost" href="' . $a->get_baseurl() . '/editpost/' . $item['id'] 
315                                                 . '" title="' . t('Edit') . '"><img src="images/pencil.gif" /></a>'
316                                         : '');
317                         $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
318
319                         $photo = $item['photo'];
320                         $thumb = $item['thumb'];
321
322                         // Post was remotely authored.
323
324                         $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
325
326                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
327                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $thumb);
328
329                         if($mode === 'profile') {
330                                 if(local_user() && ($item['contact-uid'] == local_user()) && ($item['network'] === 'dfrn') && (! $item['self'] )) {
331                         $profile_link = $redirect_url;
332                     $sparkle = ' sparkle';
333                     }
334                                 else {
335                                         $profile_link = $item['url'];
336                                         $sparkle = '';
337                                 }
338                         }
339                         elseif(strlen($item['author-link'])) {
340                                 $profile_link = $item['author-link'];
341                                 if(link_compare($item['author-link'],$item['url']) && ($item['network'] === 'dfrn') && (! $item['self'])) {
342                                         $profile_link = $redirect_url;
343                                         $sparkle = ' sparkle';
344                                 }
345                                 elseif(isset($author_contacts[$item['author-link']])) {
346                                         $profile_link = $a->get_baseurl() . '/redir/' . $author_contacts[$item['author-link']];
347                                         $sparkle = ' sparkle';
348                                 }
349                         }
350                         else 
351                                 $profile_link = $item['url'];
352
353                         $like    = ((x($alike,$item['id'])) ? format_like($alike[$item['id']],$alike[$item['id'] . '-l'],'like',$item['id']) : '');
354                         $dislike = ((x($dlike,$item['id'])) ? format_like($dlike[$item['id']],$dlike[$item['id'] . '-l'],'dislike',$item['id']) : '');
355
356                         $location = (($item['location']) ? '<a target="map" title="' . $item['location'] 
357                                 . '" href="http://maps.google.com/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
358                         $coord = (($item['coord']) ? '<a target="map" title="' . $item['coord'] 
359                                 . '" href="http://maps.google.com/?q=' . urlencode($item['coord']) . '">' . $item['coord'] . '</a>' : '');
360                         if($coord) {
361                                 if($location)
362                                         $location .= '<br /><span class="smalltext">(' . $coord . ')</span>';
363                                 else
364                                         $location = '<span class="smalltext">' . $coord . '</span>';
365                         }
366
367                         $indent = (($toplevelpost) ? '' : ' comment');
368
369                         if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0)
370                                 $indent .= ' shiny'; 
371
372                         // Build the HTML
373
374                         $tmp_item = replace_macros($template,array(
375                                 '$id' => $item['item_id'],
376                                 '$linktitle' => sprintf( t('View %s\'s profile'), $profile_name),
377                                 '$olinktitle' => sprintf( t('View %s\'s profile'), $owner_name),
378                                 '$to' => t('to'),
379                                 '$wall' => t('Wall-to-Wall'),
380                                 '$vwall' => t('via Wall-To-Wall:'),
381                                 '$profile_url' => $profile_link,
382                                 '$item_photo_menu' => item_photo_menu($item),
383                                 '$name' => $profile_name,
384                                 '$thumb' => $profile_avatar,
385                                 '$osparkle' => $osparkle,
386                                 '$sparkle' => $sparkle,
387                                 '$title' => $item['title'],
388                                 '$body' => smilies(bbcode($item['body'])),
389                                 '$ago' => relative_date($item['created']),
390                                 '$lock' => $lock,
391                                 '$location' => $location,
392                                 '$indent' => $indent,
393                                 '$owner_url' => $owner_url,
394                                 '$owner_photo' => $owner_photo,
395                                 '$owner_name' => $owner_name,
396                                 '$plink' => get_plink($item),
397                                 '$edpost' => $edpost,
398                                 '$drop' => $drop,
399                                 '$vote' => $likebuttons,
400                                 '$like' => $like,
401                                 '$dislike' => $dislike,
402                                 '$comment' => $comment
403                         ));
404
405                         $arr = array('item' => $item, 'output' => $tmp_item);
406                         call_hooks('display_item', $arr);
407
408                         $o .= $arr['output'];
409
410                 }
411         }
412
413
414         // if author collapsing is in force but didn't get closed, close it off now.
415
416         if($blowhard_count >= 3)
417                 $o .= '</div>';
418
419         return $o;
420