From 9b077ad248fabb89b62406915fccc8d062d011a3 Mon Sep 17 00:00:00 2001 From: Ajay Solutions Date: Wed, 29 Feb 2012 22:33:30 +0000 Subject: [PATCH] Added ability to re-order flat rate services by weight. Signed-off-by: Ajay Solutions --- commerce_flat_rate.install | 9 +++ commerce_flat_rate.module | 64 +++++++++++++++++- includes/commerce_flat_rate.admin.inc | 119 +++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+), 1 deletions(-) diff --git a/commerce_flat_rate.install b/commerce_flat_rate.install index b084772..9ab14ac 100644 --- a/commerce_flat_rate.install +++ b/commerce_flat_rate.install @@ -73,3 +73,12 @@ function commerce_flat_rate_schema() { return $schema; } + + +/** + * Implements hook_update() + */ +function commerce_flat_rate_update_7100() { + // add the weight column to the table + db_add_field('commerce_flat_rate_service', 'weight', array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'initial' => 0, 'description' => 'Weight column.')); +} \ No newline at end of file diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 1dd8f6a..168d5b4 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -67,7 +67,7 @@ function commerce_flat_rate_menu() { 'file' => 'includes/commerce_flat_rate.admin.inc', ); } - + return $items; } @@ -168,6 +168,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { ), 'base_rate' => $base_rate, 'data' => $data, + 'weight' => $service['weight'], ); } } @@ -200,6 +201,7 @@ function commerce_flat_rate_service_new() { ), 'data' => array(), 'is_new' => TRUE, + 'weight' => 0, ); } @@ -282,3 +284,63 @@ function commerce_flat_rate_service_delete($name, $skip_reset = FALSE) { // Notify other modules that this flat rate service has been deleted. module_invoke_all('commerce_flat_rate_service_delete', $shipping_service, $skip_reset); } + +/** + * Implements hook_theme(). + */ +function commerce_flat_rate_theme() { + return array( + 'commerce_flat_rate_services_form' => array( + 'render element' => 'form', + 'file' => 'includes/commerce_flat_rate.admin.inc', + ) + ); +} + +/** + * Implements hook_menu_alter() + */ +function commerce_flat_rate_menu_alter(&$items) { + // redefine service menu links so we can process the callbacks instead of the shipping module + // as the shipping module only outputs a static table, and we require a form + $shipping_methods = commerce_shipping_methods(); + if (!empty($shipping_methods)) { + reset($shipping_methods); + $default_method = key($shipping_methods); + } + else { + $default_method = NULL; + } + + if ($default_method == 'flat_rate') { + $items['admin/commerce/config/shipping/services'] = array( + 'title' => 'Shipping services', + 'description' => 'Manage shipping services.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('commerce_flat_rate_services_form', 'flat_rate'), + 'access arguments' => array('administer shipping'), + 'weight' => 5, + 'type' => MENU_LOCAL_TASK, + 'file' => 'includes/commerce_flat_rate.admin.inc', + 'file path' => drupal_get_path('module', 'commerce_flat_rate'), + ); + } + + foreach ($shipping_methods as $method_name => $shipping_method) { + // Convert underscores to hyphens for the menu item argument. + $method_name_arg = strtr($method_name, '_', '-'); + + if ($method_name_arg == 'flat-rate') { + $items['admin/commerce/config/shipping/services/' . $method_name_arg] = array( + 'title' => $shipping_method['title'], + 'description' => 'Manage shipping services for this shipping method.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('commerce_flat_rate_services_form', 'flat_rate'), + 'access arguments' => array('administer shipping'), + 'type' => $default_method == $method_name ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, + 'file' => 'includes/commerce_flat_rate.admin.inc', + 'file path' => drupal_get_path('module', 'commerce_flat_rate'), + ); + } + } +} \ No newline at end of file diff --git a/includes/commerce_flat_rate.admin.inc b/includes/commerce_flat_rate.admin.inc index 40f18f2..78c015a 100644 --- a/includes/commerce_flat_rate.admin.inc +++ b/includes/commerce_flat_rate.admin.inc @@ -288,3 +288,122 @@ 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)); } + +/** + * Returns a form for displaying a shipping services table + */ +function commerce_flat_rate_services_form($form, &$form_state, $method) { + $form['my_items']['#tree'] = TRUE; + $items = commerce_shipping_services($method); + uasort($items, 'drupal_sort_weight'); + + // Loop through all of the items to include in the overview. + foreach ($items as $name => $item) { + if ($item['admin_list']) { + // Build the operation links for the current item. + $arg = strtr($method . '-' . $name, '_', '-'); + $links = menu_contextual_links('commerce-shipping-services', 'admin/commerce/config/shipping/services', array($arg)); + + $form['my_items'][$name] = array( + 'title' => array( + '#type' => 'markup', + '#markup' => theme('shipping_service_admin_overview', array('shipping_service' => $item)), + ), + 'operations' => array( + '#type' => 'markup', + '#markup' => theme('links', array('links' => $links, 'attributes' => array('class' => 'links inline operations'))), + ), + // the "weight" field will be manipulated by the drag and drop table + 'weight' => array( + '#type' => 'weight', + '#delta' => 10, + '#default_value' => $item['weight'], + '#attributes' => array('class' => array('weight')), + ), + ); + } + } + + // Add a standard empty row text to the form, so we can just display it in the theme. + $empty_text = t('There are no services defined for the @title shipping method.', array('@title' => commerce_shipping_method_get_title($method))); + + $form['empty'] = array( + '#type' => 'hidden', + '#value' => $empty_text, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save Changes'), + ); + + return $form; +} + +/** + * Theme function to reprocess the form ready to add drag and drop weight functionality + */ +function theme_commerce_flat_rate_services_form($variables) { + $form = $variables['form']; + + $output = ''; + $header = array( + t('Title'), + t('Operations'), + t('Weight'), + ); + + $table_rows = array(); + //loop through each "row" in the table array + foreach ($form['my_items'] as $name => $row) { + // this array will hold the table cells for a row + $this_row = array(); + + // this ensures we only process form items that we added and nothing that drupal has added since + if (substr($name, 0, 1) == '#') { + continue; + } + + // first, add the "name" markup + $this_row[] = drupal_render($row['title']); + + // then add the "operations" markup + $this_row[] = drupal_render($row['operations']); + + // Add the weight field to the row + // the Javascript to make our table drag and drop will end up hiding this cell + $this_row[] = drupal_render($row['weight']); + + //Add the row to the array of rows + $table_rows[] = array('data' => $this_row, 'class'=>array('draggable')); + } + + $table_id = 'services_table'; + + // add JS for table dragging stuff + drupal_add_tabledrag($table_id, 'order', 'sibling', 'weight'); + + // over-write the 'my_items' form element with the markup generated from our table + $form['my_items'] = array( + '#type' => 'markup', + '#markup' => theme('table', array('header' => $header, 'rows' => $table_rows, 'sticky' => TRUE, 'empty' => $form['empty'], 'attributes' => array('id' => $table_id))), + '#weight' => '1', + ); + + // unset the form #theme element to avoid recursion + unset($form['#theme']); + // render the form + $output = drupal_render($form); + + return $output; +} + +/** + * Submit handler for the services form, to save the new weight to the database + */ +function commerce_flat_rate_services_form_submit($form, &$form_state) { + // iterate through the submitted values and store the weights in the db + foreach ($form_state['values']['my_items'] as $name => $item) { + db_query("UPDATE {commerce_flat_rate_service} SET weight=:weight WHERE name=:name", array(':weight' => $item['weight'], ':name' => $name)); + } +} \ No newline at end of file -- 1.7.9.msysgit.0