]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Mapstraction/usermap.php
Merge branch '0.9.x' into mapstraction
[quix0rs-gnu-social.git] / plugins / Mapstraction / usermap.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 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 user's notices
36  *
37  * @category Mapstraction
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class UsermapAction extends OwnerDesignAction
45 {
46     var $profile = null;
47     var $page    = null;
48     var $notices = null;
49
50     public $plugin  = null;
51
52     function prepare($args)
53     {
54         parent::prepare($args);
55
56         $nickname_arg = $this->arg('nickname');
57         $nickname     = common_canonical_nickname($nickname_arg);
58
59         // Permanent redirect on non-canonical nickname
60
61         if ($nickname_arg != $nickname) {
62             $args = array('nickname' => $nickname);
63             if ($this->arg('page') && $this->arg('page') != 1) {
64                 $args['page'] = $this->arg['page'];
65             }
66             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
67             return false;
68         }
69
70         $this->user = User::staticGet('nickname', $nickname);
71
72         if (!$this->user) {
73             $this->clientError(_('No such user.'), 404);
74             return false;
75         }
76
77         $this->profile = $this->user->getProfile();
78
79         if (!$this->profile) {
80             $this->serverError(_('User has no profile.'));
81             return false;
82         }
83
84         $page = $this->trimmed('page');
85
86         if (!empty($page) && Validate::number($page)) {
87             $this->page = $page+0;
88         } else {
89             $this->page = 1;
90         }
91
92         $this->notices = $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
93
94         return true;
95     }
96
97     function title()
98     {
99         if (!empty($this->profile->fullname)) {
100             $base = $this->profile->fullname . ' (' . $this->user->nickname . ') ';
101         } else {
102             $base = $this->user->nickname;
103         }
104
105         if ($this->page == 1) {
106             return $base;
107         } else {
108             return sprintf(_("%s map, page %d"),
109                            $base,
110                            $this->page);
111         }
112     }
113
114     function handle($args)
115     {
116         parent::handle($args);
117         $this->showPage();
118     }
119
120     function showContent()
121     {
122         $this->element('div', array('id' => 'map_canvas',
123                                     'class' => 'gray smallmap',
124                                     'style' => "width: 100%; height: 400px"));
125     }
126
127     function showScripts()
128     {
129         parent::showScripts();
130
131         $this->script(common_path('plugins/Mapstraction/usermap.js'));
132
133         $jsonArray = array();
134
135         while ($this->notices->fetch()) {
136             if (!empty($this->notices->lat) && !empty($this->notices->lon)) {
137                 $jsonNotice = $this->noticeAsJson($this->notices);
138                 $jsonArray[] = $jsonNotice;
139             }
140         }
141
142         $this->elementStart('script', array('type' => 'text/javascript'));
143         $this->raw('var _notices = ' . json_encode($jsonArray));
144         $this->elementEnd('script');
145     }
146
147     function noticeAsJson($notice)
148     {
149         // FIXME: this code should be abstracted to a neutral third
150         // party, like Notice::asJson(). I'm not sure of the ethics
151         // of refactoring from within a plugin, so I'm just abusing
152         // the ApiAction method. Don't do this unless you're me!
153
154         require_once(INSTALLDIR.'/lib/api.php');
155
156         $act = new ApiAction('/dev/null');
157
158         $arr = $act->twitterStatusArray($notice, true);
159         $arr['url'] = $notice->bestUrl();
160         $arr['html'] = htmlspecialchars($notice->rendered);
161         $arr['source'] = htmlspecialchars($arr['source']);
162
163         if (!empty($notice->reply_to)) {
164             $reply_to = Notice::staticGet('id', $notice->reply_to);
165             if (!empty($reply_to)) {
166                 $arr['in_reply_to_status_url'] = $reply_to->bestUrl();
167             }
168             $reply_to = null;
169         }
170
171         $profile = $notice->getProfile();
172         $arr['user']['profile_url'] = $profile->profileurl;
173
174         return $arr;
175     }
176
177 }