noticelistitem.php 22 KB

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