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