noticelistitem.php 22 KB

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