uc_quote.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * @file
  4. * Shipping quotes administration menu items.
  5. */
  6. /**
  7. * Default shipping settings.
  8. *
  9. * Sets the default shipping location of the store. Allows the user to
  10. * determine which quotin methods are enabled and which take precedence over
  11. * the others. Also sets the default quote and shipping types of all products
  12. * in the store. Individual products may be configured differently.
  13. *
  14. * @see uc_quote_admin_settings_submit()
  15. * @ingroup forms
  16. */
  17. function uc_quote_admin_settings($form, &$form_state) {
  18. $address = variable_get('uc_quote_store_default_address', new UcAddress());
  19. $form['uc_quote_log_errors'] = array(
  20. '#type' => 'checkbox',
  21. '#title' => t('Log errors during checkout to watchdog'),
  22. '#default_value' => variable_get('uc_quote_log_errors', FALSE),
  23. );
  24. $form['uc_quote_display_debug'] = array(
  25. '#type' => 'checkbox',
  26. '#title' => t('Display debug information to administrators.'),
  27. '#default_value' => variable_get('uc_quote_display_debug', FALSE),
  28. );
  29. $form['uc_quote_require_quote'] = array(
  30. '#type' => 'checkbox',
  31. '#title' => t('Prevent the customer from completing an order if a shipping quote is not selected.'),
  32. '#default_value' => variable_get('uc_quote_require_quote', TRUE),
  33. );
  34. $form['uc_quote_pane_description'] = array(
  35. '#type' => 'fieldset',
  36. '#title' => t('Shipping quote pane description'),
  37. '#tree' => TRUE,
  38. '#collapsible' => TRUE,
  39. '#collapsed' => TRUE,
  40. );
  41. $form['uc_quote_pane_description']['text'] = array(
  42. '#type' => 'textarea',
  43. '#title' => t('Message text'),
  44. '#default_value' => variable_get('uc_quote_pane_description', t('Shipping quotes are generated automatically when you enter your address and may be updated manually with the button below.')),
  45. );
  46. $form['uc_quote_err_msg'] = array(
  47. '#type' => 'fieldset',
  48. '#title' => t('Shipping quote error message'),
  49. '#tree' => TRUE,
  50. '#collapsible' => TRUE,
  51. '#collapsed' => TRUE,
  52. );
  53. $form['uc_quote_err_msg']['text'] = array(
  54. '#type' => 'textarea',
  55. '#title' => t('Message text'),
  56. '#default_value' => variable_get('uc_quote_err_msg', t("There were problems getting a shipping quote. Please verify the delivery and product information and try again.\nIf this does not resolve the issue, please call in to complete your order.")),
  57. );
  58. $form['default_address'] = array(
  59. '#type' => 'fieldset',
  60. '#title' => t('Default pickup address'),
  61. '#description' => t("When delivering products to customers, the original location of the product must be known in order to accurately quote the shipping cost and set up a delivery. This form provides the default location for all products in the store. If a product's individual pickup address is blank, Ubercart uses the store's default pickup address specified here."),
  62. '#collapsible' => TRUE,
  63. '#collapsed' => TRUE,
  64. );
  65. $form['default_address']['address'] = array(
  66. '#type' => 'uc_address',
  67. '#default_value' => isset($form_state['values']) ? $form_state['values'] : $address,
  68. '#required' => FALSE,
  69. );
  70. $form['actions'] = array('#type' => 'actions');
  71. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  72. return $form;
  73. }
  74. /**
  75. * Form submission handler for uc_quote_admin_settings().
  76. *
  77. * @see uc_quote_admin_settings()
  78. */
  79. function uc_quote_admin_settings_submit($form, &$form_state) {
  80. $address = new UcAddress();
  81. $address->first_name = $form_state['values']['first_name'];
  82. $address->last_name = $form_state['values']['last_name'];
  83. $address->company = $form_state['values']['company'];
  84. $address->phone = $form_state['values']['phone'];
  85. $address->street1 = $form_state['values']['street1'];
  86. $address->street2 = $form_state['values']['street2'];
  87. $address->city = $form_state['values']['city'];
  88. $address->zone = $form_state['values']['zone'];
  89. $address->postal_code = $form_state['values']['postal_code'];
  90. $address->country = $form_state['values']['country'];
  91. variable_set('uc_quote_store_default_address', $address);
  92. variable_set('uc_quote_log_errors', $form_state['values']['uc_quote_log_errors']);
  93. variable_set('uc_quote_display_debug', $form_state['values']['uc_quote_display_debug']);
  94. variable_set('uc_quote_require_quote', $form_state['values']['uc_quote_require_quote']);
  95. variable_set('uc_quote_pane_description', $form_state['values']['uc_quote_pane_description']['text']);
  96. variable_set('uc_quote_err_msg', $form_state['values']['uc_quote_err_msg']['text']);
  97. drupal_set_message(t('The configuration options have been saved.'));
  98. }
  99. /**
  100. * Settings for the shipping quote methods.
  101. *
  102. * Enables and reorders shipping quote methods. Sets the default shipping type.
  103. *
  104. * @see uc_quote_method_settings_validate()
  105. * @see uc_quote_method_settings_submit()
  106. * @see theme_uc_quote_method_settings()
  107. * @ingroup forms
  108. */
  109. function uc_quote_method_settings($form, &$form_state) {
  110. $form['methods'] = array(
  111. '#tree' => TRUE,
  112. );
  113. foreach (uc_quote_methods(TRUE) as $method) {
  114. if (isset($method['quote'])) {
  115. $id = $method['id'];
  116. // Build a list of operations links.
  117. $operations = isset($method['operations']) ? $method['operations'] : array();
  118. $operations += array('conditions' => array(
  119. 'title' => t('conditions'),
  120. 'href' => 'admin/store/settings/quotes/manage/get_quote_from_' . $id,
  121. 'weight' => 5,
  122. ));
  123. // Ensure "delete" comes towards the end of the list.
  124. if (isset($operations['delete'])) {
  125. $operations['delete']['weight'] = 10;
  126. }
  127. uasort($operations, 'drupal_sort_weight');
  128. $form['methods'][$id]['uc_quote_enabled'] = array(
  129. '#type' => 'checkbox',
  130. '#title' => check_plain($method['title']),
  131. '#default_value' => $method['enabled'],
  132. );
  133. $form['methods'][$id]['description'] = array(
  134. '#markup' => isset($method['description']) ? $method['description'] : '',
  135. );
  136. $form['methods'][$id]['uc_quote_method_weight'] = array(
  137. '#type' => 'weight',
  138. '#default_value' => $method['weight'],
  139. '#attributes' => array('class' => array('uc-quote-method-weight')),
  140. );
  141. $form['methods'][$id]['operations'] = array(
  142. '#theme' => 'links',
  143. '#links' => $operations,
  144. '#attributes' => array('class' => array('links', 'inline')),
  145. );
  146. }
  147. }
  148. $shipping_types = uc_quote_shipping_type_options();
  149. if (is_array($shipping_types)) {
  150. $form['uc_quote_type_weight'] = array(
  151. '#type' => 'fieldset',
  152. '#title' => t('List position'),
  153. '#description' => t('Determines which shipping methods are quoted at checkout when products of different shipping types are ordered. Larger values take precedence.'),
  154. '#collapsible' => TRUE,
  155. '#tree' => TRUE,
  156. );
  157. $weight = variable_get('uc_quote_type_weight', array());
  158. $shipping_methods = module_invoke_all('uc_shipping_method');
  159. $method_types = array();
  160. foreach ($shipping_methods as $method) {
  161. // Get shipping method types from shipping methods that provide quotes
  162. if (isset($method['quote'])) {
  163. $method_types[$method['quote']['type']][] = $method['title'];
  164. }
  165. }
  166. if (isset($method_types['order']) && is_array($method_types['order'])) {
  167. $count = count($method_types['order']);
  168. $form['uc_quote_type_weight']['#description'] .= format_plural($count, '<br />The %list method is compatible with any shipping type.', '<br />The %list methods are compatible with any shipping type.', array('%list' => implode(', ', $method_types['order'])));
  169. }
  170. foreach ($shipping_types as $id => $title) {
  171. $form['uc_quote_type_weight'][$id] = array(
  172. '#type' => 'weight',
  173. '#title' => $title . (isset($method_types[$id]) && is_array($method_types[$id]) ? ' (' . implode(', ', $method_types[$id]) . ')' : ''),
  174. '#delta' => 5,
  175. '#default_value' => isset($weight[$id]) ? $weight[$id] : 0,
  176. );
  177. }
  178. }
  179. $form['uc_store_shipping_type'] = array(
  180. '#type' => 'select',
  181. '#title' => t('Default order fulfillment type for products'),
  182. '#options' => $shipping_types,
  183. '#default_value' => variable_get('uc_store_shipping_type', 'small_package'),
  184. );
  185. $form['actions'] = array('#type' => 'actions');
  186. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
  187. return $form;
  188. }
  189. /**
  190. * Displays a formatted list of shipping quote methods and form elements.
  191. *
  192. * @see uc_quote_method_settings()
  193. * @ingroup themeable
  194. */
  195. function theme_uc_quote_method_settings($variables) {
  196. $form = $variables['form'];
  197. drupal_add_tabledrag('uc-quote-methods', 'order', 'sibling', 'uc-quote-method-weight');
  198. $header = array(t('Shipping method'), t('Details'), array('data' => t('List position'), 'sort' => 'asc'), t('Operations'));
  199. $rows = array();
  200. foreach (element_children($form['methods']) as $method) {
  201. $row = array(
  202. drupal_render($form['methods'][$method]['uc_quote_enabled']),
  203. drupal_render($form['methods'][$method]['description']),
  204. drupal_render($form['methods'][$method]['uc_quote_method_weight']),
  205. drupal_render($form['methods'][$method]['operations']),
  206. );
  207. $rows[] = array(
  208. 'data' => $row,
  209. 'class' => array('draggable'),
  210. );
  211. }
  212. $output = theme('table', array(
  213. 'header' => $header,
  214. 'rows' => $rows,
  215. 'attributes' => array('id' => 'uc-quote-methods'),
  216. 'empty' => t('No shipping quotes have been configured yet.'),
  217. ));
  218. $output .= drupal_render_children($form);
  219. return $output;
  220. }
  221. /**
  222. * Form validation for uc_quote_method_settings().
  223. *
  224. * Requires at least one enabled shipping method.
  225. *
  226. * @see uc_quote_method_settings()
  227. * @see uc_quote_method_settings_submit()
  228. */
  229. function uc_quote_method_settings_validate($form, &$form_state) {
  230. $none_enabled = TRUE;
  231. if (is_array($form_state['values']['methods'])) {
  232. foreach ($form_state['values']['methods'] as $method) {
  233. if ($method['uc_quote_enabled']) {
  234. $none_enabled = FALSE;
  235. }
  236. }
  237. }
  238. if ($none_enabled) {
  239. form_set_error('uc_quote_enabled', t('At least one shipping quote method must be enabled.'));
  240. }
  241. }
  242. /**
  243. * Form submission handler for uc_quote_method_settings().
  244. *
  245. * @see uc_quote_method_settings()
  246. * @see uc_quote_method_settings_validate()
  247. */
  248. function uc_quote_method_settings_submit($form, &$form_state) {
  249. $enabled = array();
  250. $method_weight = array();
  251. foreach ($form_state['values']['methods'] as $id => $method) {
  252. $enabled[$id] = $method['uc_quote_enabled'];
  253. $method_weight[$id] = $method['uc_quote_method_weight'];
  254. }
  255. variable_set('uc_quote_enabled', $enabled);
  256. variable_set('uc_quote_method_weight', $method_weight);
  257. variable_set('uc_quote_type_weight', $form_state['values']['uc_quote_type_weight']);
  258. variable_set('uc_store_shipping_type', $form_state['values']['uc_store_shipping_type']);
  259. drupal_set_message(t('The configuration options have been saved.'));
  260. }