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