]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/editpeopletag.php
Merge branch 'people_tags_rebase' into 1.0.x
[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 OwnerDesignAction
44 {
45
46     var $msg, $confirm, $confirm_args=array();
47
48     function title()
49     {
50         if ($_SERVER['REQUEST_METHOD'] == 'POST' && $this->boolean('delete')) {
51             return sprintf(_('Delete %s people tag'), $this->peopletag->tag);
52         }
53         return sprintf(_('Edit people tag %s'), $this->peopletag->tag);
54     }
55
56     /**
57      * Prepare to run
58      */
59
60     function prepare($args)
61     {
62         parent::prepare($args);
63
64         if (!common_logged_in()) {
65             $this->clientError(_('Not logged in.'));
66             return false;
67         }
68
69         $id = $this->arg('id');
70         $tagger_arg = $this->arg('tagger');
71         $tag_arg = $this->arg('tag');
72
73         $tagger = common_canonical_nickname($tagger_arg);
74         $tag = common_canonical_tag($tag_arg);
75
76         $current = common_current_user();
77
78         // Permanent redirect on non-canonical tag
79
80         if ($tagger_arg != $tagger || $tag_arg != $tag) {
81             $args = array('tagger' => $tagger, 'tag' => $tag);
82             common_redirect(common_local_url('editpeopletag', $args), 301);
83             return false;
84         }
85
86         $user = null;
87         if ($id) {
88             $this->peopletag = Profile_list::staticGet('id', $id);
89             if (!empty($this->peopletag)) {
90                 $user = User::staticGet('id', $this->peopletag->tagger);
91             }
92         } else {
93             if (!$tagger) {
94                 $this->clientError(_('No tagger or ID.'), 404);
95                 return false;
96             }
97
98             $user = User::staticGet('nickname', $tagger);
99             $this->peopletag = Profile_list::pkeyGet(array('tagger' => $user->id, 'tag' => $tag));
100         }
101
102         if (!$this->peopletag) {
103             $this->clientError(_('No such peopletag.'), 404);
104             return false;
105         }
106
107         if (!$user) {
108             // This should not be happening
109             $this->clientError(_('Not a local user.'), 404);
110             return false;
111         }
112
113         if ($current->id != $user->id) {
114             $this->clientError(_('You must be the creator of the tag to edit it.'), 404);
115             return false;
116         }
117
118         $this->tagger = $user->getProfile();
119
120         return true;
121     }
122
123     /**
124      * Handle the request
125      *
126      * On GET, show the form. On POST, try to save the group.
127      *
128      * @param array $args unused
129      *
130      * @return void
131      */
132
133     function handle($args)
134     {
135         parent::handle($args);
136         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
137             $this->trySave();
138         } else {
139             $this->showForm();
140         }
141     }
142
143     function showConfirm($msg=null, $fwd=null)
144     {
145         $this->confirm = $msg;
146         $this->confirm_args = $fwd;
147         $this->showPage();
148     }
149
150     function showConfirmForm()
151     {
152         $this->elementStart('form', array('id' => 'form_peopletag_edit_confirm',
153                                           'class' => 'form_settings',
154                                           'method' => 'post',
155                                           'action' => common_local_url('editpeopletag',
156                                               array('tagger' => $this->tagger->nickname,
157                                                     'tag' => $this->peopletag->tag))));
158         $this->elementStart('fieldset');
159         $this->hidden('token', common_session_token());
160         $this->hidden('id', $this->arg('id'));
161
162         foreach ($this->confirm_args as $key => $val) {
163             $this->hidden($key, $val);
164         }
165
166         $this->submit('form_action-no',
167                       _m('BUTTON','No'),
168                       'submit form_action-primary',
169                       'cancel');
170         $this->submit('form_action-yes',
171                       _m('BUTTON','Yes'),
172                       'submit form_action-secondary',
173                       'confirm');
174         $this->elementEnd('fieldset');
175         $this->elementEnd('form');
176     }
177
178     function showForm($msg=null)
179     {
180         $this->msg = $msg;
181         $this->showPage();
182     }
183
184     function showObjectNav()
185     {
186         $nav = new PeopletagGroupNav($this, $this->peopletag);
187         $nav->show();
188     }
189
190     function showContent()
191     {
192         if ($this->confirm) {
193             $this->showConfirmForm();
194             return;
195         }
196
197         $form = new PeopletagEditForm($this, $this->peopletag);
198         $form->show();
199
200         $form->showProfileList();
201     }
202
203     function showPageNotice()
204     {
205         if ($this->msg) {
206             $this->element('p', 'error', $this->msg);
207         } else if ($this->confirm) {
208             $this->element('p', 'instructions', $this->confirm);
209         } else {
210             $this->element('p', 'instructions',
211                            _('Use this form to edit the people tag.'));
212         }
213     }
214
215     function showScripts()
216     {
217         parent::showScripts();
218         $this->autofocus('tag');
219     }
220
221     function trySave()
222     {
223         $tag         = common_canonical_tag($this->trimmed('tag'));
224         $description = $this->trimmed('description');
225         $private     = $this->boolean('private');
226         $delete      = $this->arg('delete');
227         $confirm     = $this->arg('confirm');
228         $cancel      = $this->arg('cancel');
229
230         if ($delete && $cancel) {
231             $this->showForm(_('Delete aborted.'));
232             return;
233         }
234
235         $set_private = $private && $this->peopletag->private != $private;
236
237         if ($delete && !$confirm) {
238             $this->showConfirm(_('Deleting this tag will permanantly remove ' .
239                                  'all its subscription and membership records. ' .
240                                  'Do you still want to continue?'), array('delete' => 1));
241             return;
242         } else if (common_valid_tag($tag)) {
243             $this->showForm(_('Invalid tag.'));
244             return;
245         } else if ($tag != $this->peopletag->tag && $this->tagExists($tag)) {
246             $this->showForm(sprintf(_('You already have a tag named %s.'), $tag));
247             return;
248         } else if (Profile_list::descriptionTooLong($description)) {
249             $this->showForm(sprintf(_('description is too long (max %d chars).'), Profile_list::maxDescription()));
250             return;
251         } else if ($set_private && !$confirm && !$cancel) {
252             $fwd = array('tag' => $tag,
253                          'description' => $description,
254                          'private' => (int) $private);
255
256             $this->showConfirm(_('Setting a public tag as private will ' .
257                                  'permanently remove all the existing ' .
258                                  'subscriptions to it. Do you still want to continue?'), $fwd);
259             return;
260         }
261
262         $this->peopletag->query('BEGIN');
263
264         $orig = clone($this->peopletag);
265
266         $this->peopletag->tag         = $tag;
267         $this->peopletag->description = $description;
268         if (!$set_private || $confirm) {
269             $this->peopletag->private     = $private;
270         }
271
272         $result = $this->peopletag->update($orig);
273
274         if (!$result) {
275             common_log_db_error($this->group, 'UPDATE', __FILE__);
276             $this->serverError(_('Could not update peopletag.'));
277         }
278
279         $this->peopletag->query('COMMIT');
280
281         if ($set_private && $confirm) {
282             Profile_tag_subscription::cleanup($this->peopletag);
283         }
284
285         if ($delete) {
286             // This might take quite a bit of time.
287             $this->peopletag->delete();
288             // send home.
289             common_redirect(common_local_url('all',
290                                          array('nickname' => $this->tagger->nickname)),
291                             303);
292         }
293
294         if ($tag != $orig->tag) {
295             common_redirect(common_local_url('editpeopletag',
296                                              array('tagger' => $this->tagger->nickname,
297                                                    'tag'    => $tag)),
298                             303);
299         } else {
300             $this->showForm(_('Options saved.'));
301         }
302     }
303
304     function tagExists($tag)
305     {
306         $args = array('tagger' => $this->tagger->id, 'tag' => $tag);
307         $ptag = Profile_list::pkeyGet($args);
308
309         return !empty($ptag);
310     }
311 }