3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR . '/lib/accountsettingsaction.php';
36 require_once INSTALLDIR . '/lib/webcolor.php';
39 * Base class for setting a user or group design
41 * Shows the design setting form and also handles some things like saving
42 * background images, and fetching a default design
46 * @author Zach Copley <zach@status.net>
47 * @author Sarven Capadisli <csarven@status.net>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://status.net/
52 class DesignSettingsAction extends AccountSettingsAction
55 var $submitaction = null;
60 * @return string Title of the page
65 return _('Profile design');
69 * Instructions for use
71 * @return instructions for use
74 function getInstructions()
76 return _('Customize the way your profile looks ' .
77 'with a background image and a colour palette of your choice.');
81 * Shows the design settings form
83 * @param Design $design a working design to show
88 function showDesignForm($design)
91 $this->elementStart('form', array('method' => 'post',
92 'enctype' => 'multipart/form-data',
93 'id' => 'form_settings_design',
94 'class' => 'form_settings',
95 'action' => $this->submitaction));
96 $this->elementStart('fieldset');
97 $this->hidden('token', common_session_token());
99 $this->elementStart('fieldset', array('id' =>
100 'settings_design_background-image'));
101 $this->element('legend', null, _('Change background image'));
102 $this->elementStart('ul', 'form_data');
103 $this->elementStart('li');
104 $this->element('label', array('for' => 'design_background-image_file'),
106 $this->element('input', array('name' => 'design_background-image_file',
108 'id' => 'design_background-image_file'));
109 $this->element('p', 'form_guide', _('You can upload your personal ' .
110 'background image. The maximum file size is 2Mb.'));
111 $this->element('input', array('name' => 'MAX_FILE_SIZE',
113 'id' => 'MAX_FILE_SIZE',
114 'value' => ImageFile::maxFileSizeInt()));
115 $this->elementEnd('li');
117 if (!empty($design->backgroundimage)) {
119 $this->elementStart('li', array('id' =>
120 'design_background-image_onoff'));
122 $this->element('img', array('src' =>
123 Design::url($design->backgroundimage)));
125 $attrs = array('name' => 'design_background-image_onoff',
127 'id' => 'design_background-image_on',
131 if ($design->disposition & BACKGROUND_ON) {
132 $attrs['checked'] = 'checked';
135 $this->element('input', $attrs);
137 $this->element('label', array('for' => 'design_background-image_on',
141 $attrs = array('name' => 'design_background-image_onoff',
143 'id' => 'design_background-image_off',
147 if ($design->disposition & BACKGROUND_OFF) {
148 $attrs['checked'] = 'checked';
151 $this->element('input', $attrs);
153 $this->element('label', array('for' => 'design_background-image_off',
156 $this->element('p', 'form_guide', _('Turn background image on or off.'));
157 $this->elementEnd('li');
159 $this->elementStart('li');
160 $this->checkbox('design_background-image_repeat',
161 _('Tile background image'),
162 ($design->disposition & BACKGROUND_TILE) ? true : false);
163 $this->elementEnd('li');
166 $this->elementEnd('ul');
167 $this->elementEnd('fieldset');
169 $this->elementStart('fieldset', array('id' => 'settings_design_color'));
170 $this->element('legend', null, _('Change colours'));
171 $this->elementStart('ul', 'form_data');
175 $bgcolor = new WebColor($design->backgroundcolor);
177 $this->elementStart('li');
178 $this->element('label', array('for' => 'swatch-1'), _('Background'));
179 $this->element('input', array('name' => 'design_background',
186 $this->elementEnd('li');
188 $ccolor = new WebColor($design->contentcolor);
190 $this->elementStart('li');
191 $this->element('label', array('for' => 'swatch-2'), _('Content'));
192 $this->element('input', array('name' => 'design_content',
199 $this->elementEnd('li');
201 $sbcolor = new WebColor($design->sidebarcolor);
203 $this->elementStart('li');
204 $this->element('label', array('for' => 'swatch-3'), _('Sidebar'));
205 $this->element('input', array('name' => 'design_sidebar',
212 $this->elementEnd('li');
214 $tcolor = new WebColor($design->textcolor);
216 $this->elementStart('li');
217 $this->element('label', array('for' => 'swatch-4'), _('Text'));
218 $this->element('input', array('name' => 'design_text',
225 $this->elementEnd('li');
227 $lcolor = new WebColor($design->linkcolor);
229 $this->elementStart('li');
230 $this->element('label', array('for' => 'swatch-5'), _('Links'));
231 $this->element('input', array('name' => 'design_links',
238 $this->elementEnd('li');
240 } catch (WebColorException $e) {
241 common_log(LOG_ERR, 'Bad color values in design ID: ' .$design->id);
244 $this->elementEnd('ul');
245 $this->elementEnd('fieldset');
247 $this->submit('defaults', _('Use defaults'), 'submit form_action-default',
248 'defaults', _('Restore default designs'));
250 $this->element('input', array('id' => 'settings_design_reset',
253 'class' => 'submit form_action-primary',
254 'title' => _('Reset back to default')));
256 $this->submit('save', _('Save'), 'submit form_action-secondary',
257 'save', _('Save design'));
259 $this->elementEnd('fieldset');
260 $this->elementEnd('form');
266 * Validate input and save changes. Reload the form with a success
272 function handlePost()
274 // XXX: Robin's workaround for a bug in PHP where $_POST
275 // and $_FILE are empty in the case that the uploaded
276 // file is bigger than PHP is configured to handle.
278 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
279 if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
281 $msg = _('The server was unable to handle that much POST ' .
282 'data (%s bytes) due to its current configuration.');
284 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
289 $token = $this->trimmed('token');
290 if (!$token || $token != common_session_token()) {
291 $this->showForm(_('There was a problem with your session token. '.
292 'Try again, please.'));
296 if ($this->arg('save')) {
298 } else if ($this->arg('defaults')) {
299 $this->restoreDefaults();
301 $this->showForm(_('Unexpected form submission.'));
306 * Add the Farbtastic stylesheet
311 function showStylesheets()
313 parent::showStylesheets();
314 $this->cssLink('css/farbtastic.css','base','screen, projection, tv');
318 * Add the Farbtastic scripts
323 function showScripts()
325 parent::showScripts();
327 $this->script('js/farbtastic/farbtastic.js');
328 $this->script('js/userdesign.go.js');
330 $this->autofocus('design_background-image_file');
334 * Get a default design
336 * @return Design design
339 function defaultDesign()
341 $defaults = common_config('site', 'design');
343 $design = new Design();
347 $color = new WebColor();
349 $color->parseColor($defaults['backgroundcolor']);
350 $design->backgroundcolor = $color->intValue();
352 $color->parseColor($defaults['contentcolor']);
353 $design->contentcolor = $color->intValue();
355 $color->parseColor($defaults['sidebarcolor']);
356 $design->sidebarcolor = $color->intValue();
358 $color->parseColor($defaults['textcolor']);
359 $design->textcolor = $color->intValue();
361 $color->parseColor($defaults['linkcolor']);
362 $design->linkcolor = $color->intValue();
364 $design->backgroundimage = $defaults['backgroundimage'];
366 $design->disposition = $defaults['disposition'];
368 } catch (WebColorException $e) {
369 common_log(LOG_ERR, _('Bad default color settings: ' .
377 * Save the background image, if any, and set its disposition
379 * @param Design $design a working design to attach the img to
384 function saveBackgroundImage($design)
387 // Now that we have a Design ID we can add a file to the design.
388 // XXX: This is an additional DB hit, but figured having the image
389 // associated with the Design rather than the User was worth
392 if ($_FILES['design_background-image_file']['error'] ==
399 ImageFile::fromUpload('design_background-image_file');
400 } catch (Exception $e) {
401 $this->showForm($e->getMessage());
405 $filename = Design::filename($design->id,
406 image_type_to_extension($imagefile->type),
409 $filepath = Design::path($filename);
411 move_uploaded_file($imagefile->filepath, $filepath);
413 // delete any old backround img laying around
415 if (isset($design->backgroundimage)) {
416 @unlink(Design::path($design->backgroundimage));
419 $original = clone($design);
421 $design->backgroundimage = $filename;
423 // default to on, no tile
425 $design->setDisposition(true, false, false);
427 $result = $design->update($original);
429 if ($result === false) {
430 common_log_db_error($design, 'UPDATE', __FILE__);
431 $this->showForm(_('Couldn\'t update your design.'));
438 * Restore the user or group design to system defaults
443 function restoreDefaults()
445 $design = $this->getWorkingDesign();
446 $default = $this->defaultDesign();
447 $original = clone($design);
449 $design->backgroundcolor = $default->backgroundcolor;
450 $design->contentcolor = $default->contentcolor;
451 $design->sidebarcolor = $default->sidebarcolor;
452 $design->textcolor = $default->textcolor;
453 $design->linkcolor = $default->linkcolor;
455 $design->setDisposition(false, true, false);
457 $result = $design->update($original);
459 if ($result === false) {
460 common_log_db_error($design, 'UPDATE', __FILE__);
461 $this->showForm(_('Couldn\'t update your design.'));
465 $this->showForm(_('Design defaults restored.'), true);