commerce_omniva.module 5.89 KB
Newer Older
ram4nd's avatar
ram4nd committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

function commerce_omniva_locations() {
  $data = &drupal_static(__FUNCTION__);
  if (!isset($data)) {
    if ($cache = cache_get('commerce_omniva_locations_data')) {
      $data = $cache->data;
    }
    else {

      $request = drupal_http_request('http://www.omniva.ee/locations.csv');
      $rows = explode("\n", str_replace(';NULL', ';', $request->data));
      $data = array();
      $keys = str_getcsv($rows[0], ';');
      foreach ($rows as $key => $row) {
        if ($key === 0 || empty($row)) continue;
        $row_array = str_getcsv($row, ';');
        $data[$row_array[0]] = array_combine($keys, $row_array);
      }

21 22 23
      if (!empty($data)) {
        cache_set('commerce_omniva_locations_data', $data, 'cache');
      }
ram4nd's avatar
ram4nd committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }
  }
  return $data;
}

/***************************************************************
 * Commerce Omniva Field
 ***************************************************************/

/**
 * Implements hook_field_info().
 *
 * Provides the description of the field.
 */
function commerce_omniva_field_info() {
  return array(
    'commerce_omniva_zip' => array(
      'label' => t('List (Omniva delivery)'),
      'description' => t('Omniva terminal and post office locations.'),
      'default_widget' => 'commerce_omniva_select',
      'default_formatter' => 'commerce_omniva_zip',
      'settings' => array(
        'commerce_omniva_zip_country' => 'all',
      ),
    ),
  );
}

/**
 * Implements hook_field_settings_form().
 */
function commerce_omniva_field_settings_form($field, $instance, $has_data) {
  $form = array();
  $settings = &$field['settings'];
  $form['commerce_omniva_zip_country'] = array(
    '#type' => 'select',
    '#options' => commerce_omniva_countries_list(),
    '#default_value' => $settings['commerce_omniva_zip_country'],
    '#title' => t('Country'),
    '#description' => t('Which country would you like to be selected by default.'),
  );
  return $form;
}

/**
 * Implements hook_field_validate().
 *
 * @see field_example_field_widget_error()
 */
function commerce_omniva_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
75
    if (!empty($item['zip'])) {
ram4nd's avatar
ram4nd committed
76
      if (!is_numeric($item['zip'])) {
ram4nd's avatar
ram4nd committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'commerce_omniva_invalid',
          'message' => t('You must select a location.'),
        );
      }
    }
  }
}

/**
 * Implements hook_field_is_empty().
 *
 * hook_field_is_emtpy() is where Drupal asks us if this field is empty.
 * Return TRUE if it does not contain data, FALSE if it does. This lets
 * the form API flag an error when required fields are empty.
 */
function commerce_omniva_field_is_empty($item, $field) {
94
  return empty($item['zip']);
ram4nd's avatar
ram4nd committed
95 96 97 98 99 100
}

/**
 * Implements hook_field_formatter_info().
 *
 * I could define more that one display.
ram4nd's avatar
ram4nd committed
101
 *
ram4nd's avatar
ram4nd committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
 * @see field_example_field_formatter_view()
 */
function commerce_omniva_field_formatter_info() {
  return array(
    'commerce_omniva_zip' => array(
      'label' => t('Select list'),
      'field types' => array('commerce_omniva_zip'),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 *
 * @see field_example_field_formatter_info()
 */
function commerce_omniva_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $place = commerce_omniva_locations();
  $t_name = t('Parcel terminal location');
  switch ($display['type']) {
    case 'commerce_omniva_zip':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#type' => 'html_tag',
          '#tag' => 'p',
ram4nd's avatar
ram4nd committed
128
          '#value' => '<label> ' . $t_name . ':</label> ' . $place[$item['zip']]['NAME'],
ram4nd's avatar
ram4nd committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        );
      }
      break;
  }

  return $element;
}

/**
 * Implements hook_field_widget_info().
 *
 * @see commerce_omniva_field_widget_form()
 */
function commerce_omniva_field_widget_info() {
  return array(
    'commerce_omniva_select' => array(
      'label' => t('Omniva list'),
      'field types' => array('commerce_omniva_zip'),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 *
 * hook_widget_form() is where Drupal tells us to create form elements for
 * our field's widget.
 */
function commerce_omniva_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
158
  $value = isset($items[$delta]['zip']) ? $items[$delta]['zip'] : '';
ram4nd's avatar
ram4nd committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
  $places = commerce_omniva_locations();
  $country = $field['settings']['commerce_omniva_zip_country'];

  $options = array('' => t('Select'));
  if ($country === 'all') {
    foreach ($places as $zip => $place) {
      $options[$place['A0_NAME']][$zip] = $place['NAME'];
    }
  }
  else {
    foreach ($places as $zip => $place) {
      if (drupal_strtolower($place['A0_NAME']) === $country) {
        $options[$place['A1_NAME']][$zip] = $place['NAME'];
      }
    }
  }
  ksort($options);

  $widget = $element;
  $widget['#delta'] = $delta;

  switch ($instance['widget']['type']) {
    case 'commerce_omniva_select':
      $widget += array(
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $value,
      );
      break;
  }

190
  $element['zip'] = $widget;
ram4nd's avatar
ram4nd committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  return $element;
}

/**
 * Implements hook_field_widget_error().
 *
 * hook_field_widget_error() lets us figure out what to do with errors
 * we might have generated in hook_field_validate(). Generally, we'll just
 * call form_error().
 *
 * @see field_example_field_validate()
 * @see form_error()
 */
function commerce_omniva_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'commerce_omniva_invalid':
      form_error($element, $error['message']);
      break;
  }
}

function commerce_omniva_countries_list() {
  $places = commerce_omniva_locations();
  $out = array('all' => t('All'));
  foreach ($places as $zip => $place) {
    $out[drupal_strtolower($place['A0_NAME'])] = $place['A0_NAME'];
  }
  return $out;
}