commerce_omniva.module 7.43 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 21 22 23 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 128 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 158 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 190 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
<?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);
      }

      cache_set('commerce_omniva_locations_data', $data, 'cache');
    }
  }
  return $data;
}

/***************************************************************
 * Commerce Shipping
 ***************************************************************/
//
///**
// * Implements hook_commerce_shipping_method_info().
// */
//function commerce_omniva_commerce_shipping_method_info() {
//  $shipping_methods = array();
//
//  $shipping_methods['omniva'] = array(
//    'title' => 'Omniva',
//    'description' => t('Quote rates from Omniva'),
//  );
//
//  return $shipping_methods;
//}
//
///**
// * Implements hook_commerce_shipping_service_info().
// */
//function commerce_omniva_commerce_shipping_service_info() {
//  $shipping_services = array();
//  $locations = commerce_omniva_locations();
//  foreach ($locations as $country_code => $areas) {
//    $shipping_services['omniva-' . drupal_strtolower($country_code)] = array(
//      'title' => 'Omniva ' . $country_code,
//      'description' => 'Omniva ' . $country_code . ' parcel terminals and post offices.',
//      'shipping_method' => 'omniva',
//      'price_component' => 'shipping',
//      'callbacks' => array(
//        'rate' => 'commerce_omniva_service_rate',
//      ),
//    );
//  }
//  return $shipping_services;
//}
//
///**
// * Shipping service callback: returns shipping rates for a given order.
// */
//function commerce_omniva_service_rate($shipping_service, $order) {
//  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
//  return array(
//    'amount' => 0,
//    'currency_code' => $order_wrapper->commerce_order_total->currency_code->value(),
//    'data' => array(),
//  );
//}

/***************************************************************
 * 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) {
    if (!empty($item['ZIP'])) {
      if (! is_numeric($item['ZIP'])) {
        $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) {
  return empty($item['ZIP']);
}

/**
 * Implements hook_field_formatter_info().
 *
 * I could define more that one display.
 * @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',
          '#value' =>
            '<label> ' . $t_name . ':</label> ' .
            $place[$item['ZIP']]['NAME'],
        );
      }
      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) {
  $value = isset($items[$delta]['ZIP']) ? $items[$delta]['ZIP'] : '';
  $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;
  }

  $element['ZIP'] = $widget;
  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;
}