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