]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/groupdesignsettings.php
Merge branch '1.0.x' into testing
[quix0rs-gnu-social.git] / actions / groupdesignsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Saves a design for a given 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  Settings
23  * @package   StatusNet
24  * @author    Sarven Capadisli <csarven@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/designsettings.php';
36
37 /**
38  * Set a group's design
39  *
40  * Saves a design for a given group
41  *
42  * @category Settings
43  * @package  StatusNet
44  * @author   Zach Copley <zach@status.net>
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class GroupDesignSettingsAction extends DesignSettingsAction
50 {
51     var $group = null;
52
53     /**
54      * Sets the right action for the form, and passes request args into
55      * the base action
56      *
57      * @param array $args misc. arguments
58      *
59      * @return boolean true
60      */
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         if (!common_logged_in()) {
66             // TRANS: Client error displayed trying to change group design settings while not logged in.
67             $this->clientError(_('You must be logged in to edit a group.'));
68             return false;
69         }
70
71         $nickname_arg = $this->trimmed('nickname');
72         $nickname     = common_canonical_nickname($nickname_arg);
73
74         // Permanent redirect on non-canonical nickname
75
76         if ($nickname_arg != $nickname) {
77             $args = array('nickname' => $nickname);
78             common_redirect(common_local_url('groupdesignsettings', $args), 301);
79             return false;
80         }
81
82         if (!$nickname) {
83             // TRANS: Client error displayed trying to change group design settings without providing a group nickname.
84             $this->clientError(_('No nickname.'), 404);
85             return false;
86         }
87
88         $groupid = $this->trimmed('groupid');
89
90         if ($groupid) {
91             $this->group = User_group::staticGet('id', $groupid);
92         } else {
93             $local = Local_group::staticGet('nickname', $nickname);
94             if ($local) {
95                 $this->group = User_group::staticGet('id', $local->group_id);
96             }
97         }
98
99         if (!$this->group) {
100             // TRANS: Client error displayed trying to change group design settings while providing a nickname for a non-existing group.
101             $this->clientError(_('No such group.'), 404);
102             return false;
103         }
104
105         $cur = common_current_user();
106
107         if (!$cur->isAdmin($this->group)) {
108             // TRANS: Client error displayed trying to change group design settings without being a (group) admin.
109             $this->clientError(_('You must be an admin to edit the group.'), 403);
110             return false;
111         }
112
113         $this->submitaction = common_local_url('groupdesignsettings',
114             array('nickname' => $this->group->nickname));
115
116         return true;
117     }
118
119     /**
120      * A design for this action
121      *
122      * if the group attribute has been set, returns that group's
123      * design.
124      *
125      * @return Design a design object to use
126      */
127     function getDesign()
128     {
129
130         if (empty($this->group)) {
131             return null;
132         }
133
134         return $this->group->getDesign();
135     }
136
137     /**
138      * Title of the page
139      *
140      * @return string Title of the page
141      */
142
143     function title()
144     {
145         // TRANS: Title group design settings page.
146         return _('Group design');
147     }
148
149     /**
150      * Instructions for use
151      *
152      * @return instructions for use
153      */
154     function getInstructions()
155     {
156         // TRANS: Instructions for group design settings page.
157         return _('Customize the way your group looks ' .
158         'with a background image and a colour palette of your choice.');
159     }
160
161     /**
162      * Override to show group nav stuff
163      *
164      * @return nothing
165      */
166     function showObjectNav()
167     {
168         $nav = new GroupNav($this, $this->group);
169         $nav->show();
170     }
171
172     /**
173      * Override to show default nav stuff
174      *
175      * @return nothing
176      */
177     function showLocalNav()
178     {
179         Action::showLocalNav();
180     }
181
182     /**
183      * Get the design we want to edit
184      *
185      * @return Design
186      */
187     function getWorkingDesign()
188     {
189         $design = null;
190
191         if (isset($this->group)) {
192             $design = $this->group->getDesign();
193         }
194
195         return $design;
196     }
197
198     /**
199      * Content area of the page
200      *
201      * Shows a form for changing the design
202      *
203      * @return void
204      */
205     function showContent()
206     {
207         $design = $this->getWorkingDesign();
208
209         if (empty($design)) {
210             $design = Design::siteDesign();
211         }
212
213         $this->showDesignForm($design);
214     }
215
216     /**
217      * Save or update the group's design settings
218      *
219      * @return void
220      */
221     function saveDesign()
222     {
223         try {
224             $bgcolor = new WebColor($this->trimmed('design_background'));
225             $ccolor  = new WebColor($this->trimmed('design_content'));
226             $sbcolor = new WebColor($this->trimmed('design_sidebar'));
227             $tcolor  = new WebColor($this->trimmed('design_text'));
228             $lcolor  = new WebColor($this->trimmed('design_links'));
229         } catch (WebColorException $e) {
230             $this->showForm($e->getMessage());
231             return;
232         }
233
234         $onoff = $this->arg('design_background-image_onoff');
235
236         $on   = false;
237         $off  = false;
238         $tile = false;
239
240         if ($onoff == 'on') {
241             $on = true;
242         } else {
243             $off = true;
244         }
245
246         $repeat = $this->boolean('design_background-image_repeat');
247
248         if ($repeat) {
249             $tile = true;
250         }
251
252         $design = $this->group->getDesign();
253
254         if (!empty($design)) {
255             // update design
256
257             $original = clone($design);
258
259             $design->backgroundcolor = $bgcolor->intValue();
260             $design->contentcolor    = $ccolor->intValue();
261             $design->sidebarcolor    = $sbcolor->intValue();
262             $design->textcolor       = $tcolor->intValue();
263             $design->linkcolor       = $lcolor->intValue();
264
265             $design->setDisposition($on, $off, $tile);
266
267             $result = $design->update($original);
268
269             if ($result === false) {
270                 common_log_db_error($design, 'UPDATE', __FILE__);
271                 // TRANS: Form validation error displayed when group design settings could not be updated because of an application issue.
272                 $this->showForm(_('Unable to update your design settings.'));
273                 return;
274             }
275         } else {
276             $this->group->query('BEGIN');
277
278             // save new design
279
280             $design = new Design();
281
282             $design->backgroundcolor = $bgcolor->intValue();
283             $design->contentcolor    = $ccolor->intValue();
284             $design->sidebarcolor    = $sbcolor->intValue();
285             $design->textcolor       = $tcolor->intValue();
286             $design->linkcolor       = $lcolor->intValue();
287
288             $design->setDisposition($on, $off, $tile);
289
290             $id = $design->insert();
291
292             if (empty($id)) {
293                 common_log_db_error($id, 'INSERT', __FILE__);
294                 // TRANS: Form validation error displayed when group design settings could not be saved because of an application issue.
295                 $this->showForm(_('Unable to save your design settings.'));
296                 return;
297             }
298
299             $original               = clone($this->group);
300             $this->group->design_id = $id;
301             $result                 = $this->group->update($original);
302
303             if (empty($result)) {
304                 common_log_db_error($original, 'UPDATE', __FILE__);
305                 // TRANS: Form validation error displayed when group design settings could not be saved because of an application issue.
306                 $this->showForm(_('Unable to save your design settings.'));
307                 $this->group->query('ROLLBACK');
308                 return;
309             }
310
311             $this->group->query('COMMIT');
312         }
313
314         $this->saveBackgroundImage($design);
315
316         // TRANS: Form text to confirm saved group design settings.
317         $this->showForm(_('Design preferences saved.'), true);
318     }
319 }