uc_weightquote.admin.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Weight quote shipping method administration menu items.
  5. */
  6. /**
  7. * Configures the store default product shipping rates.
  8. *
  9. * @see uc_weightquote_admin_method_edit_form_submit()
  10. * @see uc_weightquote_admin_method_edit_form_delete()
  11. * @ingroup forms
  12. */
  13. function uc_weightquote_admin_method_edit_form($form, &$form_state, $mid = 0) {
  14. if ($mid && ($method = db_query("SELECT * FROM {uc_weightquote_methods} WHERE mid = :mid", array(':mid' => $mid))->fetchObject())) {
  15. $form['mid'] = array('#type' => 'value', '#value' => $mid);
  16. }
  17. else {
  18. $method = (object) array(
  19. 'title' => '',
  20. 'label' => '',
  21. 'base_rate' => '',
  22. 'product_rate' => '',
  23. );
  24. }
  25. $form['title'] = array(
  26. '#type' => 'textfield',
  27. '#title' => t('Shipping method title'),
  28. '#description' => t('The name shown to administrators distinguish this method from other weightquote methods.'),
  29. '#default_value' => $method->title,
  30. '#required' => TRUE,
  31. );
  32. $form['label'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('Line item label'),
  35. '#description' => t('The name shown to the customer when they choose a shipping method at checkout.'),
  36. '#default_value' => $method->label,
  37. '#required' => TRUE,
  38. );
  39. $form['base_rate'] = array(
  40. '#type' => 'uc_price',
  41. '#title' => t('Base price'),
  42. '#description' => t('The starting price for weight-based shipping costs.'),
  43. '#default_value' => $method->base_rate,
  44. '#required' => TRUE,
  45. );
  46. $form['product_rate'] = array(
  47. '#type' => 'uc_price',
  48. '#title' => t('Default cost adjustment per !unit', array('!unit' => variable_get('uc_weight_unit', 'lb'))),
  49. '#description' => t('The amount per weight unit to add to the shipping cost for an item.'),
  50. '#default_value' => $method->product_rate,
  51. '#required' => TRUE,
  52. '#field_suffix' => t('per @unit', array('@unit' => variable_get('uc_weight_unit', 'lb'))),
  53. );
  54. $form['actions'] = array('#type' => 'actions');
  55. $form['actions']['submit'] = array(
  56. '#type' => 'submit',
  57. '#value' => t('Submit'),
  58. );
  59. if (isset($form['mid'])) {
  60. $form['actions']['delete'] = array(
  61. '#type' => 'submit',
  62. '#value' => t('Delete'),
  63. '#validate' => array(),
  64. '#submit' => array('uc_weightquote_admin_method_edit_form_delete'),
  65. );
  66. }
  67. return $form;
  68. }
  69. /**
  70. * Helper function to delete a weightquote method.
  71. *
  72. * @see uc_weightquote_admin_method_edit_form()
  73. */
  74. function uc_weightquote_admin_method_edit_form_delete($form, &$form_state) {
  75. drupal_goto('admin/store/settings/quotes/weightquote/' . $form_state['values']['mid'] . '/delete');
  76. }
  77. /**
  78. * Form submission handler for uc_weightquote_admin_method_edit_form().
  79. *
  80. * @see uc_weightquote_admin_method_edit_form()
  81. */
  82. function uc_weightquote_admin_method_edit_form_submit($form, &$form_state) {
  83. if (isset($form_state['values']['mid'])) {
  84. drupal_write_record('uc_weightquote_methods', $form_state['values'], 'mid');
  85. drupal_set_message(t('Weight quote shipping method was updated.'));
  86. $form_state['redirect'] = 'admin/store/settings/quotes/methods';
  87. }
  88. else {
  89. drupal_write_record('uc_weightquote_methods', $form_state['values']);
  90. // Ensure Rules picks up the new condition.
  91. entity_flush_caches();
  92. drupal_set_message(t('Created and enabled new weight quote shipping method.'));
  93. $form_state['redirect'] = 'admin/store/settings/quotes/manage/get_quote_from_weightquote_' . $form_state['values']['mid'];
  94. }
  95. }
  96. /**
  97. * Confirms deletion of a weight-based shipping method.
  98. *
  99. * @see uc_weightquote_admin_method_confirm_delete_submit()
  100. * @ingroup forms
  101. */
  102. function uc_weightquote_admin_method_confirm_delete($form, &$form_state, $mid) {
  103. $form['mid'] = array('#type' => 'value', '#value' => $mid);
  104. return confirm_form($form, t('Do you want to delete this shipping method?'),
  105. 'admin/store/settings/quotes/methods',
  106. t('This will remove the shipping method and the product-specific overrides (if applicable). This action can not be undone.'),
  107. t('Delete'));
  108. }
  109. /**
  110. * Form submission handler for uc_weightquote_admin_method_confirm_delete().
  111. *
  112. * @see uc_weightquote_admin_method_confirm_delete()
  113. */
  114. function uc_weightquote_admin_method_confirm_delete_submit($form, &$form_state) {
  115. $mid = $form_state['values']['mid'];
  116. db_delete('uc_weightquote_methods')
  117. ->condition('mid', $mid)
  118. ->execute();
  119. db_delete('uc_weightquote_products')
  120. ->condition('mid', $mid)
  121. ->execute();
  122. rules_config_delete(array('get_quote_from_weightquote_' . $mid));
  123. drupal_set_message(t('Weight quote shipping method deleted.'));
  124. $form_state['redirect'] = 'admin/store/settings/quotes/methods';
  125. }