uc_flatrate.admin.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @file
  4. * Flat rate shipping method administration menu items.
  5. */
  6. /**
  7. * Configures the store default product shipping rates.
  8. *
  9. * @see uc_flatrate_admin_method_edit_form_submit()
  10. * @see uc_flatrate_admin_method_edit_form_delete()
  11. * @ingroup forms
  12. */
  13. function uc_flatrate_admin_method_edit_form($form, &$form_state, $mid = 0) {
  14. if ($mid && ($method = db_query("SELECT * FROM {uc_flatrate_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 flatrate 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 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 product shipping rate'),
  49. '#description' => t('Additional shipping cost per product in cart.'),
  50. '#default_value' => $method->product_rate,
  51. '#required' => TRUE,
  52. );
  53. $form['actions'] = array('#type' => 'actions');
  54. $form['actions']['submit'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Submit'),
  57. );
  58. if (isset($form['mid'])) {
  59. $form['actions']['delete'] = array(
  60. '#type' => 'submit',
  61. '#value' => t('Delete'),
  62. '#validate' => array(),
  63. '#submit' => array('uc_flatrate_admin_method_edit_form_delete'),
  64. );
  65. }
  66. return $form;
  67. }
  68. /**
  69. * Helper function to delete a flatrate method.
  70. *
  71. * @see uc_flatrate_admin_method_edit_form()
  72. */
  73. function uc_flatrate_admin_method_edit_form_delete($form, &$form_state) {
  74. drupal_goto('admin/store/settings/quotes/flatrate/' . $form_state['values']['mid'] . '/delete');
  75. }
  76. /**
  77. * Form submission handler for uc_flatrate_admin_method_edit_form().
  78. *
  79. * @see uc_flatrate_admin_method_edit_form()
  80. */
  81. function uc_flatrate_admin_method_edit_form_submit($form, &$form_state) {
  82. if (isset($form_state['values']['mid'])) {
  83. drupal_write_record('uc_flatrate_methods', $form_state['values'], 'mid');
  84. drupal_set_message(t('Flat rate shipping method was updated.'));
  85. $form_state['redirect'] = 'admin/store/settings/quotes/methods';
  86. }
  87. else {
  88. drupal_write_record('uc_flatrate_methods', $form_state['values']);
  89. // Ensure Rules picks up the new condition.
  90. entity_flush_caches();
  91. drupal_set_message(t('Created and enabled new flat rate shipping method.'));
  92. $form_state['redirect'] = 'admin/store/settings/quotes/manage/get_quote_from_flatrate_' . $form_state['values']['mid'];
  93. }
  94. }
  95. /**
  96. * Confirms deletion of a flat rate shipping method.
  97. *
  98. * @see uc_flatrate_admin_method_confirm_delete_submit()
  99. * @ingroup forms
  100. */
  101. function uc_flatrate_admin_method_confirm_delete($form, &$form_state, $mid) {
  102. $form['mid'] = array('#type' => 'value', '#value' => $mid);
  103. return confirm_form($form, t('Do you want to delete this shipping method?'),
  104. 'admin/store/settings/quotes/methods',
  105. t('This will remove the shipping method and the product-specific overrides (if applicable). This action can not be undone.'),
  106. t('Delete'));
  107. }
  108. /**
  109. * Form submission handler for uc_flatrate_admin_method_confirm_delete().
  110. *
  111. * @see uc_flatrate_admin_method_confirm_delete()
  112. */
  113. function uc_flatrate_admin_method_confirm_delete_submit($form, &$form_state) {
  114. $mid = $form_state['values']['mid'];
  115. db_delete('uc_flatrate_methods')
  116. ->condition('mid', $mid)
  117. ->execute();
  118. db_delete('uc_flatrate_products')
  119. ->condition('mid', $mid)
  120. ->execute();
  121. rules_config_delete(array('get_quote_from_flatrate_' . $mid));
  122. drupal_set_message(t('Flat rate shipping method deleted.'));
  123. $form_state['redirect'] = 'admin/store/settings/quotes/methods';
  124. }