class AvailabilityCalendarStylesFormValidator in Availability Calendars 7.4
Same name and namespace in other branches
- 7.5 availability_calendar.styles.inc \AvailabilityCalendarStylesFormValidator
- 7.3 availability_calendar.styles.inc \AvailabilityCalendarStylesFormValidator
Hierarchy
Expanded class hierarchy of AvailabilityCalendarStylesFormValidator
File
- ./
availability_calendar.styles.inc, line 352
View source
class AvailabilityCalendarStylesFormValidator {
/** @var array */
protected $styles = NULL;
/** @var 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_calendar_get_states();
foreach ($states as $sid => $state) {
$this
->colorField($state['css_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.
*
* This prevents repeating taking care of values not being set, defaults, etc.
*
* @param string $name
* The style setting to retrieve.
* @param string|null $category
* The name of the category, null for the current fieldset.
*
* @return string
* The style value or the empty string if not set.
*/
protected function getStyle($name, $category = NULL) {
if ($category === NULL) {
$category = $this->currentFieldset;
}
return isset($this->styles[$category][$name]) ? $this->styles[$category][$name] : '';
}
}