User design setting save now

This commit is contained in:
Zach Copley 2009-06-15 20:21:33 -07:00
parent 807d84d448
commit 20d9350877
2 changed files with 370 additions and 71 deletions

View File

@ -33,6 +33,7 @@ if (!defined('LACONICA')) {
} }
require_once INSTALLDIR . '/lib/accountsettingsaction.php'; require_once INSTALLDIR . '/lib/accountsettingsaction.php';
require_once INSTALLDIR . '/lib/WebColor.php';
class DesignsettingsAction extends AccountSettingsAction class DesignsettingsAction extends AccountSettingsAction
{ {
@ -105,6 +106,10 @@ class DesignsettingsAction extends AccountSettingsAction
$design = $this->defaultDesign(); $design = $this->defaultDesign();
} }
try {
$bgcolor = new WebColor($design->backgroundcolor);
$this->elementStart('li'); $this->elementStart('li');
$this->element('label', array('for' => 'swatch-0'), _('Background')); $this->element('label', array('for' => 'swatch-0'), _('Background'));
$this->element('input', array('name' => 'design_background', $this->element('input', array('name' => 'design_background',
@ -113,9 +118,12 @@ class DesignsettingsAction extends AccountSettingsAction
'class' => 'swatch', 'class' => 'swatch',
'maxlength' => '7', 'maxlength' => '7',
'size' => '7', 'size' => '7',
'value' => $design->backgroundcolor)); 'value' => '#' . $bgcolor->hexValue()));
$this->elementEnd('li'); $this->elementEnd('li');
$ccolor = new WebColor($design->contentcolor);
$this->elementStart('li'); $this->elementStart('li');
$this->element('label', array('for' => 'swatch-1'), _('Content')); $this->element('label', array('for' => 'swatch-1'), _('Content'));
$this->element('input', array('name' => 'design_content', $this->element('input', array('name' => 'design_content',
@ -124,9 +132,11 @@ class DesignsettingsAction extends AccountSettingsAction
'class' => 'swatch', 'class' => 'swatch',
'maxlength' => '7', 'maxlength' => '7',
'size' => '7', 'size' => '7',
'value' => $design->contentcolor)); 'value' => '#' . $ccolor->hexValue()));
$this->elementEnd('li'); $this->elementEnd('li');
$sbcolor = new WebColor($design->sidebarcolor);
$this->elementStart('li'); $this->elementStart('li');
$this->element('label', array('for' => 'swatch-2'), _('Sidebar')); $this->element('label', array('for' => 'swatch-2'), _('Sidebar'));
$this->element('input', array('name' => 'design_sidebar', $this->element('input', array('name' => 'design_sidebar',
@ -135,9 +145,11 @@ class DesignsettingsAction extends AccountSettingsAction
'class' => 'swatch', 'class' => 'swatch',
'maxlength' => '7', 'maxlength' => '7',
'size' => '7', 'size' => '7',
'value' => $design->sidebarcolor)); 'value' => '#' . $sbcolor->hexValue()));
$this->elementEnd('li'); $this->elementEnd('li');
$tcolor = new WebColor($design->textcolor);
$this->elementStart('li'); $this->elementStart('li');
$this->element('label', array('for' => 'swatch-3'), _('Text')); $this->element('label', array('for' => 'swatch-3'), _('Text'));
$this->element('input', array('name' => 'design_text', $this->element('input', array('name' => 'design_text',
@ -146,9 +158,11 @@ class DesignsettingsAction extends AccountSettingsAction
'class' => 'swatch', 'class' => 'swatch',
'maxlength' => '7', 'maxlength' => '7',
'size' => '7', 'size' => '7',
'value' => $design->textcolor)); 'value' => '#' . $tcolor->hexValue()));
$this->elementEnd('li'); $this->elementEnd('li');
$lcolor = new WebColor($design->linkcolor);
$this->elementStart('li'); $this->elementStart('li');
$this->element('label', array('for' => 'swatch-4'), _('Links')); $this->element('label', array('for' => 'swatch-4'), _('Links'));
$this->element('input', array('name' => 'design_links', $this->element('input', array('name' => 'design_links',
@ -157,10 +171,15 @@ class DesignsettingsAction extends AccountSettingsAction
'class' => 'swatch', 'class' => 'swatch',
'maxlength' => '7', 'maxlength' => '7',
'size' => '7', 'size' => '7',
'value' => $design->linkcolor)); 'value' => '#' . $lcolor->hexValue()));
$this->elementEnd('li'); $this->elementEnd('li');
} catch (WebColorException $e) {
common_log(LOG_ERR, 'Bad color values in design ID: ' .
$design->id);
}
$this->elementEnd('ul'); $this->elementEnd('ul');
$this->elementEnd('fieldset'); $this->elementEnd('fieldset');
@ -253,18 +272,38 @@ class DesignsettingsAction extends AccountSettingsAction
$defaults = common_config('site', 'design'); $defaults = common_config('site', 'design');
$design = new Design(); $design = new Design();
$design->backgroundcolor = $defaults['backgroundcolor'];
$design->contentcolor = $defaults['contentcolor']; try {
$design->sidebarcolor = $defaults['sidebarcolor'];
$design->textcolor = $defaults['textcolor']; $color = new WebColor();
$design->linkcolor = $defaults['linkcolor'];
$color->parseColor($defaults['backgroundcolor']);
$design->backgroundcolor = $color->intValue();
$color->parseColor($defaults['contentcolor']);
$design->contentcolor = $color->intValue();
$color->parseColor($defaults['sidebarcolor']);
$design->sidebarcolor = $color->intValue();
$color->parseColor($defaults['textcolor']);
$design->sidebarcolor = $color->intValue();
$color->parseColor($defaults['linkcolor']);
$design->linkcolor = $color->intValue();
$design->backgroundimage = $defaults['backgroundimage']; $design->backgroundimage = $defaults['backgroundimage'];
} catch (WebColorException $e) {
common_log(LOG_ERR, _('Bad default color settings: ' .
$e->getMessage()));
}
return $design; return $design;
} }
/** /**
* Save the user's design settings * Save or update the user's design settings
* *
* @return void * @return void
*/ */
@ -273,7 +312,76 @@ class DesignsettingsAction extends AccountSettingsAction
{ {
$user = common_current_user(); $user = common_current_user();
try {
$bgcolor = new WebColor($this->trimmed('design_background'));
$ccolor = new WebColor($this->trimmed('design_content'));
$sbcolor = new WebColor($this->trimmed('design_sidebar'));
$tcolor = new WebColor($this->trimmed('design_text'));
$lcolor = new WebColor($this->trimmed('design_links'));
} catch (WebColorException $e) {
$this->showForm($e->getMessage());
return;
}
$design = User::getDesign();
if (!isset($design)) {
$original = clone($design);
$original->backgroundcolor = $bgcolor->intValue();
$original->contentcolor = $ccolor->intValue();
$original->sidebarcolor = $sbcolor->intValue();
$original->textcolor = $tcolor->intValue();
$original->linkcolor = $lcolor->intValue();
$result = $design->update($original);
if ($result === false) {
common_log_db_error($design, 'UPDATE', __FILE__);
$this->showForm(_('Couldn\'t update your design.'));
return;
}
// update design
} else {
$user->query('BEGIN');
// save new design
$design = new Design();
$design->backgroundcolor = $bgcolor->intValue();
$design->contentcolor = $ccolor->intValue();
$design->sidebarcolor = $sbcolor->intValue();
$design->textcolor = $tcolor->intValue();
$design->linkcolor = $lcolor->intValue();
$design->backgroundimage = $defaults['backgroundimage'];
$id = $design->insert();
if (empty($id)) {
common_log_db_error($id, 'INSERT', __FILE__);
$this->showForm(_('Unable to save your design settings!'));
return;
}
$original = clone($user);
$user->design_id = $id;
$result = $user->update($original);
if (empty($result)) {
common_log_db_error($original, 'UPDATE', __FILE__);
$this->showForm(_('Unable to save your design settings!'));
$user->query('ROLLBACK');
return;
}
$user->query('COMMIT');
}
$this->showForm(_('Design preferences saved.'), true); $this->showForm(_('Design preferences saved.'), true);
} }

191
lib/webcolor.php Normal file
View File

@ -0,0 +1,191 @@
<?php
/**
* Laconica, the distributed open-source microblogging tool
*
* Base class for deleting things
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Personal
* @package Laconica
* @author Zach Copley <zach@controlyourself.ca>
* @copyright 2009 Control Yourself, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://laconi.ca/
*/
if (!defined('LACONICA')) {
exit(1);
}
class WebColor {
// XXX: Maybe make getters and setters for r,g,b values and tuples,
// e.g.: to support this kinda CSS representation: rgb(255,0,0)
// http://www.w3.org/TR/CSS21/syndata.html#color-units
var $red = 0;
var $green = 0;
var $blue = 0;
/**
* Constructor
*
* @return nothing
*/
function __construct($color = null)
{
if (isset($color)) {
$this->parseColor($color);
}
}
/**
* Parses input to and tries to determine whether the color
* is being specified via an integer or hex tuple and sets
* the RGB instance variables accordingly.
*
* XXX: Maybe support (r,g,b) style, and array?
*
* @param mixed $color
*
* @return nothing
*/
function parseColor($color) {
if (is_numeric($color)) {
$this->setIntColor($color);
} else {
// XXX named colors
// XXX: probably should do even more validation
if (preg_match('/(#([0-9A-Fa-f]{3,6})\b)/u', $color) > 0) {
$this->setHexColor($color);
} else {
$errmsg = _('%s is not a valid color!');
throw new WebColorException(sprintf($errmsg, $color));
}
}
}
/**
* @param string $name
*
* @return nothing
*/
function setNamedColor($name)
{
// XXX Implement this
}
/**
* Sets the RGB color values from a a hex tuple
*
* @param string $hexcolor
*
* @return nothing
*/
function setHexColor($hexcolor) {
if ($hexcolor[0] == '#') {
$hexcolor = substr($hexcolor, 1);
}
if (strlen($hexcolor) == 6) {
list($r, $g, $b) = array($hexcolor[0].$hexcolor[1],
$hexcolor[2].$hexcolor[3],
$hexcolor[4].$hexcolor[5]);
} elseif (strlen($hexcolor) == 3) {
list($r, $g, $b) = array($hexcolor[0].$hexcolor[0],
$hexcolor[1].$hexcolor[1],
$hexcolor[2].$hexcolor[2]);
} else {
return false;
}
$this->red = hexdec($r);
$this->green = hexdec($g);
$this->blue = hexdec($b);
}
/**
* Sets the RGB color values from a 24-bit integer
*
* @param int $intcolor
*
* @return nothing
*/
function setIntColor($intcolor)
{
// We could do 32 bit and have an alpha channel because
// Sarven wants one real bad, but nah.
$this->red = $intcolor >> 16;
$this->green = $intcolor >> 8 & 0xFF;
$this->blue = $intcolor & 0xFF;
}
/**
* Returns a hex tuple of the RGB color useful for output in HTML
*
* @return string
*/
function hexValue() {
$hexcolor = (strlen(dechex($this->red)) < 2 ? '0' : '' ) .
dechex($this->red);
$hexcolor .= (strlen(dechex($this->green)) < 2 ? '0' : '') .
dechex($this->green);
$hexcolor .= (strlen(dechex($this->blue)) < 2 ? '0' : '') .
dechex($this->blue);
return $hexcolor;
}
/**
* Returns a 24-bit packed integer representation of the RGB color
* for convenient storage in the DB
*
* XXX: probably could just use hexdec() instead
*
* @return int
*/
function intValue()
{
$intcolor = 256 * 256 * $this->red + 256 * $this->green + $this->blue;
return $intcolor;
}
}
class WebColorException extends Exception
{
}
?>