]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/MapstractionPlugin.php
Merge branch 'inblob' of git@gitorious.org:~evan/statusnet/evans-mainline into inblob
[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
48 class MapstractionPlugin extends Plugin
49 {
50     const VERSION = STATUSNET_VERSION;
51
52     /** provider name, one of:
53      'cloudmade', 'google', 'microsoft', 'openlayers', 'yahoo' */
54     public $provider = 'openlayers';
55     /** provider API key (or 'appid'), if required ('google' and 'yahoo' only) */
56     public $apikey = null;
57
58     /**
59      * Hook for new URLs
60      *
61      * The way to register new actions from a plugin.
62      *
63      * @param Router $m reference to router
64      *
65      * @return boolean event handler return
66      */
67
68     function onRouterInitialized($m)
69     {
70         $m->connect(':nickname/all/map',
71                     array('action' => 'allmap'),
72                     array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
73         $m->connect(':nickname/map',
74                     array('action' => 'usermap'),
75                     array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
76         return true;
77     }
78
79     /**
80      * Hook for autoloading classes
81      *
82      * This makes sure our classes get autoloaded from our directory
83      *
84      * @param string $cls name of class being used
85      *
86      * @return boolean event handler return
87      */
88
89     function onAutoload($cls)
90     {
91         switch ($cls)
92         {
93         case 'AllmapAction':
94         case 'UsermapAction':
95         case 'MapAction':
96             include_once INSTALLDIR.'/plugins/Mapstraction/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
97             return false;
98         default:
99             return true;
100         }
101     }
102
103     /**
104      * Hook for adding extra JavaScript
105      *
106      * This makes sure our scripts get loaded for map-related pages
107      *
108      * @param Action $action Action object for the page
109      *
110      * @return boolean event handler return
111      */
112
113     function onEndShowScripts($action)
114     {
115         $actionName = $action->trimmed('action');
116
117         if (!in_array($actionName,
118                       array('showstream', 'all', 'usermap', 'allmap'))) {
119             return true;
120         }
121
122         switch ($this->provider)
123         {
124         case 'cloudmade':
125             $action->script('http://tile.cloudmade.com/wml/0.2/web-maps-lite.js');
126             break;
127         case 'google':
128             $action->script(sprintf('http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=%s',
129                                     $this->apikey));
130             break;
131         case 'microsoft':
132             $action->script('http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6');
133             break;
134         case 'openlayers':
135             // XXX: is this not nice...?
136             $action->script('http://openlayers.org/api/OpenLayers.js');
137             break;
138         case 'yahoo':
139             $action->script(sprintf('http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=%s',
140                                     $this->apikey));
141             break;
142         case 'geocommons': // don't support this yet
143         default:
144             return true;
145         }
146
147         $action->script(sprintf('%s?(%s)',
148                                 common_path('plugins/Mapstraction/js/mxn.js'),
149                                 $this->provider));
150
151         $action->script(common_path('plugins/Mapstraction/usermap.js'));
152
153         $action->inlineScript(sprintf('var _provider = "%s";', $this->provider));
154
155         // usermap and allmap handle this themselves
156
157         if (in_array($actionName,
158                      array('showstream', 'all'))) {
159             $action->inlineScript('$(document).ready(function() { '.
160                                   ' var user = null; '.
161                                   (($actionName == 'showstream') ? ' user = scrapeUser(); ' : '') .
162                                   ' var notices = scrapeNotices(user); ' .
163                                   ' showMapstraction($("#map_canvas"), notices); '.
164                                   '});');
165         }
166
167         return true;
168     }
169
170     function onEndShowSections($action)
171     {
172         $actionName = $action->trimmed('action');
173         // These are the ones that have maps on 'em
174         if (!in_array($actionName,
175                       array('showstream', 'all'))) {
176             return true;
177         }
178
179         $action->elementStart('div', array('id' => 'entity_map',
180                                          'class' => 'section'));
181
182         $action->element('h2', null, _m('Map'));
183
184         $action->element('div', array('id' => 'map_canvas',
185                                     'class' => 'gray smallmap',
186                                     'style' => "width: 100%; height: 240px"));
187
188         $mapAct = ($actionName == 'showstream') ? 'usermap' : 'allmap';
189         $mapUrl =  common_local_url($mapAct,
190                                     array('nickname' => $action->trimmed('nickname')));
191
192         $action->element('a', array('href' => $mapUrl),
193                          _m("Full size"));
194
195         $action->elementEnd('div');
196     }
197
198     function onPluginVersion(&$versions)
199     {
200         $versions[] = array('name' => 'Mapstraction',
201                             'version' => self::VERSION,
202                             'author' => 'Evan Prodromou',
203                             'homepage' => 'http://status.net/wiki/Plugin:Mapstraction',
204                             'rawdescription' =>
205                             _m('Show maps of users\' and friends\' notices '.
206                                'with <a href="http://www.mapstraction.com/">Mapstraction</a> '.
207                                'JavaScript library.'));
208         return true;
209     }
210 }