123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class NoticeListItem extends Widget
- {
-
- var $notice = null;
-
- var $repeat = null;
-
- var $profile = null;
- protected $addressees = true;
- protected $attachments = true;
- protected $id_prefix = null;
- protected $options = true;
- protected $maxchars = 0;
- protected $item_tag = 'li';
- protected $pa = null;
-
- function __construct(Notice $notice, Action $out=null, array $prefs=array())
- {
- parent::__construct($out);
- if (!empty($notice->repeat_of)) {
- $original = Notice::getKV('id', $notice->repeat_of);
- if (!$original instanceof Notice) {
- $this->notice = $notice;
- } else {
- $this->notice = $original;
- $this->repeat = $notice;
- }
- } else {
- $this->notice = $notice;
- }
- $this->profile = $this->notice->getProfile();
-
-
- foreach(array('maxchars') as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = (int)$prefs[$key];
- }
- }
-
- foreach(array('addressees', 'attachments', 'options') as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = (bool)$prefs[$key];
- }
- }
-
- foreach(array('id_prefix', 'item_tag') as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = $prefs[$key];
- }
- }
- }
-
- function show()
- {
- if (empty($this->notice)) {
- common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
- return;
- } else if (empty($this->profile)) {
- common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
- return;
- }
- $this->showStart();
- if (Event::handle('StartShowNoticeItem', array($this))) {
- $this->showNotice();
- Event::handle('EndShowNoticeItem', array($this));
- }
- $this->showEnd();
- }
- 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();
- if (!empty($this->notice->reply_to) || count($this->getProfileAddressees()) > 0) {
- $this->elementStart('div', array('class' => 'parents'));
- try {
- $this->showParent();
- } catch (NoParentNoticeException $e) {
-
- } catch (InvalidUrlException $e) {
-
- }
- 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(true),
- 'class' => 'notice-title'),
- $this->notice->getTitle());
- Event::handle('EndShowNoticeTitle', array($this));
- }
- }
- function showNoticeInfo()
- {
- if (Event::handle('StartShowNoticeInfo', array($this))) {
- $this->showNoticeLink();
- $this->showNoticeSource();
- $this->showNoticeLocation();
- $this->showPermalink();
- Event::handle('EndShowNoticeInfo', array($this));
- }
- }
- function showNoticeOptions()
- {
- if (Event::handle('StartShowNoticeOptions', array($this))) {
- $user = common_current_user();
- if ($user) {
- $this->out->elementStart('div', 'notice-options');
- if (Event::handle('StartShowNoticeOptionItems', array($this))) {
- $this->showReplyLink();
- $this->showDeleteLink();
- Event::handle('EndShowNoticeOptionItems', array($this));
- }
- $this->out->elementEnd('div');
- }
- Event::handle('EndShowNoticeOptions', array($this));
- }
- }
-
- function showStart()
- {
- if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
- $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
- $class = 'h-entry notice';
- if ($this->notice->scope != 0 && $this->notice->scope != 1) {
- $class .= ' limited-scope';
- }
- try {
- $class .= ' notice-source-'.common_to_alphanumeric($this->notice->source);
- } catch (Exception $e) {
-
- }
- $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));
- }
- }
-
- function showAuthor()
- {
- $attrs = array('href' => $this->profile->profileurl,
- '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);
- $this->showAvatar($this->profile);
- $this->out->text($this->profile->getStreamName());
- $this->out->elementEnd('a');
- Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
- }
- }
- 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();
- if (!empty($pa)) {
- $this->out->elementStart('ul', 'addressees');
- $first = true;
- foreach ($pa as $addr) {
- $this->out->elementStart('li', 'h-card');
- $text = $addr['text'];
- unset($addr['text']);
- $this->out->element('a', $addr, $text);
- $this->out->elementEnd('li');
- }
- $this->out->elementEnd('ul', 'addressees');
- }
- }
- function getProfileAddressees()
- {
- if($this->pa) { return $this->pa; }
- $this->pa = array();
- $attentions = $this->getAttentionProfiles();
- foreach ($attentions as $attn) {
- $class = $attn->isGroup() ? 'group' : 'account';
- $this->pa[] = array('href' => $attn->profileurl,
- 'title' => $attn->getNickname(),
- 'class' => "addressee {$class}",
- 'text' => $attn->getStreamName());
- }
- return $this->pa;
- }
- function getAttentionProfiles()
- {
- return $this->notice->getAttentionProfiles();
- }
-
- function showNickname()
- {
- $this->out->raw('<span class="p-name">' .
- htmlspecialchars($this->profile->getNickname()) .
- '</span>');
- }
-
- function showContent()
- {
-
- $this->out->elementStart('article', array('class' => 'e-content'));
- if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
- if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
- $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
- } else {
- $this->out->raw($this->notice->getRendered());
- }
- Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
- }
- $this->out->elementEnd('article');
- }
- function showNoticeAttachments() {
- if (common_config('attachments', 'show_thumbs')) {
- $al = new InlineAttachmentList($this->notice, $this->out);
- $al->show();
- }
- }
-
- function showNoticeLink()
- {
- $this->out->elementStart('a', array('rel' => 'bookmark',
- 'class' => 'timestamp',
- 'href' => Conversation::getUrlFromNotice($this->notice)));
- $this->out->element('time', array('class' => 'dt-published',
- 'datetime' => common_date_iso8601($this->notice->created),
- 'title' => common_exact_date($this->notice->created)),
- common_date_string($this->notice->created));
- $this->out->elementEnd('a');
- }
-
- function showNoticeLocation()
- {
- return;
- try {
- $location = Notice_location::locFromStored($this->notice);
- } catch (NoResultException $e) {
- return;
- } catch (ServerException $e) {
- return;
- }
- $name = $location->getName();
- $lat = $location->lat;
- $lon = $location->lon;
- $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
- if (empty($name)) {
- $latdms = $this->decimalDegreesToDMS(abs($lat));
- $londms = $this->decimalDegreesToDMS(abs($lon));
-
- $north = _('N');
-
- $south = _('S');
-
- $east = _('E');
-
- $west = _('W');
- $name = sprintf(
-
-
-
-
-
- _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
- $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
- $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
- }
- $url = $location->getUrl();
- $this->out->text(' ');
- $this->out->elementStart('span', array('class' => 'location'));
-
- $this->out->text(_('at'));
- $this->out->text(' ');
- if (empty($url)) {
- $this->out->element('abbr', array('class' => 'geo',
- 'title' => $latlon),
- $name);
- } else {
- $xstr = new XMLStringer(false);
- $xstr->elementStart('a', array('href' => $url,
- 'rel' => 'external'));
- $xstr->element('abbr', array('class' => 'geo',
- 'title' => $latlon),
- $name);
- $xstr->elementEnd('a');
- $this->out->raw($xstr->getString());
- }
- $this->out->elementEnd('span');
- }
-
- function decimalDegreesToDMS($dec)
- {
- $deg = intval($dec);
- $tempma = abs($dec) - abs($deg);
- $tempma = $tempma * 3600;
- $min = floor($tempma / 60);
- $sec = $tempma - ($min*60);
- return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
- }
-
- function showNoticeSource()
- {
- $ns = $this->notice->getSource();
- if (!$ns instanceof Notice_source) {
- return false;
- }
-
- $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
- $this->out->text(' ');
- $this->out->elementStart('span', 'source');
-
-
- $this->out->text(_('from'));
- $this->out->text(' ');
- $name = $source_name;
- $url = $ns->url;
- $title = null;
- 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));
-
-
- if (!empty($name) && !empty($url)) {
- $this->out->elementStart('span', 'device');
- $attrs = array(
- 'href' => $url,
- 'rel' => 'external'
- );
- if (!empty($title)) {
- $attrs['title'] = $title;
- }
- $this->out->element('a', $attrs, $name);
- $this->out->elementEnd('span');
- } else {
- $this->out->element('span', 'device', $name);
- }
- $this->out->elementEnd('span');
- }
-
- function showPermalink()
- {
- $class = 'permalink u-url';
- if (!$this->notice->isLocal()) {
- $class .= ' external';
- }
- 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) {
-
- }
- try {
- $this->out->element('a',
- array('href' => $this->notice->getUrl(true),
- 'class' => $class),
-
- _('permalink'));
- } catch (InvalidUrlException $e) {
-
- }
- }
-
- function showReplyLink()
- {
- if (common_logged_in()) {
- $this->out->text(' ');
- $reply_url = common_local_url('newnotice',
- array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
- $this->out->elementStart('a', array('href' => $reply_url,
- 'class' => 'notice_reply',
-
- 'title' => _('Reply to this notice.')));
-
- $this->out->text(_('Reply'));
- $this->out->text(' ');
- $this->out->element('span', 'notice_id', $this->notice->id);
- $this->out->elementEnd('a');
- }
- }
-
- function showDeleteLink()
- {
- $user = common_current_user();
- $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
- if (!empty($user) &&
- ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
- $this->out->text(' ');
- $deleteurl = common_local_url('deletenotice',
- array('notice' => $todel->id));
- $this->out->element('a', array('href' => $deleteurl,
- 'class' => 'notice_delete popup',
-
- 'title' => _('Delete this notice from the timeline.')),
-
- _('Delete'));
- }
- }
-
- function showEnd()
- {
- if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
- $this->out->elementEnd('li');
- Event::handle('EndCloseNoticeListItemElement', array($this));
- }
- }
-
- function getNotice()
- {
- return $this->notice;
- }
- }
|