You are here

class AvailabilityCalendarsStylesFormBuilder in Availability Calendars 6.2

Same name and namespace in other branches
  1. 7.2 availability_calendars.styles.inc \AvailabilityCalendarsStylesFormBuilder

Hierarchy

Expanded class hierarchy of AvailabilityCalendarsStylesFormBuilder

File

./availability_calendars.styles.inc, line 86

View source
class AvailabilityCalendarsStylesFormBuilder {

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

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

  /* @var $styles 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 genereate checkbox
    $this->form = array(
      'availability_calendars_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_calendars_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 return the fieldset for the table styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 return the fieldset for the caption styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 return the fieldset for the table header styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 return the fieldset for the week note styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 return the fieldset for the day styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 return the fieldset for the states styles
   *
   * @return array an array with a number of form elements in a fieldset
   */
  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 (or this value
    // won't be retrieved from the variable if showing this screen in another language).
    $this->form[$this->currentFieldset]['Split day']['#title'] = t('How to render a split day');
    $states = availability_calendars_get_states();
    foreach ($states as $class => $state) {
      $this
        ->colorField(array(
        $state['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 $css_property 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)) {
      list($fieldName, $cssProperty) = each($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 $css_property 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)) {
      list($fieldName, $cssProperty) = each($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 $css_property 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)) {
      list($fieldName, $cssProperty) = each($cssProperty);
    }
    else {
      $fieldName = $cssProperty;
    }
    array_unshift($options, '<none>');
    $options = array_combine($options, $options);
    $this->form[$this->currentFieldset][$fieldName] = array(
      '#type' => 'select',
      '#title' => $cssProperty,
      '#default_value' => $this
        ->getStyle($fieldName),
      '#options' => $options,
    );
  }

  /**
   * Helper method to return 1 style setting, (to not repeat the taking care of not being set,
   * defaults, etc).
   *
   * @param string $name
   */
  protected function getStyle($name) {
    $category = $this->currentFieldset;
    return isset($this->styles[$category][$name]) ? $this->styles[$category][$name] : '';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailabilityCalendarsStylesFormBuilder::$currentFieldset protected property
AvailabilityCalendarsStylesFormBuilder::$form protected property
AvailabilityCalendarsStylesFormBuilder::$styles protected property
AvailabilityCalendarsStylesFormBuilder::colorField protected function 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…
AvailabilityCalendarsStylesFormBuilder::exec public function
AvailabilityCalendarsStylesFormBuilder::fieldsetCaption protected function Helper method to return the fieldset for the caption styles
AvailabilityCalendarsStylesFormBuilder::fieldsetDays protected function Helper method to return the fieldset for the day styles
AvailabilityCalendarsStylesFormBuilder::fieldsetHeader protected function Helper method to return the fieldset for the table header styles
AvailabilityCalendarsStylesFormBuilder::fieldsetStates protected function Helper method to return the fieldset for the states styles
AvailabilityCalendarsStylesFormBuilder::fieldsetTable protected function Helper method to return the fieldset for the table styles
AvailabilityCalendarsStylesFormBuilder::fieldsetWeekNotes protected function Helper method to return the fieldset for the week note styles
AvailabilityCalendarsStylesFormBuilder::getStyle protected function Helper method to return 1 style setting, (to not repeat the taking care of not being set, defaults, etc).
AvailabilityCalendarsStylesFormBuilder::lengthField protected function 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.
AvailabilityCalendarsStylesFormBuilder::selectField protected function Helper method to add a select field to a given fieldset.
AvailabilityCalendarsStylesFormBuilder::__construct public function