]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubMirror/actions/editmirror.php
Merge remote-tracking branch 'statusnet/master'
[quix0rs-gnu-social.git] / plugins / SubMirror / actions / editmirror.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * PHP version 5
20  *
21  * @category  Action
22  * @package   StatusNet
23  * @author    Brion Vibber <brion@status.net>
24  * @copyright 2010 StatusNet, Inc.
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
26  * @link      http://status.net/
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 /**
34  * Takes parameters:
35  *
36  *    - feed: a profile ID
37  *    - token: session token to prevent CSRF attacks
38  *    - ajax: boolean; whether to return Ajax or full-browser results
39  *
40  * Only works if the current user is logged in.
41  *
42  * @category  Action
43  * @package   StatusNet
44  * @copyright 2010 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46  * @link      http://status.net/
47  */
48 class EditMirrorAction extends BaseMirrorAction
49 {
50     /**
51      * Check pre-requisites and instantiate attributes
52      *
53      * @param Array $args array of arguments (URL, GET, POST)
54      *
55      * @return boolean success flag
56      */
57     function prepare($args)
58     {
59         parent::prepare($args);
60
61         $this->profile = $this->validateProfile($this->trimmed('profile'));
62
63         $this->mirror = SubMirror::pkeyGet(array('subscriber' => $this->user->id,
64                                                  'subscribed' => $this->profile->id));
65
66         if (!$this->mirror) {
67             // TRANS: Client error displayed when trying to edit an object that is not a feed mirror.
68             $this->clientError(_m('Requested invalid profile to edit.'));
69         }
70
71         $this->style = $this->validateStyle($this->trimmed('style'));
72
73         // DO NOT change to $this->boolean(), it will be wrong.
74         // We're checking for the presence of the setting, not its value.
75         $this->delete = (bool)$this->arg('delete');
76
77         return true;
78     }
79
80     protected function validateStyle($style)
81     {
82         $allowed = array('repeat', 'copy');
83         if (in_array($style, $allowed)) {
84             return $style;
85         } else {
86             // TRANS: Client error displayed when providing invalid input when editing a mirror.
87             $this->clientError(_m('Bad form data.'));
88         }
89     }
90
91     function saveMirror()
92     {
93         $mirror = SubMirror::getMirror($this->user, $this->profile);
94         if (!$mirror) {
95             // TRANS: Client error thrown when a mirror request is made and no result is retrieved.
96             $this->clientError(_m('The mirror request failed, because no result was retrieved.'));
97         }
98
99         if ($this->delete) {
100             $mirror->delete();
101             $oprofile = Ostatus_profile::staticGet('profile_id', $this->profile->id);
102             if ($oprofile) {
103                 $oprofile->garbageCollect();
104             }
105         } else if ($this->style != $mirror->style) {
106             $orig = clone($mirror);
107             $mirror->style = $this->style;
108             $mirror->modified = common_sql_now();
109             $mirror->update($orig);
110         }
111     }
112 }