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