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