MapstractionPlugin.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 VERSION = GNUSOCIAL_VERSION;
  48. /** provider name, one of:
  49. 'cloudmade', 'google', 'microsoft', 'openlayers', 'yahoo' */
  50. public $provider = 'openlayers';
  51. /** provider API key (or 'appid'), if required ('google' and '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. array('action' => 'allmap'),
  66. array('nickname' => Nickname::DISPLAY_FMT));
  67. $m->connect(':nickname/map',
  68. array('action' => 'usermap'),
  69. array('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 'google':
  94. $action->script(sprintf('http://maps.google.com/maps?file=api&v=2&sensor=false&key=%s',
  95. urlencode($this->apikey)));
  96. break;
  97. case 'microsoft':
  98. $action->script((StatusNet::isHTTPS()?'https':'http') + '://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6');
  99. break;
  100. case 'openlayers':
  101. // Use our included stripped & minified OpenLayers.
  102. $action->script($this->path('OpenLayers/OpenLayers.js'));
  103. break;
  104. case 'yahoo':
  105. $action->script(sprintf('http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=%s',
  106. urlencode($this->apikey)));
  107. break;
  108. case 'geocommons': // don't support this yet
  109. default:
  110. return true;
  111. }
  112. $action->script(sprintf('%s?(%s)',
  113. $this->path('js/mxn.js'),
  114. $this->provider));
  115. $action->script($this->path('usermap.js'));
  116. $action->inlineScript(sprintf('var _provider = "%s";', $this->provider));
  117. // usermap and allmap handle this themselves
  118. if (in_array($actionName,
  119. array('showstream', 'all'))) {
  120. $action->inlineScript('$(document).ready(function() { '.
  121. ' var user = null; '.
  122. (($actionName == 'showstream') ? ' user = scrapeUser(); ' : '') .
  123. ' var notices = scrapeNotices(user); ' .
  124. ' var canvas = $("#map_canvas")[0]; ' .
  125. ' if (typeof(canvas) != "undefined") { showMapstraction(canvas, notices); } '.
  126. '});');
  127. }
  128. return true;
  129. }
  130. function onEndShowSections(Action $action)
  131. {
  132. $actionName = $action->trimmed('action');
  133. // These are the ones that have maps on 'em
  134. if (!in_array($actionName,
  135. array('showstream', 'all'))) {
  136. return true;
  137. }
  138. $action->elementStart('div', array('id' => 'entity_map',
  139. 'class' => 'section'));
  140. // TRANS: Header for Map widget that displays a map with geodata for notices.
  141. $action->element('h2', null, _m('Map'));
  142. $action->element('div', array('id' => 'map_canvas',
  143. 'class' => 'gray smallmap',
  144. 'style' => "width: 100%; height: 240px"));
  145. $mapAct = ($actionName == 'showstream') ? 'usermap' : 'allmap';
  146. $mapUrl = common_local_url($mapAct,
  147. array('nickname' => $action->trimmed('nickname')));
  148. $action->element('a', array('href' => $mapUrl),
  149. // TRANS: Clickable item to allow opening the map in full size.
  150. _m('Full size'));
  151. $action->elementEnd('div');
  152. }
  153. function onPluginVersion(&$versions)
  154. {
  155. $versions[] = array('name' => 'Mapstraction',
  156. 'version' => self::VERSION,
  157. 'author' => 'Evan Prodromou',
  158. 'homepage' => 'http://status.net/wiki/Plugin:Mapstraction',
  159. 'rawdescription' =>
  160. // TRANS: Plugin description.
  161. _m('Show maps of users\' and friends\' notices '.
  162. 'with <a href="http://www.mapstraction.com/">Mapstraction</a>.'));
  163. return true;
  164. }
  165. }