]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/noticelistitem.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / lib / noticelistitem.php
index 57207339a912d9ac0ae5fbe0f504db5cbb2a1e90..2d538c6e6a9aa6f249cef1878b901acc0993df61 100644 (file)
@@ -58,6 +58,14 @@ class NoticeListItem extends Widget
     /** The profile of the author of the notice, extracted once for convenience. */
     var $profile = null;
 
+    protected $addressees = true;
+    protected $attachments = true;
+    protected $id_prefix = null;
+    protected $options = true;
+    protected $maxchars = 0;   // if <= 0 it means use full posts
+    protected $item_tag = 'li';
+    protected $pa = null;
+
     /**
      * constructor
      *
@@ -65,7 +73,7 @@ class NoticeListItem extends Widget
      *
      * @param Notice $notice The notice we'll display
      */
-    function __construct(Notice $notice, Action $out=null)
+    function __construct(Notice $notice, Action $out=null, array $prefs=array())
     {
         parent::__construct($out);
         if (!empty($notice->repeat_of)) {
@@ -79,7 +87,27 @@ class NoticeListItem extends Widget
         } else {
             $this->notice  = $notice;
         }
+
         $this->profile = $this->notice->getProfile();
+        
+        // integer preferences
+        foreach(array('maxchars') as $key) {
+            if (array_key_exists($key, $prefs)) {
+                $this->$key = (int)$prefs[$key];
+            }
+        }
+        // boolean preferences
+        foreach(array('addressees', 'attachments', 'options') as $key) {
+            if (array_key_exists($key, $prefs)) {
+                $this->$key = (bool)$prefs[$key];
+            }
+        }
+        // string preferences
+        foreach(array('id_prefix', 'item_tag') as $key) {
+            if (array_key_exists($key, $prefs)) {
+                $this->$key = $prefs[$key];
+            }
+        }
     }
 
     /**
@@ -103,9 +131,6 @@ class NoticeListItem extends Widget
         $this->showStart();
         if (Event::handle('StartShowNoticeItem', array($this))) {
             $this->showNotice();
-            $this->showNoticeAttachments();
-            $this->showNoticeInfo();
-            $this->showNoticeOptions();
             Event::handle('EndShowNoticeItem', array($this));
         }
         $this->showEnd();
@@ -113,17 +138,47 @@ class NoticeListItem extends Widget
 
     function showNotice()
     {
+        if (Event::handle('StartShowNoticeItemNotice', array($this))) {
+            $this->showNoticeHeaders();
+            $this->showContent();
+            $this->showNoticeFooter();
+            Event::handle('EndShowNoticeItemNotice', array($this));
+        }
+    }
+
+    function showNoticeHeaders()
+    {
+        $this->elementStart('section', array('class'=>'notice-headers'));
         $this->showNoticeTitle();
         $this->showAuthor();
-        $this->showAddressees();
-        $this->showContent();
+
+        if (!empty($this->notice->reply_to) || count($this->getProfileAddressees()) > 0) {
+            $this->elementStart('div', array('class' => 'parents'));
+            try {
+                $this->showParent();
+            } catch (NoParentNoticeException $e) {
+                // no parent notice
+            }
+            if ($this->addressees) { $this->showAddressees(); }
+            $this->elementEnd('div');
+        }
+        $this->elementEnd('section');
+    }
+
+    function showNoticeFooter()
+    {
+        $this->elementStart('footer');
+        $this->showNoticeInfo();
+        if ($this->options) { $this->showNoticeOptions(); }
+        if ($this->attachments) { $this->showNoticeAttachments(); }
+        $this->elementEnd('footer');
     }
 
     function showNoticeTitle()
     {
         if (Event::handle('StartShowNoticeTitle', array($this))) {
-            $this->element('a', array('href' => $this->notice->getUrl(),
-                                      'class' => 'p-name metadata'),
+            $this->element('a', array('href' => $this->notice->getUrl(true),
+                                      'class' => 'notice-title'),
                            $this->notice->getTitle());
             Event::handle('EndShowNoticeTitle', array($this));
         }
@@ -131,35 +186,30 @@ class NoticeListItem extends Widget
 
     function showNoticeInfo()
     {
-        $this->out->elementStart('div', 'entry-metadata');
         if (Event::handle('StartShowNoticeInfo', array($this))) {
             $this->showNoticeLink();
             $this->showNoticeSource();
             $this->showNoticeLocation();
-            if ($this->notice->hasConversation()) {
-                $this->showContext();
-            }
-            $this->showRepeat();
+            $this->showPermalink();
             Event::handle('EndShowNoticeInfo', array($this));
         }
-
-        $this->out->elementEnd('div');
     }
 
     function showNoticeOptions()
     {
         if (Event::handle('StartShowNoticeOptions', array($this))) {
             $user = common_current_user();
-            if ($user) {
+
+            if ($user instanceof User) {
                 $this->out->elementStart('div', 'notice-options');
                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
                     $this->showReplyLink();
-                    $this->showRepeatForm();
                     $this->showDeleteLink();
                     Event::handle('EndShowNoticeOptionItems', array($this));
                 }
                 $this->out->elementEnd('div');
             }
+
             Event::handle('EndShowNoticeOptions', array($this));
         }
     }
@@ -180,8 +230,9 @@ class NoticeListItem extends Widget
             if (!empty($this->notice->source)) {
                 $class .= ' notice-source-'.$this->notice->source;
             }
-            $this->out->elementStart('li', array('class' => $class,
-                                                 'id' => 'notice-' . $id));
+            $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
+            $this->out->elementStart($this->item_tag, array('class' => $class,
+                                                 'id' => "${id_prefix}notice-${id}"));
             Event::handle('EndOpenNoticeListItemElement', array($this));
         }
     }
@@ -197,8 +248,9 @@ class NoticeListItem extends Widget
     function showAuthor()
     {
         $attrs = array('href' => $this->profile->profileurl,
-                       'class' => 'h-card p-author',
+                       'class' => 'h-card',
                        'title' => $this->profile->getNickname());
+        if(empty($this->repeat)) { $attrs['class'] .= ' p-author'; }
 
         if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
             $this->out->elementStart('a', $attrs);
@@ -209,6 +261,19 @@ class NoticeListItem extends Widget
         }
     }
 
+    function showParent()
+    {
+        $this->out->element(
+            'a',
+            array(
+                'href' => $this->notice->getParent()->getUrl(),
+                'class' => 'u-in-reply-to',
+                'rel' => 'in-reply-to'
+            ),
+            'in reply to'
+        );
+    }
+
     function showAddressees()
     {
         $pa = $this->getProfileAddressees();
@@ -229,19 +294,20 @@ class NoticeListItem extends Widget
 
     function getProfileAddressees()
     {
-        $pa = array();
+        if($this->pa) { return $this->pa; }
+        $this->pa = array();
 
         $attentions = $this->getReplyProfiles();
 
         foreach ($attentions as $attn) {
             $class = $attn->isGroup() ? 'group' : 'account';
-            $pa[] = array('href' => $attn->profileurl,
-                          'title' => $attn->getNickname(),
-                          'class' => "addressee {$class}",
-                          'text' => $attn->getStreamName());
+            $this->pa[] = array('href' => $attn->profileurl,
+                                'title' => $attn->getNickname(),
+                                'class' => "addressee {$class}",
+                                'text' => $attn->getStreamName());
         }
 
-        return $pa;
+        return $this->pa;
     }
 
     function getReplyProfiles()
@@ -275,19 +341,16 @@ class NoticeListItem extends Widget
     function showContent()
     {
         // FIXME: URL, image, video, audio
-        $this->out->elementStart('div', array('class' => 'e-content'));
+        $this->out->elementStart('article', array('class' => 'e-content'));
         if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
-            if ($this->notice->rendered) {
-                $this->out->raw($this->notice->rendered);
+            if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
+                $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
             } else {
-                // XXX: may be some uncooked notices in the DB,
-                // we cook them right now. This should probably disappear in future
-                // versions (>> 0.4.x)
-                $this->out->raw(common_render_content($this->notice->content, $this->notice));
+                $this->out->raw($this->notice->getRendered());
             }
             Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
         }
-        $this->out->elementEnd('div');
+        $this->out->elementEnd('article');
     }
 
     function showNoticeAttachments() {
@@ -307,11 +370,10 @@ class NoticeListItem extends Widget
     function showNoticeLink()
     {
         $this->out->elementStart('a', array('rel' => 'bookmark',
-                                            'class' => 'u-url timestamp',
-                                            'href' => $this->notice->getLocalUrl()));
+                                            'class' => 'timestamp',
+                                            'href' => Conversation::getUrlFromNotice($this->notice)));
         $this->out->element('time', array('class' => 'dt-published',
                                           'datetime' => common_date_iso8601($this->notice->created),
-                                          // TRANS: Timestamp title (tooltip text) for NoticeListItem
                                           'title' => common_exact_date($this->notice->created)),
                             common_date_string($this->notice->created));
         $this->out->elementEnd('a');
@@ -328,18 +390,19 @@ class NoticeListItem extends Widget
      */
     function showNoticeLocation()
     {
-        $id = $this->notice->id;
-
-        $location = $this->notice->getLocation();
-
-        if (empty($location)) {
+        return;
+        try {
+            $location = Notice_location::locFromStored($this->notice);
+        } catch (NoResultException $e) {
+            return;
+        } catch (ServerException $e) {
             return;
         }
 
         $name = $location->getName();
 
-        $lat = $this->notice->lat;
-        $lon = $this->notice->lon;
+        $lat = $location->lat;
+        $lon = $location->lon;
         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
 
         if (empty($name)) {
@@ -416,90 +479,86 @@ class NoticeListItem extends Widget
     {
         $ns = $this->notice->getSource();
 
-        if ($ns) {
-            // TRANS: A possible notice source (web interface).
-            $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
-            $this->out->text(' ');
-            $this->out->elementStart('span', 'source');
-            // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
-            // TRANS: Followed by notice source.
-            $this->out->text(_('from'));
-            $this->out->text(' ');
+        if (!$ns instanceof Notice_source) {
+            return false;
+        }
 
-            $name  = $source_name;
-            $url   = $ns->url;
-            $title = null;
+        // TRANS: A possible notice source (web interface).
+        $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
+        $this->out->text(' ');
+        $this->out->elementStart('span', 'source');
+        // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
+        // TRANS: Followed by notice source.
+        $this->out->text(_('from'));
+        $this->out->text(' ');
 
-            if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
-                $name = $source_name;
-                $url  = $ns->url;
-            }
-            Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
+        $name  = $source_name;
+        $url   = $ns->url;
+        $title = null;
 
-            // if $ns->name and $ns->url are populated we have
-            // configured a source attr somewhere
-            if (!empty($name) && !empty($url)) {
-                $this->out->elementStart('span', 'device');
+        if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
+            $name = $source_name;
+            $url  = $ns->url;
+        }
+        Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
 
-                $attrs = array(
-                    'href' => $url,
-                    'rel' => 'external'
-                );
+        // if $ns->name and $ns->url are populated we have
+        // configured a source attr somewhere
+        if (!empty($name) && !empty($url)) {
+            $this->out->elementStart('span', 'device');
 
-                if (!empty($title)) {
-                    $attrs['title'] = $title;
-                }
+            $attrs = array(
+                'href' => $url,
+                'rel' => 'external'
+            );
 
-                $this->out->element('a', $attrs, $name);
-                $this->out->elementEnd('span');
-            } else {
-                $this->out->element('span', 'device', $name);
+            if (!empty($title)) {
+                $attrs['title'] = $title;
             }
 
+            $this->out->element('a', $attrs, $name);
             $this->out->elementEnd('span');
+        } else {
+            $this->out->element('span', 'device', $name);
         }
-    }
 
-    /**
-     * show link to notice this notice is a reply to
-     *
-     * If this notice is a reply, show a link to the notice it is replying to. The
-     * heavy lifting for figuring out replies happens at save time.
-     *
-     * @return void
-     */
-    function showContext()
-    {
-        $this->out->element('a',
-                            array('href' => $this->notice->getConversationUrl(),
-                                  'class' => 'conversation'),
-                            // TRANS: Addition in notice list item if notice is part of a conversation.
-                            _('in context'));
+        $this->out->elementEnd('span');
     }
 
     /**
-     * show a link to the author of repeat
+     * show link to single-notice view for this notice item
+     *
+     * A permalink that goes to this specific object and nothing else
      *
      * @return void
      */
-    function showRepeat()
+    function showPermalink()
     {
-        if (!empty($this->repeat)) {
-
-            $repeater = Profile::getKV('id', $this->repeat->profile_id);
-
-            $attrs = array('href' => $repeater->profileurl,
-                           'class' => 'h-card p-author',
-                           'title' => $repeater->getFancyName());
-
-            $this->out->elementStart('span', 'repeat h-entry');
-
-            // TRANS: Addition in notice list item if notice was repeated. Followed by a span with a nickname.
-            $this->out->raw(_('Repeated by').' ');
+        $class = 'permalink u-url';
+        if (!$this->notice->isLocal()) {
+            $class .= ' external';
+        }
 
-            $this->out->element('a', $attrs, $repeater->getNickname());
+        try {
+            if($this->repeat) {
+                $this->out->element('a',
+                            array('href' => $this->repeat->getUrl(),
+                                  'class' => 'u-url'),
+                            '');
+                $class = str_replace('u-url', 'u-repost-of', $class);
+            }
+        } catch (InvalidUrlException $e) {
+            // no permalink available
+        }
 
-            $this->out->elementEnd('span');
+        try {
+            $this->out->element('a',
+                        array('href' => $this->notice->getUrl(true),
+                              'class' => $class),
+                        // TRANS: Addition in notice list item for single-notice view.
+                        _('permalink'));
+        } catch (InvalidUrlException $e) {
+            // no permalink available
         }
     }
 
@@ -546,7 +605,7 @@ class NoticeListItem extends Widget
             $deleteurl = common_local_url('deletenotice',
                                           array('notice' => $todel->id));
             $this->out->element('a', array('href' => $deleteurl,
-                                           'class' => 'notice_delete',
+                                           'class' => 'notice_delete popup',
                                            // TRANS: Link title in notice list item to delete a notice.
                                            'title' => _('Delete this notice from the timeline.')),
                                            // TRANS: Link text in notice list item to delete a notice.
@@ -554,34 +613,6 @@ class NoticeListItem extends Widget
         }
     }
 
-    /**
-     * show the form to repeat a notice
-     *
-     * @return void
-     */
-    function showRepeatForm()
-    {
-        if ($this->notice->scope == Notice::PUBLIC_SCOPE ||
-            $this->notice->scope == Notice::SITE_SCOPE) {
-            $user = common_current_user();
-            if (!empty($user) &&
-                $user->id != $this->notice->profile_id) {
-                $this->out->text(' ');
-                $profile = $user->getProfile();
-                if ($profile->hasRepeated($this->notice)) {
-                    $this->out->element('span', array('class' => 'repeated',
-                                                      // TRANS: Title for repeat form status in notice list when a notice has been repeated.
-                                                      'title' => _('Notice repeated.')),
-                                        // TRANS: Repeat form status in notice list when a notice has been repeated.
-                                        _('Repeated'));
-                } else {
-                    $rf = new RepeatForm($this->out, $this->notice);
-                    $rf->show();
-                }
-            }
-        }
-    }
-
     /**
      * finish the notice
      *