123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749 |
- <?php
- defined('GNUSOCIAL') || die();
- class NoticeListItem extends Widget
- {
-
- public $notice = null;
-
- public $repeat = null;
-
- public $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;
-
- public function __construct(Notice $notice, Action $out = null, array $prefs = [])
- {
- 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 (['maxchars'] as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = (int)$prefs[$key];
- }
- }
-
- foreach (['addressees', 'attachments', 'options'] as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = (bool)$prefs[$key];
- }
- }
-
- foreach (['id_prefix', 'item_tag'] as $key) {
- if (array_key_exists($key, $prefs)) {
- $this->$key = $prefs[$key];
- }
- }
- }
-
- public function show()
- {
- if (empty($this->notice)) {
- common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
- return;
- } elseif (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();
- }
- public function showNotice()
- {
- if (Event::handle('StartShowNoticeItemNotice', array($this))) {
- $this->showNoticeHeaders();
- $this->showContent();
- $this->showNoticeFooter();
- Event::handle('EndShowNoticeItemNotice', array($this));
- }
- }
- public function showNoticeHeaders()
- {
- $this->elementStart('section', ['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');
- }
- public function showNoticeFooter()
- {
- $this->elementStart('footer');
- $this->showNoticeInfo();
- if ($this->options) {
- $this->showNoticeOptions();
- }
- if ($this->attachments) {
- $this->showNoticeAttachments();
- }
- $this->elementEnd('footer');
- }
- public function showNoticeTitle()
- {
- if (Event::handle('StartShowNoticeTitle', array($this))) {
- $nameClass = $this->notice->getTitle(false) ? 'p-name ' : '';
- $this->element(
- 'a',
- [
- 'href' => $this->notice->getUri(),
- 'class' => $nameClass . 'u-uid',
- ],
- $this->notice->getTitle()
- );
- Event::handle('EndShowNoticeTitle', array($this));
- }
- }
- public function showNoticeInfo()
- {
- if (Event::handle('StartShowNoticeInfo', array($this))) {
- $this->showContextLink();
- $this->showNoticeLink();
- $this->showNoticeSource();
- $this->showNoticeLocation();
- $this->showPermalink();
- Event::handle('EndShowNoticeInfo', array($this));
- }
- }
- public 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));
- }
- }
-
- public function showStart()
- {
- if (Event::handle('StartOpenNoticeListItemElement', [$this])) {
-
- $attrs = [];
-
- $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
- $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
- $id_decl = "${id_prefix}notice-${id}";
- $attrs['id'] = $id_decl;
-
- $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) {
-
- }
- $attrs['class'] = $class;
-
- if (!$this->notice->isLocal()) {
- $attrs['data-nosnippet'] = 'true';
- }
- $this->out->elementStart($this->item_tag, $attrs);
- Event::handle('EndOpenNoticeListItemElement', [$this]);
- }
- }
-
- public function showAuthor()
- {
- $attrs = [
- 'href' => $this->profile->getUrl(),
- 'class' => 'h-card',
- 'title' => $this->profile->getHtmlTitle(),
- ];
- 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));
- }
- }
- public function showParent()
- {
- $this->out->element(
- 'a',
- array(
- 'href' => $this->notice->getParent()->getUrl(),
- 'class' => 'u-in-reply-to',
- 'rel' => 'in-reply-to'
- ),
- 'in reply to'
- );
- }
- public function showAddressees()
- {
- $pa = $this->getProfileAddressees();
- if (!empty($pa)) {
- $this->out->elementStart('ul', 'addressees');
- $first = true;
- foreach ($pa as $addr) {
- $this->out->elementStart('li');
- $text = $addr['text'];
- unset($addr['text']);
- $this->out->element('a', $addr, $text);
- $this->out->elementEnd('li');
- }
- $this->out->elementEnd('ul');
- }
- }
- public function getProfileAddressees()
- {
- if ($this->pa) {
- return $this->pa;
- }
- $this->pa = array();
- $attentions = $this->getAttentionProfiles();
- foreach ($attentions as $attn) {
- if ($attn->isGroup()) {
- $class = 'group';
- $profileurl = common_local_url('groupbyid', array('id' => $attn->getGroup()->getID()));
- } else {
- $class = 'account';
- $profileurl = common_local_url('userbyid', array('id' => $attn->getID()));
- }
- $this->pa[] = [
- 'href' => $profileurl,
- 'title' => $attn->getHtmlTitle(),
- 'class' => "addressee {$class} p-name u-url",
- 'text' => $attn->getStreamName(),
- ];
- }
- return $this->pa;
- }
- public function getAttentionProfiles()
- {
- return $this->notice->getAttentionProfiles();
- }
-
- public function showNickname()
- {
- $this->out->raw(
- '<span class="p-name">' .
- htmlspecialchars($this->profile->getNickname()) .
- '</span>'
- );
- }
-
- public function showContent()
- {
-
- $nameClass = $this->notice->getTitle(false) ? '' : 'p-name ';
- $this->out->elementStart('article', array('class' => $nameClass . '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');
- }
- public function showNoticeAttachments()
- {
- if (common_config('attachments', 'show_thumbs')) {
- $al = new InlineAttachmentList($this->notice, $this->out);
- $al->show();
- }
- }
-
- public function showNoticeLink()
- {
- $this->out->element(
- 'time',
- [
- 'class' => 'dt-published',
- 'datetime' => common_date_iso8601($this->notice->created),
- 'title' => common_exact_date($this->notice->created),
- ],
- common_date_string($this->notice->created)
- );
- }
-
- public function showNoticeLocation()
- {
- 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',
- [
- 'class' => 'geo',
- 'title' => $latlon,
- ],
- $name
- );
- } else {
- $xstr = new XMLStringer(false);
- $xstr->elementStart(
- 'a',
- ['href' => $url, 'rel' => 'external']
- );
- $xstr->element(
- 'abbr',
- ['class' => 'geo', 'title' => $latlon],
- $name
- );
- $xstr->elementEnd('a');
- $this->out->raw($xstr->getString());
- }
- $this->out->elementEnd('span');
- }
-
- public function decimalDegreesToDMS($dec)
- {
- $deg = intval($dec);
- $tempma = abs($dec) - abs($deg);
- $tempma = $tempma * 3600;
- $min = floor($tempma / 60);
- $sec = $tempma - ($min * 60);
- return ['deg' => $deg, 'min' => $min, 'sec' => $sec];
- }
-
- public function showNoticeSource()
- {
- $ns = $this->notice->getSource();
- if (!$ns instanceof Notice_source) {
- return;
- }
-
- $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');
- }
-
- public function showPermalink()
- {
- $class = 'permalink u-url';
- if (!$this->notice->isLocal()) {
- $class .= ' external';
- }
- try {
- if ($this->repeat) {
- $this->out->element(
- 'a',
- [
- 'href' => $this->repeat->getUrl(),
- 'class' => 'u-url',
- ],
- ''
- );
- $class = str_replace('u-url', 'u-repost-of', $class);
- }
- } catch (InvalidUrlException $e) {
-
- }
- try {
- $this->out->element(
- 'a',
- [
- 'href' => $this->notice->getUrl(true),
- 'class' => $class,
- ],
-
- _('permalink')
- );
- } catch (InvalidUrlException $e) {
-
- }
- }
-
- public function showContextLink()
- {
- $this->out->element(
- 'a',
- [
- 'rel' => 'bookmark',
- 'class' => 'timestamp',
- 'href' => Conversation::getUrlFromNotice($this->notice),
- ],
-
- _('In conversation')
- );
- }
-
- public function showReplyLink()
- {
- if (common_logged_in()) {
- $this->out->text(' ');
- $reply_url = common_local_url(
- 'newnotice',
- [
- 'replyto' => $this->profile->getNickname(),
- 'inreplyto' => $this->notice->id,
- ]
- );
- $this->out->elementStart(
- 'a',
- [
- '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');
- }
- }
-
- public function showDeleteLink()
- {
- $user = common_current_user();
- $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
- if (!empty($user) &&
- !$this->notice->isVerb([ActivityVerb::DELETE]) &&
- ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
- $this->out->text(' ');
- $deleteurl = common_local_url(
- 'deletenotice',
- ['notice' => $todel->id]
- );
- $this->out->element(
- 'a',
- [
- 'href' => $deleteurl,
- 'class' => 'notice_delete popup',
-
- 'title' => _m('Delete this notice from the timeline.'),
- ],
-
- _m('Delete')
- );
- }
- }
-
- public function showEnd()
- {
- if (Event::handle('StartCloseNoticeListItemElement', [$this])) {
- $this->out->elementEnd('li');
- Event::handle('EndCloseNoticeListItemElement', [$this]);
- }
- }
-
- public function getNotice()
- {
- return $this->notice;
- }
- }
|