]> git.mxchange.org Git - friendica.git/blob - object/Item.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / object / Item.php
1 <?php
2 if(class_exists('Item'))
3         return;
4
5 require_once('object/BaseObject.php');
6 require_once('include/text.php');
7 require_once('include/diaspora.php');
8 require_once('boot.php');
9
10 /**
11  * An item
12  */
13 class Item extends BaseObject {
14         private $data = array();
15         private $template = null;
16         private $available_templates = array(
17                 'wall' => 'wall_thread.tpl',
18                 'wall2wall' => 'wallwall_thread.tpl'
19         );
20         private $comment_box_template = 'comment_item.tpl';
21         private $toplevel = false;
22         private $writable = false;
23         private $children = array();
24         private $parent = null;
25         private $conversation = null;
26         private $redirect_url = null;
27         private $owner_url = '';
28         private $owner_photo = '';
29         private $owner_name = '';
30         private $wall_to_wall = false;
31         private $threaded = false;
32         private $visiting = false;
33
34         public function __construct($data) {
35                 $a = $this->get_app();
36
37                 $this->data = $data;
38                 $this->set_template('wall');
39                 $this->toplevel = ($this->get_id() == $this->get_data_value('parent'));
40
41                 if(is_array($_SESSION['remote'])) {
42                         foreach($_SESSION['remote'] as $visitor) {
43                                 if($visitor['cid'] == $this->get_data_value('contact-id')) {
44                                         $this->visiting = true;
45                                         break;
46                                 }
47                         }
48                 }
49
50                 $this->writable = ($this->get_data_value('writable') || $this->get_data_value('self'));
51
52                 $ssl_state = ((local_user()) ? true : false);
53                 $this->redirect_url = 'redir/' . $this->get_data_value('cid') ;
54
55                 if(get_config('system','thread_allow') && $a->theme_thread_allow && !$this->is_toplevel())
56                         $this->threaded = true;
57
58                 // Prepare the children
59                 if(count($data['children'])) {
60                         foreach($data['children'] as $item) {
61                                 /*
62                                  * Only add will be displayed
63                                  */
64                                 if($item['network'] === NETWORK_MAIL && local_user() != $item['uid']) {
65                                         continue;
66                                 }
67                                 if(! visible_activity($item)) {
68                                         continue;
69                                 }
70                                 $item['pagedrop'] = $data['pagedrop'];
71                                 $child = new Item($item);
72                                 $this->add_child($child);
73                         }
74                 }
75         }
76
77         /**
78          * Get data in a form usable by a conversation template
79          *
80          * Returns:
81          *      _ The data requested on success
82          *      _ false on failure
83          */
84         public function get_template_data($conv_responses, $thread_level=1) {
85                 require_once("mod/proxy.php");
86
87                 $result = array();
88
89                 $a = $this->get_app();
90
91                 $item = $this->get_data();
92                 $edited = false;
93                 if (strcmp($item['created'], $item['edited'])<>0) {
94                       $edited = array(
95                           'label' => t('This entry was edited'),
96                           'date' => datetime_convert('UTC', date_default_timezone_get(), $item['edited'], 'r'),
97                           'relative' => relative_date($item['edited'])
98                       );
99                 }
100                 $commentww = '';
101                 $sparkle = '';
102                 $buttons = '';
103                 $dropping = false;
104                 $star = false;
105                 $ignore = false;
106                 $isstarred = "unstarred";
107                 $indent = '';
108                 $shiny = '';
109                 $osparkle = '';
110                 $total_children = $this->count_descendants();
111
112                 $conv = $this->get_conversation();
113
114
115                 $lock = ((($item['private'] == 1) || (($item['uid'] == local_user()) && (strlen($item['allow_cid']) || strlen($item['allow_gid']) 
116                         || strlen($item['deny_cid']) || strlen($item['deny_gid']))))
117                         ? t('Private Message')
118                         : false);
119                 $shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
120                 if (local_user() && link_compare($a->contact['url'],$item['author-link'])) {
121                         if ($item["event-id"] != 0) {
122                                 $edpost = array("events/event/".$item['event-id'], t("Edit"));
123                         } else {
124                                 $edpost = array("editpost/".$item['id'], t("Edit"));
125                         }
126                 } else {
127                         $edpost = false;
128                 }
129
130                 if (($this->get_data_value('uid') == local_user()) || $this->is_visiting()) {
131                         $dropping = true;
132                 }
133
134                 $drop = array(
135                         'dropping' => $dropping,
136                         'pagedrop' => ((feature_enabled($conv->get_profile_owner(),'multi_delete')) ? $item['pagedrop'] : ''),
137                         'select' => t('Select'),
138                         'delete' => t('Delete'),
139                 );
140
141                 $filer = (($conv->get_profile_owner() == local_user()) ? t("save to folder") : false);
142
143                 $diff_author    = ((link_compare($item['url'],$item['author-link'])) ? false : true);
144                 $profile_name   = htmlentities(((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
145                 if($item['author-link'] && (! $item['author-name']))
146                         $profile_name = $item['author-link'];
147
148                 $sp = false;
149                 $profile_link = best_link_url($item,$sp);
150                 if ($profile_link === 'mailbox') {
151                         $profile_link = '';
152                 }
153                 if ($sp) {
154                         $sparkle = ' sparkle';
155                 } else {
156                         $profile_link = zrl($profile_link);
157                 }
158
159                 if (!isset($item['author-thumb']) OR ($item['author-thumb'] == "")) {
160                         $author_contact = get_contact_details_by_url($item['author-link'], $conv->get_profile_owner());
161                         if ($author_contact["thumb"]) {
162                                 $item['author-thumb'] = $author_contact["thumb"];
163                         } else {
164                                 $item['author-thumb'] = $item['author-avatar'];
165                         }
166                 }
167
168                 if (!isset($item['owner-thumb']) OR ($item['owner-thumb'] == "")) {
169                         $owner_contact = get_contact_details_by_url($item['owner-link'], $conv->get_profile_owner());
170                         if ($owner_contact["thumb"]) {
171                                 $item['owner-thumb'] = $owner_contact["thumb"];
172                         } else {
173                                 $item['owner-thumb'] = $item['owner-avatar'];
174                         }
175                 }
176
177                 $locate = array('location' => $item['location'], 'coord' => $item['coord'], 'html' => '');
178                 call_hooks('render_location',$locate);
179                 $location = ((strlen($locate['html'])) ? $locate['html'] : render_location_dummy($locate));
180
181                 $searchpath = "search?tag=";
182                 $tags=array();
183                 $hashtags = array();
184                 $mentions = array();
185
186
187                 /*foreach(explode(',',$item['tag']) as $tag){
188                         $tag = trim($tag);
189                         if ($tag!="") {
190                                 $t = bbcode($tag);
191                                 $tags[] = $t;
192                                 if($t[0] == '#')
193                                         $hashtags[] = $t;
194                                 elseif($t[0] == '@')
195                                         $mentions[] = $t;
196                         }
197                 }*/
198
199                 // process action responses - e.g. like/dislike/attend/agree/whatever
200                 $response_verbs = array('like');
201                 if(feature_enabled($conv->get_profile_owner(),'dislike'))
202                         $response_verbs[] = 'dislike';
203                 if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
204                         $response_verbs[] = 'attendyes';
205                         $response_verbs[] = 'attendno';
206                         $response_verbs[] = 'attendmaybe';
207                         if($conv->is_writable()) {
208                                 $isevent = true;
209                                 $attend = array( t('I will attend'), t('I will not attend'), t('I might attend'));
210                         }
211                 }
212
213                 $responses = get_responses($conv_responses,$response_verbs,$this,$item);
214
215                 foreach ($response_verbs as $value=>$verbs) {
216                         $responses[$verbs][output]  = ((x($conv_responses[$verbs],$item['uri'])) ? format_like($conv_responses[$verbs][$item['uri']],$conv_responses[$verbs][$item['uri'] . '-l'],$verbs,$item['uri']) : '');
217
218                 }
219
220                 /*
221                  * We should avoid doing this all the time, but it depends on the conversation mode
222                  * And the conv mode may change when we change the conv, or it changes its mode
223                  * Maybe we should establish a way to be notified about conversation changes
224                  */
225                 $this->check_wall_to_wall();
226
227                 if($this->is_wall_to_wall() && ($this->get_owner_url() == $this->get_redirect_url()))
228                         $osparkle = ' sparkle';
229
230                 if($this->is_toplevel()) {
231                         if($conv->get_profile_owner() == local_user()) {
232                                 $isstarred = (($item['starred']) ? "starred" : "unstarred");
233
234                                 $star = array(
235                                         'do' => t("add star"),
236                                         'undo' => t("remove star"),
237                                         'toggle' => t("toggle star status"),
238                                         'classdo' => (($item['starred']) ? "hidden" : ""),
239                                         'classundo' => (($item['starred']) ? "" : "hidden"),
240                                         'starred' =>  t('starred'),
241                                 );
242                                 $r = q("SELECT `ignored` FROM `thread` WHERE `uid` = %d AND `iid` = %d LIMIT 1",
243                                         intval($item['uid']),
244                                         intval($item['id'])
245                                 );
246                                 if (dbm::is_result($r)) {
247                                         $ignore = array(
248                                                 'do' => t("ignore thread"),
249                                                 'undo' => t("unignore thread"),
250                                                 'toggle' => t("toggle ignore status"),
251                                                 'classdo' => (($r[0]['ignored']) ? "hidden" : ""),
252                                                 'classundo' => (($r[0]['ignored']) ? "" : "hidden"),
253                                                 'ignored' =>  t('ignored'),
254                                         );
255                                 }
256
257                                 $tagger = '';
258                                 if(feature_enabled($conv->get_profile_owner(),'commtag')) {
259                                         $tagger = array(
260                                                 'add' => t("add tag"),
261                                                 'class' => "",
262                                         );
263                                 }
264                         }
265                 } else {
266                         $indent = 'comment';
267                 }
268
269                 if($conv->is_writable()) {
270                         $buttons = array(
271                                 'like' => array( t("I like this \x28toggle\x29"), t("like")),
272                                 'dislike' => ((feature_enabled($conv->get_profile_owner(),'dislike')) ? array( t("I don't like this \x28toggle\x29"), t("dislike")) : ''),
273                         );
274                         if ($shareable) $buttons['share'] = array( t('Share this'), t('share'));
275                 }
276
277                 $comment = $this->get_comment_box($indent);
278
279                 if(strcmp(datetime_convert('UTC','UTC',$item['created']),datetime_convert('UTC','UTC','now - 12 hours')) > 0){
280                         $shiny = 'shiny';
281                 }
282
283                 localize_item($item);
284
285                 if ($item["postopts"] and !get_config("system", "suppress_language")) {
286                         //$langdata = explode(";", $item["postopts"]);
287                         //$langstr = substr($langdata[0], 5)." (".round($langdata[1]*100, 1)."%)";
288                         $langstr = "";
289                         if (substr($item["postopts"], 0, 5) == "lang=") {
290                                 $postopts = substr($item["postopts"], 5);
291
292                                 $languages = explode(":", $postopts);
293
294                                 if (sizeof($languages) == 1) {
295                                         $languages = array();
296                                         $languages[] = $postopts;
297                                 }
298
299                                 foreach ($languages as $language) {
300                                         $langdata = explode(";", $language);
301                                         if ($langstr != "")
302                                                 $langstr .= ", ";
303
304                                         //$langstr .= $langdata[0]." (".round($langdata[1]*100, 1)."%)";
305                                         $langstr .= round($langdata[1]*100, 1)."% ".$langdata[0];
306                                 }
307                         }
308                 }
309
310                 $body = prepare_body($item,true);
311
312                 list($categories, $folders) = get_cats_and_terms($item);
313
314                 if($a->theme['template_engine'] === 'internal') {
315                         $body_e = template_escape($body);
316                         $text_e = strip_tags(template_escape($body));
317                         $name_e = template_escape($profile_name);
318                         $title_e = template_escape($item['title']);
319                         $location_e = template_escape($location);
320                         $owner_name_e = template_escape($this->get_owner_name());
321                 }
322                 else {
323                         $body_e = $body;
324                         $text_e = strip_tags($body);
325                         $name_e = $profile_name;
326                         $title_e = $item['title'];
327                         $location_e = $location;
328                         $owner_name_e = $this->get_owner_name();
329                 }
330
331                 // Disable features that aren't available in several networks
332                 if (($item["item_network"] != NETWORK_DFRN) AND isset($buttons["dislike"])) {
333                         unset($buttons["dislike"],$isevent);
334                         $tagger = '';
335                 }
336
337                 if (($item["item_network"] == NETWORK_FEED) AND isset($buttons["like"]))
338                         unset($buttons["like"]);
339
340                 if (($item["item_network"] == NETWORK_MAIL) AND isset($buttons["like"]))
341                         unset($buttons["like"]);
342
343                 // Diaspora isn't able to do likes on comments - but red does
344                 if (($item["item_network"] == NETWORK_DIASPORA) AND ($indent == 'comment') AND
345                         !diaspora::is_redmatrix($item["owner-link"]) AND isset($buttons["like"]))
346                         unset($buttons["like"]);
347
348                 // Diaspora doesn't has multithreaded comments
349                 if (($item["item_network"] == NETWORK_DIASPORA) AND ($indent == 'comment'))
350                         unset($comment);
351
352                 // Facebook can like comments - but it isn't programmed in the connector yet.
353                 if (($item["item_network"] == NETWORK_FACEBOOK) AND ($indent == 'comment') AND isset($buttons["like"]))
354                         unset($buttons["like"]);
355
356                 $tmp_item = array(
357                         'template' => $this->get_template(),
358
359                         'type' => implode("",array_slice(explode("/",$item['verb']),-1)),
360                         'tags' => $item['tags'],
361                         'hashtags' => $item['hashtags'],
362                         'mentions' => $item['mentions'],
363                         'txt_cats' => t('Categories:'),
364                         'txt_folders' => t('Filed under:'),
365                         'has_cats' => ((count($categories)) ? 'true' : ''),
366                         'has_folders' => ((count($folders)) ? 'true' : ''),
367                         'categories' => $categories,
368                         'folders' => $folders,
369                         'body' => $body_e,
370                         'text' => $text_e,
371                         'id' => $this->get_id(),
372                         'guid' => urlencode($item['guid']),
373                         'isevent' => $isevent,
374                         'attend' => $attend,
375                         'linktitle' => sprintf( t('View %s\'s profile @ %s'), $profile_name, ((strlen($item['author-link'])) ? $item['author-link'] : $item['url'])),
376                         'olinktitle' => sprintf( t('View %s\'s profile @ %s'), htmlentities($this->get_owner_name()), ((strlen($item['owner-link'])) ? $item['owner-link'] : $item['url'])),
377                         'to' => t('to'),
378                         'via' => t('via'),
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' => $name_e,
384                         'thumb' => $a->remove_baseurl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
385                         'osparkle' => $osparkle,
386                         'sparkle' => $sparkle,
387                         'title' => $title_e,
388                         'localtime' => datetime_convert('UTC', date_default_timezone_get(), $item['created'], 'r'),
389                         'ago' => (($item['app']) ? sprintf( t('%s from %s'),relative_date($item['created']),$item['app']) : relative_date($item['created'])),
390                         'app' => $item['app'],
391                         'created' => relative_date($item['created']),
392                         'lock' => $lock,
393                         'location' => $location_e,
394                         'indent' => $indent,
395                         'shiny' => $shiny,
396                         'owner_url' => $this->get_owner_url(),
397                         'owner_photo' => $a->remove_baseurl(proxy_url($item['owner-thumb'], false, PROXY_SIZE_THUMB)),
398                         'owner_name' => htmlentities($owner_name_e),
399                         'plink' => get_plink($item),
400                         'edpost'    => ((feature_enabled($conv->get_profile_owner(),'edit_posts')) ? $edpost : ''),
401                         'isstarred' => $isstarred,
402                         'star'      => ((feature_enabled($conv->get_profile_owner(),'star_posts')) ? $star : ''),
403                         'ignore'      => ((feature_enabled($conv->get_profile_owner(),'ignore_posts')) ? $ignore : ''),
404                         'tagger'        => $tagger,
405                         'filer'     => ((feature_enabled($conv->get_profile_owner(),'filing')) ? $filer : ''),
406                         'drop' => $drop,
407                         'vote' => $buttons,
408                         'like' => $responses['like']['output'],
409                         'dislike'   => $responses['dislike']['output'],
410                         'responses' => $responses,
411                         'switchcomment' => t('Comment'),
412                         'comment' => $comment,
413                         'previewing' => ($conv->is_preview() ? ' preview ' : ''),
414                         'wait' => t('Please wait'),
415                         'thread_level' => $thread_level,
416                         'postopts' => $langstr,
417                         'edited' => $edited,
418                         'network' => $item["item_network"],
419                         'network_name' => network_to_name($item['item_network'], $profile_link),
420                 );
421
422                 $arr = array('item' => $item, 'output' => $tmp_item);
423                 call_hooks('display_item', $arr);
424
425                 $result = $arr['output'];
426
427                 $result['children'] = array();
428                 $children = $this->get_children();
429                 $nb_children = count($children);
430                 if($nb_children > 0) {
431                         foreach($children as $child) {
432                                 $result['children'][] = $child->get_template_data($conv_responses, $thread_level + 1);
433                         }
434                         // Collapse
435                         if(($nb_children > 2) || ($thread_level > 1)) {
436                                 $result['children'][0]['comment_firstcollapsed'] = true;
437                                 $result['children'][0]['num_comments'] = sprintf( tt('%d comment','%d comments',$total_children),$total_children );
438                                 $result['children'][0]['hidden_comments_num'] = $total_children;
439                                 $result['children'][0]['hidden_comments_text'] = tt('comment', 'comments', $total_children);
440                                 $result['children'][0]['hide_text'] = t('show more');
441                                 if($thread_level > 1) {
442                                         $result['children'][$nb_children - 1]['comment_lastcollapsed'] = true;
443                                 }
444                                 else {
445                                         $result['children'][$nb_children - 3]['comment_lastcollapsed'] = true;
446                                 }
447                         }
448                 }
449
450         if ($this->is_toplevel()) {
451             $result['total_comments_num'] = "$total_children";
452             $result['total_comments_text'] = tt('comment', 'comments', $total_children);
453         }
454
455                 $result['private'] = $item['private'];
456                 $result['toplevel'] = ($this->is_toplevel() ? 'toplevel_item' : '');
457
458                 if($this->is_threaded()) {
459                         $result['flatten'] = false;
460                         $result['threaded'] = true;
461                 }
462                 else {
463                         $result['flatten'] = true;
464                         $result['threaded'] = false;
465                 }
466
467                 return $result;
468         }
469
470         public function get_id() {
471                 return $this->get_data_value('id');
472         }
473
474         public function is_threaded() {
475                 return $this->threaded;
476         }
477
478         /**
479          * Add a child item
480          */
481         public function add_child($item) {
482                 $item_id = $item->get_id();
483                 if(!$item_id) {
484                         logger('[ERROR] Item::add_child : Item has no ID!!', LOGGER_DEBUG);
485                         return false;
486                 }
487                 if($this->get_child($item->get_id())) {
488                         logger('[WARN] Item::add_child : Item already exists ('. $item->get_id() .').', LOGGER_DEBUG);
489                         return false;
490                 }
491                 /*
492                  * Only add what will be displayed
493                  */
494                 if($item->get_data_value('network') === NETWORK_MAIL && local_user() != $item->get_data_value('uid')) {
495                         return false;
496                 }
497                 if(activity_match($item->get_data_value('verb'),ACTIVITY_LIKE) || activity_match($item->get_data_value('verb'),ACTIVITY_DISLIKE)) {
498                         return false;
499                 }
500
501                 $item->set_parent($this);
502                 $this->children[] = $item;
503                 return end($this->children);
504         }
505
506         /**
507          * Get a child by its ID
508          */
509         public function get_child($id) {
510                 foreach($this->get_children() as $child) {
511                         if($child->get_id() == $id)
512                                 return $child;
513                 }
514                 return null;
515         }
516
517         /**
518          * Get all ou children
519          */
520         public function get_children() {
521                 return $this->children;
522         }
523
524         /**
525          * Set our parent
526          */
527         protected function set_parent($item) {
528                 $parent = $this->get_parent();
529                 if($parent) {
530                         $parent->remove_child($this);
531                 }
532                 $this->parent = $item;
533                 $this->set_conversation($item->get_conversation());
534         }
535
536         /**
537          * Remove our parent
538          */
539         protected function remove_parent() {
540                 $this->parent = null;
541                 $this->conversation = null;
542         }
543
544         /**
545          * Remove a child
546          */
547         public function remove_child($item) {
548                 $id = $item->get_id();
549                 foreach($this->get_children() as $key => $child) {
550                         if($child->get_id() == $id) {
551                                 $child->remove_parent();
552                                 unset($this->children[$key]);
553                                 // Reindex the array, in order to make sure there won't be any trouble on loops using count()
554                                 $this->children = array_values($this->children);
555                                 return true;
556                         }
557                 }
558                 logger('[WARN] Item::remove_child : Item is not a child ('. $id .').', LOGGER_DEBUG);
559                 return false;
560         }
561
562         /**
563          * Get parent item
564          */
565         protected function get_parent() {
566                 return $this->parent;
567         }
568
569         /**
570          * set conversation
571          */
572         public function set_conversation($conv) {
573                 $previous_mode = ($this->conversation ? $this->conversation->get_mode() : '');
574
575                 $this->conversation = $conv;
576
577                 // Set it on our children too
578                 foreach($this->get_children() as $child)
579                         $child->set_conversation($conv);
580         }
581
582         /**
583          * get conversation
584          */
585         public function get_conversation() {
586                 return $this->conversation;
587         }
588
589         /**
590          * Get raw data
591          *
592          * We shouldn't need this
593          */
594         public function get_data() {
595                 return $this->data;
596         }
597
598         /**
599          * Get a data value
600          *
601          * Returns:
602          *      _ value on success
603          *      _ false on failure
604          */
605         public function get_data_value($name) {
606                 if(!isset($this->data[$name])) {
607 //                      logger('[ERROR] Item::get_data_value : Item has no value name "'. $name .'".', LOGGER_DEBUG);
608                         return false;
609                 }
610
611                 return $this->data[$name];
612         }
613
614         /**
615          * Set template
616          */
617         private function set_template($name) {
618                 $a = get_app();
619
620                 if(!x($this->available_templates, $name)) {
621                         logger('[ERROR] Item::set_template : Template not available ("'. $name .'").', LOGGER_DEBUG);
622                         return false;
623                 }
624
625                 $this->template = $this->available_templates[$name];
626         }
627
628         /**
629          * Get template
630          */
631         private function get_template() {
632                 return $this->template;
633         }
634
635         /**
636          * Check if this is a toplevel post
637          */
638         private function is_toplevel() {
639                 return $this->toplevel;
640         }
641
642         /**
643          * Check if this is writable
644          */
645         private function is_writable() {
646                 $conv = $this->get_conversation();
647
648                 if($conv) {
649                         // This will allow us to comment on wall-to-wall items owned by our friends
650                         // and community forums even if somebody else wrote the post.
651
652                         // bug #517 - this fixes for conversation owner
653                         if($conv->get_mode() == 'profile' && $conv->get_profile_owner() == local_user())
654                                 return true; 
655
656                         // this fixes for visitors
657                         return ($this->writable || ($this->is_visiting() && $conv->get_mode() == 'profile'));
658                 }
659                 return $this->writable;
660         }
661
662         /**
663          * Count the total of our descendants
664          */
665         private function count_descendants() {
666                 $children = $this->get_children();
667                 $total = count($children);
668                 if($total > 0) {
669                         foreach($children as $child) {
670                                 $total += $child->count_descendants();
671                         }
672                 }
673                 return $total;
674         }
675
676         /**
677          * Get the template for the comment box
678          */
679         private function get_comment_box_template() {
680                 return $this->comment_box_template;
681         }
682
683         /**
684          * Get the comment box
685          *
686          * Returns:
687          *      _ The comment box string (empty if no comment box)
688          *      _ false on failure
689          */
690         private function get_comment_box($indent) {
691                 $a = $this->get_app();
692                 if(!$this->is_toplevel() && !(get_config('system','thread_allow') && $a->theme_thread_allow)) {
693                         return '';
694                 }
695
696                 $comment_box = '';
697                 $conv = $this->get_conversation();
698                 $template = get_markup_template($this->get_comment_box_template());
699                 $ww = '';
700                 if( ($conv->get_mode() === 'network') && $this->is_wall_to_wall() )
701                         $ww = 'ww';
702
703                 if($conv->is_writable() && $this->is_writable()) {
704                         $qc = $qcomment =  null;
705
706                         /*
707                          * Hmmm, code depending on the presence of a particular plugin?
708                          * This should be better if done by a hook
709                          */
710                         if(in_array('qcomment',$a->plugins)) {
711                                 $qc = ((local_user()) ? get_pconfig(local_user(),'qcomment','words') : null);
712                                 $qcomment = (($qc) ? explode("\n",$qc) : null);
713                         }
714                         $comment_box = replace_macros($template,array(
715                                 '$return_path' => $a->query_string,
716                                 '$threaded' => $this->is_threaded(),
717 //                              '$jsreload' => (($conv->get_mode() === 'display') ? $_SESSION['return_url'] : ''),
718                                 '$jsreload' => '',
719                                 '$type' => (($conv->get_mode() === 'profile') ? 'wall-comment' : 'net-comment'),
720                                 '$id' => $this->get_id(),
721                                 '$parent' => $this->get_id(),
722                                 '$qcomment' => $qcomment,
723                                 '$profile_uid' =>  $conv->get_profile_owner(),
724                                 '$mylink' => $a->remove_baseurl($a->contact['url']),
725                                 '$mytitle' => t('This is you'),
726                                 '$myphoto' => $a->remove_baseurl($a->contact['thumb']),
727                                 '$comment' => t('Comment'),
728                                 '$submit' => t('Submit'),
729                                 '$edbold' => t('Bold'),
730                                 '$editalic' => t('Italic'),
731                                 '$eduline' => t('Underline'),
732                                 '$edquote' => t('Quote'),
733                                 '$edcode' => t('Code'),
734                                 '$edimg' => t('Image'),
735                                 '$edurl' => t('Link'),
736                                 '$edvideo' => t('Video'),
737                                 '$preview' => ((feature_enabled($conv->get_profile_owner(),'preview')) ? t('Preview') : ''),
738                                 '$indent' => $indent,
739                                 '$sourceapp' => t($a->sourcename),
740                                 '$ww' => (($conv->get_mode() === 'network') ? $ww : ''),
741                                 '$rand_num' => random_digits(12)
742                         ));
743                 }
744
745                 return $comment_box;
746         }
747
748         private function get_redirect_url() {
749                 return $this->redirect_url;
750         }
751
752         /**
753          * Check if we are a wall to wall item and set the relevant properties
754          */
755         protected function check_wall_to_wall() {
756                 $a = $this->get_app();
757                 $conv = $this->get_conversation();
758                 $this->wall_to_wall = false;
759
760                 if($this->is_toplevel()) {
761                         if($conv->get_mode() !== 'profile') {
762                                 if($this->get_data_value('wall') AND !$this->get_data_value('self')) {
763                                         // On the network page, I am the owner. On the display page it will be the profile owner.
764                                         // This will have been stored in $a->page_contact by our calling page.
765                                         // Put this person as the wall owner of the wall-to-wall notice.
766
767                                         $this->owner_url = zrl($a->page_contact['url']);
768                                         $this->owner_photo = $a->page_contact['thumb'];
769                                         $this->owner_name = $a->page_contact['name'];
770                                         $this->wall_to_wall = true;
771                                 }
772                                 else if($this->get_data_value('owner-link')) {
773
774                                         $owner_linkmatch = (($this->get_data_value('owner-link')) && link_compare($this->get_data_value('owner-link'),$this->get_data_value('author-link')));
775                                         $alias_linkmatch = (($this->get_data_value('alias')) && link_compare($this->get_data_value('alias'),$this->get_data_value('author-link')));
776                                         $owner_namematch = (($this->get_data_value('owner-name')) && $this->get_data_value('owner-name') == $this->get_data_value('author-name'));
777
778                                         if((! $owner_linkmatch) && (! $alias_linkmatch) && (! $owner_namematch)) {
779
780                                                 // The author url doesn't match the owner (typically the contact)
781                                                 // and also doesn't match the contact alias. 
782                                                 // The name match is a hack to catch several weird cases where URLs are 
783                                                 // all over the park. It can be tricked, but this prevents you from
784                                                 // seeing "Bob Smith to Bob Smith via Wall-to-wall" and you know darn
785                                                 // well that it's the same Bob Smith. 
786
787                                                 // But it could be somebody else with the same name. It just isn't highly likely. 
788
789
790                                                 $this->owner_photo = $this->get_data_value('owner-avatar');
791                                                 $this->owner_name = $this->get_data_value('owner-name');
792                                                 $this->wall_to_wall = true;
793                                                 // If it is our contact, use a friendly redirect link
794                                                 if((link_compare($this->get_data_value('owner-link'),$this->get_data_value('url'))) 
795                                                         && ($this->get_data_value('network') === NETWORK_DFRN)) {
796                                                         $this->owner_url = $this->get_redirect_url();
797                                                 }
798                                                 else
799                                                         $this->owner_url = zrl($this->get_data_value('owner-link'));
800                                         }
801                                 }
802                         }
803                 }
804
805                 if(!$this->wall_to_wall) {
806                         $this->set_template('wall');
807                         $this->owner_url = '';
808                         $this->owner_photo = '';
809                         $this->owner_name = '';
810                 }
811         }
812
813         private function is_wall_to_wall() {
814                 return $this->wall_to_wall;
815         }
816
817         private function get_owner_url() {
818                 return $this->owner_url;
819         }
820
821         private function get_owner_photo() {
822                 return $this->owner_photo;
823         }
824
825         private function get_owner_name() {
826                 return $this->owner_name;
827         }
828
829         private function is_visiting() {
830                 return $this->visiting;
831         }
832
833
834
835
836 }
837 ?>