PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/ecommerce/ec_store/ec_store.admin.inc

https://bitbucket.org/9_3designs/web
Pascal | 385 lines | 263 code | 21 blank | 101 comment | 6 complexity | 40dae2c68505369be35eb08e1b634851 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * @file
  4. * All store admin functions.
  5. */
  6. /**
  7. * Main store settings page, at 'admin/config/store/store'.
  8. */
  9. function ec_store_ec_settings($form, &$form_state) {
  10. $site_mail = variable_get('site_mail', ini_get('sendmail_from'));
  11. if (empty($site_mail)) {
  12. $site_mail = 'no e-mail address found';
  13. }
  14. $form['ec_display_email'] = array(
  15. '#type' => 'checkbox',
  16. '#title' => t('Display email address for non-anonymous customers'),
  17. '#default_value' => variable_get('ec_display_email', 1),
  18. );
  19. // Links settings.
  20. $form['ec_links'] = array(
  21. '#type' => 'fieldset',
  22. '#title' => t('Links'),
  23. '#collapsible' => TRUE,
  24. '#collapsed' => TRUE,
  25. );
  26. $form['ec_links']['ec_goto_cart_empty'] = array(
  27. '#type' => 'textfield',
  28. '#title' => t('Empty cart link'),
  29. '#default_value' => variable_get('ec_goto_cart_empty', ''),
  30. '#description' => t('If the user cart is empty, you can send him/her to a given page.'),
  31. );
  32. return system_settings_form($form);
  33. }
  34. /**
  35. * Configure all workflow statuses.
  36. */
  37. function ec_store_workflow_settings($form, &$form_state) {
  38. $form = array();
  39. $workflow = ec_store_transaction_workflow('types');
  40. $form['ec_store_workflow'] = array(
  41. '#tree' => TRUE,
  42. );
  43. foreach ($workflow as $key => $item) {
  44. $form['ec_store_workflow'][$key]['workflow'] = array(
  45. '#type' => 'value',
  46. '#value' => $item['workflow'],
  47. );
  48. $form['ec_store_workflow'][$key]['description'] = array(
  49. '#markup' => $item['description'],
  50. );
  51. $options = array(
  52. EC_WORKFLOW_TYPE_IN_PROGRESS => t('In progress'),
  53. EC_WORKFLOW_TYPE_COMPLETE => t('Complete'),
  54. EC_WORKFLOW_TYPE_CANCEL => t('Cancel'),
  55. );
  56. $form['ec_store_workflow'][$key]['type'] = array(
  57. '#markup' => $options[$item['type']],
  58. );
  59. $form['ec_store_workflow'][$key]['weight'] = array(
  60. '#type' => 'weight',
  61. '#default_value' => $item['weight'],
  62. '#delta' => count($workflow) + 1,
  63. );
  64. $form['ec_store_workflow'][$key]['operations'] = array(
  65. '#markup' => l(t('edit'), 'admin/config/store/store/workflow/' . $item['workflow'] . '/edit') . ' ' . l(t('delete'), 'admin/config/store/store/workflow/' . $item['workflow'] . '/delete'),
  66. );
  67. }
  68. $form['update'] = array(
  69. '#type' => 'submit',
  70. '#value' => t('Update'),
  71. );
  72. return $form;
  73. }
  74. /**
  75. * @todo Please document this function.
  76. * @see http://drupal.org/node/1354
  77. */
  78. function ec_store_workflow_settings_submit(&$form, &$form_state) {
  79. foreach ($form_state['values']['ec_store_workflow'] as $record) {
  80. drupal_write_record('ec_workflow_statuses', $record, 'workflow');
  81. }
  82. drupal_set_message(t('Workflow settings updated'));
  83. }
  84. /**
  85. * @todo Please document this function.
  86. * @see http://drupal.org/node/1354
  87. */
  88. function ec_store_workflow_settings_edit($form, $form_state, $workflowid = NULL) {
  89. $form = array();
  90. if ($workflowid) {
  91. $workflow = ec_store_transaction_workflow('types');
  92. if (isset($workflow[$workflowid])) {
  93. $item = $workflow[$workflowid];
  94. $form['workflow'] = array(
  95. '#type' => 'value',
  96. '#value' => $item['workflow'],
  97. );
  98. }
  99. else {
  100. drupal_not_found();
  101. }
  102. }
  103. else {
  104. $item = array();
  105. }
  106. $form['description'] = array(
  107. '#type' => 'textfield',
  108. '#title' => t('Description'),
  109. '#default_value' => isset($item['description']) ? $item['description'] : NULL,
  110. '#description' => t('Provide a short descriptions which will be used to identify which workflow state the transaction is in'),
  111. );
  112. $form['help'] = array(
  113. '#type' => 'textarea',
  114. '#title' => t('Help'),
  115. '#default_value' => isset($item['help']) ? $item['help'] : NULL,
  116. '#description' => t('Provide information on what the stage of the transaction workflow should be done.'),
  117. );
  118. $options = array(
  119. EC_WORKFLOW_TYPE_IN_PROGRESS => t('In progress'),
  120. EC_WORKFLOW_TYPE_COMPLETE => t('Complete'),
  121. EC_WORKFLOW_TYPE_CANCEL => t('Cancel'),
  122. );
  123. $form['type'] = array(
  124. '#type' => 'select',
  125. '#title' => t('Type of workflow'),
  126. '#default_value' => isset($item['type']) ? $item['type'] : NULL,
  127. '#options' => $options,
  128. '#description' => t('Provide type of workflow which will allow the system to idenitify the general purpose of the workflow.'),
  129. );
  130. $form['save'] = array(
  131. '#type' => 'submit',
  132. '#value' => t('Save'),
  133. );
  134. if (!empty($item)) {
  135. $form['delete'] = array(
  136. '#type' => 'submit',
  137. '#value' => t('Delete'),
  138. '#submit' => array('ec_store_workflow_settings_edit_submit_delete'),
  139. );
  140. }
  141. return $form;
  142. }
  143. /**
  144. * @todo Please document this function.
  145. * @see http://drupal.org/node/1354
  146. */
  147. function ec_store_workflow_settings_edit_submit(&$form, &$form_state) {
  148. if (isset($form_state['values']['workflow'])) {
  149. drupal_write_record('ec_workflow_statuses', $form_state['values'], 'workflow');
  150. }
  151. else {
  152. drupal_write_record('ec_workflow_statuses', $form_state['values']);
  153. }
  154. drupal_set_message(t('Workflow %name has been updated', array('%name' => $form_state['values']['description'])));
  155. $form_state['redirect'] = 'admin/config/store/store/workflow';
  156. }
  157. /**
  158. * @todo Please document this function.
  159. * @see http://drupal.org/node/1354
  160. */
  161. function ec_store_workflow_settings_edit_submit_delete(&$form, &$form_state) {
  162. drupal_goto('admin/config/store/store/workflow/' . $form_state['values']['workflow'] . '/delete');
  163. }
  164. /**
  165. * @todo Please document this function.
  166. * @see http://drupal.org/node/1354
  167. */
  168. function ec_store_workflow_settings_delete($form, $form_state, $workflowid) {
  169. $form = array();
  170. if ($workflowid) {
  171. $workflow = ec_store_transaction_workflow('types');
  172. if (isset($workflow[$workflowid])) {
  173. $item = $workflow[$workflowid];
  174. $form['workflow'] = array(
  175. '#type' => 'value',
  176. '#value' => $item['workflow'],
  177. );
  178. $form['name'] = array(
  179. '#type' => 'value',
  180. '#value' => $item['description'],
  181. );
  182. }
  183. else {
  184. drupal_not_found();
  185. }
  186. }
  187. else {
  188. drupal_not_found();
  189. }
  190. return confirm_form($form, t('Do you really want to delete the %name workflow', array('%name' => $item['description'])), 'admin/config/store/store/workflow', t('This action is permanent and cannot be undone'), t('Delete'), t('Cancel'));
  191. }
  192. /**
  193. * @todo Please document this function.
  194. * @see http://drupal.org/node/1354
  195. */
  196. function ec_store_workflow_settings_delete_submit(&$form, &$form_state) {
  197. db_delete('ec_workflow_statuses')
  198. ->condition('workflow', $form_state['values']['workflow'])
  199. ->execute();
  200. drupal_set_message(t('Workflow %description has been deleted', array('%description' => $form_state['values']['name'])));
  201. $form_state['redirect'] = 'admin/config/store/store/workflow';
  202. }
  203. /**
  204. * Called by theme_store_invoice().
  205. */
  206. function _theme_ec_store_invoice($txn, $print_mode = TRUE, $trial = FALSE) {
  207. global $base_url;
  208. $header = array();
  209. $row = array();
  210. if (empty($txn->mail) && $txn->uid > 0) {
  211. $txn->mail = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(':uid' => $txn->uid))->fetchField();
  212. }
  213. if (!empty($txn->items)) {
  214. $header = array(t('Quantity'), t('Item'), t('Price'));
  215. $shippable = FALSE;
  216. foreach ($txn->items as $p) {
  217. $prod = ec_product_load($p);
  218. if (product_is_shippable($p->vid)) {
  219. $shippable = TRUE;
  220. }
  221. $price = ec_store_adjust_misc($txn, $p);
  222. $subtotal += (product_has_quantity($p) ? $p->qty * $price : $price);
  223. $details = '';
  224. if (0 && is_array($p->data)) {
  225. foreach ($p->data as $key => $value) {
  226. if (!empty($value)) {
  227. $items[] = '<strong>' . check_plain($key) . ': </strong>' . check_plain($value);
  228. }
  229. }
  230. if (!empty($items)) {
  231. $details = theme('item_list', array('items' => $items));
  232. }
  233. }
  234. $row[] = array(
  235. array(
  236. 'data' => $p->qty,
  237. 'align' => 'center',
  238. 'valign' => 'top',
  239. ),
  240. '<em>' . check_plain($p->title) . '</em> ' . (($prod->sku != '') ?
  241. '[' . check_plain($prod->sku) . ']' : '') . '<br />' . $details,
  242. array(
  243. 'data' => format_currency($price),
  244. 'valign' => 'top',
  245. 'align' => 'right',
  246. ),
  247. );
  248. }
  249. if (is_array($txn->misc)) {
  250. foreach ($txn->misc as $misc) {
  251. if (empty($misc->seen)) {
  252. $price = isset($misc->qty) ? $misc->price * $misc->qty : $misc->price;
  253. $row[] = array(array(
  254. 'data' => "<strong>{$misc->description}</strong>: " .
  255. format_currency($price),
  256. 'colspan' => 3,
  257. 'align' => 'right',
  258. ));
  259. }
  260. }
  261. }
  262. $row[] = array(array(
  263. 'data' => '<hr size="1" noshade="noshade" />',
  264. 'colspan' => 3,
  265. 'align' => 'right',
  266. ));
  267. $row[] = array(array(
  268. 'data' => '<strong>' . t('Total:') . '</strong> ' .
  269. format_currency(ec_store_transaction_calc_gross($txn)),
  270. 'colspan' => 3,
  271. 'align' => 'right',
  272. ));
  273. }
  274. $payment_info = '<div><strong>' . t('Ordered On:') . '</strong> ' .
  275. format_date($txn->created) . '</div>';
  276. if (!empty($txn->duedate)) {
  277. $payment_info .= '<div><strong>' . t('Due Date:') . '</strong> ' .
  278. format_date($txn->duedate) . '</div>';
  279. }
  280. $payment_info .= '<div><strong>' . t('Transaction ID:') . '</strong> ' .
  281. ($trial ? t('Trial Invoice - Not Yet Posted') : $txn->txnid) . '</div>';
  282. $css = base_path() . drupal_get_path('module', 'store') . '/css/invoice.css';
  283. $site_name = t('%site-name Invoice', array('%site-name' => variable_get('site_name', 'drupal')));
  284. module_load_include('inc', 'ec_store', 'ec_store');
  285. if ($shipping_to = theme('ec_store_format_address', array('txn' => $txn, 'type' => 'shipping', 'html' => 'html'))) {
  286. $shipping_label = t('Shipping to');
  287. }
  288. if ($billing_to = theme('ec_store_format_address', array('txn' => $txn, 'type' => 'billing', 'html' => 'html'))) {
  289. $billing_label = t('Billing to');
  290. }
  291. if (!empty($txn->ship)) {
  292. $shipping_method_label = t('Shipping method:');
  293. $shipping_method = ec_store_format_shipping_method($txn);
  294. }
  295. $email_label = t('E-mail:');
  296. $items_label = t('Items ordered');
  297. $items_view = theme('table', array('header' => $header, 'rows' => $row, 'attributes' => array('cellpadding' => 3, 'cellspacing' => 3)));
  298. $payment_label = t('Payment Info');
  299. if (!empty($print_mode)) {
  300. $output .= <<<EOD
  301. <html>
  302. <head>
  303. <style type="text/css" media="all">@import url('$css');</style>
  304. </head>
  305. <body>
  306. EOD;
  307. }
  308. $output .= <<<EOD
  309. <h1>$site_name</h1>
  310. <table cellspacing="5">
  311. <tr>
  312. <th align="left">$shipping_label</th>
  313. <th align="left">$billing_label</th>
  314. </tr>
  315. <tr>
  316. <td>$shipping_to</td>
  317. <td>$billing_to</td>
  318. </tr>
  319. </table>
  320. <p><strong>$shipping_method_label</strong> $shipping_method</p>
  321. <p><strong>$email_label</strong> $txn->mail</p>
  322. <h2>$items_label</h2>
  323. $items_view
  324. <h2>$payment_label</h2>
  325. $payment_info
  326. EOD;
  327. if (!empty($print_mode)) {
  328. $output .= <<<EOD
  329. </body>
  330. </html>
  331. EOD;
  332. }
  333. if (empty($print_mode)) {
  334. return $output;
  335. }
  336. echo $output;
  337. }