--- uc_tablequote.module 2009-03-31 19:42:02.000000000 +0100 +++ uc_tablequote.fix.module 2010-03-15 12:36:37.140625000 +0000 @@ -9,15 +9,141 @@ function uc_tablequote_menu(){ $items['admin/store/settings/quotes/methods/tablequote'] = array( 'title' => 'Weight/Order quote', - 'page callback' => 'drupal_get_form', + 'page callback' => 'tablequote_method_list', 'page arguments' => array('uc_tablequote_admin_settings'), 'access arguments' => array('configure quotes'), 'type' => MENU_LOCAL_TASK, ); - + $items['admin/store/settings/quotes/methods/tablequote/edit_table'] = array( + 'title' => 'Edit Weight/Order Quote', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('uc_tablequote_edit_table'), + 'access arguments' => array('configure quotes'), + 'type' => MENU_LOCAL_TASK + ); + $items['admin/store/settings/quotes/methods/tablequote/add'] = array( + 'title' => 'Add Weight/Order Quote', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('uc_tablequote_edit'), + 'access arguments' => array('configure quotes'), + 'type' => MENU_LOCAL_TASK + ); + $items['admin/store/settings/quotes/methods/tablequote/edit'] = array( + 'title' => 'Edit Weight/Order Quote', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('uc_tablequote_edit'), + 'access arguments' => array('configure quotes'), + 'type' => MENU_LOCAL_TASK + ); return $items; } +function tablequote_method_list() { + $result = db_query('SELECT * FROM {uc_tablequote_methods} ORDER BY weight ASC'); + + $header = array( + t('Title'), + t('Label'), + t('Mode'), + t('Weight'), + array('data' => t('Operations'), 'colspan' => 3), + ); + + $rows = array(); + while($row = db_fetch_object($result)) { + $rows[] = array( + l($row->title, 'admin/store/settings/quotes/methods/tablequote/edit/' . $row->qmid), + l($row->label, 'admin/store/settings/quotes/methods/tablequote/edit/' . $row->qmid), + $row->quote_mode, + $row->weight, + l(t('edit'), 'admin/store/settings/quotes/methods/tablequote/edit/' . $row->qmid), + l(t('edit table'), 'admin/store/settings/quotes/methods/tablequote/edit_table/' . $row->qmid), + l(t('conditions'), CA_UI_PATH .'/uc_tablequote_get_quote_' . $row->qmid .'/edit/conditions') + ); + } + + $rows[] = array('', '', '', '', '', '', l('add new method', 'admin/store/settings/quotes/methods/tablequote/add')); + + return theme('table', $header, $rows); +} + +function uc_tablequote_edit() { + $qmid = arg(7); + $method = db_fetch_object(db_query('SELECT * FROM {uc_tablequote_methods} WHERE qmid = %d', $qmid)); + + $form['title'] = array( + '#type' => 'textfield', + '#default_value' => isset($method) ? $method->title : '', + '#title' => t('Title'), + ); + + $form['label'] = array( + '#type' => 'textfield', + '#default_value' => isset($method) ? $method->label : '', + '#title' => t('Label'), + '#description' => t('The label is what is shown to the user.') + ); + + $form['weight'] = array( + '#type' => 'textfield', + '#default_value' => isset($method) ? $method->weight : '', + '#length' => 2, + '#title' => t('Weight'), + ); + + $form['quote_mode'] = array( + '#type' => 'select', + '#title' => t('Type of calculation'), + '#description' => t('Choose whether shipping costs should be calculated on the total weight or order total.'), + '#default_value' => isset($method) ? $method->quote_mode : '', + '#options' => array( + 'weight' => t('Weight'), + 'order' => t('Total order'), + ), + ); + + if(isset($qmid)) { + $form['qmid'] = array( + '#type' => 'hidden', + '#value' => $qmid + ); + } + + $form['buttons']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save Method') + ); + + $form['buttons']['delete'] = array( + '#type' => 'submit', + '#value' => t('Delete Method'), + '#submit' => array('uc_tablequote_method_delete'), + ); + + return $form; +} + +function uc_tablequote_edit_submit($form, &$form_state) { + if(isset($form_state['values']['qmid'])) { + db_query('UPDATE {uc_tablequote_methods} SET title = "%s", label = "%s", weight = %d, quote_mode = "%s" WHERE qmid = %d', $form_state['values']['title'], $form_state['values']['label'], $form_state['values']['weight'], $form_state['values']['quote_mode'], $form_state['values']['qmid']); + } else { + db_query('INSERT INTO {uc_tablequote_methods} (title, label, weight, quote_mode) VALUES ("%s", "%s", %d, "%s")', $form_state['values']['title'], $form_state['values']['label'], $form_state['values']['weight'], $form_state['values']['quote_mode']); + } + + drupal_set_message(t('Successfully updated shipping method!'), 'message'); + drupal_goto('admin/store/settings/quotes/methods/tablequote'); +} + +function uc_tablequote_method_delete($form, &$form_state) { + if(isset($form_state['values']['qmid'])) { + db_query('DELETE FROM {uc_tablequote} WHERE qmid = %d', $form_state['values']['qmid']); + db_query('DELETE FROM {uc_tablequote_methods} WHERE qmid = %d', $form_state['values']['qmid']); + + drupal_set_message(t('Successfully updated shipping method!'), 'message'); + } + drupal_goto('admin/store/settings/quotes/methods/tablequote'); +} + /** * Implementation of Übercart's hook_shipping_method(). */ @@ -27,20 +153,22 @@ function uc_tablequote_shipping_method() //$enabled = variable_get('uc_quote_enabled', array('tablequote' => true)); $enabled = variable_get('uc_quote_enabled', array()); $weight = variable_get('uc_quote_method_weight', array('tablequote' => 0)); - $methods['tablequote'] = array( - 'id' => 'tablequote', - 'module' => 'uc_tablequote', - 'title' => t('Wieght/order quote shipping'), - 'enabled' => $enabled['tablequote'], - 'quote' => array( - 'type' => 'order', - 'callback' => 'uc_tablequote_quote', - 'accessorials' => array( - t('Shipping cost'), - ), - ), - 'weight' => $weight['tablequote'], - ); + $result = db_query('SELECT * FROM {uc_tablequote_methods} ORDER BY weight ASC'); while($method = db_fetch_object($result)) { + $methods['tablequote_' . $method->qmid] = array( + 'id' => 'tablequote_' . $method->qmid, + 'module' => 'uc_tablequote', + 'title' => $method->title, + 'enabled' => $enabled['tablequote_' . $method->qmid], + 'quote' => array( + 'type' => 'order', + 'callback' => 'uc_tablequote_quote', + 'accessorials' => array( + $method->label, + ), + ), + 'weight' => $weight['tablequote_' . $method->qmid], + ); + } return $methods; } @@ -54,37 +182,40 @@ function uc_tablequote_shipping_method() function uc_tablequote_ca_predicate() { $enabled = variable_get('uc_quote_enabled', array()); - $predicates = array( - 'uc_tablequote_get_quote' => array( - '#title' => t('Shipping quote via weight/order'), - '#trigger' => 'get_quote_from_tablequote', + $result = db_query('SELECT qmid, title FROM {uc_tablequote_methods} ORDER BY weight ASC'); + + while($method = db_fetch_object($result)) { + $predicates['uc_tablequote_get_quote_' . $method->qmid] = array( + '#title' => t('Shipping quote via @title', array('@title' => $method->title)), + '#trigger' => 'get_quote_from_tablequote_' . $method->qmid, '#class' => 'uc_tablequote', - '#status' => $enabled['tablequote'], + '#status' => $enabled['tablequote_' . $method->qmid], '#actions' => array( array( '#name' => 'uc_quote_action_get_quote', - '#title' => t('Fetch a weight/order shipping quote'), + '#title' => t('Fetch a @title shipping quote', array('@title' => $method->title)), '#argument_map' => array( 'order' => 'order', 'method' => 'method', ), ), ), - ), - ); - + ); + } + return $predicates; } /** * Configures the store shipping rates */ -function uc_tablequote_admin_settings(){ +function uc_tablequote_edit_table() { $form = array(); $form['rates'] = array( '#tree' => true ); - $result = db_query("SELECT * FROM {uc_tablequote}"); + $qmid = arg(7); + $result = db_query("SELECT * FROM {uc_tablequote} WHERE qmid = %d ORDER BY min ASC", $qmid); while ($r = db_fetch_object($result)) { $qid = $r->qid; $form['rates'][$qid]['delete'] = array( @@ -117,34 +248,18 @@ function uc_tablequote_admin_settings(){ '#default_value' => variable_get('uc_tablequote_rate', 0), '#size' => 12, ); - $result = db_query("SELECT type FROM {uc_tablequote} tq"); - $num_rows = db_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq')); - if ($num_rows == 0) { - $form['uc_tablequote']['uc_tablequote_type'] = array( - '#type' => 'select', - '#title' => t('Type of calculation'), - '#description' => t('Choose whether shipping costs should be calculated on the total weight or total order.'), - '#default_value' => variable_get('uc_tablequote_type', 'weight'), - '#options' => array( - 'weight' => t('Weight'), - 'order' => t('Total order'), - ), - ); - } - else { - while ($row = db_fetch_object($result)) { - variable_set('uc_tablequote_type',$row->type); - } - $form['uc_tablequote']['uc_tablequote_type'] = array( - '#value' => t('

Type of calculation: ').variable_get('uc_tablequote_type','weight'), - ); - } + $form['qmid'] = array( + '#type' => 'hidden', + '#value' => $qmid, + ); + $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Submit Changes') ); + $form['buttons']['finished'] = array('#type' => 'markup', '#value' => l('Return to methods', 'admin/store/settings/quotes/methods/tablequote')); return $form; } -function uc_tablequote_admin_settings_validate($form, &$form_state){ +function uc_tablequote_edit_table_validate($form, &$form_state){ // check if everything is a number greater than or equal to 0 if ($form_state['values']['uc_tablequote_minimum'] < 0 || !is_numeric($form_state['values']['uc_tablequote_minimum'])) { form_set_error('uc_tablequote_minimum', t('The minimum value must be a number not less than 0.')); @@ -164,7 +279,7 @@ function uc_tablequote_admin_settings_va if ($form_state['values']['uc_tablequote_maximum'] > 0) { //check to make sure the new settings dont overlap any existing ranges - $result = db_query("SELECT * FROM {uc_tablequote} ORDER BY min"); + $result = db_query("SELECT * FROM {uc_tablequote} WHERE qmid = %d ORDER BY min", $form_state['values']['qmid']); while ($r = db_fetch_object($result)) { if($form_state['values']['uc_tablequote_minimum'] <= $r->max && $form_state['values']['uc_tablequote_minimum'] >= $r->min) { form_set_error('uc_tablequote_minimum', t('The minimum value conflicts with an existing Range.')); @@ -176,7 +291,7 @@ function uc_tablequote_admin_settings_va } } -function uc_tablequote_admin_settings_submit($form, &$form_state){ +function uc_tablequote_edit_table_submit($form, &$form_state){ // check for and handle any deletions if(is_array($form_state['values']['rates'])) { foreach ($form_state['values']['rates'] as $qid => $value) @@ -184,21 +299,13 @@ function uc_tablequote_admin_settings_su db_query("DELETE FROM {uc_tablequote} WHERE qid = %d", $qid); } } - + + $method = db_fetch_object(db_query('SELECT * FROM {uc_tablequote_methods} WHERE qmid = %d', $form_state['values']['qmid'])); // check for and insert new rate quote - if ($form_state['values']['uc_tablequote_maximum'] > $form_state['values']['uc_tablequote_minimum'] && $form_state['values']['uc_tablequote_maximum'] > 0){ - //variable_set('uc_tablequote_type',$form_state['values']['uc_tablequote_type']); - $result = db_query("SELECT type FROM {uc_tablequote} tq"); - $num_rows = db_result(db_query('SELECT COUNT(type) FROM {uc_tablequote} tq')); - if ($num_rows == 0) { - db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (%f, %f, %f, '%s')", $form_state['values']['uc_tablequote_minimum'], $form_state['values']['uc_tablequote_maximum'], $form_state['values']['uc_tablequote_rate'], $form_state['values']['uc_tablequote_type']); - variable_set('uc_tablequote_type',$form_state['values']['uc_tablequote_type']); - } - else { - db_query("INSERT INTO {uc_tablequote} (min, max, rate, type) VALUES (%f, %f, %f, '%s')", $form_state['values']['uc_tablequote_minimum'], $form_state['values']['uc_tablequote_maximum'], $form_state['values']['uc_tablequote_rate'], variable_get('uc_tablequote_type','weight')); - } + if ($form_state['values']['uc_tablequote_maximum'] > $form_state['values']['uc_tablequote_minimum'] && $form_state['values']['uc_tablequote_maximum'] > 0) { + db_query("INSERT INTO {uc_tablequote} (min, max, rate, qmid) VALUES (%f, %f, %f, %d)", $form_state['values']['uc_tablequote_minimum'], $form_state['values']['uc_tablequote_maximum'], $form_state['values']['uc_tablequote_rate'], $method->qmid); } - + drupal_set_message(t('The configuration options have been saved.')); } @@ -207,16 +314,18 @@ function uc_tablequote_admin_settings_su */ function uc_tablequote_theme($form){ return array( - 'uc_tablequote_admin_settings' => array( + 'uc_tablequote_edit_table' => array( 'arguments' => array('form' => NULL), ), ); } -function theme_uc_tablequote_admin_settings($form){ +function theme_uc_tablequote_edit_table($form){ + $qmid = $form['qmid']['#value']; + $method = db_fetch_object(db_query('SELECT * FROM {uc_tablequote_methods} WHERE qmid = %d', $qmid)); $header = array(t('Delete'), t('Minimum value'), t('Maximum value'), t('Shipping Rate')); - $result = db_query("SELECT * FROM {uc_tablequote} ORDER BY min"); - $num_rows = db_result(db_query('SELECT COUNT(*) FROM {uc_tablequote} ORDER BY min')); + $result = db_query("SELECT * FROM {uc_tablequote} WHERE qmid = %d ORDER BY min", $qmid); + $num_rows = db_result(db_query('SELECT COUNT(*) FROM {uc_tablequote} WHERE qmid = %d ORDER BY min', $qmid)); if ($num_rows == 0) { $rows[] = array(array('data' => t('No rates found.'), 'colspan' => 4)); } @@ -231,7 +340,7 @@ function theme_uc_tablequote_admin_setti $row[] = $rate; $rows[] = $row; } - $rows[] = array(array('data' => t('Shipping quotes based on: ').variable_get('uc_tablequote_type','weight'), 'colspan' => 4)); + $rows[] = array(array('data' => t('Shipping quotes based on: ') . $method->quote_mode, 'colspan' => 4)); } $output .= theme('table', $header, $rows); @@ -242,9 +351,13 @@ function theme_uc_tablequote_admin_setti /****************************************************************************** * Module Functions * ******************************************************************************/ -function uc_tablequote_quote(&$form_state, $products, $details){ +function uc_tablequote_quote($products, $details, $method){ + $method = explode('_', $method['id']); + $qmid = $method[1]; + $rate = 0; $items = uc_cart_get_contents(); + foreach ($items as $item) { if (variable_get('uc_tablequote_type','weight') == 'weight') { $total += $item->weight * $item->qty; @@ -254,7 +367,7 @@ function uc_tablequote_quote(&$form_stat } } - $result = db_query("SELECT * FROM {uc_tablequote}"); + $result = db_query("SELECT * FROM {uc_tablequote} WHERE qmid = %d", $qmid); while ($r = db_fetch_object($result)) { if($total <= $r->max && $total >= $r->min) { $rate = $r->rate; @@ -262,9 +375,9 @@ function uc_tablequote_quote(&$form_stat } $method = uc_tablequote_shipping_method(); if ($rate > 0) { - $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote']['quote']['accessorials'][0]); + $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote_'. $qmid]['quote']['accessorials'][0]); } else { - $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote']['quote']['accessorials'][0]); + $quotes[] = array('rate' => $rate, 'format' => uc_currency_format($rate), 'option_label' => $method['tablequote_'. $qmid]['quote']['accessorials'][0]); } return $quotes; } \ No newline at end of file