3 * Laconica, 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@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/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR . '/lib/accountsettingsaction.php';
36 require_once INSTALLDIR . '/lib/webcolor.php';
38 class DesignsettingsAction extends AccountSettingsAction
43 * @return string Title of the page
48 return _('Profile design');
52 * Instructions for use
54 * @return instructions for use
57 function getInstructions()
59 return _('Customize the way your profile looks ' .
60 'with a background image and a colour palette of your choice.');
64 * Content area of the page
66 * Shows a form for changing the password
71 function showContent()
73 $user = common_current_user();
74 $design = $user->getDesign();
77 $design = $this->defaultDesign();
80 $this->elementStart('form', array('method' => 'post',
81 'enctype' => 'multipart/form-data',
82 'id' => 'form_settings_design',
83 'class' => 'form_settings',
85 common_local_url('designsettings')));
86 $this->elementStart('fieldset');
87 $this->hidden('token', common_session_token());
89 $this->elementStart('fieldset', array('id' =>
90 'settings_design_background-image'));
91 $this->element('legend', null, _('Change background image'));
92 $this->elementStart('ul', 'form_data');
93 $this->elementStart('li');
94 $this->element('label', array('for' => 'design_background-image_file'),
96 $this->element('input', array('name' => 'design_background-image_file',
98 'id' => 'design_background-image_file'));
99 $this->element('p', 'form_guide', _('You can upload your personal ' .
100 'background image. The maximum file size is 2Mb.'));
101 $this->element('input', array('name' => 'MAX_FILE_SIZE',
103 'id' => 'MAX_FILE_SIZE',
104 'value' => ImageFile::maxFileSizeInt()));
105 $this->elementEnd('li');
107 if (!empty($design->backgroundimage)) {
109 $this->elementStart('li', array('id' => 'design_background-image_onoff'));
111 $this->element('img', array('src' =>
112 Design::url($design->backgroundimage)));
114 $attrs = array('name' => 'design_background-image_onoff',
116 'id' => 'design_background-image_on',
120 if ($design->disposition & BACKGROUND_ON) {
121 $attrs['checked'] = 'checked';
124 $this->element('input', $attrs);
126 $this->element('label', array('for' => 'design_background-image_on',
130 $attrs = array('name' => 'design_background-image_onoff',
132 'id' => 'design_background-image_off',
136 if ($design->disposition & BACKGROUND_OFF) {
137 $attrs['checked'] = 'checked';
140 $this->element('input', $attrs);
142 $this->element('label', array('for' => 'design_background-image_off',
145 $this->element('p', 'form_guide', _('Turn background image on or off.'));
146 $this->elementEnd('li');
149 $this->elementStart('li');
150 $this->checkbox('design_background-image_repeat',
151 _('Tile background image'),
152 ($design->disposition & BACKGROUND_TILE) ? true : false );
153 $this->elementEnd('li');
155 $this->elementEnd('ul');
156 $this->elementEnd('fieldset');
158 $this->elementStart('fieldset', array('id' => 'settings_design_color'));
159 $this->element('legend', null, _('Change colours'));
160 $this->elementStart('ul', 'form_data');
164 $bgcolor = new WebColor($design->backgroundcolor);
166 $this->elementStart('li');
167 $this->element('label', array('for' => 'swatch-1'), _('Background'));
168 $this->element('input', array('name' => 'design_background',
174 'value' => '#' . $bgcolor->hexValue()));
175 $this->elementEnd('li');
177 $ccolor = new WebColor($design->contentcolor);
179 $this->elementStart('li');
180 $this->element('label', array('for' => 'swatch-2'), _('Content'));
181 $this->element('input', array('name' => 'design_content',
187 'value' => '#' . $ccolor->hexValue()));
188 $this->elementEnd('li');
190 $sbcolor = new WebColor($design->sidebarcolor);
192 $this->elementStart('li');
193 $this->element('label', array('for' => 'swatch-3'), _('Sidebar'));
194 $this->element('input', array('name' => 'design_sidebar',
200 'value' => '#' . $sbcolor->hexValue()));
201 $this->elementEnd('li');
203 $tcolor = new WebColor($design->textcolor);
205 $this->elementStart('li');
206 $this->element('label', array('for' => 'swatch-4'), _('Text'));
207 $this->element('input', array('name' => 'design_text',
213 'value' => '#' . $tcolor->hexValue()));
214 $this->elementEnd('li');
216 $lcolor = new WebColor($design->linkcolor);
218 $this->elementStart('li');
219 $this->element('label', array('for' => 'swatch-5'), _('Links'));
220 $this->element('input', array('name' => 'design_links',
226 'value' => '#' . $lcolor->hexValue()));
228 $this->elementEnd('li');
230 } catch (WebColorException $e) {
231 common_log(LOG_ERR, 'Bad color values in design ID: ' .
235 $this->elementEnd('ul');
236 $this->elementEnd('fieldset');
238 $this->element('input', array('id' => 'settings_design_reset',
241 'class' => 'submit form_action-primary',
242 'title' => _('Reset back to default')));
244 $this->submit('save', _('Save'), 'submit form_action-secondary',
245 'save', _('Save design'));
247 $this->elementEnd('fieldset');
248 $this->elementEnd('form');
254 * Validate input and save changes. Reload the form with a success
260 function handlePost()
262 // XXX: Robin's workaround for a bug in PHP where $_POST
263 // and $_FILE are empty in the case that the uploaded
264 // file is bigger than PHP is configured to handle.
266 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
267 if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
269 $msg = _('The server was unable to handle that much POST ' .
270 'data (%s bytes) due to its current configuration.');
272 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
277 $token = $this->trimmed('token');
278 if (!$token || $token != common_session_token()) {
279 $this->showForm(_('There was a problem with your session token. '.
280 'Try again, please.'));
284 if ($this->arg('save')) {
286 } else if ($this->arg('reset')) {
287 $this->resetDesign();
289 $this->showForm(_('Unexpected form submission.'));
294 * Add the Farbtastic stylesheet
299 function showStylesheets()
301 parent::showStylesheets();
303 common_path('theme/base/css/farbtastic.css?version='.LACONICA_VERSION);
305 $this->element('link', array('rel' => 'stylesheet',
306 'type' => 'text/css',
307 'href' => $farbtasticStyle,
308 'media' => 'screen, projection, tv'));
312 * Add the Farbtastic scripts
317 function showScripts()
319 parent::showScripts();
321 $farbtasticPack = common_path('js/farbtastic/farbtastic.js');
322 $userDesignGo = common_path('js/userdesign.go.js');
324 $this->element('script', array('type' => 'text/javascript',
325 'src' => $farbtasticPack));
326 $this->element('script', array('type' => 'text/javascript',
327 'src' => $userDesignGo));
331 * Get a default user design
333 * @return Design design
336 function defaultDesign()
338 $defaults = common_config('site', 'design');
340 $design = new Design();
344 $color = new WebColor();
346 $color->parseColor($defaults['backgroundcolor']);
347 $design->backgroundcolor = $color->intValue();
349 $color->parseColor($defaults['contentcolor']);
350 $design->contentcolor = $color->intValue();
352 $color->parseColor($defaults['sidebarcolor']);
353 $design->sidebarcolor = $color->intValue();
355 $color->parseColor($defaults['textcolor']);
356 $design->sidebarcolor = $color->intValue();
358 $color->parseColor($defaults['linkcolor']);
359 $design->linkcolor = $color->intValue();
361 $design->backgroundimage = $defaults['backgroundimage'];
363 $deisng->disposition = $defaults['disposition'];
365 } catch (WebColorException $e) {
366 common_log(LOG_ERR, _('Bad default color settings: ' .
374 * Save or update the user's design settings
379 function saveDesign()
383 $bgcolor = new WebColor($this->trimmed('design_background'));
384 $ccolor = new WebColor($this->trimmed('design_content'));
385 $sbcolor = new WebColor($this->trimmed('design_sidebar'));
386 $tcolor = new WebColor($this->trimmed('design_text'));
387 $lcolor = new WebColor($this->trimmed('design_links'));
389 } catch (WebColorException $e) {
390 $this->showForm($e->getMessage());
394 $onoff = $this->arg('design_background-image_onoff');
400 if ($onoff == 'on') {
406 $repeat = $this->boolean('design_background-image_repeat');
412 $user = common_current_user();
413 $design = $user->getDesign();
415 if (!empty($design)) {
417 $original = clone($design);
419 $design->backgroundcolor = $bgcolor->intValue();
420 $design->contentcolor = $ccolor->intValue();
421 $design->sidebarcolor = $sbcolor->intValue();
422 $design->textcolor = $tcolor->intValue();
423 $design->linkcolor = $lcolor->intValue();
424 $design->backgroundimage = $filepath;
426 $design->setDisposition($on, $off, $tile);
428 $result = $design->update($original);
430 if ($result === false) {
431 common_log_db_error($design, 'UPDATE', __FILE__);
432 $this->showForm(_('Couldn\'t update your design.'));
439 $user->query('BEGIN');
442 $design = new Design();
444 $design->backgroundcolor = $bgcolor->intValue();
445 $design->contentcolor = $ccolor->intValue();
446 $design->sidebarcolor = $sbcolor->intValue();
447 $design->textcolor = $tcolor->intValue();
448 $design->linkcolor = $lcolor->intValue();
449 $design->backgroundimage = $filepath;
451 $design->setDisposition($on, $off, $tile);
453 $id = $design->insert();
456 common_log_db_error($id, 'INSERT', __FILE__);
457 $this->showForm(_('Unable to save your design settings!'));
461 $original = clone($user);
462 $user->design_id = $id;
463 $result = $user->update($original);
465 if (empty($result)) {
466 common_log_db_error($original, 'UPDATE', __FILE__);
467 $this->showForm(_('Unable to save your design settings!'));
468 $user->query('ROLLBACK');
472 $user->query('COMMIT');
476 // Now that we have a Design ID we can add a file to the design.
477 // XXX: This is an additional DB hit, but figured having the image
478 // associated with the Design rather than the User was worth
481 if ($_FILES['design_background-image_file']['error'] ==
488 ImageFile::fromUpload('design_background-image_file');
489 } catch (Exception $e) {
490 $this->showForm($e->getMessage());
494 $filename = Design::filename($design->id,
495 image_type_to_extension($imagefile->type),
498 $filepath = Design::path($filename);
500 move_uploaded_file($imagefile->filepath, $filepath);
502 $original = clone($design);
503 $design->backgroundimage = $filename;
504 $result = $design->update($original);
506 if ($result === false) {
507 common_log_db_error($design, 'UPDATE', __FILE__);
508 $this->showForm(_('Couldn\'t update your design.'));
513 $this->showForm(_('Design preferences saved.'), true);