You are here

class AvailabilityCalendarStylesFormValidator in Availability Calendars 7.3

Same name and namespace in other branches
  1. 7.5 availability_calendar.styles.inc \AvailabilityCalendarStylesFormValidator
  2. 7.4 availability_calendar.styles.inc \AvailabilityCalendarStylesFormValidator

Hierarchy

Expanded class hierarchy of AvailabilityCalendarStylesFormValidator

File

./availability_calendar.styles.inc, line 332

View source
class AvailabilityCalendarStylesFormValidator {

  /* @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_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, (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] : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailabilityCalendarStylesFormValidator::$currentFieldset protected property
AvailabilityCalendarStylesFormValidator::$styles protected property
AvailabilityCalendarStylesFormValidator::colorField protected function
AvailabilityCalendarStylesFormValidator::exec public function Validates the styles settings. Form errors are set on error.
AvailabilityCalendarStylesFormValidator::fieldsetCaption protected function
AvailabilityCalendarStylesFormValidator::fieldsetDays protected function
AvailabilityCalendarStylesFormValidator::fieldsetHeader protected function
AvailabilityCalendarStylesFormValidator::fieldsetStates protected function
AvailabilityCalendarStylesFormValidator::fieldsetTable protected function
AvailabilityCalendarStylesFormValidator::fieldsetWeekNotes protected function
AvailabilityCalendarStylesFormValidator::getStyle protected function Helper method to return 1 style setting, (to not repeat the taking care of not being set, defaults, etc).
AvailabilityCalendarStylesFormValidator::lengthField protected function
AvailabilityCalendarStylesFormValidator::validateColorValue protected function Check for a CSS color declaration: We check for a part of http://www.w3.org/TR/CSS21/syndata.html#color-units
AvailabilityCalendarStylesFormValidator::validateLengthValue protected function 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:
AvailabilityCalendarStylesFormValidator::__construct public function