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