You are here

class AvailabilityCalendarStylesFormBuilder in Availability Calendars 7.5

Same name and namespace in other branches
  1. 7.3 availability_calendar.styles.inc \AvailabilityCalendarStylesFormBuilder
  2. 7.4 availability_calendar.styles.inc \AvailabilityCalendarStylesFormBuilder

Hierarchy

Expanded class hierarchy of AvailabilityCalendarStylesFormBuilder

File

./availability_calendar.styles.inc, line 91

View source
class AvailabilityCalendarStylesFormBuilder {

  /** @var array */
  protected $form = array();

  /** @var string */
  protected $currentFieldset = '';

  /** @var array */
  protected $styles = NULL;
  public function __construct($styles) {
    $this->styles = $styles;
  }
  public function exec() {
    $this
      ->fieldsetTable();
    $this
      ->fieldsetCaption();
    $this
      ->fieldsetHeader();
    $this
      ->fieldsetWeekNotes();
    $this
      ->fieldsetDays();
    $this
      ->fieldsetStates();

    // Place all fieldsets within another fieldset and add the generate checkbox.
    $this->form = array(
      'availability_calendar_styles_generate' => array(
        '#type' => 'checkbox',
        '#title' => t('Generate the css file.'),
        '#default_value' => $this->styles['generate'],
        '#description' => t("Check whether you want to generate a CSS file based on the styles on this page. If you don't check this, the CSS file won't be generated nor included. As a consequence, you will need to define all styling in your theme."),
      ),
      'availability_calendar_styles' => array(
        '#type' => 'fieldset',
        '#title' => t('CSS Styles'),
        '#tree' => TRUE,
        '#description' => t("Below you can fill in a number of basic styles that define how your calendar will be displayed. If a style is left empty (or @n for selects) that style won't be generated.", array(
          '@n' => '<none>',
        )),
      ) + $this->form,
    );
    return $this->form;
  }

  /**
   * Helper method to create the fieldset for the table styles.
   */
  protected function fieldsetTable() {
    $this->currentFieldset = 'table';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('Table'),
      '#description' => t('Styles that define how the table will be displayed.'),
    );
    $this
      ->selectField('font-size', array(
      'larger',
      'smaller',
      'xx-small',
      'x-small',
      'small',
      'medium',
      'large',
      'x-large',
      'xx-large',
    ));
    $this
      ->colorField('color');
    $this
      ->colorField('background-color');
    $this
      ->lengthField('border-width');
    $this
      ->colorField('border-color');
  }

  /**
   * Helper method to create the fieldset for the caption styles.
   */
  protected function fieldsetCaption() {
    $this->currentFieldset = 'caption';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('Caption'),
      '#description' => t('Styles that define how the name of the month will be displayed.'),
    );
    $this
      ->selectField('text-align', array(
      'left',
      'right',
      'center',
      'inherit',
    ));
    $this
      ->selectField('font-weight', array(
      'normal',
      'bold',
      'bolder',
      'lighter',
      'inherit',
    ));
    $this
      ->selectField('font-style', array(
      'normal',
      'italic',
      'oblique',
      'inherit',
    ));
    $this
      ->selectField('font-size', array(
      'larger',
      'smaller',
      'xx-small',
      'x-small',
      'small',
      'medium',
      'large',
      'x-large',
      'xx-large',
      'inherit',
    ));
  }

  /**
   * Helper method to create the fieldset for the table header styles.
   */
  protected function fieldsetHeader() {
    $this->currentFieldset = 'header';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('Day names'),
      '#description' => t('Styles that define how the names of the weekdays will be displayed.'),
    );
    $this
      ->lengthField('height');
    $this
      ->selectField('text-align', array(
      'left',
      'right',
      'center',
      'inherit',
    ));
    $this
      ->selectField('font-weight', array(
      'normal',
      'bold',
      'bolder',
      'lighter',
      'inherit',
    ));
    $this
      ->selectField('font-style', array(
      'normal',
      'italic',
      'oblique',
      'inherit',
    ));
    $this
      ->selectField('font-size', array(
      'larger',
      'smaller',
      'xx-small',
      'x-small',
      'small',
      'medium',
      'large',
      'x-large',
      'xx-large',
      'inherit',
    ));
  }

  /**
   * Helper method to create the fieldset for the week note styles.
   */
  protected function fieldsetWeekNotes() {
    $this->currentFieldset = 'week_notes';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('Week notes'),
      '#description' => t('Styles that define how a week note cell will be displayed.'),
    );
    $this
      ->lengthField('width');
  }

  /**
   * Helper method to create the fieldset for the day styles.
   */
  protected function fieldsetDays() {
    $this->currentFieldset = 'days';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('Days'),
      '#description' => t('Styles that define how a day cell will be displayed.'),
    );
    $this
      ->lengthField('width');
    $this
      ->lengthField('height');
    $this
      ->selectField('text-align', array(
      'left',
      'right',
      'center',
      'inherit',
    ));
    $this
      ->selectField('vertical-align', array(
      'baseline',
      'sub',
      'super',
      'top',
      'text-top',
      'middle',
      'bottom',
      'text-bottom',
    ));
  }

  /**
   * Helper method to create the fieldset for the states styles.
   */
  protected function fieldsetStates() {
    $this->currentFieldset = 'states';
    $this->form[$this->currentFieldset] = array(
      '#type' => 'fieldset',
      '#title' => t('States'),
      '#description' => t('Styles that define how the states will be displayed.'),
    );
    $this
      ->selectField('split-day', array(
      '/',
      '\\',
      '|',
      '―',
    ));

    // Translate the title 'Split day' but only after creating the form element
    // (otherwise the default value can't be retrieved from the styles array).
    $this->form[$this->currentFieldset]['split-day']['#title'] = t('How to render a split day');
    $states = availability_calendar_get_states();
    foreach ($states as $sid => $state) {
      $this
        ->colorField(array(
        $state['css_class'] => $state['label'],
      ));
    }
  }

  /**
   * Helper method to add a length field to a given fieldset.
   *
   * By extracting this, we get standardized settings for length fields. E.g:
   * for now only pixels are allowed, but this function could add a dropdown for
   * unit selection.
   *
   * @param string|array $cssProperty
   *   The name of the css color property to add. If this is not the same as the
   *   form field name, pass in an array with 1 element:
   *   <field name> => <css property>.
   */
  protected function lengthField($cssProperty) {
    if (is_array($cssProperty)) {
      $fieldName = key($cssProperty);
      $cssProperty = current($cssProperty);
    }
    else {
      $fieldName = $cssProperty;
    }
    $this->form[$this->currentFieldset][$fieldName] = array(
      '#type' => 'textfield',
      '#title' => $cssProperty,
      '#default_value' => $this
        ->getStyle($fieldName),
      '#size' => 8,
      '#maxlength' => 6,
      '#field_suffix' => 'px',
    );
  }

  /**
   * Helper method to add a color field to a given fieldset.
   *
   * By extracting this, we get standardized settings and handling for color
   * fields. E.g: for now only color codes are allowed, but this function could
   * add support for other notations
   * (http://www.w3.org/TR/CSS21/syndata.html#color-units) or a color picker.
   *
   * @param string|array $cssProperty
   *   The name of the css color property to add. If this is not the same as the
   *   form field name, pass in an array with 1 element:
   *   <field name> => <css property>.
   */
  protected function colorField($cssProperty) {
    if (is_array($cssProperty)) {
      $fieldName = key($cssProperty);
      $cssProperty = current($cssProperty);
    }
    else {
      $fieldName = $cssProperty;
    }
    $this->form[$this->currentFieldset][$fieldName] = array(
      '#type' => 'textfield',
      '#title' => $cssProperty,
      '#default_value' => $this
        ->getStyle($fieldName),
      '#size' => 8,
      '#maxlength' => 7,
      '#field_prefix' => '#',
    );
  }

  /**
   * Helper method to add a select field to a given fieldset.
   *
   * @param string|array $cssProperty
   *   The name of the css color property to add. If this is not the same as the
   *   form field name, pass in an array with 1 element:
   *   <field name> => <css property>.
   * @param array $options
   *   The options to present. An option <none> will be added in front of
   *   this list, to allow to select for not setting and thus not generating
   *   this property.
   */
  protected function selectField($cssProperty, $options) {
    if (is_array($cssProperty)) {
      $fieldName = key($cssProperty);
      $cssProperty = current($cssProperty);
    }
    else {
      $fieldName = $cssProperty;
    }

    // We only translate the label, not the key value that will be stored.
    array_unshift($options, '<none>');
    $options = array_combine($options, $options);
    $options['<none>'] = t('<none>');
    $this->form[$this->currentFieldset][$fieldName] = array(
      '#type' => 'select',
      '#title' => $cssProperty,
      '#default_value' => $this
        ->getStyle($fieldName),
      '#options' => $options,
    );
  }

  /**
   * 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.
   *
   * @return string
   *   The style value or the empty string if not set.
   */
  protected function getStyle($name) {
    $category = $this->currentFieldset;
    return isset($this->styles[$category][$name]) ? $this->styles[$category][$name] : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailabilityCalendarStylesFormBuilder::$currentFieldset protected property @var string
AvailabilityCalendarStylesFormBuilder::$form protected property @var array
AvailabilityCalendarStylesFormBuilder::$styles protected property @var array
AvailabilityCalendarStylesFormBuilder::colorField protected function Helper method to add a color field to a given fieldset.
AvailabilityCalendarStylesFormBuilder::exec public function
AvailabilityCalendarStylesFormBuilder::fieldsetCaption protected function Helper method to create the fieldset for the caption styles.
AvailabilityCalendarStylesFormBuilder::fieldsetDays protected function Helper method to create the fieldset for the day styles.
AvailabilityCalendarStylesFormBuilder::fieldsetHeader protected function Helper method to create the fieldset for the table header styles.
AvailabilityCalendarStylesFormBuilder::fieldsetStates protected function Helper method to create the fieldset for the states styles.
AvailabilityCalendarStylesFormBuilder::fieldsetTable protected function Helper method to create the fieldset for the table styles.
AvailabilityCalendarStylesFormBuilder::fieldsetWeekNotes protected function Helper method to create the fieldset for the week note styles.
AvailabilityCalendarStylesFormBuilder::getStyle protected function Helper method to return 1 style setting.
AvailabilityCalendarStylesFormBuilder::lengthField protected function Helper method to add a length field to a given fieldset.
AvailabilityCalendarStylesFormBuilder::selectField protected function Helper method to add a select field to a given fieldset.
AvailabilityCalendarStylesFormBuilder::__construct public function