From 5a76390d4629e6df2d90dad7448b36d523afddc9 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 8 Jan 2015 20:07:10 +0100 Subject: [PATCH] Reuse NoticeList for NoticeSection listing Something smarter than the 'addressees' and 'attachments' booleans etc. is desired. --- lib/noticelist.php | 34 ++++++++++++++++++--- lib/noticelistitem.php | 31 +++++++++++++++---- lib/noticesection.php | 68 +++++++++--------------------------------- 3 files changed, 70 insertions(+), 63 deletions(-) diff --git a/lib/noticelist.php b/lib/noticelist.php index 346974f1d2..5c0e67adb0 100644 --- a/lib/noticelist.php +++ b/lib/noticelist.php @@ -53,15 +53,35 @@ class NoticeList extends Widget var $notice = null; + protected $addressees = true; + protected $attachments = true; + protected $maxchars = 0; + protected $options = true; + protected $show_n = NOTICES_PER_PAGE; + /** * constructor * * @param Notice $notice stream of notices from DB_DataObject */ - function __construct(Notice $notice, $out=null) + function __construct(Notice $notice, $out=null, array $prefs=array()) { parent::__construct($out); $this->notice = $notice; + + // integer preferences + foreach(array('show_n', '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]; + } + } + } /** @@ -70,7 +90,9 @@ class NoticeList extends Widget * "Uses up" the stream by looping through it. So, probably can't * be called twice on the same list. * - * @return int count of notices listed. + * @param integer $n The amount of notices to show. + * + * @return int Total amount of notices actually available. */ function show() { @@ -79,7 +101,7 @@ class NoticeList extends Widget $notices = $this->notice->fetchAll(); $total = count($notices); - $notices = array_slice($notices, 0, NOTICES_PER_PAGE); + $notices = array_slice($notices, 0, $this->show_n); self::prefill($notices); @@ -113,7 +135,11 @@ class NoticeList extends Widget */ function newListItem(Notice $notice) { - return new NoticeListItem($notice, $this->out); + $prefs = array('addressees' => $this->addressees, + 'attachments' => $this->attachments, + 'maxchars' => $this->maxchars, + 'options' => $this->options); + return new NoticeListItem($notice, $this->out, $prefs); } static function prefill(array &$notices) diff --git a/lib/noticelistitem.php b/lib/noticelistitem.php index dc1a1a02b1..be55c522c8 100644 --- a/lib/noticelistitem.php +++ b/lib/noticelistitem.php @@ -58,6 +58,11 @@ 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 $options = true; + protected $maxchars = 0; // if <= 0 it means use full posts + /** * constructor * @@ -65,7 +70,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 +84,21 @@ 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]; + } + } } /** @@ -123,7 +142,7 @@ class NoticeListItem extends Widget $this->elementStart('section', array('class'=>'notice-headers')); $this->showNoticeTitle(); $this->showAuthor(); - $this->showAddressees(); + if ($this->addressees) { $this->showAddressees(); } $this->elementEnd('section'); } @@ -131,8 +150,8 @@ class NoticeListItem extends Widget { $this->elementStart('footer'); $this->showNoticeInfo(); - $this->showNoticeAttachments(); - $this->showNoticeOptions(); + if ($this->attachments) { $this->showNoticeAttachments(); } + if ($this->options) { $this->showNoticeOptions(); } $this->elementEnd('footer'); } @@ -289,7 +308,9 @@ class NoticeListItem extends Widget // FIXME: URL, image, video, audio $this->out->elementStart('article', array('class' => 'e-content')); if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) { - if ($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) . '[…]'); + } elseif ($this->notice->rendered) { $this->out->raw($this->notice->rendered); } else { // XXX: may be some uncooked notices in the DB, diff --git a/lib/noticesection.php b/lib/noticesection.php index b7f38ba665..51c00d91bc 100644 --- a/lib/noticesection.php +++ b/lib/noticesection.php @@ -27,9 +27,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +if (!defined('GNUSOCIAL') && !defined('GNUSOCIAL')) { exit(1); } define('NOTICES_PER_SECTION', 6); @@ -50,65 +48,27 @@ define('NOTICES_PER_SECTION', 6); class NoticeSection extends Section { + protected $addressees = false; + protected $attachments = false; protected $maxchars = 140; + protected $options = false; + protected $show_n = NOTICES_PER_SECTION; function showContent() { - $notices = $this->getNotices(); - $cnt = 0; - $this->out->elementStart('ol', 'notices xoxo'); - while ($notices->fetch() && ++$cnt <= NOTICES_PER_SECTION) { - $this->showNotice($notices); - } - - $this->out->elementEnd('ol'); - return ($cnt > NOTICES_PER_SECTION); + // args: notice object, html outputter, preference array for list and items + $list = new NoticeList($this->getNotices(), $this->out, + array('addressees' => $this->addressees, + 'attachments' => $this->attachments, + 'maxchars'=> $this->maxchars, + 'options' => $this->options, + 'show_n' => $this->show_n)); + $total = $list->show(); // returns total amount of notices available + return ($total > NOTICES_PER_SECTION); // do we have more to show? } function getNotices() { return null; } - - function showNotice(Notice $notice) - { - $profile = $notice->getProfile(); - if (empty($profile)) { - common_log(LOG_WARNING, sprintf("Notice %d has no profile", - $notice->id)); - return; - } - $this->out->elementStart('li', 'h-entry notice'); - $this->out->elementStart('div', 'h-card'); - $this->out->elementStart('a', array('title' => $profile->getBestName(), - 'href' => $profile->profileurl, - 'class' => 'p-author u-url p-name')); - $avatarUrl = $profile->avatarUrl(AVATAR_MINI_SIZE); - $this->out->element('img', array('src' => $avatarUrl, - 'width' => AVATAR_MINI_SIZE, - 'height' => AVATAR_MINI_SIZE, - 'class' => 'avatar u-photo', - 'alt' => $profile->getBestName())); - $this->out->text($profile->getBestName()); - $this->out->elementEnd('a'); - - $this->out->elementStart('p', 'e-content'); - $this->out->text(mb_strlen($notice->content) > $this->maxchars - ? mb_substr($notice->content, 0, $this->maxchars) . '[…]' - : $notice->content); - $this->out->elementEnd('p'); - - $this->out->elementStart('div', 'entry_content'); - $nli = new NoticeListItem($notice, $this->out); - $nli->showNoticeLink(); - $this->out->elementEnd('div'); - - if (!empty($notice->value)) { - $this->out->elementStart('p'); - $this->out->text($notice->value); - $this->out->elementEnd('p'); - } - $this->out->elementEnd('div'); - $this->out->elementEnd('li'); - } }