]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/actions/map.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / Mapstraction / actions / map.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a map of user's notices
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  Mapstraction
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009-2011 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  * Show a map of notices
36  *
37  * @category Mapstraction
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @author   Craig Andrews <candrews@integralblue.com>
41  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
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 class MapAction extends Action
46 {
47     var $profile = null;
48     var $page    = null;
49     var $notices = null;
50
51     function prepare(array $args=array())
52     {
53         parent::prepare($args);
54
55         $nickname_arg = $this->arg('nickname');
56         $nickname     = Nickname::normalize($nickname_arg);
57
58         // Permanent redirect on non-canonical nickname
59
60         if ($nickname_arg != $nickname) {
61             $args = array('nickname' => $nickname);
62             if ($this->arg('page') && $this->arg('page') != 1) {
63                 $args['page'] = $this->arg['page'];
64             }
65             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
66         }
67
68         $this->user = User::getKV('nickname', $nickname);
69
70         if (!$this->user) {
71             // TRANS: Client error displayed when referring to a non-existing user.
72             $this->clientError(_m('No such user.'), 404);
73         }
74
75         $this->profile = $this->user->getProfile();
76
77         if (!$this->profile) {
78             // TRANS: Error message displayed when referring to a user without a profile.
79             $this->serverError(_m('User has no profile.'));
80         }
81
82         $page = $this->trimmed('page');
83
84         if (!empty($page) && Validate::number($page)) {
85             $this->page = $page+0;
86         } else {
87             $this->page = 1;
88         }
89
90         $this->notices = empty($this->tag)
91           ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1)
92             : $this->user->getTaggedNotices($this->tag, ($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null);
93
94         return true;
95     }
96
97     function handle(array $args=array())
98     {
99         parent::handle($args);
100         $this->showPage();
101     }
102
103     function showContent()
104     {
105         $this->element('div', array('id' => 'map_canvas',
106                                     'class' => 'gray smallmap',
107                                     'style' => "width: 100%; height: 400px"));
108     }
109
110     /**
111      * Hook for adding extra JavaScript
112      *
113      * @param Action $action Action object for the page
114      *
115      * @return boolean event handler return
116      */
117     function showScripts()
118     {
119         parent::showScripts();
120         $jsonArray = array();
121
122         while ($this->notice->fetch()) {
123             try {
124                 $notloc = Notice_location::locFromStored($this->notice);
125                 $jsonArray[] = $this->noticeAsJson($this->notice);
126             } catch (ServerException $e) {
127                 // no location data
128             }
129         }
130
131         $this->inlineScript('$(document).ready(function() { '.
132                             ' var _notices = ' . json_encode($jsonArray).'; ' .
133                             'showMapstraction($("#map_canvas"), _notices); });');
134
135         return true;
136     }
137
138     function noticeAsJson(Notice $notice)
139     {
140         // FIXME: this code should be abstracted to a neutral third
141         // party, like Notice::asJson(). I'm not sure of the ethics
142         // of refactoring from within a plugin, so I'm just abusing
143         // the ApiAction method. Don't do this unless you're me!
144
145         $act = new ApiAction('/dev/null');
146
147         $arr = $act->twitterStatusArray($notice, true);
148         $arr['url'] = $notice->getUrl(true);
149         $arr['html'] = $notice->rendered;
150         $arr['source'] = $arr['source'];
151
152         if (!empty($notice->reply_to)) {
153             $reply_to = Notice::getKV('id', $notice->reply_to);
154             if (!empty($reply_to)) {
155                 $arr['in_reply_to_status_url'] = $reply_to->getUrl(true);
156             }
157             $reply_to = null;
158         }
159
160         $profile = $notice->getProfile();
161         $arr['user']['profile_url'] = $profile->profileurl;
162
163         return $arr;
164     }
165 }