noticetree.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Max Shinn <trombonechamp@gmail.com>
  25. * @copyright 2011 Free Software Foundation, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  27. */
  28. //functions to sort replies
  29. class NoticeTree extends NoticeList
  30. {
  31. function show()
  32. {
  33. $this->out->elementStart('div', array('id' =>'notices_primary'));
  34. // TRANS: Header on conversation page. Hidden by default (h2).
  35. $this->out->element('h2', null, _('Notices'));
  36. $this->out->elementStart('ol', array('class' => 'notices xoxo'));
  37. $cnt = 0;
  38. while ($this->notice->fetch() && $cnt <= NOTICES_PER_PAGE) {
  39. if (!empty($this->notice->reply_to))
  40. continue;
  41. $cnt++;
  42. if ($cnt > NOTICES_PER_PAGE) {
  43. break;
  44. }
  45. try {
  46. $this->showNoticePlus($this->newListItem($this->notice));
  47. } catch (Exception $e) {
  48. // we log exceptions and continue
  49. print "ERROR!" . $e->getMessage();
  50. common_log(LOG_ERR, $e->getMessage());
  51. continue;
  52. }
  53. }
  54. $this->out->elementEnd('ol');
  55. $this->out->elementEnd('div');
  56. return $cnt;
  57. }
  58. function _cmpDate($a, $b) { return strcmp($a->notice->modified, $b->notice->modified); }
  59. function _cmpFav($a, $b) { return (int)$a->faves < (int)$b->faves; }
  60. function showNoticePlus($item)
  61. {
  62. $replies = new Notice();
  63. $replies->reply_to = $item->notice->id;
  64. // We take responsibility for doing the li
  65. $this->out->elementStart('li', array('class' => 'h-entry notice',
  66. 'id' => 'notice-' . $item->notice->id));
  67. $item->show();
  68. if ($replies->find()) {
  69. $this->out->elementStart('ol', array('class' => 'notices'));
  70. $replieslist = array();
  71. while ($replies->fetch()) {
  72. $replieslist[] = $this->newListItem(clone($replies));
  73. }
  74. //Sorting based on url argument
  75. if($_GET['sort'] == 'faves')
  76. usort($replieslist, array($this, '_cmpFav'));
  77. else
  78. usort($replieslist, array($this, '_cmpDate'));
  79. foreach($replieslist as $reply) {
  80. $this->showNoticePlus($reply);
  81. }
  82. $this->out->elementEnd('ol');
  83. }
  84. $this->out->elementEnd('li');
  85. }
  86. function newListItem($notice)
  87. {
  88. return new NoticeTreeItem($notice, $this->out);
  89. }
  90. }
  91. class NoticeTreeItem extends NoticeListItem
  92. {
  93. function __construct($notice, $out=null)
  94. {
  95. parent::__construct($notice, $out);
  96. //TODO: Rewrite this
  97. //Showing number of favorites
  98. $fave = new Fave();
  99. $fave->notice_id = $this->notice->id;
  100. $cnt = 0;
  101. if ($fave->find()) {
  102. while ($fave->fetch())
  103. $cnt++;
  104. }
  105. $this->faves = $cnt;
  106. }
  107. function showStart()
  108. {
  109. return;
  110. }
  111. function showEnd()
  112. {
  113. if ($this->faves > 0) {
  114. $this->out->text(_m("Favorited by $this->faves user"));
  115. if ($this->faves > 1) $this->out->text("s"); //there has to be a better way to do this...
  116. }
  117. //Show response form
  118. $this->out->elementStart('div', array('id' => 'form' . $this->notice->id, 'class' => 'replyform'));
  119. $noticeform = new ReplyForm($this->out, null, null, null, $this->notice->id);
  120. $noticeform->show();
  121. $this->out->elementEnd('div');
  122. return;
  123. }
  124. //Just changing the link...
  125. function showReplyLink()
  126. {
  127. if (common_logged_in()) {
  128. $this->out->text(' ');
  129. //Why doesn't common_local_url work here?
  130. $reply_url = '/notice/respond?replyto=' . $this->profile->nickname . '&inreplyto=' . $this->notice->id;
  131. $this->out->elementStart('a', array('href' => $reply_url,
  132. 'class' => 'notice_reply',
  133. 'title' => _('Reply to this notice')));
  134. $this->out->text(_('Reply'));
  135. $this->out->text(' ');
  136. $this->out->element('span', 'notice_id', $this->notice->id);
  137. $this->out->elementEnd('a');
  138. }
  139. }
  140. }
  141. class ReplyForm extends NoticeForm
  142. {
  143. //Changing the text. We have to either repeat ids or get hacky. I vote repeat ids.
  144. function formData()
  145. {
  146. if (Event::handle('StartShowNoticeFormData', array($this))) {
  147. // XXX: vary by defined max size
  148. $this->out->element('textarea', array('id' => 'notice_data-text',
  149. 'cols' => 35,
  150. 'rows' => 4,
  151. 'name' => 'status_textarea'),
  152. ($this->content) ? $this->content : '');
  153. $contentLimit = Notice::maxContent();
  154. if ($contentLimit > 0) {
  155. $this->out->elementStart('dl', 'form_note');
  156. $this->out->element('dt', null, _('Available characters'));
  157. $this->out->element('dd', array('id' => 'notice_text-count'),
  158. $contentLimit);
  159. $this->out->elementEnd('dl');
  160. }
  161. if (common_config('attachments', 'uploads')) {
  162. $this->out->element('label', array('for' => 'notice_data-attach'),_('Attach'));
  163. $this->out->element('input', array('id' => 'notice_data-attach',
  164. 'type' => 'file',
  165. 'name' => 'attach',
  166. 'title' => _('Attach a file')));
  167. $this->out->hidden('MAX_FILE_SIZE', common_config('attachments', 'file_quota'));
  168. }
  169. if ($this->action) {
  170. $this->out->hidden('notice_return-to', $this->action, 'returnto');
  171. }
  172. $this->out->hidden('notice_in-reply-to', $this->inreplyto, 'inreplyto');
  173. if ($this->profile->shareLocation()) {
  174. $this->out->hidden('notice_data-lat', empty($this->lat) ? (empty($this->profile->lat) ? null : $this->profile->lat) : $this->lat, 'lat');
  175. $this->out->hidden('notice_data-lon', empty($this->lon) ? (empty($this->profile->lon) ? null : $this->profile->lon) : $this->lon, 'lon');
  176. $this->out->hidden('notice_data-location_id', empty($this->location_id) ? (empty($this->profile->location_id) ? null : $this->profile->location_id) : $this->location_id, 'location_id');
  177. $this->out->hidden('notice_data-location_ns', empty($this->location_ns) ? (empty($this->profile->location_ns) ? null : $this->profile->location_ns) : $this->location_ns, 'location_ns');
  178. $this->out->elementStart('div', array('id' => 'notice_data-geo_wrap',
  179. 'title' => common_local_url('geocode')));
  180. $this->out->checkbox('notice_data-geo', _('Share my location'), true);
  181. $this->out->elementEnd('div');
  182. $this->out->inlineScript(' var NoticeDataGeo_text = {'.
  183. 'ShareDisable: ' .json_encode(_('Do not share my location')).','.
  184. 'ErrorTimeout: ' .json_encode(_('Sorry, retrieving your geo location is taking longer than expected, please try again later')).
  185. '}');
  186. }
  187. Event::handle('EndShowNoticeFormData', array($this));
  188. }
  189. }
  190. }