You are here

addtocalendar.inc in Add To Calendar Button (AddEvent.com) 7.2

Includes settings form and addtocalendar build processor.

File

includes/addtocalendar.inc
View source
<?php

/**
 * @file
 * Includes settings form and addtocalendar build processor.
 */

/**
 * Settings form for widets and field type.
 *
 * @param array $settings
 *   Wudget settings.
 * @param array $field_list
 *   Contains field list.
 *
 * @return array|mixed
 *   Contains form fields.
 */
function addtocalendar_settings_form(array $settings, array $field_list) {
  $settings_form['addtocalendar_show'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Add to Calendar'),
    '#default_value' => isset($settings['addtocalendar_show']) ? $settings['addtocalendar_show'] : '',
  );
  $settings_form['addtocalendar_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add to Calendar Settings'),
    '#states' => array(
      "visible" => array(
        "input[name*='addtocalendar_show']" => array(
          "checked" => TRUE,
        ),
      ),
    ),
  );
  $settings_form['addtocalendar_settings']['display_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Display Text'),
    '#default_value' => isset($settings['addtocalendar_settings']['display_text']) ? $settings['addtocalendar_settings']['display_text'] : t('Add to Calendar'),
  );
  if (isset($settings['widget_setting_form'])) {
    $settings_form['addtocalendar_settings']['disable_text'] = array(
      '#type' => 'textfield',
      '#title' => t('Disable Text'),
      '#default_value' => isset($settings['addtocalendar_settings']['disable_text']) ? $settings['addtocalendar_settings']['disable_text'] : t('Add to Calendar is disabled'),
    );
  }
  $settings_form['addtocalendar_settings']['style'] = array(
    '#type' => 'select',
    '#title' => t('Select Style'),
    '#options' => array(
      0 => t('No Styling'),
      'blue' => t('Blue'),
      'glow_orange' => t('Glow Orange'),
    ),
    '#default_value' => !empty($settings['addtocalendar_settings']['style']) ? $settings['addtocalendar_settings']['style'] : 'blue',
  );
  $field_options = array();
  foreach ($field_list as $id => $field_i) {
    $field_options[$id] = $field_i['label'];
  }
  $field_options = array_merge(array(
    'token' => t('Use Token/Static Content'),
  ), array(
    'title' => t('Title'),
  ), $field_options);
  $info = array(
    'atc_title' => t('Title'),
    'atc_description' => t('Description'),
    'atc_location' => t('Location'),
    'atc_organizer' => t('Organizer'),
    'atc_date_start' => t('Start date'),
    'atc_date_end' => t('End date'),
  );
  foreach ($info as $id => $label) {
    $settings_form['addtocalendar_settings'][$id]['field'] = array(
      '#type' => 'select',
      '#title' => t('@label : field', [
        '@label' => $label,
      ]),
      '#options' => $field_options,
      '#description' => t('Select field to be used as @label for calendar events', array(
        '@label' => $label,
      )),
      '#default_value' => !empty($settings['addtocalendar_settings'][$id]['field']) ? $settings['addtocalendar_settings'][$id]['field'] : '',
    );
    $default_value = $label;
    if ($id == 'atc_date_start' || $id == 'atc_date_end') {
      $default_value = date('Y-m-d H:i:s', time());
    }
    $settings_form['addtocalendar_settings'][$id]['tokenized'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($label . ': Static/Tokenized Content'),
      '#default_value' => !empty($settings['addtocalendar_settings'][$id]['tokenized']) ? $settings['addtocalendar_settings'][$id]['tokenized'] : $default_value,
      '#description' => t('Select field to be used as @label for calendar events', [
        '@label' => $label,
      ]),
    );
  }
  if (!isset($settings['widget_setting_form'])) {
    unset($settings_form['addtocalendar_settings']['atc_date_start']['field']);
    unset($settings_form['addtocalendar_settings']['atc_date_start']['tokenized']);
  }
  $settings_form['addtocalendar_settings']['atc_organizer_email'] = array(
    '#type' => 'textfield',
    '#title' => t('Organizer email: Static/Tokenized Content'),
    '#default_value' => !empty($settings['addtocalendar_settings']['atc_organizer_email']) ? $settings['addtocalendar_settings']['atc_organizer_email'] : variable_get('site_mail', ''),
  );
  $settings_form['addtocalendar_settings']['atc_privacy'] = array(
    '#type' => 'select',
    '#title' => t('Privacy'),
    '#options' => array(
      'public' => t('Public'),
      'private' => t('Private'),
    ),
    '#description' => t('Use public for free access to event information from any places. User private if the event is closed to public access.'),
    '#default_value' => !empty($settings['addtocalendar_settings']['atc_privacy']) ? $settings['addtocalendar_settings']['atc_privacy'] : '',
  );
  $settings_form['addtocalendar_settings']['data_secure'] = array(
    '#type' => 'select',
    '#title' => t('Security level'),
    '#options' => array(
      'auto' => t('Auto'),
      'true' => t('Use https only'),
      'false' => t('Use http only'),
    ),
    '#default_value' => !empty($settings['addtocalendar_settings']['data_secure']) ? $settings['addtocalendar_settings']['data_secure'] : '',
  );
  $settings_form['addtocalendar_settings']['data_calendars'] = array(
    '#type' => 'checkboxes',
    '#options' => array(
      'iCalendar' => t('iCalendar'),
      'Google Calendar' => t('Google Calendar'),
      'Outlook' => t('Outlook'),
      'Outlook Online' => t('Outlook Online'),
      'Yahoo! Calendar' => t('Yahoo! Calendar'),
    ),
    '#default_value' => !empty($settings['addtocalendar_settings']['data_calendars']) ? $settings['addtocalendar_settings']['data_calendars'] : array(),
    '#title' => t('List of calendars to show in button list.'),
  );
  return $settings_form;
}

/**
 * Process setting form fields.
 *
 * @param string $entity_type
 *   Contains Entity type.
 * @param object $entity
 *   Contains entity array.
 * @param array $settings
 *   Conatains settings array.
 * @param array $formatter_settings
 *   Contains formatter settings.
 *
 * @return mixed
 *   Returns build form array.
 */
function addtocalendar_preprocess_setting_fields($entity_type, $entity, array $settings, array $formatter_settings) {
  $build['addtocalendar'] = array();
  if ($formatter_settings['addtocalendar_show']) {
    $timezone = variable_get('date_default_timezone', '');
    $build['addtocalendar_button_text'] = array(
      '#type' => 'html_tag',
      '#tag' => 'a',
      '#value' => $settings['display_text'],
      '#attributes' => array(
        'class' => 'atcb-link',
      ),
    );
    $info = array(
      'atc_title',
      'atc_description',
      'atc_location',
      'atc_organizer',
      'atc_date_start',
      'atc_date_end',
    );

    // Process fields.
    foreach ($info as $value) {
      $class_value = $value;
      if ($settings[$value]['field'] == 'token') {
        $class_value = $settings[$value]['tokenized'];
      }
      elseif ($settings[$value]['field'] == 'title') {
        $class_value = entity_label($entity_type, $entity);
      }
      elseif (isset($entity->{$settings[$value]['field']})) {
        $class_value = isset($entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value']) ? $entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value'] : '';
      }
      if ($value == 'atc_date_start' || $value == 'atc_date_end') {
        $field_info = field_info_field($settings[$value]['field']);
        $class_value = _addtocalendar_convert_to_local_time($settings[$value]['tokenized'], $timezone);
        if (!empty($field_info) && $field_info['type'] == 'datetime') {
          if (isset($entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value2'])) {
            if ($value == 'atc_date_end') {
              $class_value = _addtocalendar_convert_to_local_time($entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value2'], $timezone);
            }
            else {
              $class_value = _addtocalendar_convert_to_local_time($entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value'], $timezone);
            }
          }
          else {
            $class_value = _addtocalendar_convert_to_local_time($entity->{$settings[$value]['field']}[LANGUAGE_NONE][0]['value'], $timezone);
          }
        }
      }
      $build['addtocalendar'][$value] = array(
        '#type' => 'html_tag',
        '#tag' => 'var',
        '#value' => $class_value,
        '#attributes' => array(
          'class' => $value,
        ),
      );
    }
    $build['addtocalendar']['atc_organizer_email'] = array(
      '#type' => 'html_tag',
      '#tag' => 'var',
      '#value' => variable_get('site_mail', ''),
      '#attributes' => array(
        'class' => 'atc_organizer_email',
      ),
    );
    $build['addtocalendar']['atc_timezone'] = array(
      '#type' => 'html_tag',
      '#tag' => 'var',
      '#value' => $timezone,
      '#attributes' => array(
        'class' => 'atc_timezone',
      ),
    );
    $build['addtocalendar']['atc_privacy'] = array(
      '#type' => 'html_tag',
      '#tag' => 'var',
      '#value' => $settings['atc_privacy'],
      '#attributes' => array(
        'class' => 'atc_privacy',
      ),
    );
    $build['addtocalendar'] = array(
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#value' => render($build['addtocalendar_button_text']) . '<var class="atc_event">' . render($build['addtocalendar']) . '</var>',
      '#attributes' => array(
        'class' => array(
          'addtocalendar',
        ),
      ),
    );
    if ($settings['data_calendars']) {
      $value = '';
      foreach ($settings['data_calendars'] as $key => $set) {
        if ($set) {
          $value .= $key . ', ';
        }
      }
      if ($value) {
        $build['addtocalendar']['#attributes']['data-calendars'] = $value;
      }
    }
    $build['addtocalendar']['#attributes']['data-secure'] = $settings['data_secure'];

    // Styling.
    switch ($settings['style']) {
      case 'blue':
        $style['class'] = 'atc-style-blue';
        $build['addtocalendar']['#attached'] = array(
          'css' => array(
            '//addtocalendar.com/atc/1.5/atc-style-blue.css' => array(
              'type' => 'external',
            ),
          ),
        );
        break;
      case 'glow_orange':
        $style['class'] = 'atc-style-glow-orange';
        $build['addtocalendar']['#attached'] = array(
          'css' => array(
            '//addtocalendar.com/atc/1.5/atc-style-glow-orange.css' => array(
              'type' => 'external',
            ),
          ),
        );
        break;
      default:
        $build['addtocalendar']['#attached'] = array(
          'css' => array(
            '//addtocalendar.com/atc/1.5/atc-base.css' => array(
              'type' => 'external',
            ),
          ),
        );
        break;
    }
    $build['addtocalendar']['#attached']['js'] = array(
      'https://addtocalendar.com/atc/1.5/atc.min.js' => array(
        'external',
      ),
    );
    if (!empty($style)) {
      $build['addtocalendar']['#attributes']['class'][] = $style['class'];
    }
  }
  else {
    $disable_text = isset($settings['disable_text']) ? $settings['disable_text'] : '';
    $build['addtocalendar'] = array(
      '#prefix' => '<div class="wrapper">',
      '#suffix' => '<div>' . $disable_text . '</div></div>',
    );
  }
  return $build;
}

Functions

Namesort descending Description
addtocalendar_preprocess_setting_fields Process setting form fields.
addtocalendar_settings_form Settings form for widets and field type.