map.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a map of user's notices
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: 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 Mapstraction
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009-2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Show a map of notices
  34. *
  35. * @category Mapstraction
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @author Craig Andrews <candrews@integralblue.com>
  39. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. */
  43. class MapAction extends Action
  44. {
  45. var $profile = null;
  46. var $page = null;
  47. var $notices = null;
  48. function prepare($args)
  49. {
  50. parent::prepare($args);
  51. $nickname_arg = $this->arg('nickname');
  52. $nickname = Nickname::normalize($nickname_arg);
  53. // Permanent redirect on non-canonical nickname
  54. if ($nickname_arg != $nickname) {
  55. $args = array('nickname' => $nickname);
  56. if ($this->arg('page') && $this->arg('page') != 1) {
  57. $args['page'] = $this->arg['page'];
  58. }
  59. common_redirect(common_local_url($this->trimmed('action'), $args), 301);
  60. }
  61. $this->user = User::getKV('nickname', $nickname);
  62. if (!$this->user) {
  63. // TRANS: Client error displayed when referring to a non-existing user.
  64. $this->clientError(_m('No such user.'), 404);
  65. }
  66. $this->profile = $this->user->getProfile();
  67. if (!$this->profile) {
  68. // TRANS: Error message displayed when referring to a user without a profile.
  69. $this->serverError(_m('User has no profile.'));
  70. }
  71. $page = $this->trimmed('page');
  72. if (!empty($page) && Validate::number($page)) {
  73. $this->page = $page+0;
  74. } else {
  75. $this->page = 1;
  76. }
  77. $this->notices = empty($this->tag)
  78. ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1)
  79. : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null);
  80. return true;
  81. }
  82. function handle($args)
  83. {
  84. parent::handle($args);
  85. $this->showPage();
  86. }
  87. function showContent()
  88. {
  89. $this->element('div', array('id' => 'map_canvas',
  90. 'class' => 'gray smallmap',
  91. 'style' => "width: 100%; height: 400px"));
  92. }
  93. /**
  94. * Hook for adding extra JavaScript
  95. *
  96. * @param Action $action Action object for the page
  97. *
  98. * @return boolean event handler return
  99. */
  100. function showScripts()
  101. {
  102. parent::showScripts();
  103. $jsonArray = array();
  104. while ($this->notice->fetch()) {
  105. if (!empty($this->notice->lat) && !empty($this->notice->lon)) {
  106. $jsonNotice = $this->noticeAsJson($this->notice);
  107. $jsonArray[] = $jsonNotice;
  108. }
  109. }
  110. $this->inlineScript('$(document).ready(function() { '.
  111. ' var _notices = ' . json_encode($jsonArray).'; ' .
  112. 'showMapstraction($("#map_canvas"), _notices); });');
  113. return true;
  114. }
  115. function noticeAsJson($notice)
  116. {
  117. // FIXME: this code should be abstracted to a neutral third
  118. // party, like Notice::asJson(). I'm not sure of the ethics
  119. // of refactoring from within a plugin, so I'm just abusing
  120. // the ApiAction method. Don't do this unless you're me!
  121. $act = new ApiAction('/dev/null');
  122. $arr = $act->twitterStatusArray($notice, true);
  123. $arr['url'] = $notice->getUrl(true);
  124. $arr['html'] = $notice->rendered;
  125. $arr['source'] = $arr['source'];
  126. if (!empty($notice->reply_to)) {
  127. $reply_to = Notice::getKV('id', $notice->reply_to);
  128. if (!empty($reply_to)) {
  129. $arr['in_reply_to_status_url'] = $reply_to->getUrl(true);
  130. }
  131. $reply_to = null;
  132. }
  133. $profile = $notice->getProfile();
  134. $arr['user']['profile_url'] = $profile->profileurl;
  135. return $arr;
  136. }
  137. }