]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/favor.php
a distributed -> the distributed
[quix0rs-gnu-social.git] / actions / favor.php
1 <?php
2
3 /**
4  * Favor action.
5  *
6  * PHP version 5
7  *
8  * @category Action
9  * @package  StatusNet
10  * @author   Evan Prodromou <evan@controlyourself.ca>
11  * @author   Robin Millette <millette@controlyourself.ca>
12  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
13  * @link     http://laconi.ca/
14  *
15  * StatusNet - the distributed open-source microblogging tool
16  * Copyright (C) 2008, 2009, StatusNet, Inc.
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Affero General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU Affero General Public License for more details.
27  *
28  * You should have received a copy of the GNU Affero General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32 if (!defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR.'/lib/mail.php';
37 require_once INSTALLDIR.'/lib/disfavorform.php';
38
39 /**
40  * Favor class.
41  *
42  * @category Action
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @author   Robin Millette <millette@controlyourself.ca>
46  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
47  * @link     http://laconi.ca/
48  */
49 class FavorAction extends Action
50 {
51     /**
52      * Class handler.
53      *
54      * @param array $args query arguments
55      *
56      * @return void
57      */
58     function handle($args)
59     {
60         parent::handle($args);
61         if (!common_logged_in()) {
62             $this->clientError(_('Not logged in.'));
63             return;
64         }
65         $user = common_current_user();
66         if ($_SERVER['REQUEST_METHOD'] != 'POST') {
67             common_redirect(common_local_url('showfavorites',
68                 array('nickname' => $user->nickname)));
69             return;
70         }
71         $id     = $this->trimmed('notice');
72         $notice = Notice::staticGet($id);
73         $token  = $this->trimmed('token-'.$notice->id);
74         if (!$token || $token != common_session_token()) {
75             $this->clientError(_("There was a problem with your session token. Try again, please."));
76             return;
77         }
78         if ($user->hasFave($notice)) {
79             $this->clientError(_('This notice is already a favorite!'));
80             return;
81         }
82         $fave = Fave::addNew($user, $notice);
83         if (!$fave) {
84             $this->serverError(_('Could not create favorite.'));
85             return;
86         }
87         $this->notify($notice, $user);
88         $user->blowFavesCache();
89         if ($this->boolean('ajax')) {
90             $this->startHTML('text/xml;charset=utf-8');
91             $this->elementStart('head');
92             $this->element('title', null, _('Disfavor favorite'));
93             $this->elementEnd('head');
94             $this->elementStart('body');
95             $disfavor = new DisFavorForm($this, $notice);
96             $disfavor->show();
97             $this->elementEnd('body');
98             $this->elementEnd('html');
99         } else {
100             common_redirect(common_local_url('showfavorites',
101                                              array('nickname' => $user->nickname)),
102                             303);
103         }
104     }
105
106     /**
107      * Notifies a user when his notice is favorited.
108      *
109      * @param class $notice favorited notice
110      * @param class $user   user declaring a favorite
111      *
112      * @return void
113      */
114     function notify($notice, $user)
115     {
116         $other = User::staticGet('id', $notice->profile_id);
117         if ($other && $other->id != $user->id) {
118             if ($other->email && $other->emailnotifyfav) {
119                 mail_notify_fave($other, $user, $notice);
120             }
121             // XXX: notify by IM
122             // XXX: notify by SMS
123         }
124     }
125 }
126