Commit e9e792d8 authored by ram4nd's avatar ram4nd

Initial commit.

parents
name = Commerce Omniva
description = Provides a shipping method for Omniva.
package = Commerce (shipping)
dependencies[] = commerce_shipping
core = 7.x
\ No newline at end of file
<?php
/**
* Implements hook_field_schema().
*
* Defines the database schema of the field, using the format used by the
* Schema API.
*
* The data we will store here is just one 7-character element, even
* though the widget presents the three portions separately.
*
* All implementations of hook_field_schema() must be in the module's
* .install file.
*
* @see http://drupal.org/node/146939
* @see schemaapiyour
* @see hook_field_schema()
*/
function commerce_omniva_field_schema($field) {
$columns = array(
'zip' => array(
'type' => 'varchar',
'length' => 10,
'not null' => FALSE,
),
);
$indexes = array(
'zip' => array('zip'),
);
return array(
'columns' => $columns,
'indexes' => $indexes,
);
}
<?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;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment