uc_ups.admin.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. /**
  3. * @file
  4. * UPS administration menu items.
  5. */
  6. /**
  7. * UPS Online Tool settings.
  8. *
  9. * Records UPS account information necessary to use the service. Allows testing
  10. * or production mode. Configures which UPS services are quoted to customers.
  11. *
  12. * @see uc_ups_admin_settings_validate()
  13. * @see uc_ups_admin_settings_submit()
  14. * @ingroup forms
  15. */
  16. function uc_ups_admin_settings($form, &$form_state) {
  17. // Put fieldsets into vertical tabs
  18. $form['ups-settings'] = array(
  19. '#type' => 'vertical_tabs',
  20. '#attached' => array(
  21. 'js' => array(
  22. 'vertical-tabs' => drupal_get_path('module', 'uc_ups') . '/uc_ups.admin.js',
  23. ),
  24. ),
  25. );
  26. // Container for credential forms
  27. $form['uc_ups_credentials'] = array(
  28. '#type' => 'fieldset',
  29. '#title' => t('Credentials'),
  30. '#description' => t('Account number and authorization information.'),
  31. '#collapsible' => TRUE,
  32. '#collapsed' => TRUE,
  33. '#group' => 'ups-settings',
  34. );
  35. $form['uc_ups_credentials']['uc_ups_access_license'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t('UPS OnLine Tools XML Access Key'),
  38. '#default_value' => variable_get('uc_ups_access_license', ''),
  39. '#required' => TRUE,
  40. );
  41. $form['uc_ups_credentials']['uc_ups_shipper_number'] = array(
  42. '#type' => 'textfield',
  43. '#title' => t('UPS Shipper #'),
  44. '#description' => t('The 6-character string identifying your UPS account as a shipper.'),
  45. '#default_value' => variable_get('uc_ups_shipper_number', ''),
  46. '#required' => TRUE,
  47. );
  48. $form['uc_ups_credentials']['uc_ups_user_id'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('UPS.com user ID'),
  51. '#default_value' => variable_get('uc_ups_user_id', ''),
  52. '#required' => TRUE,
  53. );
  54. $form['uc_ups_credentials']['uc_ups_password'] = array(
  55. '#type' => 'password',
  56. '#title' => t('Password'),
  57. '#default_value' => variable_get('uc_ups_password', ''),
  58. );
  59. $form['uc_ups_credentials']['uc_ups_connection_address'] = array(
  60. '#type' => 'select',
  61. '#title' => t('Server mode'),
  62. '#description' => t('Use the Testing server while developing and configuring your site. Switch to the Production server only after you have demonstrated that transactions on the Testing server are working and you are ready to go live.'),
  63. '#options' => array('https://wwwcie.ups.com/ups.app/xml/' => t('Testing'),
  64. 'https://onlinetools.ups.com/ups.app/xml/' => t('Production'),
  65. ),
  66. '#default_value' => variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/'),
  67. );
  68. $form['services'] = array(
  69. '#type' => 'fieldset',
  70. '#title' => t('Service options'),
  71. '#description' => t('Set the conditions that will return a UPS quote.'),
  72. '#collapsible' => TRUE,
  73. '#collapsed' => TRUE,
  74. '#group' => 'ups-settings',
  75. );
  76. $form['services']['uc_ups_services'] = array(
  77. '#type' => 'checkboxes',
  78. '#title' => t('UPS services'),
  79. '#default_value' => variable_get('uc_ups_services', _uc_ups_service_list()),
  80. '#options' => _uc_ups_service_list(),
  81. '#description' => t('Select the UPS services that are available to customers.'),
  82. );
  83. // Container for quote options.
  84. $form['uc_ups_quote_options'] = array(
  85. '#type' => 'fieldset',
  86. '#title' => t('Quote options'),
  87. '#description' => t('Preferences that affect computation of quote.'),
  88. '#collapsible' => TRUE,
  89. '#collapsed' => TRUE,
  90. '#group' => 'ups-settings',
  91. );
  92. $form['uc_ups_quote_options']['uc_ups_all_in_one'] = array(
  93. '#type' => 'radios',
  94. '#title' => t('Product packages'),
  95. '#default_value' => variable_get('uc_ups_all_in_one', 1),
  96. '#options' => array(
  97. 0 => t('Each product in its own package'),
  98. 1 => t('All products in one package'),
  99. ),
  100. '#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.'),
  101. );
  102. // Form to select package types.
  103. $form['uc_ups_quote_options']['uc_ups_pkg_type'] = array(
  104. '#type' => 'select',
  105. '#title' => t('Default Package Type'),
  106. '#default_value' => variable_get('uc_ups_pkg_type', _uc_ups_pkg_types()),
  107. '#options' => _uc_ups_pkg_types(),
  108. '#description' => t('Type of packaging to be used. May be overridden on a per-product basis via the product node edit form.'),
  109. );
  110. $form['uc_ups_quote_options']['uc_ups_classification'] = array(
  111. '#type' => 'select',
  112. '#title' => t('UPS Customer classification'),
  113. '#options' => array(
  114. '01' => t('Wholesale'),
  115. '03' => t('Occasional'),
  116. '04' => t('Retail'),
  117. ),
  118. '#default_value' => variable_get('uc_ups_classification', '04'),
  119. '#description' => t('The kind of customer you are to UPS. For daily pickups the default is wholesale; for customer counter pickups the default is retail; for other pickups the default is occasional.'),
  120. );
  121. $form['uc_ups_quote_options']['uc_ups_negotiated_rates'] = array(
  122. '#type' => 'radios',
  123. '#title' => t('Negotiated rates'),
  124. '#default_value' => variable_get('uc_ups_negotiated_rates', 0),
  125. '#options' => array(1 => t('Yes'), 0 => t('No')),
  126. '#description' => t('Is your UPS account receiving negotiated rates on shipments?'),
  127. );
  128. // Form to select pickup type.
  129. $form['uc_ups_quote_options']['uc_ups_pickup_type'] = array(
  130. '#type' => 'select',
  131. '#title' => t('Pickup type'),
  132. '#options' => array(
  133. '01' => 'Daily Pickup',
  134. '03' => 'Customer Counter',
  135. '06' => 'One Time Pickup',
  136. '07' => 'On Call Air',
  137. '11' => 'Suggested Retail Rates',
  138. '19' => 'Letter Center',
  139. '20' => 'Air Service Center',
  140. ),
  141. '#default_value' => variable_get('uc_ups_pickup_type', '01'),
  142. );
  143. $form['uc_ups_quote_options']['uc_ups_residential_quotes'] = array(
  144. '#type' => 'radios',
  145. '#title' => t('Assume UPS shipping quotes will be delivered to'),
  146. '#default_value' => variable_get('uc_ups_residential_quotes', 0),
  147. '#options' => array(
  148. 0 => t('Business locations'),
  149. 1 => t('Residential locations (extra fees)'),
  150. ),
  151. );
  152. $form['uc_ups_quote_options']['uc_ups_unit_system'] = array(
  153. '#type' => 'select',
  154. '#title' => t('System of measurement'),
  155. '#default_value' => variable_get('uc_ups_unit_system', variable_get('uc_length_unit', 'in')),
  156. '#options' => array(
  157. 'in' => t('British'),
  158. 'cm' => t('Metric'),
  159. ),
  160. '#description' => t('Choose the standard system of measurement for your country.'),
  161. );
  162. $form['uc_ups_quote_options']['uc_ups_insurance'] = array(
  163. '#type' => 'checkbox',
  164. '#title' => t('Package insurance'),
  165. '#default_value' => variable_get('uc_ups_insurance', TRUE),
  166. '#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.'),
  167. );
  168. // Container for markup forms.
  169. $form['uc_ups_markups'] = array(
  170. '#type' => 'fieldset',
  171. '#title' => t('Markups'),
  172. '#description' => t('Modifiers to the shipping weight and quoted rate.'),
  173. '#collapsible' => TRUE,
  174. '#collapsed' => TRUE,
  175. '#group' => 'ups-settings',
  176. );
  177. // Form to select type of rate markup.
  178. $form['uc_ups_markups']['uc_ups_rate_markup_type'] = array(
  179. '#type' => 'select',
  180. '#title' => t('Rate markup type'),
  181. '#default_value' => variable_get('uc_ups_rate_markup_type', 'percentage'),
  182. '#options' => array(
  183. 'percentage' => t('Percentage (%)'),
  184. 'multiplier' => t('Multiplier (×)'),
  185. 'currency' => t('Addition (!currency)', array('!currency' => variable_get('uc_currency_sign', '$'))),
  186. ),
  187. );
  188. // Form to select rate markup amount.
  189. $form['uc_ups_markups']['uc_ups_rate_markup'] = array(
  190. '#type' => 'textfield',
  191. '#title' => t('Shipping rate markup'),
  192. '#default_value' => variable_get('uc_ups_rate_markup', '0'),
  193. '#description' => t('Markup shipping rate quote by currency amount, percentage, or multiplier.'),
  194. );
  195. // Form to select type of weight markup.
  196. $form['uc_ups_markups']['uc_ups_weight_markup_type'] = array(
  197. '#type' => 'select',
  198. '#title' => t('Weight markup type'),
  199. '#default_value' => variable_get('uc_ups_weight_markup_type', 'percentage'),
  200. '#options' => array(
  201. 'percentage' => t('Percentage (%)'),
  202. 'multiplier' => t('Multiplier (×)'),
  203. 'mass' => t('Addition (!mass)', array('!mass' => '#')),
  204. ),
  205. '#disabled' => TRUE,
  206. );
  207. // Form to select weight markup amount.
  208. $form['uc_ups_markups']['uc_ups_weight_markup'] = array(
  209. '#type' => 'textfield',
  210. '#title' => t('Shipping weight markup'),
  211. '#default_value' => variable_get('uc_ups_weight_markup', '0'),
  212. '#description' => t('Markup UPS shipping weight on a per-package basis before quote, by weight amount, percentage, or multiplier.'),
  213. '#disabled' => TRUE,
  214. );
  215. // Container for label printing.
  216. $form['uc_ups_labels'] = array(
  217. '#type' => 'fieldset',
  218. '#title' => t('Label Printing'),
  219. '#description' => t('Preferences for UPS Shipping Label Printing. Additional permissions from UPS are required to use this feature.'),
  220. '#collapsible' => TRUE,
  221. '#collapsed' => TRUE,
  222. '#group' => 'ups-settings',
  223. );
  224. $period = drupal_map_assoc(array(86400, 302400, 604800, 1209600, 2419200, 0), 'format_interval');
  225. $period[0] = t('Forever');
  226. // Form to select how long labels stay on server.
  227. $form['uc_ups_labels']['uc_ups_label_lifetime'] = array(
  228. '#type' => 'select',
  229. '#title' => t('Label lifetime'),
  230. '#default_value' => variable_get('uc_ups_label_lifetime', 0),
  231. '#options' => $period,
  232. '#description' => t('Controls how long labels are stored on the server before being automatically deleted. Cron must be enabled for automatic deletion. Default is never delete the labels, keep them forever.'),
  233. );
  234. // Taken from system_settings_form(). Only, don't use its submit handler.
  235. $form['actions']['#type'] = 'actions';
  236. $form['actions']['submit'] = array(
  237. '#type' => 'submit',
  238. '#value' => t('Save configuration'),
  239. );
  240. $form['actions']['cancel'] = array(
  241. '#markup' => l(t('Cancel'), 'admin/store/settings/quotes'),
  242. );
  243. if (!empty($_POST) && form_get_errors()) {
  244. drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  245. }
  246. if (!isset($form['#theme'])) {
  247. $form['#theme'] = 'system_settings_form';
  248. }
  249. return $form;
  250. }
  251. /**
  252. * Validation handler for uc_ups_admin_settings.
  253. *
  254. * Requires password only if it hasn't been set.
  255. *
  256. * @see uc_ups_admin_settings()
  257. * @see uc_ups_admin_settings_submit()
  258. */
  259. function uc_ups_admin_settings_validate($form, &$form_state) {
  260. $old_password = variable_get('uc_ups_password', '');
  261. if (!$form_state['values']['uc_ups_password']) {
  262. if ($old_password) {
  263. form_set_value($form['uc_ups_credentials']['uc_ups_password'], $old_password, $form_state);
  264. }
  265. else {
  266. form_set_error('uc_ups_password', t('Password field is required.'));
  267. }
  268. }
  269. if (!is_numeric($form_state['values']['uc_ups_rate_markup'])) {
  270. form_set_error('uc_ups_rate_markup', t('Rate markup must be a numeric value.'));
  271. }
  272. if (!is_numeric($form_state['values']['uc_ups_weight_markup'])) {
  273. form_set_error('uc_ups_weight_markup', t('Weight markup must be a numeric value.'));
  274. }
  275. }
  276. /**
  277. * Submit handler for uc_ups_admin_settings().
  278. *
  279. * Emulates system_settings_form_submit(), but only on a subset of the
  280. * form values.
  281. *
  282. * @see uc_ups_admin_settings()
  283. * @see uc_ups_admin_settings_validate()
  284. */
  285. function uc_ups_admin_settings_submit($form, &$form_state) {
  286. $fields = array(
  287. 'uc_ups_access_license',
  288. 'uc_ups_shipper_number',
  289. 'uc_ups_user_id',
  290. 'uc_ups_password',
  291. 'uc_ups_connection_address',
  292. 'uc_ups_services',
  293. 'uc_ups_pickup_type',
  294. 'uc_ups_pkg_type',
  295. 'uc_ups_classification',
  296. 'uc_ups_negotiated_rates',
  297. 'uc_ups_residential_quotes',
  298. 'uc_ups_rate_markup_type',
  299. 'uc_ups_rate_markup',
  300. 'uc_ups_weight_markup_type',
  301. 'uc_ups_weight_markup',
  302. 'uc_ups_label_lifetime',
  303. 'uc_ups_all_in_one',
  304. 'uc_ups_unit_system',
  305. 'uc_ups_insurance',
  306. );
  307. foreach ($fields as $key) {
  308. $value = $form_state['values'][$key];
  309. if (is_array($value) && isset($form_state['values']['array_filter'])) {
  310. $value = array_keys(array_filter($value));
  311. }
  312. variable_set($key, $value);
  313. }
  314. drupal_set_message(t('The configuration options have been saved.'));
  315. cache_clear_all();
  316. drupal_theme_rebuild();
  317. }