uc_store.admin.inc 17 KB

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