class AvailabilityCalendarsStylesFormValidator in Availability Calendars 6.2
Same name and namespace in other branches
- 7.2 availability_calendars.styles.inc \AvailabilityCalendarsStylesFormValidator
Hierarchy
Expanded class hierarchy of AvailabilityCalendarsStylesFormValidator
File
- ./
availability_calendars.styles.inc, line 322
View source
class AvailabilityCalendarsStylesFormValidator {
/* @var $styles array */
protected $styles = NULL;
/* @var $currentFieldset string */
protected $currentFieldset = '';
public function __construct($styles) {
$this->styles = $styles;
}
/**
* Validates the styles settings.
* Form errors are set on error.
*/
public function exec() {
$this
->fieldsetTable();
$this
->fieldsetCaption();
$this
->fieldsetHeader();
$this
->fieldsetWeekNotes();
$this
->fieldsetDays();
$this
->fieldsetStates();
}
protected function fieldsetTable() {
$this->currentFieldset = 'table';
$this
->colorField('color');
$this
->colorField('background-color');
$this
->lengthField('border-width');
$this
->colorField('border-color');
}
protected function fieldsetCaption() {
$this->currentFieldset = 'caption';
}
protected function fieldsetHeader() {
$this->currentFieldset = 'header';
$this
->lengthField('height');
}
protected function fieldsetWeekNotes() {
$this->currentFieldset = 'week_notes';
$this
->lengthField('width');
}
protected function fieldsetDays() {
$this->currentFieldset = 'days';
$this
->lengthField('width');
$this
->lengthField('height');
}
protected function fieldsetStates() {
$this->currentFieldset = 'states';
if ($this
->getStyle('split-day') != '') {
// We need cell width and height for split days
if ($this
->getStyle('width', 'days') != '') {
form_set_error('days][width', t('The day cell width is needed to generate styles for split days.'));
}
if ($this
->getStyle('height', 'days') != '') {
form_set_error('days][height', t('The day cell height is needed to generate styles for split days.'));
}
}
$states = availability_calendars_get_states();
foreach ($states as $class => $state) {
$this
->colorField($state['class']);
}
}
protected function lengthField($name) {
$value = $this
->getStyle($name);
if (!empty($value) && !$this
->validateLengthValue($value)) {
form_set_error($this->currentFieldset . '][' . $name, t('Not a valid width or height specification.'));
}
}
protected function colorField($name) {
$value = $this
->getStyle($name);
if (!empty($value) && !$this
->validateColorValue($value)) {
form_set_error($this->currentFieldset . '][' . $name, t('Not a valid color code.'));
}
}
/**
* Check for a CSS length declaration:
* We check for a part of http://www.w3.org/TR/CSS21/syndata.html#numbers
* and http://www.w3.org/TR/CSS21/syndata.html#length-units:
* - only non-signed pixel values are allowed
*
* @param string $value
* @return boolean
*/
protected function validateLengthValue($value) {
$pattern = '/^(\\d*\\.)?\\d+(px)?$/';
return preg_match($pattern, $value) === 1;
}
/**
* Check for a CSS color declaration:
* We check for a part of http://www.w3.org/TR/CSS21/syndata.html#color-units
* - only hex code formats are allowed
*
* @param string $value
* @return boolean
*/
protected function validateColorValue($value) {
$pattern = '/^#?[0-9a-fA-F]{3}[0-9a-fA-F]{3}?$/';
return preg_match($pattern, $value) === 1;
}
/**
* Helper method to return 1 style setting, (to not repeat the taking care of not being set,
* defaults, etc).
*
* @param string $name
* @param string $category the name of the category, NULL for the current fieldset
*/
protected function getStyle($name, $category = NULL) {
if ($category === NULL) {
$category = $this->currentFieldset;
}
return isset($this->styles[$category][$name]) ? $this->styles[$category][$name] : '';
}
}