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