Reuse NoticeList for NoticeSection listing

Something smarter than the 'addressees' and 'attachments' booleans etc.
is desired.
This commit is contained in:
Mikael Nordfeldth 2015-01-08 20:07:10 +01:00
parent 5981b5c8d9
commit 5a76390d46
3 changed files with 70 additions and 63 deletions

View File

@ -53,15 +53,35 @@ class NoticeList extends Widget
var $notice = null; var $notice = null;
protected $addressees = true;
protected $attachments = true;
protected $maxchars = 0;
protected $options = true;
protected $show_n = NOTICES_PER_PAGE;
/** /**
* constructor * constructor
* *
* @param Notice $notice stream of notices from DB_DataObject * @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); parent::__construct($out);
$this->notice = $notice; $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 * "Uses up" the stream by looping through it. So, probably can't
* be called twice on the same list. * 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() function show()
{ {
@ -79,7 +101,7 @@ class NoticeList extends Widget
$notices = $this->notice->fetchAll(); $notices = $this->notice->fetchAll();
$total = count($notices); $total = count($notices);
$notices = array_slice($notices, 0, NOTICES_PER_PAGE); $notices = array_slice($notices, 0, $this->show_n);
self::prefill($notices); self::prefill($notices);
@ -113,7 +135,11 @@ class NoticeList extends Widget
*/ */
function newListItem(Notice $notice) 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) static function prefill(array &$notices)

View File

@ -58,6 +58,11 @@ class NoticeListItem extends Widget
/** The profile of the author of the notice, extracted once for convenience. */ /** The profile of the author of the notice, extracted once for convenience. */
var $profile = null; var $profile = null;
protected $addressees = true;
protected $attachments = true;
protected $options = true;
protected $maxchars = 0; // if <= 0 it means use full posts
/** /**
* constructor * constructor
* *
@ -65,7 +70,7 @@ class NoticeListItem extends Widget
* *
* @param Notice $notice The notice we'll display * @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); parent::__construct($out);
if (!empty($notice->repeat_of)) { if (!empty($notice->repeat_of)) {
@ -79,7 +84,21 @@ class NoticeListItem extends Widget
} else { } else {
$this->notice = $notice; $this->notice = $notice;
} }
$this->profile = $this->notice->getProfile(); $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->elementStart('section', array('class'=>'notice-headers'));
$this->showNoticeTitle(); $this->showNoticeTitle();
$this->showAuthor(); $this->showAuthor();
$this->showAddressees(); if ($this->addressees) { $this->showAddressees(); }
$this->elementEnd('section'); $this->elementEnd('section');
} }
@ -131,8 +150,8 @@ class NoticeListItem extends Widget
{ {
$this->elementStart('footer'); $this->elementStart('footer');
$this->showNoticeInfo(); $this->showNoticeInfo();
$this->showNoticeAttachments(); if ($this->attachments) { $this->showNoticeAttachments(); }
$this->showNoticeOptions(); if ($this->options) { $this->showNoticeOptions(); }
$this->elementEnd('footer'); $this->elementEnd('footer');
} }
@ -289,7 +308,9 @@ class NoticeListItem extends Widget
// FIXME: URL, image, video, audio // FIXME: URL, image, video, audio
$this->out->elementStart('article', 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 (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); $this->out->raw($this->notice->rendered);
} else { } else {
// XXX: may be some uncooked notices in the DB, // XXX: may be some uncooked notices in the DB,

View File

@ -27,9 +27,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { if (!defined('GNUSOCIAL') && !defined('GNUSOCIAL')) { exit(1); }
exit(1);
}
define('NOTICES_PER_SECTION', 6); define('NOTICES_PER_SECTION', 6);
@ -50,65 +48,27 @@ define('NOTICES_PER_SECTION', 6);
class NoticeSection extends Section class NoticeSection extends Section
{ {
protected $addressees = false;
protected $attachments = false;
protected $maxchars = 140; protected $maxchars = 140;
protected $options = false;
protected $show_n = NOTICES_PER_SECTION;
function showContent() function showContent()
{ {
$notices = $this->getNotices(); // args: notice object, html outputter, preference array for list and items
$cnt = 0; $list = new NoticeList($this->getNotices(), $this->out,
$this->out->elementStart('ol', 'notices xoxo'); array('addressees' => $this->addressees,
while ($notices->fetch() && ++$cnt <= NOTICES_PER_SECTION) { 'attachments' => $this->attachments,
$this->showNotice($notices); 'maxchars'=> $this->maxchars,
} 'options' => $this->options,
'show_n' => $this->show_n));
$this->out->elementEnd('ol'); $total = $list->show(); // returns total amount of notices available
return ($cnt > NOTICES_PER_SECTION); return ($total > NOTICES_PER_SECTION); // do we have more to show?
} }
function getNotices() function getNotices()
{ {
return null; 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');
}
} }