]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editpeopletag.php
Merge ActivitySpam plugin
[quix0rs-gnu-social.git] / actions / editpeopletag.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Edit an existing group
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  Group
23  * @package   StatusNet
24  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25  * @link      http://status.net/
26  */
27
28 if (!defined('STATUSNET') && !defined('LACONICA')) {
29     exit(1);
30 }
31
32 /**
33  * Add a new group
34  *
35  * This is the form for adding a new group
36  *
37  * @category Group
38  * @package  StatusNet
39  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40  * @link     http://status.net/
41  */
42
43 class EditpeopletagAction extends Action
44 {
45     var $msg, $confirm, $confirm_args=array();
46
47     function title()
48     {
49         if ($_SERVER['REQUEST_METHOD'] == 'POST' && $this->boolean('delete')) {
50             // TRANS: Title for edit list page after deleting a tag.
51             // TRANS: %s is a list.
52             return sprintf(_('Delete %s list'), $this->peopletag->tag);
53         }
54         // TRANS: Title for edit list page.
55         // TRANS: %s is a list.
56         return sprintf(_('Edit list %s'), $this->peopletag->tag);
57     }
58
59     /**
60      * Prepare to run
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         if (!common_logged_in()) {
68             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
69             $this->clientError(_('Not logged in.'));
70             return false;
71         }
72
73         $id = $this->arg('id');
74         if (common_config('singleuser', 'enabled')) {
75             $tagger_arg = User::singleUserNickname();
76         } else {
77             $tagger_arg = $this->arg('tagger');
78         }
79         $tag_arg = $this->arg('tag');
80
81         $tagger = common_canonical_nickname($tagger_arg);
82         $tag = common_canonical_tag($tag_arg);
83
84         $current = common_current_user();
85
86         // Permanent redirect on non-canonical tag
87
88         if ($tagger_arg != $tagger || $tag_arg != $tag) {
89             $args = array('tagger' => $tagger, 'tag' => $tag);
90             common_redirect(common_local_url('editpeopletag', $args), 301);
91             return false;
92         }
93
94         $user = null;
95         if ($id) {
96             $this->peopletag = Profile_list::staticGet('id', $id);
97             if (!empty($this->peopletag)) {
98                 $user = User::staticGet('id', $this->peopletag->tagger);
99             }
100         } else {
101             if (!$tagger) {
102                 // TRANS: Error message displayed when trying to perform an action that requires a tagging user or ID.
103                 $this->clientError(_('No tagger or ID.'), 404);
104                 return false;
105             }
106
107             $user = User::staticGet('nickname', $tagger);
108             $this->peopletag = Profile_list::pkeyGet(array('tagger' => $user->id, 'tag' => $tag));
109         }
110
111         if (!$this->peopletag) {
112             // TRANS: Client error displayed when referring to a non-existing list.
113             $this->clientError(_('No such list.'), 404);
114             return false;
115         }
116
117         if (!$user) {
118             // This should not be happening
119             // TRANS: Client error displayed when referring to non-local user.
120             $this->clientError(_('Not a local user.'), 404);
121             return false;
122         }
123
124         if ($current->id != $user->id) {
125             // TRANS: Client error displayed when reting to edit a tag that was not self-created.
126             $this->clientError(_('You must be the creator of the tag to edit it.'), 404);
127             return false;
128         }
129
130         $this->tagger = $user->getProfile();
131
132         return true;
133     }
134
135     /**
136      * Handle the request
137      *
138      * On GET, show the form. On POST, try to save the group.
139      *
140      * @param array $args unused
141      *
142      * @return void
143      */
144     function handle($args)
145     {
146         parent::handle($args);
147         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
148             $this->trySave();
149         } else {
150             $this->showForm();
151         }
152     }
153
154     function showConfirm($msg=null, $fwd=null)
155     {
156         $this->confirm = $msg;
157         $this->confirm_args = $fwd;
158         $this->showPage();
159     }
160
161     function showConfirmForm()
162     {
163         $this->elementStart('form', array('id' => 'form_peopletag_edit_confirm',
164                                           'class' => 'form_settings',
165                                           'method' => 'post',
166                                           'action' => common_local_url('editpeopletag',
167                                               array('tagger' => $this->tagger->nickname,
168                                                     'tag' => $this->peopletag->tag))));
169         $this->elementStart('fieldset');
170         $this->hidden('token', common_session_token());
171         $this->hidden('id', $this->arg('id'));
172
173         foreach ($this->confirm_args as $key => $val) {
174             $this->hidden($key, $val);
175         }
176
177         $this->submit('form_action-no',
178                       _m('BUTTON','No'),
179                       'submit form_action-primary',
180                       'cancel');
181         $this->submit('form_action-yes',
182                       _m('BUTTON','Yes'),
183                       'submit form_action-secondary',
184                       'confirm');
185         $this->elementEnd('fieldset');
186         $this->elementEnd('form');
187     }
188
189     function showForm($msg=null)
190     {
191         $this->msg = $msg;
192         $this->showPage();
193     }
194
195     function showObjectNav()
196     {
197         $nav = new PeopletagGroupNav($this, $this->peopletag);
198         $nav->show();
199     }
200
201     function showContent()
202     {
203         if ($this->confirm) {
204             $this->showConfirmForm();
205             return;
206         }
207
208         $form = new PeopletagEditForm($this, $this->peopletag);
209         $form->show();
210
211         $form->showProfileList();
212     }
213
214     function showPageNotice()
215     {
216         if ($this->msg) {
217             $this->element('p', 'error', $this->msg);
218         } else if ($this->confirm) {
219             $this->element('p', 'instructions', $this->confirm);
220         } else {
221             $this->element('p', 'instructions',
222                            // TRANS: Form instruction for edit list form.
223                            _('Use this form to edit the list.'));
224         }
225     }
226
227     function showScripts()
228     {
229         parent::showScripts();
230         $this->autofocus('tag');
231     }
232
233     function trySave()
234     {
235         $tag         = common_canonical_tag($this->trimmed('tag'));
236         $description = $this->trimmed('description');
237         $private     = $this->boolean('private');
238         $delete      = $this->arg('delete');
239         $confirm     = $this->arg('confirm');
240         $cancel      = $this->arg('cancel');
241
242         if ($delete && $cancel) {
243             // TRANS: Form validation error displayed if the form data for deleting a tag was incorrect.
244             $this->showForm(_('Delete aborted.'));
245             return;
246         }
247
248         $set_private = $private && $this->peopletag->private != $private;
249
250         if ($delete && !$confirm) {
251             // TRANS: Text in confirmation dialog for deleting a tag.
252             $this->showConfirm(_('Deleting this tag will permanantly remove ' .
253                                  'all its subscription and membership records. ' .
254                                  'Do you still want to continue?'), array('delete' => 1));
255             return;
256         } else if (common_valid_tag($tag)) {
257             // TRANS: Form validation error displayed if a given tag is invalid.
258             $this->showForm(_('Invalid tag.'));
259             return;
260         } else if ($tag != $this->peopletag->tag && $this->tagExists($tag)) {
261             // TRANS: Form validation error displayed if a given tag is already present.
262             // TRANS: %s is the already present tag.
263             $this->showForm(sprintf(_('You already have a tag named %s.'), $tag));
264             return;
265         } else if (Profile_list::descriptionTooLong($description)) {
266             $this->showForm(sprintf(
267                     // TRANS: Client error shown when providing too long a description when editing a list.
268                     // TRANS: %d is the maximum number of allowed characters.
269                     _m('Description is too long (maximum %d character).',
270                       'Description is too long (maximum %d characters).',
271                       Profile_list::maxDescription()),
272                     Profile_list::maxDescription()));
273             return;
274         } else if ($set_private && !$confirm && !$cancel) {
275             $fwd = array('tag' => $tag,
276                          'description' => $description,
277                          'private' => (int) $private);
278
279             // TRANS: Text in confirmation dialog for setting a tag from public to private.
280             $this->showConfirm(_('Setting a public tag as private will ' .
281                                  'permanently remove all the existing ' .
282                                  'subscriptions to it. Do you still want to continue?'), $fwd);
283             return;
284         }
285
286         $this->peopletag->query('BEGIN');
287
288         $orig = clone($this->peopletag);
289
290         $this->peopletag->tag         = $tag;
291         $this->peopletag->description = $description;
292         if (!$set_private || $confirm) {
293             $this->peopletag->private     = $private;
294         }
295
296         $result = $this->peopletag->update($orig);
297
298         if (!$result) {
299             common_log_db_error($this->group, 'UPDATE', __FILE__);
300             // TRANS: Server error displayed when updating a list fails.
301             $this->serverError(_('Could not update list.'));
302         }
303
304         $this->peopletag->query('COMMIT');
305
306         if ($set_private && $confirm) {
307             Profile_tag_subscription::cleanup($this->peopletag);
308         }
309
310         if ($delete) {
311             // This might take quite a bit of time.
312             $this->peopletag->delete();
313             // send home.
314             common_redirect(common_local_url('all',
315                                          array('nickname' => $this->tagger->nickname)),
316                             303);
317         }
318
319         if ($tag != $orig->tag) {
320             common_redirect(common_local_url('editpeopletag',
321                                              array('tagger' => $this->tagger->nickname,
322                                                    'tag'    => $tag)),
323                             303);
324         } else {
325             // TRANS: Edit list form success message.
326             $this->showForm(_('Options saved.'));
327         }
328     }
329
330     function tagExists($tag)
331     {
332         $args = array('tagger' => $this->tagger->id, 'tag' => $tag);
333         $ptag = Profile_list::pkeyGet($args);
334
335         return !empty($ptag);
336     }
337 }