Commit 9f25c5b8 authored by ram4nd's avatar ram4nd

Init branch with startup of some functionality. Not usable yet.

parent 1a8d5199
<?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()
* @file Installs neccessary fields.
*/
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,
);
/**
* Implements hook_install().
*/
function commerce_omniva_install() {
// Create the field which asks customer's phone number.
$f_name = 'commerce_omniva_zip';
if (!field_info_field($f_name)) {
$field = array(
'field_name' => $f_name,
'type' => 'text',
);
field_create_field($field);
$instance = array(
'field_name' => $f_name,
'entity_type' => 'commerce_customer_profile',
'bundle' => 'shipping',
'label' => 'Omniva terminal',
'required' => TRUE,
);
field_create_instance($instance);
}
}
<?php
/**
* @file Implements the Omniva package terminal system into Drupal Commerce.
*/
/**
* Get Omniva package automate locations.
*/
function commerce_omniva_locations() {
$data = &drupal_static(__FUNCTION__);
if (!isset($data)) {
......@@ -12,7 +19,7 @@ function commerce_omniva_locations() {
$rows = explode("\n", str_replace(';NULL', ';', $request->data));
$data = array();
$keys = str_getcsv($rows[0], ';');
foreach ($rows as $key => $row) {
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);
......@@ -24,244 +31,250 @@ function commerce_omniva_locations() {
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
***************************************************************/
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;
}
/**
* Implements hook_field_info().
*
* Provides the description of the field.
* Implements hook_menu().
*/
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',
),
),
function commerce_omniva_menu() {
$items = array();
$items['admin/commerce/config/advanced-settings/commerce_omniva'] = array(
'title' => 'Commerce Omniva settings',
'description' => 'Settings for the Commerce Omniva module',
'page callback' => 'drupal_get_form',
'page arguments' => array('commerce_omniva_admin'),
'access arguments' => array('administer shipping'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_field_settings_form().
* Creates the configuration page.
*/
function commerce_omniva_field_settings_form($field, $instance, $has_data) {
function commerce_omniva_admin() {
$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.'),
$form['commerce_omniva_shipping_cost'] = array(
'#type' => 'textfield',
'#title' => t('Shipping cost'),
'#default_value' => variable_get('commerce_omniva_shipping_cost', 10),
'#size' => 6,
'#maxlength' => 6,
'#description' => t('Set the shipping cost.'),
'#required' => TRUE,
);
$form['commerce_omniva_shipping_currency'] = array(
'#type' => 'textfield',
'#title' => t('Shipping currency'),
'#default_value' => variable_get('commerce_omniva_shipping_currency', 'EUR'),
'#size' => 3,
'#maxlength' => 3,
'#description' => t('Set the currency.'),
'#required' => TRUE,
);
return $form;
return system_settings_form($form);
}
/**
* Implements hook_field_validate().
*
* @see field_example_field_widget_error()
* Implements hook_admin_validate().
*/
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.'),
);
}
}
function commerce_omniva_admin_validate($form, &$form_state) {
// Validate shipping cost.
$cost = $form_state['values']['commerce_omniva_shipping_cost'];
if (!is_numeric($cost)) {
form_set_error('commerce_omniva_shipping_cost', t('Shipping cost must be numeric.'));
}
}
/**
* 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']);
// Valdiate shipping currency.
$cur = $form_state['values']['commerce_omniva_shipping_currency'];
if (!array_key_exists($cur, commerce_currencies())) {
$err = $cur . ' is not a valid country code.';
form_set_error('commerce_omniva_shipping_currency', t($err));
}
}
/***************************************************************
* Commerce Shipping
***************************************************************/
/**
* Implements hook_field_formatter_info().
*
* I could define more that one display.
*
* @see field_example_field_formatter_view()
* Implements hook_commerce_shipping_method_info().
*/
function commerce_omniva_field_formatter_info() {
return array(
'commerce_omniva_zip' => array(
'label' => t('Select list'),
'field types' => array('commerce_omniva_zip'),
),
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_field_formatter_view().
*
* @see field_example_field_formatter_info()
* Implements hook_commerce_shipping_service_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;
function commerce_omniva_commerce_shipping_service_info() {
$shipping_services = array();
foreach (commerce_omniva_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',
'details_form_submit' => 'commerce_omniva_service_submit',
),
);
}
return $element;
return $shipping_services;
}
/**
* Implements hook_field_widget_info().
*
* @see commerce_omniva_field_widget_form()
* Shipping service callback: returns shipping rates for a given order.
*/
function commerce_omniva_field_widget_info() {
function commerce_omniva_service_rate($shipping_service, $order) {
$amount = variable_get('commerce_omniva_shipping_cost', 3);
$cc = variable_get('commerce_omniva_shipping_currency', 'EUR');
return array(
'commerce_omniva_select' => array(
'label' => t('Omniva list'),
'field types' => array('commerce_omniva_zip'),
),
);
'amount' => $amount * 100,
'currency_code' => $cc,
'data' => array());
}
/**
* Implements hook_field_widget_form().
*
* hook_widget_form() is where Drupal tells us to create form elements for
* our field's widget.
* Implements hook_commerce_shipping_service_rate_options_alter().
*/
function commerce_omniva_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['zip']) ? $items[$delta]['zip'] : '';
function commerce_omniva_commerce_shipping_service_rate_options_alter(&$options, $order) {
$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;
foreach ($places as $zip => &$place) {
$options['omniva_' . $place['A0_NAME'] . '_' . $zip] = $place['NAME'];
}
$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()
* @todo Save the right value
*/
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_service_submit($details_form, $details_values, $line_item) {
//$s_service = $line_item->data['shipping_service']['name'];
//$id = substr($s_service, -1);
//
//$order_wrapper = entity_metadata_wrapper("commerce_order", $line_item->order_id);
//$post_code = $order_wrapper->commerce_customer_shipping->commerce_customer_address->value()['postal_code'];
//$data = json_decode(drupal_http_request("https://ohjelmat.posti.fi/pup/v1/pickuppoints?zipcode=$post_code")->data)[0];
//$longitude = $data->MapLongitude;
//$latitude = $data->MapLatitude;
//$number_of_locations = variable_get('commerce_finnish_smartpost_maxposts', 5);
//$data2 = json_decode(drupal_http_request("https://ohjelmat.posti.fi/pup/v1/pickuppoints?type=smartpost&longitude=$longitude&latitude=$latitude&top=$number_of_locations")->data);
//$post_office = $data2[$id];
//
//$shipping = $order_wrapper->commerce_customer_shipping->commerce_customer_address->value();
//$name = $shipping['name_line'];
//$phone_number = $order_wrapper->commerce_customer_shipping->commerce_smartpost_phonenumber->value();
//$smartpost_address = $name . "\n" . $post_office->LabelName . "\n" . $post_office->Address . "\n" . $phone_number;
//
//$order_wrapper->commerce_smartpost_address = $smartpost_address;
//$order_wrapper->save();
}
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;
}
/***************************************************************
* Commerce Omniva Field
***************************************************************/
//
///**
// * 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_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_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;
//}
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