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