uc_store.admin.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /**
  3. * @file
  4. * Store administration menu items.
  5. */
  6. /**
  7. * Menu callback which provides the store administration overview page.
  8. */
  9. function uc_store_admin() {
  10. module_load_include('inc', 'system', 'system.admin');
  11. // Check for status report errors.
  12. if (system_status(TRUE) && user_access('administer site configuration')) {
  13. drupal_set_message(t('One or more problems were detected with your Drupal installation. Check the <a href="@status">status report</a> for more information.', array('@status' => url('admin/reports/status'))), 'error');
  14. }
  15. $blocks = array();
  16. if ($admin = db_query("SELECT menu_name, mlid FROM {menu_links} WHERE link_path = 'admin/store' AND module = 'system'")->fetchAssoc()) {
  17. $result = db_query("
  18. SELECT m.*, ml.*
  19. FROM {menu_links} ml
  20. INNER JOIN {menu_router} m ON ml.router_path = m.path
  21. WHERE ml.link_path != 'admin/help' AND menu_name = :menu_name AND ml.plid = :mlid AND hidden = 0", $admin, array('fetch' => PDO::FETCH_ASSOC));
  22. foreach ($result as $item) {
  23. _menu_link_translate($item);
  24. if (!$item['access']) {
  25. continue;
  26. }
  27. // The link 'description' either derived from the hook_menu 'description'
  28. // or entered by the user via menu module is saved as the title attribute.
  29. if (!empty($item['localized_options']['attributes']['title'])) {
  30. $item['description'] = $item['localized_options']['attributes']['title'];
  31. }
  32. $block = $item;
  33. $block['content'] = theme('admin_block_content', array('content' => system_admin_menu_block($item)));
  34. if (!empty($block['content'])) {
  35. $block['show'] = TRUE;
  36. }
  37. // Prepare for sorting as in function _menu_tree_check_access().
  38. // The weight is offset so it is always positive, with a uniform 5-digits.
  39. $blocks[(50000 + $item['weight']) . ' ' . $item['title'] . ' ' . $item['mlid']] = $block;
  40. }
  41. }
  42. ksort($blocks);
  43. $build['blocks'] = array(
  44. '#theme' => 'admin_page',
  45. '#blocks' => $blocks,
  46. );
  47. if ($results = module_invoke_all('uc_store_status')) {
  48. foreach ($results as $message) {
  49. switch ($message['status']) {
  50. case 'warning':
  51. $icon = 'alert.gif';
  52. break;
  53. case 'error':
  54. $icon = 'error.gif';
  55. break;
  56. default:
  57. $icon = 'info.gif';
  58. }
  59. $icon = theme('image', array('path' => drupal_get_path('module', 'uc_store') . '/images/' . $icon));
  60. $rows[] = array(
  61. array('data' => $icon, 'class' => array('icon')),
  62. array('data' => $message['title'], 'class' => array('title')),
  63. array('data' => $message['desc'], 'class' => array('message')),
  64. );
  65. }
  66. $build['status'] = array(
  67. '#theme' => 'table',
  68. '#caption' => '<h2>' . t('Store status') . '</h2>',
  69. '#rows' => $rows,
  70. '#attributes' => array('class' => array('uc-store-status')),
  71. );
  72. }
  73. return $build;
  74. }
  75. /**
  76. * Displays main reports page.
  77. */
  78. function uc_store_reports() {
  79. $menu = menu_get_item('admin/store/reports');
  80. $content = system_admin_menu_block($menu);
  81. $build['menu'] = array(
  82. '#theme' => 'admin_block_content',
  83. '#content' => $content,
  84. '#weight' => 5,
  85. );
  86. return $build;
  87. }
  88. /**
  89. * Displays store configuration page.
  90. */
  91. function uc_store_configuration_page() {
  92. $menu = menu_get_item('admin/store/settings');
  93. $content = system_admin_menu_block($menu);
  94. $build['menu'] = array(
  95. '#theme' => 'admin_block_content',
  96. '#content' => $content,
  97. );
  98. return $build;
  99. }
  100. /**
  101. * Form to configure address fields.
  102. *
  103. * @see uc_store_address_fields_form_submit()
  104. * @see theme_uc_store_address_fields_form()
  105. * @ingroup forms
  106. */
  107. function uc_store_address_fields_form($form, &$form_state) {
  108. $form['uc_address_fields']['#tree'] = TRUE;
  109. $form['uc_address_fields_required']['#tree'] = TRUE;
  110. $form['uc_address_fields_weight']['#tree'] = TRUE;
  111. $fields = array(
  112. 'first_name' => t('First name'),
  113. 'last_name' => t('Last name'),
  114. 'company' => t('Company'),
  115. 'street1' => t('Street address 1'),
  116. 'street2' => t('Street address 2'),
  117. 'city' => t('City'),
  118. 'zone' => t('State/Province'),
  119. 'country' => t('Country'),
  120. 'postal_code' => t('Postal code'),
  121. 'phone' => t('Phone number'),
  122. );
  123. $current = variable_get('uc_address_fields', drupal_map_assoc(array('first_name', 'last_name', 'phone', 'company', 'street1', 'street2', 'city', 'zone', 'postal_code', 'country')));
  124. $required = variable_get('uc_address_fields_required', drupal_map_assoc(array('first_name', 'last_name', 'street1', 'city', 'zone', 'postal_code', 'country')));
  125. $weight = uc_store_address_field_weights();
  126. foreach ($fields as $field => $label) {
  127. $form['uc_address_fields'][$field] = array(
  128. '#type' => 'checkbox',
  129. '#default_value' => isset($current[$field]) ? TRUE : FALSE,
  130. );
  131. $form['uc_address_fields_required'][$field] = array(
  132. '#type' => 'checkbox',
  133. '#default_value' => isset($required[$field]) ? TRUE : FALSE,
  134. );
  135. $form['uc_address_fields_weight'][$field] = array(
  136. '#type' => 'weight',
  137. '#default_value' => (isset($weight[$field])) ? $weight[$field] : 0,
  138. '#attributes' => array('class' => array('uc-store-address-fields-weight')),
  139. );
  140. $form['fields'][$field]['default'] = array(
  141. '#markup' => $label,
  142. );
  143. $form['fields'][$field]['uc_field_' . $field] = array(
  144. '#type' => 'textfield',
  145. '#default_value' => uc_get_field_name($field),
  146. '#size' => 32,
  147. );
  148. $form['fields'][$field]['#weight'] = (isset($weight[$field])) ? $weight[$field] : 99;
  149. }
  150. $form['#submit'][] = 'uc_store_address_fields_form_submit';
  151. if (function_exists('i18n_variable_form_alter_settings')) {
  152. if ($i18n_variables = i18n_variable_form_alter_settings($form, i18n_variable_list())) {
  153. $form['#submit'][] = 'i18n_variable_form_submit';
  154. $form['#i18n_variables'] = $i18n_variables;
  155. $form += i18n_variable_form_selector();
  156. }
  157. }
  158. $form = system_settings_form($form);
  159. unset($form['#theme']);
  160. return $form;
  161. }
  162. /**
  163. * Returns HTML for uc_store_address_fields_form().
  164. *
  165. * @param $variables
  166. * An associative array containing:
  167. * - form: A render element representing the form.
  168. *
  169. * @see uc_store_address_fields_form()
  170. * @ingroup themeable
  171. */
  172. function theme_uc_store_address_fields_form($variables) {
  173. $form = $variables['form'];
  174. $header = array(t('Field'), t('Title'), t('Enabled'), t('Required'), t('List position'));
  175. // Sort fields by weight
  176. uasort($form['fields'], 'element_sort');
  177. foreach (element_children($form['fields']) as $field) {
  178. $row = array(
  179. drupal_render($form['fields'][$field]['default']),
  180. drupal_render($form['fields'][$field]['uc_field_' . $field]),
  181. drupal_render($form['uc_address_fields'][$field]),
  182. drupal_render($form['uc_address_fields_required'][$field]),
  183. drupal_render($form['uc_address_fields_weight'][$field]),
  184. );
  185. $rows[] = array(
  186. 'data' => $row,
  187. 'class' => array('draggable'),
  188. );
  189. }
  190. drupal_add_tabledrag('uc-store-address-fields-weight-table', 'order', 'sibling', 'uc-store-address-fields-weight');
  191. $output = theme('table', array(
  192. 'header' => $header,
  193. 'rows' => $rows,
  194. 'attributes' => array('id' => 'uc-store-address-fields-weight-table'),
  195. ));
  196. $output .= drupal_render_children($form);
  197. return $output;
  198. }
  199. /**
  200. * Saves the address fields settings.
  201. *
  202. * @see uc_store_address_fields_form()
  203. */
  204. function uc_store_address_fields_form_submit($form, &$form_state) {
  205. $form_state['values']['uc_address_fields'] = array_filter($form_state['values']['uc_address_fields']);
  206. $form_state['values']['uc_address_fields_required'] = array_filter($form_state['values']['uc_address_fields_required']);
  207. }
  208. /**
  209. * Form builder for store settings.
  210. *
  211. * @see uc_store_settings_form_validate()
  212. * @ingroup forms
  213. */
  214. function uc_store_settings_form($form, &$form_state) {
  215. $form['store'] = array('#type' => 'vertical_tabs');
  216. $form['basic'] = array(
  217. '#type' => 'fieldset',
  218. '#title' => t('Basic information'),
  219. '#group' => 'store',
  220. );
  221. $form['basic']['uc_store_name'] = uc_textfield(t('Store name'), uc_store_name(), FALSE, NULL, 64);
  222. $form['basic']['uc_store_owner'] = uc_textfield(t('Store owner'), variable_get('uc_store_owner', NULL), FALSE, NULL, 64);
  223. $form['basic']['uc_store_email'] = array(
  224. '#type' => 'textfield',
  225. '#title' => t('E-mail address'),
  226. '#description' => NULL,
  227. '#size' => 32,
  228. '#maxlength' => 128,
  229. '#required' => TRUE,
  230. '#default_value' => uc_store_email(),
  231. );
  232. $form['basic']['uc_store_email_include_name'] = array(
  233. '#type' => 'checkbox',
  234. '#title' => t('Include the store name in the "From" line of store e-mails.'),
  235. '#description' => t('May not be available on all server configurations. Turn off if this causes problems.'),
  236. '#default_value' => variable_get('uc_store_email_include_name', TRUE),
  237. );
  238. $form['basic']['uc_store_phone'] = uc_textfield(t('Phone number'), variable_get('uc_store_phone', NULL), FALSE);
  239. $form['basic']['uc_store_fax'] = uc_textfield(t('Fax number'), variable_get('uc_store_fax', NULL), FALSE);
  240. $form['basic']['uc_store_help_page'] = array(
  241. '#type' => 'textfield',
  242. '#title' => t('Store help page'),
  243. '#description' => t('The Drupal page for the store help link.'),
  244. '#default_value' => variable_get('uc_store_help_page', ''),
  245. '#size' => 32,
  246. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  247. );
  248. $form['address'] = array(
  249. '#type' => 'fieldset',
  250. '#title' => t('Store address'),
  251. '#group' => 'store',
  252. );
  253. $form['address']['address'] = array(
  254. '#type' => 'uc_address',
  255. '#default_value' => array(
  256. 'uc_store_street1' => variable_get('uc_store_street1', ''),
  257. 'uc_store_street2' => variable_get('uc_store_street2', ''),
  258. 'uc_store_city' => variable_get('uc_store_city', ''),
  259. 'uc_store_zone' => variable_get('uc_store_zone', 0),
  260. 'uc_store_country' => isset($form_state['values']) ? $form_state['values']['uc_store_country'] : uc_store_default_country(),
  261. 'uc_store_postal_code' => variable_get('uc_store_postal_code', ''),
  262. ),
  263. '#required' => FALSE,
  264. '#key_prefix' => 'uc_store',
  265. );
  266. $form['currency'] = array(
  267. '#type' => 'fieldset',
  268. '#title' => t('Currency format'),
  269. '#group' => 'store',
  270. );
  271. $form['currency']['uc_currency_code'] = array(
  272. '#type' => 'textfield',
  273. '#title' => t('Default currency'),
  274. '#description' => t('While not used directly in formatting, the currency code is used by other modules as the primary currency for your site. Enter here your three character <a href="!url">ISO 4217</a> currency code.', array('!url' => 'http://en.wikipedia.org/wiki/ISO_4217#Active_codes')),
  275. '#default_value' => variable_get('uc_currency_code', 'USD'),
  276. '#maxlength' => 3,
  277. '#size' => 5,
  278. );
  279. $form['currency']['example'] = array(
  280. '#type' => 'textfield',
  281. '#title' => t('Current format'),
  282. '#value' => uc_currency_format(1000.1234),
  283. '#disabled' => TRUE,
  284. '#size' => 10,
  285. );
  286. $form['currency']['uc_currency_sign'] = uc_textfield(t('Currency Sign'), variable_get('uc_currency_sign', '$'), FALSE, NULL, 10, 10);
  287. $form['currency']['uc_sign_after_amount'] = array(
  288. '#type' => 'checkbox',
  289. '#title' => t('Display currency sign after amount.'),
  290. '#default_value' => variable_get('uc_sign_after_amount', FALSE),
  291. );
  292. $form['currency']['uc_currency_thou'] = uc_textfield(t('Thousands Marker'), variable_get('uc_currency_thou', ','), FALSE, NULL, 10, 10);
  293. $form['currency']['uc_currency_dec'] = uc_textfield(t('Decimal Marker'), variable_get('uc_currency_dec', '.'), FALSE, NULL, 10, 10);
  294. $form['currency']['uc_currency_prec'] = array(
  295. '#type' => 'select',
  296. '#title' => t('Number of decimal places'),
  297. '#options' => drupal_map_assoc(array(0, 1, 2)),
  298. '#default_value' => variable_get('uc_currency_prec', 2),
  299. );
  300. $form['weight'] = array(
  301. '#type' => 'fieldset',
  302. '#title' => t('Weight format'),
  303. '#description' => t('Supply a format string for each unit. !value represents the weight value.'),
  304. '#group' => 'store',
  305. );
  306. $units = array(
  307. 'lb' => t('Pounds'),
  308. 'oz' => t('Ounces'),
  309. 'kg' => t('Kilograms'),
  310. 'g' => t('Grams'),
  311. );
  312. $form['weight']['uc_weight_unit'] = array(
  313. '#type' => 'select',
  314. '#title' => t('Default unit of measurement'),
  315. '#default_value' => variable_get('uc_weight_unit', 'lb'),
  316. '#options' => $units,
  317. );
  318. foreach ($units as $unit => $name) {
  319. $form['weight']['uc_weight_format_' . $unit] = array(
  320. '#type' => 'textfield',
  321. '#title' => t('@unit format string', array('@unit' => $name)),
  322. '#default_value' => variable_get('uc_weight_format_' . $unit, '!value ' . $unit),
  323. );
  324. }
  325. $form['length'] = array(
  326. '#type' => 'fieldset',
  327. '#title' => t('Length format'),
  328. '#description' => t('Supply a format string for each unit. !value represents the length value.'),
  329. '#group' => 'store',
  330. );
  331. $units = array(
  332. 'in' => t('Inches'),
  333. 'ft' => t('Feet'),
  334. 'cm' => t('Centimeters'),
  335. 'mm' => t('Millimeters'),
  336. );
  337. $form['length']['uc_length_unit'] = array(
  338. '#type' => 'select',
  339. '#title' => t('Default unit of measurement'),
  340. '#default_value' => variable_get('uc_length_unit', 'in'),
  341. '#options' => $units,
  342. );
  343. foreach ($units as $unit => $name) {
  344. $form['length']['uc_length_format_' . $unit] = array(
  345. '#type' => 'textfield',
  346. '#title' => t('@unit format string', array('@unit' => $name)),
  347. '#default_value' => variable_get('uc_length_format_' . $unit, '!value ' . $unit),
  348. );
  349. }
  350. $form['display'] = array(
  351. '#type' => 'fieldset',
  352. '#title' => t('Display settings'),
  353. '#group' => 'store',
  354. );
  355. $form['display']['uc_customer_list_address'] = array(
  356. '#type' => 'radios',
  357. '#title' => t('Primary customer address'),
  358. '#description' => t('Select the address to be used on customer lists and summaries.'),
  359. '#options' => array(
  360. 'billing' => t('Billing address'),
  361. 'shipping' => t('Shipping address'),
  362. ),
  363. '#default_value' => variable_get('uc_customer_list_address', 'billing'),
  364. );
  365. $form['display']['uc_footer_message'] = array(
  366. '#type' => 'radios',
  367. '#title' => t('Footer message for store pages'),
  368. '#options' => array_merge(
  369. array(0 => t('Randomly select a message from the list below.')),
  370. _uc_store_footer_options(),
  371. array('none' => t('(Do not display a message in the footer.)'))
  372. ),
  373. '#default_value' => variable_get('uc_footer_message', 0),
  374. '#weight' => 10,
  375. );
  376. return system_settings_form($form);
  377. }
  378. /**
  379. * Validates store e-mail address for uc_store_settings_form().
  380. *
  381. * @see uc_store_settings_form()
  382. */
  383. function uc_store_settings_form_validate($form, &$form_state) {
  384. $mail = trim($form_state['values']['uc_store_email']);
  385. if (!valid_email_address($mail)) {
  386. form_set_error('uc_store_email', t('The e-mail address %mail is not valid.', array('%mail' => $mail)));
  387. }
  388. }
  389. /**
  390. * Implements hook_uc_store_status().
  391. */
  392. function uc_store_uc_store_status() {
  393. $messages = array();
  394. // Check to see if there are any updated CIF files that need to be installed.
  395. $countries = array();
  396. $result = db_query("SELECT * FROM {uc_countries}");
  397. foreach ($result as $country) {
  398. $countries[t($country->country_name)] = $country;
  399. }
  400. uksort($countries, 'strnatcasecmp');
  401. $files = _uc_country_import_list();
  402. $updates = array();
  403. if (is_array($countries)) {
  404. foreach ($countries as $country) {
  405. if ($country->version < $files[$country->country_id]['version'] && $country->version > 0) {
  406. $updates[] = $country->country_name;
  407. }
  408. unset($files[$country->country_id]);
  409. }
  410. }
  411. if (count($updates)) {
  412. $messages[] = array(
  413. 'status' => 'warning',
  414. 'title' => t('Countries'),
  415. 'desc' => t('Updates are available for the following installed countries: %countries. You may update these countries on the <a href="@url">country settings</a> page.', array('%countries' => implode(', ', $updates), '@url' => url('admin/store/settings/countries'))),
  416. );
  417. }
  418. else {
  419. $messages[] = array(
  420. 'status' => 'ok',
  421. 'title' => t('Countries'),
  422. 'desc' => t('Country definitions are up-to-date.'),
  423. );
  424. }
  425. // Check to see if the store e-mail address has been set.
  426. if (variable_get('uc_store_email', '') == '') {
  427. $messages[] = array(
  428. 'status' => 'error',
  429. 'title' => t('Store settings'),
  430. 'desc' => t('Store e-mail address has not been set. Please enter it <a href="@url">here</a>.', array('@url' => url('admin/store/settings/store'))),
  431. );
  432. }
  433. else {
  434. $messages[] = array(
  435. 'status' => 'ok',
  436. 'title' => t('Store settings'),
  437. 'desc' => t('Store e-mail address is set.'),
  438. );
  439. }
  440. return $messages;
  441. }