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