MapstractionPlugin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to provide map visualization of location data
  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 Action
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 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. * Plugin to provide map visualization of location data
  34. *
  35. * This plugin uses the Mapstraction JavaScript library to
  36. *
  37. * @category Plugin
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  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. * @seeAlso Location
  44. */
  45. class MapstractionPlugin extends Plugin
  46. {
  47. const PLUGIN_VERSION = '2.0.0';
  48. /** provider name, one of:
  49. 'cloudmade', 'microsoft', 'openlayers', 'yahoo' */
  50. public $provider = 'openlayers';
  51. /** provider API key (or 'appid'), if required ('yahoo' only) */
  52. public $apikey = null;
  53. /**
  54. * Hook for new URLs
  55. *
  56. * The way to register new actions from a plugin.
  57. *
  58. * @param Router $m reference to router
  59. *
  60. * @return boolean event handler return
  61. */
  62. function onRouterInitialized($m)
  63. {
  64. $m->connect(':nickname/all/map',
  65. ['action' => 'allmap'],
  66. ['nickname' => Nickname::DISPLAY_FMT]);
  67. $m->connect(':nickname/map',
  68. ['action' => 'usermap'],
  69. ['nickname' => Nickname::DISPLAY_FMT]);
  70. return true;
  71. }
  72. /**
  73. * Hook for adding extra JavaScript
  74. *
  75. * This makes sure our scripts get loaded for map-related pages
  76. *
  77. * @param Action $action Action object for the page
  78. *
  79. * @return boolean event handler return
  80. */
  81. function onEndShowScripts($action)
  82. {
  83. $actionName = $action->trimmed('action');
  84. if (!in_array($actionName,
  85. array('showstream', 'all', 'usermap', 'allmap'))) {
  86. return true;
  87. }
  88. switch ($this->provider)
  89. {
  90. case 'cloudmade':
  91. $action->script('http://tile.cloudmade.com/wml/0.2/web-maps-lite.js');
  92. break;
  93. case 'microsoft':
  94. $action->script((GNUsocial::isHTTPS()?'https':'http') + '://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6');
  95. break;
  96. case 'openlayers':
  97. // Use our included stripped & minified OpenLayers.
  98. $action->script($this->path('OpenLayers/OpenLayers.js'));
  99. break;
  100. case 'yahoo':
  101. $action->script(sprintf('http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=%s',
  102. urlencode($this->apikey)));
  103. break;
  104. case 'geocommons': // don't support this yet
  105. default:
  106. return true;
  107. }
  108. $action->script(sprintf('%s?(%s)',
  109. $this->path('js/mxn.js'),
  110. $this->provider));
  111. $action->script($this->path('usermap.js'));
  112. $action->inlineScript(sprintf('var _provider = "%s";', $this->provider));
  113. // usermap and allmap handle this themselves
  114. if (in_array($actionName,
  115. array('showstream', 'all'))) {
  116. $action->inlineScript('$(document).ready(function() { '.
  117. ' var user = null; '.
  118. (($actionName == 'showstream') ? ' user = scrapeUser(); ' : '') .
  119. ' var notices = scrapeNotices(user); ' .
  120. ' var canvas = $("#map_canvas")[0]; ' .
  121. ' if (typeof(canvas) != "undefined") { showMapstraction(canvas, notices); } '.
  122. '});');
  123. }
  124. return true;
  125. }
  126. function onEndShowSections(Action $action)
  127. {
  128. $actionName = $action->trimmed('action');
  129. // These are the ones that have maps on 'em
  130. if (!in_array($actionName,
  131. array('showstream', 'all'))) {
  132. return true;
  133. }
  134. $action->elementStart('div', array('id' => 'entity_map',
  135. 'class' => 'section'));
  136. // TRANS: Header for Map widget that displays a map with geodata for notices.
  137. $action->element('h2', null, _m('Map'));
  138. $action->element('div', array('id' => 'map_canvas',
  139. 'class' => 'gray smallmap',
  140. 'style' => "width: 100%; height: 240px"));
  141. $mapAct = ($actionName == 'showstream') ? 'usermap' : 'allmap';
  142. $mapUrl = common_local_url($mapAct,
  143. array('nickname' => $action->trimmed('nickname')));
  144. $action->element('a', array('href' => $mapUrl),
  145. // TRANS: Clickable item to allow opening the map in full size.
  146. _m('Full size'));
  147. $action->elementEnd('div');
  148. }
  149. public function onPluginVersion(array &$versions): bool
  150. {
  151. $versions[] = array('name' => 'Mapstraction',
  152. 'version' => self::PLUGIN_VERSION,
  153. 'author' => 'Evan Prodromou',
  154. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Mapstraction',
  155. 'rawdescription' =>
  156. // TRANS: Plugin description.
  157. _m('Show maps of users\' and friends\' notices '.
  158. 'with <a href="http://www.mapstraction.com/">Mapstraction</a>.'));
  159. return true;
  160. }
  161. }