PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/noticelistitem.php

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