uc_order.admin.inc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. <?php
  2. /**
  3. * @file
  4. * Order administration menu items.
  5. */
  6. /**
  7. * Redirects to the customers listing page.
  8. */
  9. function uc_order_customers() {
  10. drupal_goto('admin/store/customers/view');
  11. }
  12. /**
  13. * Redirects to the orders listing page.
  14. */
  15. function uc_order_orders() {
  16. drupal_goto('admin/store/orders/view');
  17. }
  18. /**
  19. * Generates the settings form for orders.
  20. *
  21. * @ingroup forms
  22. */
  23. function uc_order_settings_form($form, &$form_state) {
  24. $form['invoice']['uc_cust_order_invoice_template'] = array(
  25. '#type' => 'select',
  26. '#title' => t('On-site invoice template'),
  27. '#description' => t('Select the invoice template to use when invoices are viewed on the site.'),
  28. '#options' => uc_order_template_options(),
  29. '#default_value' => variable_get('uc_cust_order_invoice_template', 'customer'),
  30. );
  31. if (module_exists('uc_cart')) {
  32. $form['invoice']['uc_cust_order_invoice_template']['#description'] .= '<br />' . t('This is separate from the template used to e-mail invoices to customers which is configured through <a href="!url">Rules</a>.', array('!url' => url('admin/store/settings/checkout/rules')));
  33. }
  34. return system_settings_form($form);
  35. }
  36. /**
  37. * Displays the order workflow form for order state and status customization.
  38. *
  39. * @see uc_order_workflow_form_submit()
  40. * @see theme_uc_order_state_table()
  41. * @see theme_uc_order_status_table()
  42. * @ingroup forms
  43. */
  44. function uc_order_workflow_form($form, &$form_state) {
  45. $states = uc_order_state_list();
  46. $statuses = uc_order_status_list();
  47. $form['order_states'] = array(
  48. '#type' => 'fieldset',
  49. '#title' => t('Order states'),
  50. '#collapsible' => TRUE,
  51. '#collapsed' => TRUE,
  52. '#theme' => 'uc_order_state_table',
  53. '#tree' => TRUE,
  54. );
  55. foreach ($states as $state_id => $state) {
  56. $form['order_states'][$state_id]['title'] = array(
  57. '#markup' => $state['title'],
  58. );
  59. // Create the select box for specifying a default status per order state.
  60. $options = array();
  61. foreach ($statuses as $status) {
  62. if ($status['state'] == $state_id) {
  63. $options[$status['id']] = $status['title'];
  64. }
  65. }
  66. if (empty($options)) {
  67. $form['order_states'][$state_id]['default'] = array(
  68. '#markup' => t('- N/A -'),
  69. );
  70. }
  71. else {
  72. $form['order_states'][$state_id]['default'] = array(
  73. '#type' => 'select',
  74. '#options' => $options,
  75. '#default_value' => uc_order_state_default($state_id),
  76. );
  77. }
  78. }
  79. $form['order_statuses'] = array(
  80. '#type' => 'fieldset',
  81. '#title' => t('Order statuses'),
  82. '#collapsible' => FALSE,
  83. '#theme' => 'uc_order_status_table',
  84. '#tree' => TRUE,
  85. );
  86. // Build the state option array for the order status table.
  87. $options = array();
  88. foreach ($states as $state_id => $state) {
  89. $options[$state_id] = $state['title'];
  90. }
  91. foreach ($statuses as $status) {
  92. $form['order_statuses'][$status['id']]['id'] = array(
  93. '#markup' => $status['id'],
  94. );
  95. $form['order_statuses'][$status['id']]['title'] = array(
  96. '#type' => 'textfield',
  97. '#default_value' => $status['title'],
  98. '#size' => 32,
  99. '#required' => TRUE,
  100. );
  101. $form['order_statuses'][$status['id']]['weight'] = array(
  102. '#type' => 'weight',
  103. '#delta' => 20,
  104. '#default_value' => $status['weight'],
  105. );
  106. $form['order_statuses'][$status['id']]['locked'] = array(
  107. '#type' => 'value',
  108. '#value' => $status['locked'],
  109. );
  110. if ($status['locked']) {
  111. $form['order_statuses'][$status['id']]['state'] = array(
  112. '#markup' => uc_order_state_data($status['state'], 'title'),
  113. );
  114. }
  115. else {
  116. $form['order_statuses'][$status['id']]['state'] = array(
  117. '#type' => 'select',
  118. '#options' => $options,
  119. '#default_value' => $status['state'],
  120. );
  121. $form['order_statuses'][$status['id']]['remove'] = array(
  122. '#type' => 'checkbox',
  123. );
  124. }
  125. }
  126. $form['actions'] = array('#type' => 'actions');
  127. $form['actions']['submit'] = array(
  128. '#type' => 'submit',
  129. '#value' => t('Submit changes'),
  130. );
  131. return $form;
  132. }
  133. /**
  134. * Form submission handler for uc_order_workflow_form().
  135. *
  136. * @see uc_order_workflow_form()
  137. */
  138. function uc_order_workflow_form_submit($form, &$form_state) {
  139. foreach ($form_state['values']['order_states'] as $key => $value) {
  140. variable_set('uc_state_' . $key . '_default', $value['default']);
  141. }
  142. foreach ($form_state['values']['order_statuses'] as $key => $value) {
  143. if ($value['locked'] != TRUE && $value['remove'] == TRUE) {
  144. db_delete('uc_order_statuses')
  145. ->condition('order_status_id', $key)
  146. ->execute();
  147. drupal_set_message(t('Order status %status removed.', array('%status' => $key)));
  148. }
  149. else {
  150. $fields = array(
  151. 'title' => $value['title'],
  152. 'weight' => $value['weight'],
  153. );
  154. // The state cannot be changed if the status is locked.
  155. if ($value['locked'] == FALSE) {
  156. $fields['state'] = $value['state'];
  157. }
  158. $query = db_update('uc_order_statuses')
  159. ->fields($fields)
  160. ->condition('order_status_id', $key)
  161. ->execute();
  162. }
  163. }
  164. drupal_set_message(t('Order workflow information saved.'));
  165. }
  166. /**
  167. * Themes the order state table in the order workflow settings.
  168. *
  169. * @see uc_order_workflow_form()
  170. * @ingroup themeable
  171. */
  172. function theme_uc_order_state_table($variables) {
  173. $form = $variables['form'];
  174. $header = array(t('State'), t('Default order status'));
  175. foreach (element_children($form) as $state_id) {
  176. $rows[] = array(
  177. drupal_render($form[$state_id]['title']),
  178. drupal_render($form[$state_id]['default']),
  179. );
  180. }
  181. return theme('table', array('header' => $header, 'rows' => $rows));
  182. }
  183. /**
  184. * Themes the order status table in the order workflow settings.
  185. *
  186. * @see uc_order_workflow_form()
  187. * @ingroup themeable
  188. */
  189. function theme_uc_order_status_table($variables) {
  190. $form = $variables['form'];
  191. $header = array(t('ID'), t('Title'), t('List position'), t('State'), t('Remove'));
  192. foreach (element_children($form) as $state_id) {
  193. $rows[] = array(
  194. drupal_render($form[$state_id]['id']),
  195. drupal_render($form[$state_id]['title']),
  196. drupal_render($form[$state_id]['weight']),
  197. drupal_render($form[$state_id]['state']),
  198. array('data' => drupal_render($form[$state_id]['remove']), 'align' => 'center'),
  199. );
  200. }
  201. return theme('table', array('header' => $header, 'rows' => $rows));
  202. }
  203. /**
  204. * Presents the form to create a custom order status.
  205. *
  206. * @see uc_order_status_create_form_validate()
  207. * @see uc_order_status_create_form_submit()
  208. * @ingroup forms
  209. */
  210. function uc_order_status_create_form($form, &$form_state) {
  211. $form['status_id'] = array(
  212. '#type' => 'textfield',
  213. '#title' => t('Order status ID'),
  214. '#description' => t('Must be a unique ID with no spaces.'),
  215. '#size' => 32,
  216. '#maxlength' => 32,
  217. '#required' => TRUE,
  218. );
  219. $form['status_title'] = array(
  220. '#type' => 'textfield',
  221. '#title' => t('Title'),
  222. '#description' => t('The order status title displayed to users.'),
  223. '#size' => 32,
  224. '#maxlength' => 48,
  225. '#required' => TRUE,
  226. );
  227. // Build the state option array for the order status table.
  228. $options = array();
  229. foreach (uc_order_state_list() as $state) {
  230. $options[$state['id']] = $state['title'];
  231. }
  232. $form['status_state'] = array(
  233. '#type' => 'select',
  234. '#title' => t('Order state'),
  235. '#description' => t('Set which order state this status is for.'),
  236. '#options' => $options,
  237. '#default_value' => 'post_checkout',
  238. );
  239. $form['status_weight'] = array(
  240. '#type' => 'weight',
  241. '#title' => t('List position'),
  242. '#delta' => 20,
  243. '#default_value' => 0,
  244. );
  245. $form['actions'] = array('#type' => 'actions');
  246. $form['actions']['create'] = array(
  247. '#type' => 'submit',
  248. '#value' => t('Create'),
  249. );
  250. $form['actions']['cancel'] = array(
  251. '#markup' => l(t('Cancel'), 'admin/store/settings/orders/workflow'),
  252. );
  253. return $form;
  254. }
  255. /**
  256. * Ensures the new status id is unique and has no spaces.
  257. *
  258. * @see uc_order_status_create_form()
  259. * @see uc_order_status_create_form_submit()
  260. */
  261. function uc_order_status_create_form_validate($form, &$form_state) {
  262. $new_status = strtolower(trim($form_state['values']['status_id']));
  263. if (strpos($new_status, ' ') !== FALSE || $new_status == 'all') {
  264. form_set_error('status_id', t('You have entered an invalid status ID.'));
  265. }
  266. $statuses = uc_order_status_list();
  267. foreach ($statuses as $status) {
  268. if ($new_status == $status['id']) {
  269. form_set_error('status_id', t('This ID is already in use. Please specify a unique ID.'));
  270. }
  271. }
  272. }
  273. /**
  274. * Form submission handler for uc_order_status_create_form_submit().
  275. *
  276. * @see uc_order_status_create_form()
  277. * @see uc_order_status_create_form_validate()
  278. */
  279. function uc_order_status_create_form_submit($form, &$form_state) {
  280. db_insert('uc_order_statuses')
  281. ->fields(array(
  282. 'order_status_id' => $form_state['values']['status_id'],
  283. 'title' => $form_state['values']['status_title'],
  284. 'state' => $form_state['values']['status_state'],
  285. 'weight' => $form_state['values']['status_weight'],
  286. 'locked' => 0,
  287. ))
  288. ->execute();
  289. drupal_set_message(t('Custom order status created.'));
  290. $form_state['redirect'] = 'admin/store/settings/orders/workflow';
  291. }
  292. /**
  293. * Creates a new order and redirect to its edit screen.
  294. *
  295. * @see uc_order_create_form_create_validate()
  296. * @see uc_order_create_form_create_submit()
  297. * @ingroup forms
  298. */
  299. function uc_order_create_form($form, &$form_state) {
  300. $form['customer_type'] = array(
  301. '#type' => 'radios',
  302. '#options' => array(
  303. 'search' => t('Search for an existing customer.'),
  304. 'create' => t('Create a new customer account.'),
  305. 'none' => t('No customer account required.'),
  306. ),
  307. '#required' => TRUE,
  308. '#default_value' => 'search',
  309. '#ajax' => array(
  310. 'callback' => 'uc_order_create_form_customer',
  311. 'wrapper' => 'uc-order-customer',
  312. 'progress' => array('type' => 'throbber'),
  313. ),
  314. );
  315. $form['customer'] = array(
  316. '#prefix' => '<div id="uc-order-customer">',
  317. '#suffix' => '</div>',
  318. '#tree' => TRUE,
  319. );
  320. // Create form elements needed for customer search.
  321. // Shown only when the 'Search for an existing customer.' radio is selected.
  322. if (!isset($form_state['values']['customer_type']) ||
  323. $form_state['values']['customer_type'] == 'search') {
  324. // Container for customer search fields.
  325. $form['customer'] += array(
  326. '#type' => 'fieldset',
  327. '#title' => t('Customer search'),
  328. '#description' => t('Enter full or partial information in one or more of the following fields, then press the "Search" button. Search results will match all the provided information.'),
  329. );
  330. // Customer first name.
  331. $form['customer']['first_name'] = array(
  332. '#type' => 'textfield',
  333. '#title' => t('First name'),
  334. '#size' => 24,
  335. '#maxlength' => 32,
  336. );
  337. // Customer last name.
  338. $form['customer']['last_name'] = array(
  339. '#type' => 'textfield',
  340. '#title' => t('Last name'),
  341. '#size' => 24,
  342. '#maxlength' => 32,
  343. );
  344. // Customer e-mail address.
  345. $form['customer']['email'] = array(
  346. '#type' => 'textfield',
  347. '#title' => t('E-mail'),
  348. '#size' => 24,
  349. '#maxlength' => 96,
  350. );
  351. // Customer username.
  352. $form['customer']['username'] = array(
  353. '#type' => 'textfield',
  354. '#title' => t('Username'),
  355. '#size' => 24,
  356. '#maxlength' => 96,
  357. );
  358. $form['customer']['search'] = array(
  359. '#type' => 'button',
  360. '#value' => t('Search'),
  361. '#ajax' => array(
  362. 'callback' => 'uc_order_create_form_customer_search',
  363. 'wrapper' => 'uc-order-customer-results',
  364. 'progress' => array('type' => 'throbber'),
  365. ),
  366. );
  367. $form['customer']['uid'] = array(
  368. '#prefix' => '<div id="uc-order-customer-results">',
  369. '#suffix' => '</div>',
  370. );
  371. // Search for existing customer by e-mail address.
  372. if (isset($form_state['values']['customer']['email'])) {
  373. $query = db_select('users', 'u')->distinct();
  374. $query->leftJoin('uc_orders', 'o', 'u.uid = o.uid');
  375. $query->fields('u', array('uid', 'name', 'mail'))
  376. ->fields('o', array('billing_first_name', 'billing_last_name'))
  377. ->condition('u.uid', 0, '>')
  378. ->condition(db_or()
  379. ->isNull('o.billing_first_name')
  380. ->condition('o.billing_first_name', db_like(trim($form_state['values']['customer']['first_name'])) . '%', 'LIKE')
  381. )
  382. ->condition(db_or()
  383. ->isNull('o.billing_last_name')
  384. ->condition('o.billing_last_name', db_like(trim($form_state['values']['customer']['last_name'])) . '%', 'LIKE')
  385. )
  386. ->condition(db_or()
  387. ->condition('o.primary_email', db_like(trim($form_state['values']['customer']['email'])) . '%', 'LIKE')
  388. ->condition('u.mail', db_like(trim($form_state['values']['customer']['email'])) . '%', 'LIKE')
  389. )
  390. ->condition('u.name', db_like(trim($form_state['values']['customer']['username'])) . '%', 'LIKE')
  391. ->orderBy('o.created', 'DESC')
  392. ->range(0, $limit = 11);
  393. $result = $query->execute();
  394. $options = array();
  395. foreach ($result as $user) {
  396. $name = '';
  397. if (!empty($user->billing_first_name) && !empty($user->billing_last_name)) {
  398. $name = $user->billing_first_name . ' ' . $user->billing_last_name . ' ';
  399. }
  400. // Options formated as "First Last <email@example.com> (username)".
  401. $options[$user->uid] = $name . '&lt;' . $user->mail . '&gt;' . ' (' . $user->name . ')';
  402. }
  403. $max = FALSE;
  404. if (count($options) == $limit) {
  405. array_pop($options);
  406. $max = TRUE;
  407. }
  408. if (!empty($options)) {
  409. // Display search results.
  410. $form['customer']['uid'] += array(
  411. '#type' => 'radios',
  412. '#title' => t('Select customer'),
  413. '#description' => $max ? t('More than !limit results found. Refine your search to find other customers.', array('!limit' => $limit - 1)) : '',
  414. '#options' => $options,
  415. '#default_value' => key($options),
  416. );
  417. }
  418. else {
  419. // No search results found.
  420. $form['customer']['uid'] += array(
  421. '#markup' => '<p>' . t('Search returned no results.') . '</p>',
  422. );
  423. }
  424. }
  425. }
  426. // Create form elements needed for new customer creation.
  427. // Shown only when the 'Create a new customer account.' radio is selected.
  428. elseif ($form_state['values']['customer_type'] == 'create') {
  429. // Container for new customer information.
  430. $form['customer'] += array(
  431. '#type' => 'fieldset',
  432. '#title' => t('New customer details'),
  433. );
  434. // Customer e-mail address.
  435. $form['customer']['email'] = array(
  436. '#type' => 'textfield',
  437. '#title' => t('Customer e-mail address'),
  438. '#size' => 24,
  439. '#maxlength' => 96,
  440. );
  441. // Option to notify customer.
  442. $form['customer']['sendmail'] = array(
  443. '#type' => 'checkbox',
  444. '#title' => t('E-mail account details to customer.'),
  445. );
  446. }
  447. $form['actions'] = array('#type' => 'actions');
  448. $form['actions']['submit'] = array(
  449. '#type' => 'submit',
  450. '#value' => t('Create order'),
  451. '#validate' => array('uc_order_create_form_create_validate'),
  452. '#submit' => array('uc_order_create_form_create_submit'),
  453. );
  454. return $form;
  455. }
  456. /**
  457. * Ajax callback: updates the customer selection fields.
  458. */
  459. function uc_order_create_form_customer($form, &$form_state) {
  460. return $form['customer'];
  461. }
  462. /**
  463. * Ajax callback: updates the customer search results.
  464. */
  465. function uc_order_create_form_customer_search($form, &$form_state) {
  466. return $form['customer']['uid'];
  467. }
  468. /**
  469. * Form validation handler for customer search.
  470. *
  471. * @see uc_order_create_form()
  472. * @ingroup forms
  473. */
  474. function uc_order_create_form_create_validate($form, &$form_state) {
  475. switch ($form_state['values']['customer_type']) {
  476. case 'search':
  477. if (empty($form_state['values']['customer']['uid'])) {
  478. form_set_error('customer][uid', t('Please select a customer.'));
  479. }
  480. break;
  481. case 'create':
  482. $email = trim($form_state['values']['customer']['email']);
  483. if (!valid_email_address($email)) {
  484. form_set_error('customer][mail', t('Invalid e-mail address.'));
  485. }
  486. $uid = db_query('SELECT uid FROM {users} WHERE mail LIKE :mail', array(':mail' => $email))->fetchField();
  487. if ($uid) {
  488. form_set_error('customer][mail', t('An account already exists for that e-mail.'));
  489. }
  490. break;
  491. }
  492. }
  493. /**
  494. * Form submission handler for customer search.
  495. *
  496. * @see uc_order_create_form()
  497. * @ingroup forms
  498. */
  499. function uc_order_create_form_create_submit($form, &$form_state) {
  500. global $user;
  501. switch ($form_state['values']['customer_type']) {
  502. case 'search':
  503. $uid = $form_state['values']['customer']['uid'];
  504. break;
  505. case 'create':
  506. // Create new account.
  507. $email = trim($form_state['values']['customer']['email']);
  508. $fields = array(
  509. 'name' => uc_store_email_to_username($email),
  510. 'mail' => $email,
  511. 'pass' => user_password(),
  512. 'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
  513. );
  514. $account = user_save(NULL, $fields);
  515. $uid = $account->uid;
  516. if ($form_state['values']['customer']['sendmail']) {
  517. // Manually set the password so it appears in the e-mail.
  518. $account->password = $fields['pass'];
  519. drupal_mail('user', 'register_admin_created', $email, uc_store_mail_recipient_language($email), array('account' => $account), uc_store_email_from());
  520. drupal_set_message(t('A welcome message has been e-mailed to the new user.'));
  521. }
  522. break;
  523. default:
  524. $uid = 0;
  525. }
  526. $order = uc_order_new($uid, 'post_checkout');
  527. uc_order_comment_save($order->order_id, $user->uid, t('Order created by the administration.'), 'admin');
  528. $form_state['redirect'] = 'admin/store/orders/' . $order->order_id . '/edit';
  529. }
  530. /**
  531. * Creates a new order for the specified customer, ready for editing.
  532. */
  533. function uc_order_create_for_user($account) {
  534. global $user;
  535. $order = uc_order_new($account->uid, 'post_checkout');
  536. uc_order_comment_save($order->order_id, $user->uid, t('Order created by the administration.'), 'admin');
  537. drupal_goto('admin/store/orders/' . $order->order_id . '/edit');
  538. }
  539. /**
  540. * Displays a form to select a previously entered address.
  541. *
  542. * @see uc_order_address_book_form()
  543. */
  544. function uc_order_address_book() {
  545. $uid = intval($_POST['uid']);
  546. $type = $_POST['type'];
  547. $func = $_POST['func'];
  548. $form = drupal_get_form('uc_order_address_book_form', $uid, $type, $func);
  549. print drupal_render($form);
  550. exit();
  551. }
  552. /**
  553. * Presents previously entered addresses as selectable options.
  554. *
  555. * @see uc_order_address_book()
  556. * @ingroup forms
  557. */
  558. function uc_order_address_book_form($form, &$form_state, $uid = 0, $type = 'billing', $func = '') {
  559. $select = uc_select_address($uid, $type, $func);
  560. if ($uid == 0) {
  561. $form['desc'] = array('#markup' => '<br />' . t('You must select a customer before address<br />information is available.<br />') . '<br />');
  562. }
  563. elseif (is_null($select)) {
  564. $form['desc'] = array('#markup' => '<br />' . t('No addresses found for customer.') . '<br />');
  565. }
  566. else {
  567. $form['addresses'] = uc_select_address($uid, $type, $func, t('Select an address'));
  568. $form['addresses']['#prefix'] = '<div style="float: left; margin-right: 1em;">';
  569. $form['addresses']['#suffix'] = '</div>';
  570. }
  571. $form['close'] = array(
  572. '#type' => 'button',
  573. '#value' => t('Close'),
  574. '#attributes' => array('onclick' => "return close_address_select('#" . $type . "_address_select');"),
  575. );
  576. return $form;
  577. }
  578. /**
  579. * Presents the customer search results and let one of them be chosen.
  580. *
  581. * @see uc_order_select_customer_form()
  582. */
  583. function uc_order_select_customer($email = NULL) {
  584. $build = array();
  585. $options = NULL;
  586. // Return the search results and let them pick one!
  587. if (arg(4) == 'search') {
  588. $first_name = str_replace('*', '%', db_like($_POST['first_name']));
  589. $last_name = str_replace('*', '%', db_like($_POST['last_name']));
  590. $email = str_replace('*', '%', db_like($_POST['email']));
  591. $query = db_select('users', 'u')->distinct();
  592. $query->leftJoin('uc_orders', 'o', 'u.uid = o.uid');
  593. $query->fields('u', array('uid', 'mail'))
  594. ->fields('o', array('billing_first_name', 'billing_last_name'))
  595. ->condition('u.uid', 0, '>')
  596. ->orderBy('o.billing_last_name');
  597. if ($first_name && $first_name !== '%') {
  598. $query->condition('o.billing_first_name', $first_name, 'LIKE');
  599. }
  600. if ($last_name && $last_name !== '%') {
  601. $query->condition('o.billing_last_name', $last_name, 'LIKE');
  602. }
  603. if ($email && $email !== '%') {
  604. $query->condition(db_or()
  605. ->condition('o.primary_email', $email, 'LIKE')
  606. ->condition('u.mail', $email, 'LIKE')
  607. );
  608. }
  609. $result = $query->execute();
  610. $options = array();
  611. foreach ($result as $user) {
  612. if (empty($user->billing_first_name) && empty($user->billing_last_name)) {
  613. $name = '';
  614. }
  615. else {
  616. $name = $user->billing_last_name . ', ' . $user->billing_first_name . ' ';
  617. }
  618. $options[$user->uid . ':' . $user->mail] = $name . '(' . $user->mail . ')';
  619. }
  620. if (count($options) == 0) {
  621. $build['description'] = array('#markup' => '<p>' . t('Search returned no results.') . '</p>');
  622. $options = NULL;
  623. }
  624. else {
  625. $build['description'] = array('<p>' . t('Search returned the following:') . '</p>');
  626. }
  627. }
  628. // Check to see if the e-mail address for a new user is unique.
  629. if (arg(5) == 'check') {
  630. $email = check_plain($_POST['email']);
  631. $build['email'] = array('#markup' => '');
  632. if (!valid_email_address($email)) {
  633. $build['email']['#markup'] .= t('Invalid e-mail address.') . '<br />';
  634. }
  635. $result = db_query("SELECT uid, mail FROM {users} WHERE mail = :mail", array(':mail' => $email));
  636. if ($user = $result->fetchObject()) {
  637. $build['email']['#markup'] .= t('An account already exists for that e-mail.') . '<br /><br />';
  638. $build['email']['#markup'] .= '<b>' . t('Use this account now?') . '</b><br />'
  639. . t('User @uid - @mail', array('@uid' => $user->uid, '@mail' => $user->mail)) . ' <input type="button" '
  640. . 'onclick="select_existing_customer(' . $user->uid . ', \''
  641. . $user->mail . '\');" value="' . t('Apply') . '" /><br /><br /><hr /><br/>';
  642. }
  643. else {
  644. $name = uc_store_email_to_username($email);
  645. $fields = array(
  646. 'name' => $name,
  647. 'mail' => $email,
  648. 'pass' => user_password(6),
  649. 'status' => variable_get('uc_new_customer_status_active', TRUE) ? 1 : 0,
  650. );
  651. $account = user_save('', $fields);
  652. if ($_POST['sendmail'] == 'true') {
  653. // Manually set the password so it appears in the e-mail.
  654. $account->password = $fields['pass'];
  655. // Send the e-mail through the user module.
  656. drupal_mail('user', 'register_admin_created', $email, uc_store_mail_recipient_language($email), array('account' => $account), uc_store_email_from());
  657. $build['email']['#markup'] .= t('Account details sent to e-mail provided.<br /><br /><strong>Username:</strong> @username<br /><strong>Password:</strong> @password', array('@username' => $fields['name'], '@password' => $fields['pass'])) . '<br /><br />';
  658. }
  659. $build['result'] = array(
  660. '#markup' => '<strong>' . t('Use this account now?') . '</strong><br />'
  661. . t('User @uid - @mail', array('@uid' => $account->uid, '@mail' => $account->mail)) . ' <input type="button" '
  662. . 'onclick="select_existing_customer(' . $account->uid . ', \''
  663. . $account->mail . '\');" value="' . t('Apply') . '" /><br /><br /><hr /><br/>',
  664. );
  665. }
  666. }
  667. $build['customer_select_form'] = drupal_get_form('uc_order_select_customer_form', $options);
  668. print drupal_render($build);
  669. exit();
  670. }
  671. /**
  672. * Form to choose a customer from a list.
  673. *
  674. * @see uc_order_select_customer()
  675. * @ingroup forms
  676. */
  677. function uc_order_select_customer_form($form, &$form_state, $options = NULL) {
  678. if (is_null(arg(4))) {
  679. $form['desc'] = array(
  680. '#markup' => '<div>' . t('Search for a customer based on these fields.')
  681. . '<br />' . t('Use * as a wildcard to match any character.') . '<br />'
  682. . '(<em>' . t('Leave a field empty to ignore it in the search.')
  683. . '</em>)</div>',
  684. );
  685. $form['first_name'] = array(
  686. '#type' => 'textfield',
  687. '#title' => t('First name'),
  688. '#size' => 24,
  689. '#maxlength' => 32,
  690. );
  691. $form['last_name'] = array(
  692. '#type' => 'textfield',
  693. '#title' => t('Last name'),
  694. '#size' => 24,
  695. '#maxlength' => 32,
  696. );
  697. $form['email'] = array(
  698. '#type' => 'textfield',
  699. '#title' => t('E-mail'),
  700. '#size' => 24,
  701. '#maxlength' => 96,
  702. );
  703. }
  704. elseif (arg(4) == 'search' && !is_null($options)) {
  705. $form['cust_select'] = array(
  706. '#type' => 'select',
  707. '#title' => t('Select a customer'),
  708. '#size' => 7,
  709. '#options' => $options,
  710. '#default_value' => key($options),
  711. '#attributes' => array('ondblclick' => 'return select_customer_search();'),
  712. );
  713. }
  714. elseif (arg(4) == 'new') {
  715. $form['desc'] = array(
  716. '#markup' => '<div>' . t('Enter an e-mail address for the new customer.') . '</div>',
  717. );
  718. $form['email'] = array(
  719. '#type' => 'textfield',
  720. '#title' => t('E-mail'),
  721. '#size' => 24,
  722. '#maxlength' => 96,
  723. );
  724. }
  725. $form['actions'] = array('#type' => 'actions');
  726. if (is_null(arg(4))) {
  727. $form['actions']['search'] = array(
  728. '#type' => 'submit',
  729. '#value' => t('Search'),
  730. '#attributes' => array('onclick' => 'return load_customer_search_results();'),
  731. );
  732. }
  733. elseif (arg(4) == 'search') {
  734. if (!is_null($options)) {
  735. $form['actions']['select'] = array(
  736. '#type' => 'submit',
  737. '#value' => t('Select'),
  738. '#attributes' => array('onclick' => 'return select_customer_search();'),
  739. );
  740. }
  741. $form['actions']['back'] = array(
  742. '#type' => 'submit',
  743. '#value' => t('Back'),
  744. '#attributes' => array('onclick' => 'return load_customer_search();'),
  745. );
  746. }
  747. elseif (arg(4) == 'new') {
  748. $form['sendmail'] = array(
  749. '#type' => 'checkbox',
  750. '#title' => t('E-mail customer account details.'),
  751. );
  752. $form['actions']['submit'] = array(
  753. '#type' => 'submit',
  754. '#value' => t('Submit'),
  755. '#attributes' => array('onclick' => 'return check_new_customer_address();'),
  756. );
  757. }
  758. $form['actions']['close'] = array(
  759. '#type' => 'submit',
  760. '#value' => t('Close'),
  761. '#attributes' => array('onclick' => 'return close_customer_select();'),
  762. );
  763. return $form;
  764. }
  765. /**
  766. * Returns the sortable table listing of a customer's orders.
  767. *
  768. * This function is deprecated; this listing is now provided by Views.
  769. *
  770. * @param $uid
  771. * The user ID whose orders you wish to list.
  772. */
  773. function uc_order_history($user) {
  774. drupal_set_title(t('My order history'));
  775. $header = array(
  776. array('data' => t('Date'), 'field' => 'o.created', 'sort' => 'desc'),
  777. array('data' => t('Order #'), 'field' => 'o.order_id'),
  778. array('data' => t('Status'), 'field' => 'os.title'),
  779. array('data' => t('Products'), 'field' => 'products'),
  780. array('data' => t('Total'), 'field' => 'o.order_total')
  781. );
  782. $rows = array();
  783. $query = db_select('uc_orders', 'o');
  784. $o_order_id = $query->addField('o', 'order_id');
  785. $o_created = $query->addField('o', 'created');
  786. $o_status = $query->addField('o', 'order_status');
  787. $o_total = $query->addField('o', 'order_total');
  788. $o_uid = $query->addField('o', 'uid');
  789. $query->condition($o_uid, $user->uid)
  790. ->condition($o_status, uc_order_status_list('general', TRUE), 'IN');
  791. $count_query = $query->countQuery();
  792. $query = $query->extend('PagerDefault')->extend('TableSort');
  793. $os = $query->leftJoin('uc_order_statuses', 'os', 'o.order_status = os.order_status_id');
  794. $op = $query->leftJoin('uc_order_products', 'op', 'o.order_id = op.order_id');
  795. $os_title = $query->addField('os', 'title');
  796. $op_products = $query->addExpression('SUM(op.qty)', 'products');
  797. $query->groupBy('o.order_id')
  798. ->groupBy('o.created')
  799. ->groupBy('os.title')
  800. ->groupBy('o.order_total')
  801. ->groupBy('o.order_status')
  802. ->groupBy('o.uid')
  803. ->orderByHeader($header)
  804. ->limit(20);
  805. $query->setCountQuery($count_query);
  806. $result = $query->execute();
  807. // Build a table based on the customer's orders.
  808. foreach ($result as $order) {
  809. $link = l($order->order_id, 'user/' . $user->uid . '/orders/' . $order->order_id);
  810. if (user_access('view all orders')) {
  811. $link .= '<span class="order-admin-icons">' . uc_order_actions($order, TRUE) . '</span>';
  812. }
  813. $rows[] = array(
  814. array('data' => format_date($order->created, 'uc_store')),
  815. array('data' => $link),
  816. array('data' => check_plain($order->title)),
  817. array('data' => (!is_null($order->products) ? $order->products : 0), 'align' => 'center'),
  818. array('data' => array('#theme' => 'uc_price', '#price' => $order->order_total), 'align' => 'right'),
  819. );
  820. }
  821. $build = array();
  822. $build['orders'] = array(
  823. '#theme' => 'table',
  824. '#header' => $header,
  825. '#rows' => $rows,
  826. '#attributes' => array('class' => array('uc-order-history')),
  827. '#empty' => t('No orders available.'),
  828. );
  829. $build['pager'] = array(
  830. '#theme' => 'pager',
  831. '#element' => 0,
  832. '#weight' => 5,
  833. );
  834. return $build;
  835. }
  836. /**
  837. * Displays the order edit screen, constructed via hook_uc_order_pane().
  838. *
  839. * @see uc_order_edit_form_validate()
  840. * @see uc_order_edit_form_submit()
  841. * @see theme_uc_order_edit_form()
  842. * @see uc_order_edit_form_delete()
  843. * @ingroup forms
  844. */
  845. function uc_order_edit_form($form, &$form_state, $order) {
  846. if (isset($form_state['order'])) {
  847. $order = $form_state['order'];
  848. }
  849. else {
  850. $form_state['order'] = $order;
  851. }
  852. $form['#order'] = $order;
  853. $form['order_id'] = array('#type' => 'hidden', '#value' => $order->order_id);
  854. $form['order_uid'] = array('#type' => 'hidden', '#value' => $order->uid);
  855. $modified = isset($form_state['values']['order_modified']) ? $form_state['values']['order_modified'] : $order->modified;
  856. $form['order_modified'] = array('#type' => 'hidden', '#value' => $modified);
  857. $panes = _uc_order_pane_list('edit');
  858. foreach ($panes as $id => $pane) {
  859. if (in_array('edit', $pane['show'])) {
  860. $func = $pane['callback'];
  861. if (function_exists($func)) {
  862. $func('edit-form', $order, $form, $form_state);
  863. }
  864. }
  865. }
  866. $form['actions'] = array('#type' => 'actions');
  867. $form['actions']['submit-changes'] = array(
  868. '#type' => 'submit',
  869. '#value' => t('Submit changes'),
  870. '#attributes' => array('class' => array('save-button')),
  871. );
  872. if (uc_order_can_delete($order)) {
  873. $form['actions']['delete'] = array(
  874. '#type' => 'submit',
  875. '#value' => t('Delete'),
  876. '#submit' => array('uc_order_edit_form_delete'),
  877. );
  878. }
  879. field_attach_form('uc_order', $order, $form, $form_state);
  880. form_load_include($form_state, 'inc', 'uc_store', 'includes/uc_ajax_attach');
  881. $form['#process'][] = 'uc_ajax_process_form';
  882. return $form;
  883. }
  884. /**
  885. * Prevents order edits from colliding.
  886. *
  887. * @see uc_order_edit_form()
  888. * @see uc_order_edit_form_submit()
  889. * @see theme_uc_order_edit_form()
  890. */
  891. function uc_order_edit_form_validate($form, &$form_state) {
  892. $order = uc_order_load($form_state['values']['order_id']);
  893. if ($form_state['values']['order_modified'] != $order->modified) {
  894. form_set_error('order_modified', t('This order has been modified by another user, changes cannot be saved.'));
  895. }
  896. entity_form_field_validate('uc_order', $form, $form_state);
  897. // Build list of changes to be applied.
  898. $panes = _uc_order_pane_list();
  899. foreach ($panes as $id => $pane) {
  900. if (in_array('edit', $pane['show'])) {
  901. $func = $pane['callback'];
  902. if (function_exists($func)) {
  903. if (($changes = $func('edit-process', $order, $form, $form_state)) != NULL) {
  904. foreach ($changes as $key => $value) {
  905. $form_state['order']->$key = $value;
  906. }
  907. }
  908. }
  909. }
  910. }
  911. }
  912. /**
  913. * Form submission handler for uc_order_edit_form().
  914. *
  915. * @see uc_order_edit_form()
  916. * @see uc_order_edit_form_validate()
  917. * @see theme_uc_order_edit_form()
  918. */
  919. function uc_order_edit_form_submit($form, &$form_state) {
  920. $order = uc_order_load($form_state['values']['order_id']);
  921. $log = array();
  922. foreach ($form_state['order'] as $key => $value) {
  923. if (!isset($order->$key) || $order->$key !== $value) {
  924. if (!is_array($value)) {
  925. $log[$key] = array('old' => $order->$key, 'new' => $value);
  926. }
  927. $order->$key = $value;
  928. }
  929. }
  930. if (module_exists('uc_stock')) {
  931. $qtys = array();
  932. foreach ($order->products as $product) {
  933. $qtys[$product->order_product_id] = $product->qty;
  934. }
  935. }
  936. $order->products = array();
  937. if (isset($form_state['values']['products']) && is_array($form_state['values']['products'])) {
  938. foreach ($form_state['values']['products'] as $product) {
  939. if (!isset($product['remove']) && intval($product['qty']) > 0) {
  940. $product['data'] = unserialize($product['data']);
  941. $product = (object)$product;
  942. $order->products[] = $product;
  943. if (module_exists('uc_stock')) {
  944. $temp = $product->qty;
  945. $product->qty = $product->qty - $qtys[$product->order_product_id];
  946. uc_stock_adjust_product_stock($product, 0, $order);
  947. $product->qty = $temp;
  948. }
  949. }
  950. else {
  951. $log['remove_' . $product['nid']] = $product['title'] . ' removed from order.';
  952. }
  953. }
  954. }
  955. // Load line items again, since some may have been updated by the form.
  956. $order->line_items = uc_order_load_line_items($order);
  957. uc_order_log_changes($order->order_id, $log);
  958. field_attach_submit('uc_order', $order, $form, $form_state);
  959. uc_order_save($order);
  960. drupal_set_message(t('Order changes saved.'));
  961. }
  962. /**
  963. * Formats the uc_order_edit_form().
  964. *
  965. * @see uc_order_edit_form()
  966. * @see uc_order_edit_form_validate()
  967. * @see uc_order_edit_form_submit()
  968. * @ingroup themeable
  969. */
  970. function theme_uc_order_edit_form($variables) {
  971. $form = $variables['form'];
  972. $output = '';
  973. $panes = _uc_order_pane_list();
  974. foreach ($panes as $id => $pane) {
  975. if (in_array('edit', $pane['show'])) {
  976. $func = $pane['callback'];
  977. if (function_exists($func) && ($contents = $func('edit-theme', $form['#order'], $form)) != NULL) {
  978. $output .= '<div class="order-pane ' . $pane['class'] . '" id="order-pane-' . $id . '">';
  979. $title = isset($pane['display title']) ? $pane['display title'] : $pane['title'];
  980. if ($title) {
  981. $output .= '<div class="order-pane-title">' . $title . ':' . '</div>';
  982. }
  983. $output .= $contents . '</div>';
  984. }
  985. }
  986. }
  987. $last = '<div class="order-pane abs-left">' . drupal_render($form['order_id']) . drupal_render($form['order_modified'])
  988. . drupal_render($form['form_id']) . drupal_render($form['form_token'])
  989. . drupal_render($form['form_build_id'])
  990. . drupal_render($form['actions'])
  991. . '</div>';
  992. $output .= drupal_render_children($form) . $last;
  993. return $output;
  994. }
  995. /**
  996. * Handles order delete button action.
  997. *
  998. * @see uc_order_edit_form()
  999. */
  1000. function uc_order_edit_form_delete($form, &$form_state) {
  1001. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/delete';
  1002. }
  1003. /**
  1004. * Form to add a line item to an order.
  1005. *
  1006. * @see uc_order_add_line_item_form_validate()
  1007. * @see uc_order_add_line_item_submit()
  1008. */
  1009. function uc_order_add_line_item_form($form, &$form_state, $order, $line_item_id) {
  1010. $func = _uc_line_item_data($line_item_id, 'callback');
  1011. if (!function_exists($func) || ($form = $func('form', $order->order_id)) == NULL) {
  1012. $form['title'] = array(
  1013. '#type' => 'textfield',
  1014. '#title' => t('Line item title'),
  1015. '#description' => t('Display title of the line item.'),
  1016. '#size' => 32,
  1017. '#maxlength' => 128,
  1018. '#default_value' => _uc_line_item_data($line_item_id, 'title'),
  1019. );
  1020. $form['amount'] = array(
  1021. '#type' => 'uc_price',
  1022. '#title' => t('Line item amount'),
  1023. '#allow_negative' => TRUE,
  1024. );
  1025. }
  1026. $form['order_id'] = array(
  1027. '#type' => 'hidden',
  1028. '#value' => $order->order_id,
  1029. );
  1030. $form['line_item_id'] = array(
  1031. '#type' => 'hidden',
  1032. '#value' => $line_item_id,
  1033. );
  1034. $form['actions'] = array('#type' => 'actions');
  1035. $form['actions']['submit'] = array(
  1036. '#type' => 'submit',
  1037. '#value' => t('Add line item'),
  1038. '#suffix' => l(t('Cancel'), 'admin/store/orders/' . $order->order_id . '/edit'),
  1039. );
  1040. return $form;
  1041. }
  1042. /**
  1043. * Validates new line item data.
  1044. *
  1045. * @see uc_order_add_line_item_form()
  1046. */
  1047. function uc_order_add_line_item_form_validate($form, &$form_state) {
  1048. $func = _uc_line_item_data($form_state['values']['line_item_id'], 'callback');
  1049. if (function_exists($func) && ($form = $func('form', $form_state['values']['order_id'])) != NULL) {
  1050. $func('validate', $form, $form_state);
  1051. }
  1052. }
  1053. /**
  1054. * Form submission handler for uc_order_add_line_item_form().
  1055. *
  1056. * @see uc_order_add_line_item_form()
  1057. */
  1058. function uc_order_add_line_item_form_submit($form, &$form_state) {
  1059. $func = _uc_line_item_data($form_state['values']['line_item_id'], 'callback');
  1060. if (function_exists($func) && ($form = $func('form', $form_state['values']['order_id'])) != NULL) {
  1061. $func('submit', $form, $form_state);
  1062. }
  1063. else {
  1064. uc_order_line_item_add($form_state['values']['order_id'], $form_state['values']['line_item_id'], $form_state['values']['title'], $form_state['values']['amount']);
  1065. drupal_set_message(t('Line item added to order.'));
  1066. }
  1067. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'] . '/edit';
  1068. }
  1069. /**
  1070. * Sets recipients of an invoice, then mails it.
  1071. *
  1072. * @see uc_order_mail_invoice_form_validate()
  1073. * @see uc_order_mail_invoice_form_submit()
  1074. * @ingroup forms
  1075. */
  1076. function uc_order_mail_invoice_form($form, &$form_state, $order) {
  1077. $form['order_id'] = array(
  1078. '#type' => 'hidden',
  1079. '#value' => $order->order_id,
  1080. );
  1081. $form['email'] = array(
  1082. '#type' => 'textfield',
  1083. '#title' => t('Recipient e-mail address'),
  1084. '#default_value' => $order->primary_email,
  1085. );
  1086. $form['actions'] = array('#type' => 'actions');
  1087. $form['actions']['submit' ] = array(
  1088. '#type' => 'submit',
  1089. '#value' => t('Mail invoice'),
  1090. );
  1091. return $form;
  1092. }
  1093. /**
  1094. * Only mail invoices to valid email addresses.
  1095. *
  1096. * @see uc_order_mail_invoice_form()
  1097. */
  1098. function uc_order_mail_invoice_form_validate($form, &$form_state) {
  1099. $recipient = check_plain($form_state['values']['email']);
  1100. if (empty($recipient) || !valid_email_address($recipient)) {
  1101. form_set_error('email', t('Invalid e-mail address.'));
  1102. }
  1103. }
  1104. /**
  1105. * Form submission handler for uc_order_mail_invoice_form().
  1106. *
  1107. * @see uc_order_mail_invoice_form()
  1108. */
  1109. function uc_order_mail_invoice_form_submit($form, &$form_state) {
  1110. $order = uc_order_load($form_state['values']['order_id']);
  1111. $recipient = check_plain($form_state['values']['email']);
  1112. $params = array('order' => $order);
  1113. $sent = drupal_mail('uc_order', 'invoice', $recipient, uc_store_mail_recipient_language($recipient), $params, uc_store_email_from());
  1114. if (!$sent) {
  1115. drupal_set_message(t('E-mail failed.'));
  1116. }
  1117. else {
  1118. $message = t('Invoice e-mailed to @email.', array('@email' => $recipient));
  1119. drupal_set_message($message);
  1120. uc_order_log_changes($order->order_id, array($message));
  1121. }
  1122. }
  1123. /**
  1124. * Displays a log of changes made to an order.
  1125. */
  1126. function uc_order_log($order) {
  1127. $result = db_query("SELECT * FROM {uc_order_log} WHERE order_id = :id ORDER BY order_log_id DESC", array(':id' => $order->order_id));
  1128. $header = array(t('Time'), t('User'), t('Changes'));
  1129. $rows = array();
  1130. foreach ($result as $change) {
  1131. $rows[] = array(
  1132. format_date($change->created, 'short'),
  1133. theme('uc_uid', array('uid' => $change->uid)),
  1134. $change->changes,
  1135. );
  1136. }
  1137. $build['log'] = array(
  1138. '#theme' => 'table',
  1139. '#header' => $header,
  1140. '#rows' => $rows,
  1141. '#empty' => t('No changes have been logged for this order.'),
  1142. );
  1143. return $build;
  1144. }
  1145. /**
  1146. * Confirmation form to delete an order.
  1147. *
  1148. * @see uc_order_delete_confirm_form_submit()
  1149. * @ingroup forms
  1150. */
  1151. function uc_order_delete_confirm_form($form, &$form_state, $order) {
  1152. if (!uc_order_can_delete($order)) {
  1153. drupal_set_message(t('It is not possible to delete order @id.', array('@id' => $order->order_id)));
  1154. drupal_goto('admin/store/orders');
  1155. }
  1156. $form['order_id'] = array(
  1157. '#type' => 'value',
  1158. '#value' => $order->order_id
  1159. );
  1160. return confirm_form($form, t('Are you sure you want to delete order @order_id?', array('@order_id' => $order->order_id)), 'admin/store/orders', NULL, t('Delete'));
  1161. }
  1162. /**
  1163. * Form submission handler for uc_order_delete_confirm_form().
  1164. *
  1165. * @see uc_order_delete_confirm_form()
  1166. */
  1167. function uc_order_delete_confirm_form_submit($form, &$form_state) {
  1168. // Delete the specified order.
  1169. uc_order_delete($form_state['values']['order_id']);
  1170. // Display a message to the user and return to the order admin page.
  1171. drupal_set_message(t('Order @order_id completely removed from the database.', array('@order_id' => $form_state['values']['order_id'])));
  1172. $form_state['redirect'] = 'admin/store/orders';
  1173. }