noticelistitem.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * An item in a notice list
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Widget
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * widget for displaying a single notice
  33. *
  34. * This widget has the core smarts for showing a single notice: what to display,
  35. * where, and under which circumstances. Its key method is show(); this is a recipe
  36. * that calls all the other show*() methods to build up a single notice. The
  37. * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
  38. * author info (since that's implicit by the data in the page).
  39. *
  40. * @category UI
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. * @see NoticeList
  46. * @see ProfileNoticeListItem
  47. */
  48. class NoticeListItem extends Widget
  49. {
  50. /** The notice this item will show. */
  51. var $notice = null;
  52. /** The notice that was repeated. */
  53. var $repeat = null;
  54. /** The profile of the author of the notice, extracted once for convenience. */
  55. var $profile = null;
  56. protected $addressees = true;
  57. protected $attachments = true;
  58. protected $id_prefix = null;
  59. protected $options = true;
  60. protected $maxchars = 0; // if <= 0 it means use full posts
  61. protected $item_tag = 'li';
  62. protected $pa = null;
  63. /**
  64. * constructor
  65. *
  66. * Also initializes the profile attribute.
  67. *
  68. * @param Notice $notice The notice we'll display
  69. */
  70. function __construct(Notice $notice, Action $out=null, array $prefs=array())
  71. {
  72. parent::__construct($out);
  73. if (!empty($notice->repeat_of)) {
  74. $original = Notice::getKV('id', $notice->repeat_of);
  75. if (!$original instanceof Notice) { // could have been deleted
  76. $this->notice = $notice;
  77. } else {
  78. $this->notice = $original;
  79. $this->repeat = $notice;
  80. }
  81. } else {
  82. $this->notice = $notice;
  83. }
  84. $this->profile = $this->notice->getProfile();
  85. // integer preferences
  86. foreach(array('maxchars') as $key) {
  87. if (array_key_exists($key, $prefs)) {
  88. $this->$key = (int)$prefs[$key];
  89. }
  90. }
  91. // boolean preferences
  92. foreach(array('addressees', 'attachments', 'options') as $key) {
  93. if (array_key_exists($key, $prefs)) {
  94. $this->$key = (bool)$prefs[$key];
  95. }
  96. }
  97. // string preferences
  98. foreach(array('id_prefix', 'item_tag') as $key) {
  99. if (array_key_exists($key, $prefs)) {
  100. $this->$key = $prefs[$key];
  101. }
  102. }
  103. }
  104. /**
  105. * recipe function for displaying a single notice.
  106. *
  107. * This uses all the other methods to correctly display a notice. Override
  108. * it or one of the others to fine-tune the output.
  109. *
  110. * @return void
  111. */
  112. function show()
  113. {
  114. if (empty($this->notice)) {
  115. common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
  116. return;
  117. } else if (empty($this->profile)) {
  118. common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
  119. return;
  120. }
  121. $this->showStart();
  122. if (Event::handle('StartShowNoticeItem', array($this))) {
  123. $this->showNotice();
  124. Event::handle('EndShowNoticeItem', array($this));
  125. }
  126. $this->showEnd();
  127. }
  128. function showNotice()
  129. {
  130. if (Event::handle('StartShowNoticeItemNotice', array($this))) {
  131. $this->showNoticeHeaders();
  132. $this->showContent();
  133. $this->showNoticeFooter();
  134. Event::handle('EndShowNoticeItemNotice', array($this));
  135. }
  136. }
  137. function showNoticeHeaders()
  138. {
  139. $this->elementStart('section', array('class'=>'notice-headers'));
  140. $this->showNoticeTitle();
  141. $this->showAuthor();
  142. if (!empty($this->notice->reply_to) || count($this->getProfileAddressees()) > 0) {
  143. $this->elementStart('div', array('class' => 'parents'));
  144. try {
  145. $this->showParent();
  146. } catch (NoParentNoticeException $e) {
  147. // no parent notice
  148. } catch (InvalidUrlException $e) {
  149. // parent had an invalid URL so we can't show it
  150. }
  151. if ($this->addressees) { $this->showAddressees(); }
  152. $this->elementEnd('div');
  153. }
  154. $this->elementEnd('section');
  155. }
  156. function showNoticeFooter()
  157. {
  158. $this->elementStart('footer');
  159. $this->showNoticeInfo();
  160. if ($this->options) { $this->showNoticeOptions(); }
  161. if ($this->attachments) { $this->showNoticeAttachments(); }
  162. $this->elementEnd('footer');
  163. }
  164. function showNoticeTitle()
  165. {
  166. if (Event::handle('StartShowNoticeTitle', array($this))) {
  167. $nameClass = $this->notice->getTitle(false) ? 'p-name ' : '';
  168. $this->element('a', array('href' => $this->notice->getUri(),
  169. 'class' => $nameClass . 'u-uid'),
  170. $this->notice->getTitle());
  171. Event::handle('EndShowNoticeTitle', array($this));
  172. }
  173. }
  174. function showNoticeInfo()
  175. {
  176. if (Event::handle('StartShowNoticeInfo', array($this))) {
  177. $this->showContextLink();
  178. $this->showNoticeLink();
  179. $this->showNoticeSource();
  180. $this->showNoticeLocation();
  181. $this->showPermalink();
  182. Event::handle('EndShowNoticeInfo', array($this));
  183. }
  184. }
  185. function showNoticeOptions()
  186. {
  187. if (Event::handle('StartShowNoticeOptions', array($this))) {
  188. $user = common_current_user();
  189. if ($user) {
  190. $this->out->elementStart('div', 'notice-options');
  191. if (Event::handle('StartShowNoticeOptionItems', array($this))) {
  192. $this->showReplyLink();
  193. $this->showDeleteLink();
  194. Event::handle('EndShowNoticeOptionItems', array($this));
  195. }
  196. $this->out->elementEnd('div');
  197. }
  198. Event::handle('EndShowNoticeOptions', array($this));
  199. }
  200. }
  201. /**
  202. * start a single notice.
  203. *
  204. * @return void
  205. */
  206. function showStart()
  207. {
  208. if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
  209. $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
  210. $class = 'h-entry notice';
  211. if ($this->notice->scope != 0 && $this->notice->scope != 1) {
  212. $class .= ' limited-scope';
  213. }
  214. try {
  215. $class .= ' notice-source-'.common_to_alphanumeric($this->notice->source);
  216. } catch (Exception $e) {
  217. // either source or what we filtered out was a zero-length string
  218. }
  219. $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
  220. $this->out->elementStart($this->item_tag, array('class' => $class,
  221. 'id' => "${id_prefix}notice-${id}"));
  222. Event::handle('EndOpenNoticeListItemElement', array($this));
  223. }
  224. }
  225. /**
  226. * show the author of a notice
  227. *
  228. * By default, this shows the avatar and (linked) nickname of the author.
  229. *
  230. * @return void
  231. */
  232. function showAuthor()
  233. {
  234. $attrs = array('href' => $this->profile->getUrl(),
  235. 'class' => 'h-card',
  236. 'title' => $this->profile->getHtmlTitle());
  237. if(empty($this->repeat)) { $attrs['class'] .= ' p-author'; }
  238. if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
  239. $this->out->elementStart('a', $attrs);
  240. $this->showAvatar($this->profile);
  241. $this->out->text($this->profile->getStreamName());
  242. $this->out->elementEnd('a');
  243. Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
  244. }
  245. }
  246. function showParent()
  247. {
  248. $this->out->element(
  249. 'a',
  250. array(
  251. 'href' => $this->notice->getParent()->getUrl(),
  252. 'class' => 'u-in-reply-to',
  253. 'rel' => 'in-reply-to'
  254. ),
  255. 'in reply to'
  256. );
  257. }
  258. function showAddressees()
  259. {
  260. $pa = $this->getProfileAddressees();
  261. if (!empty($pa)) {
  262. $this->out->elementStart('ul', 'addressees');
  263. $first = true;
  264. foreach ($pa as $addr) {
  265. $this->out->elementStart('li');
  266. $text = $addr['text'];
  267. unset($addr['text']);
  268. $this->out->element('a', $addr, $text);
  269. $this->out->elementEnd('li');
  270. }
  271. $this->out->elementEnd('ul', 'addressees');
  272. }
  273. }
  274. function getProfileAddressees()
  275. {
  276. if($this->pa) { return $this->pa; }
  277. $this->pa = array();
  278. $attentions = $this->getAttentionProfiles();
  279. foreach ($attentions as $attn) {
  280. if ($attn->isGroup()) {
  281. $class = 'group';
  282. $profileurl = common_local_url('groupbyid', array('id' => $attn->getGroup()->getID()));
  283. } else {
  284. $class = 'account';
  285. $profileurl = common_local_url('userbyid', array('id' => $attn->getID()));
  286. }
  287. $this->pa[] = array('href' => $profileurl,
  288. 'title' => $attn->getHtmlTitle(),
  289. 'class' => "addressee {$class} p-name u-url",
  290. 'text' => $attn->getStreamName());
  291. }
  292. return $this->pa;
  293. }
  294. function getAttentionProfiles()
  295. {
  296. return $this->notice->getAttentionProfiles();
  297. }
  298. /**
  299. * show the nickname of the author
  300. *
  301. * Links to the author's profile page
  302. *
  303. * @return void
  304. */
  305. function showNickname()
  306. {
  307. $this->out->raw('<span class="p-name">' .
  308. htmlspecialchars($this->profile->getNickname()) .
  309. '</span>');
  310. }
  311. /**
  312. * show the content of the notice
  313. *
  314. * Shows the content of the notice. This is pre-rendered for efficiency
  315. * at save time. Some very old notices might not be pre-rendered, so
  316. * they're rendered on the spot.
  317. *
  318. * @return void
  319. */
  320. function showContent()
  321. {
  322. // FIXME: URL, image, video, audio
  323. $nameClass = $this->notice->getTitle(false) ? '' : 'p-name ';
  324. $this->out->elementStart('article', array('class' => $nameClass . 'e-content'));
  325. if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
  326. if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
  327. $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
  328. } else {
  329. $this->out->raw($this->notice->getRendered());
  330. }
  331. Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
  332. }
  333. $this->out->elementEnd('article');
  334. }
  335. function showNoticeAttachments() {
  336. if (common_config('attachments', 'show_thumbs')) {
  337. $al = new InlineAttachmentList($this->notice, $this->out);
  338. $al->show();
  339. }
  340. }
  341. /**
  342. * show the link to the main page for the notice
  343. *
  344. * Displays a local link to the rendered notice, with "relative" time.
  345. *
  346. * @return void
  347. */
  348. function showNoticeLink()
  349. {
  350. $this->out->element('time', array('class' => 'dt-published',
  351. 'datetime' => common_date_iso8601($this->notice->created),
  352. 'title' => common_exact_date($this->notice->created)),
  353. common_date_string($this->notice->created));
  354. }
  355. /**
  356. * show the notice location
  357. *
  358. * shows the notice location in the correct language.
  359. *
  360. * If an URL is available, makes a link. Otherwise, just a span.
  361. *
  362. * @return void
  363. */
  364. function showNoticeLocation()
  365. {
  366. try {
  367. $location = Notice_location::locFromStored($this->notice);
  368. } catch (NoResultException $e) {
  369. return;
  370. } catch (ServerException $e) {
  371. return;
  372. }
  373. $name = $location->getName();
  374. $lat = $location->lat;
  375. $lon = $location->lon;
  376. $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
  377. if (empty($name)) {
  378. $latdms = $this->decimalDegreesToDMS(abs($lat));
  379. $londms = $this->decimalDegreesToDMS(abs($lon));
  380. // TRANS: Used in coordinates as abbreviation of north.
  381. $north = _('N');
  382. // TRANS: Used in coordinates as abbreviation of south.
  383. $south = _('S');
  384. // TRANS: Used in coordinates as abbreviation of east.
  385. $east = _('E');
  386. // TRANS: Used in coordinates as abbreviation of west.
  387. $west = _('W');
  388. $name = sprintf(
  389. // TRANS: Coordinates message.
  390. // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
  391. // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
  392. // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
  393. // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
  394. _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
  395. $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
  396. $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
  397. }
  398. $url = $location->getUrl();
  399. $this->out->text(' ');
  400. $this->out->elementStart('span', array('class' => 'location'));
  401. // TRANS: Followed by geo location.
  402. $this->out->text(_('at'));
  403. $this->out->text(' ');
  404. if (empty($url)) {
  405. $this->out->element('abbr', array('class' => 'geo',
  406. 'title' => $latlon),
  407. $name);
  408. } else {
  409. $xstr = new XMLStringer(false);
  410. $xstr->elementStart('a', array('href' => $url,
  411. 'rel' => 'external'));
  412. $xstr->element('abbr', array('class' => 'geo',
  413. 'title' => $latlon),
  414. $name);
  415. $xstr->elementEnd('a');
  416. $this->out->raw($xstr->getString());
  417. }
  418. $this->out->elementEnd('span');
  419. }
  420. /**
  421. * @param number $dec decimal degrees
  422. * @return array split into 'deg', 'min', and 'sec'
  423. */
  424. function decimalDegreesToDMS($dec)
  425. {
  426. $deg = intval($dec);
  427. $tempma = abs($dec) - abs($deg);
  428. $tempma = $tempma * 3600;
  429. $min = floor($tempma / 60);
  430. $sec = $tempma - ($min*60);
  431. return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
  432. }
  433. /**
  434. * Show the source of the notice
  435. *
  436. * Either the name (and link) of the API client that posted the notice,
  437. * or one of other other channels.
  438. *
  439. * @return void
  440. */
  441. function showNoticeSource()
  442. {
  443. $ns = $this->notice->getSource();
  444. if (!$ns instanceof Notice_source) {
  445. return false;
  446. }
  447. // TRANS: A possible notice source (web interface).
  448. $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
  449. $this->out->text(' ');
  450. $this->out->elementStart('span', 'source');
  451. // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
  452. // TRANS: Followed by notice source.
  453. $this->out->text(_('from'));
  454. $this->out->text(' ');
  455. $name = $source_name;
  456. $url = $ns->url;
  457. $title = null;
  458. if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
  459. $name = $source_name;
  460. $url = $ns->url;
  461. }
  462. Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
  463. // if $ns->name and $ns->url are populated we have
  464. // configured a source attr somewhere
  465. if (!empty($name) && !empty($url)) {
  466. $this->out->elementStart('span', 'device');
  467. $attrs = array(
  468. 'href' => $url,
  469. 'rel' => 'external'
  470. );
  471. if (!empty($title)) {
  472. $attrs['title'] = $title;
  473. }
  474. $this->out->element('a', $attrs, $name);
  475. $this->out->elementEnd('span');
  476. } else {
  477. $this->out->element('span', 'device', $name);
  478. }
  479. $this->out->elementEnd('span');
  480. }
  481. /**
  482. * show link to single-notice view for this notice item
  483. *
  484. * A permalink that goes to this specific object and nothing else
  485. *
  486. * @return void
  487. */
  488. function showPermalink()
  489. {
  490. $class = 'permalink u-url';
  491. if (!$this->notice->isLocal()) {
  492. $class .= ' external';
  493. }
  494. try {
  495. if($this->repeat) {
  496. $this->out->element('a',
  497. array('href' => $this->repeat->getUrl(),
  498. 'class' => 'u-url'),
  499. '');
  500. $class = str_replace('u-url', 'u-repost-of', $class);
  501. }
  502. } catch (InvalidUrlException $e) {
  503. // no permalink available
  504. }
  505. try {
  506. $this->out->element('a',
  507. array('href' => $this->notice->getUrl(true),
  508. 'class' => $class),
  509. // TRANS: Addition in notice list item for single-notice view.
  510. _('permalink'));
  511. } catch (InvalidUrlException $e) {
  512. // no permalink available
  513. }
  514. }
  515. /**
  516. * Show link to conversation view.
  517. */
  518. function showContextLink()
  519. {
  520. $this->out->element('a', array('rel' => 'bookmark',
  521. 'class' => 'timestamp',
  522. 'href' => Conversation::getUrlFromNotice($this->notice)),
  523. // TRANS: A link to the conversation view of a notice, on the local server.
  524. _('In conversation'));
  525. }
  526. /**
  527. * show a link to reply to the current notice
  528. *
  529. * Should either do the reply in the current notice form (if available), or
  530. * link out to the notice-posting form. A little flakey, doesn't always work.
  531. *
  532. * @return void
  533. */
  534. function showReplyLink()
  535. {
  536. if (common_logged_in()) {
  537. $this->out->text(' ');
  538. $reply_url = common_local_url('newnotice',
  539. array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
  540. $this->out->elementStart('a', array('href' => $reply_url,
  541. 'class' => 'notice_reply',
  542. // TRANS: Link title in notice list item to reply to a notice.
  543. 'title' => _('Reply to this notice.')));
  544. // TRANS: Link text in notice list item to reply to a notice.
  545. $this->out->text(_('Reply'));
  546. $this->out->text(' ');
  547. $this->out->element('span', 'notice_id', $this->notice->id);
  548. $this->out->elementEnd('a');
  549. }
  550. }
  551. /**
  552. * if the user is the author, let them delete the notice
  553. *
  554. * @return void
  555. */
  556. function showDeleteLink()
  557. {
  558. $user = common_current_user();
  559. $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
  560. if (!empty($user) &&
  561. ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
  562. $this->out->text(' ');
  563. $deleteurl = common_local_url('deletenotice',
  564. array('notice' => $todel->id));
  565. $this->out->element('a', array('href' => $deleteurl,
  566. 'class' => 'notice_delete popup',
  567. // TRANS: Link title in notice list item to delete a notice.
  568. 'title' => _('Delete this notice from the timeline.')),
  569. // TRANS: Link text in notice list item to delete a notice.
  570. _('Delete'));
  571. }
  572. }
  573. /**
  574. * finish the notice
  575. *
  576. * Close the last elements in the notice list item
  577. *
  578. * @return void
  579. */
  580. function showEnd()
  581. {
  582. if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
  583. $this->out->elementEnd('li');
  584. Event::handle('EndCloseNoticeListItemElement', array($this));
  585. }
  586. }
  587. /**
  588. * Get the notice in question
  589. *
  590. * For hooks, etc., this may be useful
  591. *
  592. * @return Notice The notice we're showing
  593. */
  594. function getNotice()
  595. {
  596. return $this->notice;
  597. }
  598. }