noticelistitem.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. $this->element('a', array('href' => $this->notice->getUrl(true),
  168. 'class' => 'notice-title'),
  169. $this->notice->getTitle());
  170. Event::handle('EndShowNoticeTitle', array($this));
  171. }
  172. }
  173. function showNoticeInfo()
  174. {
  175. if (Event::handle('StartShowNoticeInfo', array($this))) {
  176. $this->showNoticeLink();
  177. $this->showNoticeSource();
  178. $this->showNoticeLocation();
  179. $this->showPermalink();
  180. Event::handle('EndShowNoticeInfo', array($this));
  181. }
  182. }
  183. function showNoticeOptions()
  184. {
  185. if (Event::handle('StartShowNoticeOptions', array($this))) {
  186. $user = common_current_user();
  187. if ($user) {
  188. $this->out->elementStart('div', 'notice-options');
  189. if (Event::handle('StartShowNoticeOptionItems', array($this))) {
  190. $this->showReplyLink();
  191. $this->showDeleteLink();
  192. Event::handle('EndShowNoticeOptionItems', array($this));
  193. }
  194. $this->out->elementEnd('div');
  195. }
  196. Event::handle('EndShowNoticeOptions', array($this));
  197. }
  198. }
  199. /**
  200. * start a single notice.
  201. *
  202. * @return void
  203. */
  204. function showStart()
  205. {
  206. if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
  207. $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
  208. $class = 'h-entry notice';
  209. if ($this->notice->scope != 0 && $this->notice->scope != 1) {
  210. $class .= ' limited-scope';
  211. }
  212. if (!empty($this->notice->source)) {
  213. $class .= ' notice-source-'.$this->notice->source;
  214. }
  215. $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
  216. $this->out->elementStart($this->item_tag, array('class' => $class,
  217. 'id' => "${id_prefix}notice-${id}"));
  218. Event::handle('EndOpenNoticeListItemElement', array($this));
  219. }
  220. }
  221. /**
  222. * show the author of a notice
  223. *
  224. * By default, this shows the avatar and (linked) nickname of the author.
  225. *
  226. * @return void
  227. */
  228. function showAuthor()
  229. {
  230. $attrs = array('href' => $this->profile->profileurl,
  231. 'class' => 'h-card',
  232. 'title' => $this->profile->getNickname());
  233. if(empty($this->repeat)) { $attrs['class'] .= ' p-author'; }
  234. if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
  235. $this->out->elementStart('a', $attrs);
  236. $this->showAvatar($this->profile);
  237. $this->out->text($this->profile->getStreamName());
  238. $this->out->elementEnd('a');
  239. Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
  240. }
  241. }
  242. function showParent()
  243. {
  244. $this->out->element(
  245. 'a',
  246. array(
  247. 'href' => $this->notice->getParent()->getUrl(),
  248. 'class' => 'u-in-reply-to',
  249. 'rel' => 'in-reply-to'
  250. ),
  251. 'in reply to'
  252. );
  253. }
  254. function showAddressees()
  255. {
  256. $pa = $this->getProfileAddressees();
  257. if (!empty($pa)) {
  258. $this->out->elementStart('ul', 'addressees');
  259. $first = true;
  260. foreach ($pa as $addr) {
  261. $this->out->elementStart('li', 'h-card');
  262. $text = $addr['text'];
  263. unset($addr['text']);
  264. $this->out->element('a', $addr, $text);
  265. $this->out->elementEnd('li');
  266. }
  267. $this->out->elementEnd('ul', 'addressees');
  268. }
  269. }
  270. function getProfileAddressees()
  271. {
  272. if($this->pa) { return $this->pa; }
  273. $this->pa = array();
  274. $attentions = $this->getAttentionProfiles();
  275. foreach ($attentions as $attn) {
  276. $class = $attn->isGroup() ? 'group' : 'account';
  277. $this->pa[] = array('href' => $attn->profileurl,
  278. 'title' => $attn->getNickname(),
  279. 'class' => "addressee {$class}",
  280. 'text' => $attn->getStreamName());
  281. }
  282. return $this->pa;
  283. }
  284. function getAttentionProfiles()
  285. {
  286. return $this->notice->getAttentionProfiles();
  287. }
  288. /**
  289. * show the nickname of the author
  290. *
  291. * Links to the author's profile page
  292. *
  293. * @return void
  294. */
  295. function showNickname()
  296. {
  297. $this->out->raw('<span class="p-name">' .
  298. htmlspecialchars($this->profile->getNickname()) .
  299. '</span>');
  300. }
  301. /**
  302. * show the content of the notice
  303. *
  304. * Shows the content of the notice. This is pre-rendered for efficiency
  305. * at save time. Some very old notices might not be pre-rendered, so
  306. * they're rendered on the spot.
  307. *
  308. * @return void
  309. */
  310. function showContent()
  311. {
  312. // FIXME: URL, image, video, audio
  313. $this->out->elementStart('article', array('class' => 'e-content'));
  314. if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
  315. if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
  316. $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
  317. } else {
  318. $this->out->raw($this->notice->getRendered());
  319. }
  320. Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
  321. }
  322. $this->out->elementEnd('article');
  323. }
  324. function showNoticeAttachments() {
  325. if (common_config('attachments', 'show_thumbs')) {
  326. $al = new InlineAttachmentList($this->notice, $this->out);
  327. $al->show();
  328. }
  329. }
  330. /**
  331. * show the link to the main page for the notice
  332. *
  333. * Displays a local link to the rendered notice, with "relative" time.
  334. *
  335. * @return void
  336. */
  337. function showNoticeLink()
  338. {
  339. $this->out->elementStart('a', array('rel' => 'bookmark',
  340. 'class' => 'timestamp',
  341. 'href' => Conversation::getUrlFromNotice($this->notice)));
  342. $this->out->element('time', array('class' => 'dt-published',
  343. 'datetime' => common_date_iso8601($this->notice->created),
  344. 'title' => common_exact_date($this->notice->created)),
  345. common_date_string($this->notice->created));
  346. $this->out->elementEnd('a');
  347. }
  348. /**
  349. * show the notice location
  350. *
  351. * shows the notice location in the correct language.
  352. *
  353. * If an URL is available, makes a link. Otherwise, just a span.
  354. *
  355. * @return void
  356. */
  357. function showNoticeLocation()
  358. {
  359. return;
  360. try {
  361. $location = Notice_location::locFromStored($this->notice);
  362. } catch (NoResultException $e) {
  363. return;
  364. } catch (ServerException $e) {
  365. return;
  366. }
  367. $name = $location->getName();
  368. $lat = $location->lat;
  369. $lon = $location->lon;
  370. $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
  371. if (empty($name)) {
  372. $latdms = $this->decimalDegreesToDMS(abs($lat));
  373. $londms = $this->decimalDegreesToDMS(abs($lon));
  374. // TRANS: Used in coordinates as abbreviation of north.
  375. $north = _('N');
  376. // TRANS: Used in coordinates as abbreviation of south.
  377. $south = _('S');
  378. // TRANS: Used in coordinates as abbreviation of east.
  379. $east = _('E');
  380. // TRANS: Used in coordinates as abbreviation of west.
  381. $west = _('W');
  382. $name = sprintf(
  383. // TRANS: Coordinates message.
  384. // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
  385. // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
  386. // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
  387. // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
  388. _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
  389. $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
  390. $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
  391. }
  392. $url = $location->getUrl();
  393. $this->out->text(' ');
  394. $this->out->elementStart('span', array('class' => 'location'));
  395. // TRANS: Followed by geo location.
  396. $this->out->text(_('at'));
  397. $this->out->text(' ');
  398. if (empty($url)) {
  399. $this->out->element('abbr', array('class' => 'geo',
  400. 'title' => $latlon),
  401. $name);
  402. } else {
  403. $xstr = new XMLStringer(false);
  404. $xstr->elementStart('a', array('href' => $url,
  405. 'rel' => 'external'));
  406. $xstr->element('abbr', array('class' => 'geo',
  407. 'title' => $latlon),
  408. $name);
  409. $xstr->elementEnd('a');
  410. $this->out->raw($xstr->getString());
  411. }
  412. $this->out->elementEnd('span');
  413. }
  414. /**
  415. * @param number $dec decimal degrees
  416. * @return array split into 'deg', 'min', and 'sec'
  417. */
  418. function decimalDegreesToDMS($dec)
  419. {
  420. $deg = intval($dec);
  421. $tempma = abs($dec) - abs($deg);
  422. $tempma = $tempma * 3600;
  423. $min = floor($tempma / 60);
  424. $sec = $tempma - ($min*60);
  425. return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
  426. }
  427. /**
  428. * Show the source of the notice
  429. *
  430. * Either the name (and link) of the API client that posted the notice,
  431. * or one of other other channels.
  432. *
  433. * @return void
  434. */
  435. function showNoticeSource()
  436. {
  437. $ns = $this->notice->getSource();
  438. if (!$ns instanceof Notice_source) {
  439. return false;
  440. }
  441. // TRANS: A possible notice source (web interface).
  442. $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
  443. $this->out->text(' ');
  444. $this->out->elementStart('span', 'source');
  445. // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
  446. // TRANS: Followed by notice source.
  447. $this->out->text(_('from'));
  448. $this->out->text(' ');
  449. $name = $source_name;
  450. $url = $ns->url;
  451. $title = null;
  452. if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
  453. $name = $source_name;
  454. $url = $ns->url;
  455. }
  456. Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
  457. // if $ns->name and $ns->url are populated we have
  458. // configured a source attr somewhere
  459. if (!empty($name) && !empty($url)) {
  460. $this->out->elementStart('span', 'device');
  461. $attrs = array(
  462. 'href' => $url,
  463. 'rel' => 'external'
  464. );
  465. if (!empty($title)) {
  466. $attrs['title'] = $title;
  467. }
  468. $this->out->element('a', $attrs, $name);
  469. $this->out->elementEnd('span');
  470. } else {
  471. $this->out->element('span', 'device', $name);
  472. }
  473. $this->out->elementEnd('span');
  474. }
  475. /**
  476. * show link to single-notice view for this notice item
  477. *
  478. * A permalink that goes to this specific object and nothing else
  479. *
  480. * @return void
  481. */
  482. function showPermalink()
  483. {
  484. $class = 'permalink u-url';
  485. if (!$this->notice->isLocal()) {
  486. $class .= ' external';
  487. }
  488. try {
  489. if($this->repeat) {
  490. $this->out->element('a',
  491. array('href' => $this->repeat->getUrl(),
  492. 'class' => 'u-url'),
  493. '');
  494. $class = str_replace('u-url', 'u-repost-of', $class);
  495. }
  496. } catch (InvalidUrlException $e) {
  497. // no permalink available
  498. }
  499. try {
  500. $this->out->element('a',
  501. array('href' => $this->notice->getUrl(true),
  502. 'class' => $class),
  503. // TRANS: Addition in notice list item for single-notice view.
  504. _('permalink'));
  505. } catch (InvalidUrlException $e) {
  506. // no permalink available
  507. }
  508. }
  509. /**
  510. * show a link to reply to the current notice
  511. *
  512. * Should either do the reply in the current notice form (if available), or
  513. * link out to the notice-posting form. A little flakey, doesn't always work.
  514. *
  515. * @return void
  516. */
  517. function showReplyLink()
  518. {
  519. if (common_logged_in()) {
  520. $this->out->text(' ');
  521. $reply_url = common_local_url('newnotice',
  522. array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
  523. $this->out->elementStart('a', array('href' => $reply_url,
  524. 'class' => 'notice_reply',
  525. // TRANS: Link title in notice list item to reply to a notice.
  526. 'title' => _('Reply to this notice.')));
  527. // TRANS: Link text in notice list item to reply to a notice.
  528. $this->out->text(_('Reply'));
  529. $this->out->text(' ');
  530. $this->out->element('span', 'notice_id', $this->notice->id);
  531. $this->out->elementEnd('a');
  532. }
  533. }
  534. /**
  535. * if the user is the author, let them delete the notice
  536. *
  537. * @return void
  538. */
  539. function showDeleteLink()
  540. {
  541. $user = common_current_user();
  542. $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
  543. if (!empty($user) &&
  544. ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
  545. $this->out->text(' ');
  546. $deleteurl = common_local_url('deletenotice',
  547. array('notice' => $todel->id));
  548. $this->out->element('a', array('href' => $deleteurl,
  549. 'class' => 'notice_delete popup',
  550. // TRANS: Link title in notice list item to delete a notice.
  551. 'title' => _('Delete this notice from the timeline.')),
  552. // TRANS: Link text in notice list item to delete a notice.
  553. _('Delete'));
  554. }
  555. }
  556. /**
  557. * finish the notice
  558. *
  559. * Close the last elements in the notice list item
  560. *
  561. * @return void
  562. */
  563. function showEnd()
  564. {
  565. if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
  566. $this->out->elementEnd('li');
  567. Event::handle('EndCloseNoticeListItemElement', array($this));
  568. }
  569. }
  570. /**
  571. * Get the notice in question
  572. *
  573. * For hooks, etc., this may be useful
  574. *
  575. * @return Notice The notice we're showing
  576. */
  577. function getNotice()
  578. {
  579. return $this->notice;
  580. }
  581. }