uc_usps.admin.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @file
  4. * USPS administration menu items.
  5. */
  6. /**
  7. * Configures USPS settings.
  8. *
  9. * @see uc_usps_admin_settings_validate()
  10. * @see uc_usps_admin_settings_submit()
  11. * @ingroup forms
  12. */
  13. function uc_usps_admin_settings($form, &$form_state) {
  14. // Put fieldsets into vertical tabs.
  15. $form['usps-settings'] = array(
  16. '#type' => 'vertical_tabs',
  17. '#attached' => array(
  18. 'js' => array(
  19. 'vertical-tabs' => drupal_get_path('module', 'uc_usps') . '/uc_usps.admin.js',
  20. ),
  21. ),
  22. );
  23. // Container for credential forms.
  24. $form['uc_usps_credentials'] = array(
  25. '#type' => 'fieldset',
  26. '#title' => t('Credentials'),
  27. '#description' => t('Account number and authorization information.'),
  28. '#collapsible' => TRUE,
  29. '#collapsed' => TRUE,
  30. '#group' => 'usps-settings',
  31. );
  32. $form['uc_usps_credentials']['uc_usps_user_id'] = array(
  33. '#type' => 'textfield',
  34. '#title' => t('USPS user ID'),
  35. '#description' => t('To acquire or locate your user ID, refer to the <a href="!url">USPS documentation</a>.', array('!url' => 'http://drupal.org/node/1308256')),
  36. '#default_value' => variable_get('uc_usps_user_id', ''),
  37. );
  38. $form['domestic'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('USPS Domestic'),
  41. '#description' => t('Set the conditions that will return a USPS quote.'),
  42. '#collapsible' => TRUE,
  43. '#collapsed' => TRUE,
  44. '#group' => 'usps-settings',
  45. );
  46. $form['domestic']['uc_usps_online_rates'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Display USPS "online" rates'),
  49. '#default_value' => variable_get('uc_usps_online_rates', FALSE),
  50. '#description' => t('Show your customer standard USPS rates (default) or discounted "online" rates. Online rates apply only if you, the merchant, pay for and print out postage from the USPS <a href="https://cns.usps.com/labelInformation.shtml">Click-N-Ship</a> web site.'),
  51. );
  52. $form['domestic']['uc_usps_env_services'] = array(
  53. '#type' => 'checkboxes',
  54. '#title' => t('USPS envelope services'),
  55. '#default_value' => variable_get('uc_usps_env_services', array_keys(_uc_usps_env_services())),
  56. '#options' => _uc_usps_env_services(),
  57. '#description' => t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
  58. );
  59. $form['domestic']['uc_usps_services'] = array(
  60. '#type' => 'checkboxes',
  61. '#title' => t('USPS parcel services'),
  62. '#default_value' => variable_get('uc_usps_services', array_keys(_uc_usps_services())),
  63. '#options' => _uc_usps_services(),
  64. '#description' => t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
  65. );
  66. $form['international'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('USPS International'),
  69. '#description' => t('Set the conditions that will return a USPS International quote.'),
  70. '#collapsible' => TRUE,
  71. '#collapsed' => TRUE,
  72. '#group' => 'usps-settings',
  73. );
  74. $form['international']['uc_usps_intl_env_services'] = array(
  75. '#type' => 'checkboxes',
  76. '#title' => t('USPS international envelope services'),
  77. '#default_value' => variable_get('uc_usps_intl_env_services', array_keys(_uc_usps_intl_env_services())),
  78. '#options' => _uc_usps_intl_env_services(),
  79. '#description' => t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
  80. );
  81. $form['international']['uc_usps_intl_services'] = array(
  82. '#type' => 'checkboxes',
  83. '#title' => t('USPS international parcel services'),
  84. '#default_value' => variable_get('uc_usps_intl_services', array_keys(_uc_usps_intl_services())),
  85. '#options' => _uc_usps_intl_services(),
  86. '#description' => t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
  87. );
  88. // Container for quote options.
  89. $form['uc_usps_quote_options'] = array(
  90. '#type' => 'fieldset',
  91. '#title' => t('Quote options'),
  92. '#description' => t('Preferences that affect computation of quote.'),
  93. '#collapsible' => TRUE,
  94. '#collapsed' => TRUE,
  95. '#group' => 'usps-settings',
  96. );
  97. $form['uc_usps_quote_options']['uc_usps_all_in_one'] = array(
  98. '#type' => 'radios',
  99. '#title' => t('Product packages'),
  100. '#default_value' => variable_get('uc_usps_all_in_one', 1),
  101. '#options' => array(
  102. 0 => t('Each product in its own package'),
  103. 1 => t('All products in one package'),
  104. ),
  105. '#description' => t('Indicate whether each product is quoted as shipping separately or all in one package. Orders with one kind of product will still use the package quantity to determine the number of packages needed, however.'),
  106. );
  107. // Insurance.
  108. $form['uc_usps_quote_options']['uc_usps_insurance'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Package insurance'),
  111. '#default_value' => variable_get('uc_usps_insurance', FALSE),
  112. '#description' => t('When enabled, the quotes presented to the customer will include the cost of insurance for the full sales price of all products in the order.'),
  113. '#disabled' => TRUE,
  114. );
  115. // Delivery Confirmation.
  116. $form['uc_usps_quote_options']['uc_usps_delivery_confirmation'] = array(
  117. '#type' => 'checkbox',
  118. '#title' => t('Delivery confirmation'),
  119. '#default_value' => variable_get('uc_usps_delivery_confirmation', FALSE),
  120. '#description' => t('When enabled, the quotes presented to the customer will include the cost of delivery confirmation for all packages in the order.'),
  121. '#disabled' => TRUE,
  122. );
  123. // Signature Confirmation.
  124. $form['uc_usps_quote_options']['uc_usps_signature_confirmation'] = array(
  125. '#type' => 'checkbox',
  126. '#title' => t('Signature confirmation'),
  127. '#default_value' => variable_get('uc_usps_signature_confirmation', FALSE),
  128. '#description' => t('When enabled, the quotes presented to the customer will include the cost of signature confirmation for all packages in the order.'),
  129. '#disabled' => TRUE,
  130. );
  131. // Container for markup forms.
  132. $form['uc_usps_markups'] = array(
  133. '#type' => 'fieldset',
  134. '#title' => t('Markups'),
  135. '#description' => t('Modifiers to the shipping weight and quoted rate.'),
  136. '#collapsible' => TRUE,
  137. '#collapsed' => TRUE,
  138. '#group' => 'usps-settings',
  139. );
  140. $form['uc_usps_markups']['uc_usps_rate_markup_type'] = array(
  141. '#type' => 'select',
  142. '#title' => t('Rate markup type'),
  143. '#default_value' => variable_get('uc_usps_rate_markup_type', 'percentage'),
  144. '#options' => array(
  145. 'percentage' => t('Percentage (%)'),
  146. 'multiplier' => t('Multiplier (×)'),
  147. 'currency' => t('Addition (!currency)', array('!currency' => variable_get('uc_currency_sign', '$'))),
  148. ),
  149. );
  150. $form['uc_usps_markups']['uc_usps_rate_markup'] = array(
  151. '#type' => 'textfield',
  152. '#title' => t('Shipping rate markup'),
  153. '#default_value' => variable_get('uc_usps_rate_markup', '0'),
  154. '#description' => t('Markup shipping rate quote by dollar amount, percentage, or multiplier.'),
  155. );
  156. // Form to select type of weight markup.
  157. $form['uc_usps_markups']['uc_usps_weight_markup_type'] = array(
  158. '#type' => 'select',
  159. '#title' => t('Weight markup type'),
  160. '#default_value' => variable_get('uc_usps_weight_markup_type', 'percentage'),
  161. '#options' => array(
  162. 'percentage' => t('Percentage (%)'),
  163. 'multiplier' => t('Multiplier (×)'),
  164. 'mass' => t('Addition (!mass)', array('!mass' => '#')),
  165. ),
  166. '#disabled' => TRUE,
  167. );
  168. // Form to select weight markup amount.
  169. $form['uc_usps_markups']['uc_usps_weight_markup'] = array(
  170. '#type' => 'textfield',
  171. '#title' => t('Shipping weight markup'),
  172. '#default_value' => variable_get('uc_usps_weight_markup', '0'),
  173. '#description' => t('Markup shipping weight on a per-package basis before quote, by weight amount, percentage, or multiplier.'),
  174. '#disabled' => TRUE,
  175. );
  176. // Taken from system_settings_form(). Only, don't use its submit handler.
  177. $form['actions']['#type'] = 'actions';
  178. $form['actions']['submit'] = array(
  179. '#type' => 'submit',
  180. '#value' => t('Save configuration'),
  181. );
  182. $form['actions']['cancel'] = array(
  183. '#markup' => l(t('Cancel'), 'admin/store/settings/quotes'),
  184. );
  185. if (!empty($_POST) && form_get_errors()) {
  186. drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  187. }
  188. if (!isset($form['#theme'])) {
  189. $form['#theme'] = 'system_settings_form';
  190. }
  191. return $form;
  192. }
  193. /**
  194. * Validation handler for uc_usps_admin_settings form.
  195. *
  196. * @see uc_usps_admin_settings()
  197. * @see uc_usps_admin_settings_submit()
  198. */
  199. function uc_usps_admin_settings_validate($form, &$form_state) {
  200. if (!is_numeric($form_state['values']['uc_usps_rate_markup'])) {
  201. form_set_error('uc_usps_rate_markup', t('Rate markup must be a numeric value.'));
  202. }
  203. if (!is_numeric($form_state['values']['uc_usps_weight_markup'])) {
  204. form_set_error('uc_usps_weight_markup', t('Weight markup must be a numeric value.'));
  205. }
  206. }
  207. /**
  208. * Submit handler for uc_usps_admin_settings form.
  209. *
  210. * @see uc_usps_admin_settings()
  211. * @see uc_usps_admin_settings_validate()
  212. */
  213. function uc_usps_admin_settings_submit($form, &$form_state) {
  214. $fields = array(
  215. 'uc_usps_user_id',
  216. 'uc_usps_online_rates',
  217. 'uc_usps_env_services',
  218. 'uc_usps_services',
  219. 'uc_usps_intl_env_services',
  220. 'uc_usps_intl_services',
  221. 'uc_usps_rate_markup_type',
  222. 'uc_usps_rate_markup',
  223. 'uc_usps_weight_markup_type',
  224. 'uc_usps_weight_markup',
  225. 'uc_usps_all_in_one',
  226. 'uc_usps_insurance',
  227. 'uc_usps_delivery_confirmation',
  228. 'uc_usps_signature_confirmation',
  229. );
  230. foreach ($fields as $key) {
  231. $value = $form_state['values'][$key];
  232. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  233. $value = array_keys(array_filter($value));
  234. }
  235. variable_set($key, $value);
  236. }
  237. drupal_set_message(t('The configuration options have been saved.'));
  238. cache_clear_all();
  239. drupal_theme_rebuild();
  240. }