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