]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newgroup.php
Email notify-on-fave moved to Profile_prefs (run upgrade.php)
[quix0rs-gnu-social.git] / actions / newgroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Add a new 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  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @copyright 2013 Free Software Foundation, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Add a new group
38  *
39  * This is the form for adding a new group
40  *
41  * @category Group
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class NewgroupAction extends FormAction
48 {
49     function title()
50     {
51         // TRANS: Title for form to create a group.
52         return _('New group');
53     }
54
55     /**
56      * Prepare to run
57      */
58     protected function prepare(array $args=array())
59     {
60         parent::prepare($args);
61
62         // $this->scoped is the current user profile
63         if (!$this->scoped->hasRight(Right::CREATEGROUP)) {
64             // TRANS: Client exception thrown when a user tries to create a group while banned.
65             $this->clientError(_('You are not allowed to create groups on this site.'), 403);
66         }
67
68         return true;
69     }
70
71     public function showContent()
72     {
73         $form = new GroupEditForm($this);
74         $form->show();
75     }
76
77     public function showInstructions()
78     {
79         $this->element('p', 'instructions',
80                        // TRANS: Form instructions for group create form.
81                        _('Use this form to create a new group.'));
82     }
83
84     protected function handlePost()
85     {
86         parent::handlePost();
87
88         if (Event::handle('StartGroupSaveForm', array($this))) {
89             $nickname = Nickname::normalize($this->trimmed('newnickname'), true);
90
91             $fullname    = $this->trimmed('fullname');
92             $homepage    = $this->trimmed('homepage');
93             $description = $this->trimmed('description');
94             $location    = $this->trimmed('location');
95             $private     = $this->boolean('private');
96             $aliasstring = $this->trimmed('aliases');
97
98             if (!is_null($homepage) && (strlen($homepage) > 0) &&
99                        !common_valid_http_url($homepage)) {
100                 // TRANS: Group create form validation error.
101                 throw new ClientException(_('Homepage is not a valid URL.'));
102             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
103                 // TRANS: Group create form validation error.
104                 throw new ClientException(_('Full name is too long (maximum 255 characters).'));
105             } else if (User_group::descriptionTooLong($description)) {
106                 // TRANS: Group create form validation error.
107                 // TRANS: %d is the maximum number of allowed characters.
108                 throw new ClientException(sprintf(_m('Description is too long (maximum %d character).',
109                                            'Description is too long (maximum %d characters).',
110                                            User_group::maxDescription()),
111                                         User_group::maxDescription()));
112             } else if (!is_null($location) && mb_strlen($location) > 255) {
113                 // TRANS: Group create form validation error.
114                 throw new ClientException(_('Location is too long (maximum 255 characters).'));
115             }
116
117             if (!empty($aliasstring)) {
118                 $aliases = array_map(array('Nickname', 'normalize'), array_unique(preg_split('/[\s,]+/', $aliasstring)));
119             } else {
120                 $aliases = array();
121             }
122
123             if (count($aliases) > common_config('group', 'maxaliases')) {
124                 // TRANS: Group create form validation error.
125                 // TRANS: %d is the maximum number of allowed aliases.
126                 throw new ClientException(sprintf(_m('Too many aliases! Maximum %d allowed.',
127                                            'Too many aliases! Maximum %d allowed.',
128                                            common_config('group', 'maxaliases')),
129                                         common_config('group', 'maxaliases')));
130                 return;
131             }
132
133             if ($private) {
134                 $force_scope = 1;
135                 $join_policy = User_group::JOIN_POLICY_MODERATE;
136             } else {
137                 $force_scope = 0;
138                 $join_policy = User_group::JOIN_POLICY_OPEN;
139             }
140
141             // This is set up in parent->prepare and checked in self->prepare
142             assert(!is_null($this->scoped));
143
144             $group = User_group::register(array('nickname' => $nickname,
145                                                 'fullname' => $fullname,
146                                                 'homepage' => $homepage,
147                                                 'description' => $description,
148                                                 'location' => $location,
149                                                 'aliases'  => $aliases,
150                                                 'userid'   => $this->scoped->id,
151                                                 'join_policy' => $join_policy,
152                                                 'force_scope' => $force_scope,
153                                                 'local'    => true));
154
155             $this->group = $group;
156
157             Event::handle('EndGroupSaveForm', array($this));
158
159             common_redirect($group->homeUrl(), 303);
160         }
161     }
162 }