]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/MapstractionPlugin.php
Quick fix for Safari XHTML error... the map doesn't currently work with Safari, but...
[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     /** provider name, one of:
51      'cloudmade', 'google', 'microsoft', 'openlayers', 'yahoo' */
52     public $provider = 'openlayers';
53     /** provider API key (or 'appid'), if required ('google' and 'yahoo' only) */
54     public $apikey = null;
55
56     /**
57      * Hook for new URLs
58      *
59      * The way to register new actions from a plugin.
60      *
61      * @param Router $m reference to router
62      *
63      * @return boolean event handler return
64      */
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
87     function onAutoload($cls)
88     {
89         switch ($cls)
90         {
91         case 'AllmapAction':
92         case 'UsermapAction':
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
110     function onEndShowScripts($action)
111     {
112         $actionName = $action->trimmed('action');
113         // These are the ones that have maps on 'em
114         if (!in_array($actionName,
115                       array('showstream', 'all', 'allmap', 'usermap'))) {
116             return true;
117         }
118
119         switch ($this->provider)
120         {
121         case 'cloudmade':
122             $action->script('http://tile.cloudmade.com/wml/0.2/web-maps-lite.js');
123             break;
124         case 'google':
125             $action->script(sprintf('http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=%s',
126                                     $this->apikey));
127             break;
128         case 'microsoft':
129             $action->script('http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6');
130             break;
131         case 'openlayers':
132             // XXX: is this not nice...?
133             $action->script('http://openlayers.org/api/OpenLayers.js');
134             break;
135         case 'yahoo':
136             $action->script(sprintf('http://api.maps.yahoo.com/ajaxymap?v=3.8&appid=%s',
137                                     $this->apikey));
138             break;
139         case 'geocommons': // don't support this yet
140         default:
141             return true;
142         }
143
144         $action->script(sprintf('%s?(%s)',
145                                 common_path('plugins/Mapstraction/js/mxn.js'),
146                                 $this->provider));
147
148         $action->script(common_path('plugins/Mapstraction/usermap.js'));
149
150         $action->elementStart('script', array('type' => 'text/javascript'));
151         $action->raw(sprintf('var _provider = "%s";', $this->provider));
152         $action->elementEnd('script');
153
154         switch ($actionName) {
155         case 'usermap':
156         case 'showstream':
157             $notice = empty($action->tag)
158               ? $action->user->getNotices(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1)
159                 : $action->user->getTaggedNotices($action->tag, ($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null);
160             break;
161         case 'all':
162         case 'allmap':
163             $cur = common_current_user();
164             if (!empty($cur) && $cur->id == $action->user->id) {
165                 $notice = $action->user->noticeInbox(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
166             } else {
167                 $notice = $action->user->noticesWithFriends(($action->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
168             }
169             break;
170         }
171
172         $jsonArray = array();
173
174         while ($notice->fetch()) {
175             if (!empty($notice->lat) && !empty($notice->lon)) {
176                 $jsonNotice = $this->noticeAsJson($notice);
177                 $jsonArray[] = $jsonNotice;
178             }
179         }
180
181         $action->elementStart('script', array('type' => 'text/javascript'));
182         $action->raw('/*<![CDATA[*/'); // XHTML compat for Safari
183         $action->raw('var _notices = ' . json_encode($jsonArray));
184         $action->raw('/*]]>*/'); // XHTML compat for Safari
185         $action->elementEnd('script');
186
187         return true;
188     }
189
190     function onEndShowSections($action)
191     {
192         $actionName = $action->trimmed('action');
193         // These are the ones that have maps on 'em
194         if (!in_array($actionName,
195                       array('showstream', 'all'))) {
196             return true;
197         }
198
199         $action->elementStart('div', array('id' => 'entity_map',
200                                          'class' => 'section'));
201
202         $action->element('h2', null, _('Map'));
203
204         $action->element('div', array('id' => 'map_canvas',
205                                     'class' => 'gray smallmap',
206                                     'style' => "width: 100%; height: 240px"));
207
208         $mapAct = ($actionName == 'showstream') ? 'usermap' : 'allmap';
209         $mapUrl =  common_local_url($mapAct,
210                                     array('nickname' => $action->trimmed('nickname')));
211
212         $action->element('a', array('href' => $mapUrl),
213                          _("Full size"));
214
215         $action->elementEnd('div');
216     }
217
218     function noticeAsJson($notice)
219     {
220         // FIXME: this code should be abstracted to a neutral third
221         // party, like Notice::asJson(). I'm not sure of the ethics
222         // of refactoring from within a plugin, so I'm just abusing
223         // the ApiAction method. Don't do this unless you're me!
224
225         require_once(INSTALLDIR.'/lib/api.php');
226
227         $act = new ApiAction('/dev/null');
228
229         $arr = $act->twitterStatusArray($notice, true);
230         $arr['url'] = $notice->bestUrl();
231         $arr['html'] = $notice->rendered;
232         $arr['source'] = $arr['source'];
233
234         if (!empty($notice->reply_to)) {
235             $reply_to = Notice::staticGet('id', $notice->reply_to);
236             if (!empty($reply_to)) {
237                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
238             }
239             $reply_to = null;
240         }
241
242         $profile = $notice->getProfile();
243         $arr['user']['profile_url'] = $profile->profileurl;
244
245         return $arr;
246     }
247 }