From d8a7e05ab190af48330cd95be88472422faf2005 Mon Sep 17 00:00:00 2001 From: kotnik Date: Fri, 1 Jun 2012 15:42:52 +0200 Subject: [PATCH] Issue #1612702 by kotnik: Adds support for default flat rate service. --- commerce_flat_rate.install | 7 +++++ commerce_flat_rate.module | 17 +++++++++++ includes/commerce_flat_rate.admin.inc | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/commerce_flat_rate.install b/commerce_flat_rate.install index b084772..ff94306 100644 --- a/commerce_flat_rate.install +++ b/commerce_flat_rate.install @@ -49,6 +49,13 @@ function commerce_flat_rate_schema() { 'not null' => TRUE, 'default' => 0, ), + 'default_service' => array( + 'description' => 'Boolean indicating whether or not this service should be default.', + 'type' => 'int', + 'size' => 'tiny', + 'not null' => TRUE, + 'default' => 0, + ), 'amount' => array( 'description' => 'The amount of the base rate of the service.', 'type' => 'int', diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 1dd8f6a..2fd4c0f 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -66,6 +66,21 @@ function commerce_flat_rate_menu() { 'weight' => 10, 'file' => 'includes/commerce_flat_rate.admin.inc', ); + + if (!$shipping_service['default_service']) { + $items['admin/commerce/config/shipping/services/' . $service_name_arg . '/default'] = array( + 'title' => 'Set as default', + 'description' => 'Set this flat rate service as default.', + 'page callback' => 'commerce_flat_rate_service_default_page', + 'page arguments' => array($name), + 'access callback' => 'commerce_flat_rate_service_access', + 'access arguments' => array('update'), + 'type' => MENU_LOCAL_TASK, + 'context' => MENU_CONTEXT_INLINE, + 'weight' => 20, + 'file' => 'includes/commerce_flat_rate.admin.inc', + ); + } } return $items; @@ -162,6 +177,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { 'description' => $service['description'], 'shipping_method' => 'flat_rate', 'rules_component' => !empty($service['rules_component']), + 'default_service' => !empty($service['default_service']), 'price_component' => $price_component_type, 'callbacks' => array( 'rate' => 'commerce_flat_rate_service_rate_order', @@ -193,6 +209,7 @@ function commerce_flat_rate_service_new() { 'display_title' => '', 'description' => '', 'rules_component' => TRUE, + 'default_service' => FALSE, 'base_rate' => array( 'amount' => 0, 'currency_code' => commerce_default_currency(), diff --git a/includes/commerce_flat_rate.admin.inc b/includes/commerce_flat_rate.admin.inc index 40f18f2..7ef9681 100644 --- a/includes/commerce_flat_rate.admin.inc +++ b/includes/commerce_flat_rate.admin.inc @@ -288,3 +288,56 @@ function commerce_flat_rate_service_delete_form_submit($form, &$form_state) { function commerce_flat_rate_service_delete_page($name) { return drupal_get_form('commerce_flat_rate_service_delete_form', commerce_shipping_service_load($name)); } + +/** + * Displays the set as default confirmation form for an existing flat rate service. + * + * @param $name + * The machine-name of the flat rate service to delete. + */ +function commerce_flat_rate_service_default_page($name) { + return drupal_get_form('commerce_flat_rate_service_default_form', commerce_shipping_service_load($name)); +} + +/** + * Builds the form for defaulting flat rate service. + */ +function commerce_flat_rate_service_default_form($form, &$form_state, $shipping_service) { + $form_state['shipping_service'] = $shipping_service; + + $form = confirm_form($form, + t('Are you sure you want to set the %title flat rate service as default?', array('%title' => $shipping_service['title'])), + 'admin/commerce/config/shipping/services/flat-rate', + '', + t('Set as default'), + t('Cancel'), + 'confirm' + ); + + return $form; +} + +/** + * Submit callback for commerce_flat_rate_service_delete_form(). + */ +function commerce_flat_rate_service_default_form_submit($form, &$form_state) { + $default_shipping_service = FALSE; + foreach (commerce_shipping_services('flat_rate') as $name => $shipping_service) { + if ($shipping_service['name'] != $form_state['shipping_service']['name']) { + $shipping_service['default_service'] = FALSE; + commerce_flat_rate_service_save($shipping_service, TRUE); + } + else { + $default_shipping_service = $form_state['shipping_service']; + $default_shipping_service['default_service'] = TRUE; + } + } + + if ($default_shipping_service) { + commerce_flat_rate_service_save($default_shipping_service); + drupal_set_message(t('The flat rate service %title has been set as default.', array('%title' => $shipping_service['title']))); + watchdog('commerce_flat_rate', 'Default flat rate service set to %title.', array('%title' => $shipping_service['title']), WATCHDOG_NOTICE); + } + + $form_state['redirect'] = 'admin/commerce/config/shipping/services/flat-rate'; +} -- 1.7.10.2