uc_coupon_purchase.pages.inc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @file
  4. * Page callbacks for uc_coupon.
  5. */
  6. /**
  7. * Display a list of purchased coupons.
  8. */
  9. function uc_coupon_purchase_view($account) {
  10. drupal_set_title(t('My coupons and credits'));
  11. $result = db_query('SELECT c.* FROM {uc_coupon_purchase_users} u INNER JOIN {uc_coupons} c ON u.cid = c.cid WHERE u.uid = :uid ORDER BY valid_until ASC, code ASC', array(':uid' => $account->uid));
  12. $header = array(t('Code'), t('Value'), t('Validity'), t('Used'), t('Remaining'));
  13. $rows = array();
  14. foreach ($result as $coupon) {
  15. if ($coupon->bulk) {
  16. $coupon->data = unserialize($coupon->data);
  17. $codes = array();
  18. for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
  19. $codes[] = uc_coupon_get_bulk_code($coupon, $id);
  20. }
  21. }
  22. else {
  23. $codes = array($coupon->code);
  24. }
  25. $value = theme('uc_coupon_discount', array('coupon' => $coupon));
  26. if (!$coupon->status) {
  27. $valid = t('Inactive');
  28. }
  29. elseif (!$coupon->valid_until) {
  30. $valid = t('Unlimited');
  31. }
  32. else {
  33. $valid_from = format_date($coupon->valid_from, 'custom', variable_get('date_format_uc_store', 'm/d/Y'));
  34. $valid_until = format_date($coupon->valid_until, 'custom', variable_get('date_format_uc_store', 'm/d/Y'));
  35. $valid = $valid_from . ' - ' . $valid_until;
  36. }
  37. $usage = uc_coupon_count_usage($coupon->cid);
  38. $icon = theme('image', array('path' => drupal_get_path('module', 'uc_store') . '/images/print.gif', 'width' => t('Print')));
  39. foreach ($codes as $code) {
  40. if (substr($code, -1) == '*') {
  41. // For the base code of bulk coupons, just give a link to print all.
  42. $title = t('All %code', array('%code' => $code));
  43. $link = l($title, 'user/' . $account->uid . '/coupons/' . $coupon->cid);
  44. $link .= ' ' . l($icon, 'user/' . $account->uid . '/coupons/' . $coupon->cid . '/print', array('html' => TRUE));
  45. $rows[] = array($link, '', '', '', '');
  46. }
  47. else {
  48. $link = l($code, 'user/' . $account->uid . '/coupons/' . $coupon->cid . '/view/' . $code);
  49. $link .= ' ' . l($icon, 'user/' . $account->uid . '/coupons/' . $coupon->cid . '/print/' . $code, array('html' => TRUE));
  50. if ($coupon->type === 'credit') {
  51. $used = empty($usage['value']['codes'][$code]) ? 0 : $usage['value']['codes'][$code];
  52. $remaining = uc_currency_format($coupon->value - $used);
  53. $used = uc_currency_format($used);
  54. }
  55. else {
  56. $used = empty($usage['codes'][$code]) ? 0 : $usage['codes'][$code];
  57. $remaining = $coupon->max_uses > 0 ? $coupon->max_uses - $used : t('Unlimited');
  58. }
  59. $rows[] = array($link, $value, $valid, $used, $remaining);
  60. }
  61. }
  62. }
  63. if (count($rows)) {
  64. $output = "<p>" . t("The table below lists the discount coupons and credits available to you.") . "</p>";
  65. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  66. }
  67. else {
  68. $output = "<p>" . t('You currently have no coupons or credits available.') . "</p>";
  69. }
  70. return $output;
  71. }
  72. /**
  73. * Print a purchased coupon.
  74. */
  75. function uc_coupon_purchase_print($account, $coupon, $op = 'view', $code = NULL) {
  76. drupal_set_title(t('Coupon #@cid', array('@cid' => $coupon->cid)), PASS_THROUGH);
  77. module_load_include('inc', 'uc_coupon', 'uc_coupon.admin');
  78. $url = 'user/' . $account->uid . '/coupons/' . $coupon->cid . '/print' . ($code ? "/$code" : '');
  79. $output = '<p>' . l($coupon->bulk && !$code ? t('Print coupons') : t('Print coupon'), $url) . '</p>';
  80. // Add the owner account object to the coupon.
  81. $coupon->owner = $account;
  82. // Does not return if op = 'print'
  83. $output .= uc_coupon_print($coupon, $code, $op);
  84. return $output;
  85. }