uc_coupon.entity.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * @file
  4. * Entity Controller and Metadata Controller classes for uc_coupon.
  5. */
  6. /**
  7. * Controller class for uc_coupon entity.
  8. */
  9. class UcCouponController extends EntityAPIController {
  10. /**
  11. * @see EntityAPIController::delete()
  12. */
  13. public function delete($ids, DatabaseTransaction $transaction = NULL) {
  14. parent::delete($ids, $transaction);
  15. db_delete('uc_coupons_orders')
  16. ->condition('cid', $ids, 'IN')
  17. ->execute();
  18. }
  19. /**
  20. * @see EntityAPIController::save()
  21. */
  22. public function save($coupon, DatabaseTransaction $transaction = NULL) {
  23. if (empty($coupon->cid)) {
  24. $coupon->created = REQUEST_TIME;
  25. $coupon->bulk_seed = md5(uniqid());
  26. }
  27. parent::save($coupon, $transaction);
  28. }
  29. /**
  30. * @see EntityAPIController::buildContent()
  31. */
  32. public function buildContent($coupon, $view_mode = 'full', $langcode = NULL, $content = array()) {
  33. $rows = array();
  34. $rows[] = array(t('Name'), check_plain($coupon->name));
  35. if (!$coupon->status) {
  36. $status = t('Inactive');
  37. }
  38. elseif (!$coupon->valid_until) {
  39. $status = t('Active');
  40. }
  41. elseif (REQUEST_TIME < $coupon->valid_from) {
  42. $status = t('Not valid until @date', array('@date' => _uc_coupon_format_date($coupon->valid_from, variable_get('date_format_uc_store', 'm/d/Y H:iT'))));
  43. }
  44. elseif (REQUEST_TIME > $coupon->valid_until) {
  45. $status = t('Expired on @date', array('@date' => _uc_coupon_format_date($coupon->valid_until, variable_get('date_format_uc_store', 'm/d/Y H:iT'))));
  46. }
  47. else {
  48. $status = t('Active until @date', array('@date' => _uc_coupon_format_date($coupon->valid_until, variable_get('date_format_uc_store', 'm/d/Y H:iT'))));
  49. }
  50. $rows[] = array(t('Status'), $status);
  51. if (!$coupon->bulk) {
  52. $rows[] = array(t('Code'), check_plain($coupon->code) . ' (' . l(t('Print'), 'admin/store/coupons/' . $coupon->cid . '/print') . ')');
  53. if ($coupon->max_uses) {
  54. $rows[] = array(t('Maximum uses'), $coupon->max_uses);
  55. }
  56. }
  57. else {
  58. $codes = '<strong>' . check_plain($coupon->code) . ' &times; ' . check_plain($coupon->data['bulk_number']) . '</strong>';
  59. $codes .= ' (' . l(t('Download codes'), 'admin/store/coupons/' . $coupon->cid . '/codes') . ')';
  60. $codes .= ' (' . l(t('Print all'), 'admin/store/coupons/' . $coupon->cid . '/print') . ')';
  61. for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
  62. $code = uc_coupon_get_bulk_code($coupon, $id);
  63. $codes .= '<br />' . check_plain($code) . ' (' . l(t('Print'), 'admin/store/coupons/' . $coupon->cid . '/print/' . $code) . ')';
  64. }
  65. $rows[] = array(t('Codes'), $codes);
  66. //$rows[] = array(t('Bulk seed'), check_plain($coupon->bulk_seed));
  67. if ($coupon->max_uses) {
  68. $rows[] = array(t('Maximum uses per code'), $coupon->max_uses);
  69. }
  70. }
  71. $rows[] = array(t('Discount value'), theme('uc_coupon_discount', array('coupon' => $coupon)));
  72. switch ($coupon->data['apply_to']) {
  73. case 'subtotal':
  74. $applies = t('Order subtotal');
  75. break;
  76. case 'products_total':
  77. $applies = t('Total of matching products');
  78. break;
  79. case 'products':
  80. $applies = t('Matching products');
  81. break;
  82. case 'cheapest':
  83. $applies = format_plural($coupon->data['apply_count'], 'Cheapest product', '@count cheapest products');
  84. break;
  85. case 'expensive':
  86. $applies = format_plural($coupon->data['apply_count'], 'Most expensive product', '@count most expensive products');
  87. break;
  88. }
  89. $rows[] = array(t('Applied to'), $applies);
  90. if ($coupon->data['apply_to'] != 'subtotal') {
  91. $restrict = array();
  92. if (isset($coupon->data['product_types'])) {
  93. $key = format_plural(count($coupon->data['product_types']), 'All products in class', 'All products in classes');
  94. $restrict[$key] = $coupon->data['product_types'];
  95. }
  96. if (isset($coupon->data['products'])) {
  97. $products = array();
  98. foreach ($coupon->data['products'] as $nid) {
  99. $products[] = check_plain(db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))->fetchField());
  100. }
  101. if (isset($coupon->data['negate_products'])) {
  102. $restrict[t('All products except')] = $products;
  103. }
  104. else {
  105. $restrict[format_plural(count($products), 'Product', 'Products')] = $products;
  106. }
  107. }
  108. if (isset($coupon->data['skus'])) {
  109. $restrict[format_plural(count($coupon->data['skus']), 'SKU', 'SKUs')] = $coupon->data['skus'];
  110. }
  111. if (isset($coupon->data['terms'])) {
  112. $terms = array();
  113. foreach ($coupon->data['terms'] as $tid) {
  114. $terms[] = check_plain(db_query("SELECT name FROM {taxonomy_term_data} WHERE tid = :tid", array(':tid' => $tid))->fetchField());
  115. }
  116. if (isset($coupon->data['negate_terms'])) {
  117. $restrict[t('All taxonomy terms except')] = $terms;
  118. }
  119. else {
  120. $restrict[format_plural(count($terms), 'Taxonomy term', 'Taxonomy terms')] = $terms;
  121. }
  122. }
  123. if ($restrict) {
  124. $or = FALSE;
  125. foreach ($restrict as $title => &$restriction) {
  126. if ($or) {
  127. $title = t('or') . ' ' . $title;
  128. }
  129. $restriction = $title . ': <em>' . implode('</em>, <em>', $restriction) . '</em>';
  130. $or = TRUE;
  131. }
  132. $rows[] = array(t('Product restrictions'), implode($restrict, '<br />'));
  133. }
  134. }
  135. $restrict = array();
  136. if (isset($coupon->data['users'])) {
  137. $users = array();
  138. foreach ($coupon->data['users'] as $uid) {
  139. $users[] = check_plain(db_query("SELECT name FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField());
  140. }
  141. if (isset($coupon->data['negate_users'])) {
  142. $restrict[t('All users except')] = $users;
  143. }
  144. else {
  145. $restrict[format_plural(count($users), 'User', 'Users')] = $users;
  146. }
  147. }
  148. if (isset($coupon->data['max_uses_per_user'])) {
  149. $restrict['Maximum uses per user'] = array($coupon->data['max_uses_per_user']);
  150. }
  151. if (isset($coupon->data['roles'])) {
  152. if (isset($coupon->data['negate_roles'])) {
  153. $restrict[t('All roles except')] = $coupon->data['roles'];
  154. }
  155. else {
  156. $restrict[format_plural(count($users), 'Role', 'Roles')] = $coupon->data['roles'];
  157. }
  158. }
  159. if ($restrict) {
  160. foreach ($restrict as $title => &$restriction) {
  161. $restriction = $title . ': <em>' . implode('</em>, <em>', $restriction) . '</em>';
  162. }
  163. $rows[] = array(t('User restrictions'), implode($restrict, '<br />'));
  164. }
  165. if ($coupon->minimum_order > 0) {
  166. $rows[] = array(t('Order restrictions'), t('Minimum subtotal') . ': <em>' . uc_currency_format($coupon->minimum_order) . '</em>');
  167. }
  168. $combo_list = array();
  169. if (!empty($coupon->data['combinations'])) {
  170. $combos = db_query('SELECT cid, name FROM {uc_coupons} where cid IN (:cids)', array(':cids' => $coupon->data['combinations']));
  171. foreach ($combos as $combo) {
  172. $combo_list[] = check_plain("$combo->name [cid:$combo->cid]");
  173. }
  174. }
  175. if (isset($coupon->data['negate_combinations'])) {
  176. $ctext = empty($combo_list) ? t('Any.') : t('Any but:') . ' ' . implode(', ', $combo_list);
  177. }
  178. else {
  179. $ctext = empty($combo_list) ? t('None.') : t('Only:') . ' ' . implode(', ', $combo_list);
  180. }
  181. $rows[] = array(t('Allowed Combinations'), $ctext);
  182. foreach ($rows as &$row) {
  183. $row[0] = array(
  184. 'header' => TRUE,
  185. 'data' => $row[0],
  186. );
  187. }
  188. $content['admin_summary'] = array(
  189. '#title' => t('Administrative Summary'),
  190. '#theme' => 'table',
  191. '#rows' => $rows
  192. );
  193. return parent::buildContent($coupon, $view_mode, $langcode, $content);
  194. }
  195. }
  196. /**
  197. * Metadata Controller for uc_coupon entity
  198. */
  199. class UcCouponMetadataController extends EntityDefaultMetadataController {
  200. public function entityPropertyInfo() {
  201. $prop_info = parent::entityPropertyInfo();
  202. $props = &$prop_info['uc_coupon']['properties'];
  203. //dpm($prop_info);
  204. // Copy the descriptions from the schema. Drupal discards this information, so we have to
  205. // call uc_order_schema() directly.
  206. module_load_include('install', 'uc_coupon', 'uc_coupon');
  207. $schema = uc_coupon_schema();
  208. foreach ($schema['uc_coupons']['fields'] as $name => $info) {
  209. if (is_array($props[$name]) && !empty($info['description'])) {
  210. $props[$name]['description'] = $info['description'];
  211. }
  212. }
  213. $props['type']['options list'] = '_uc_coupon_type_options';
  214. $props['status']['options list']['type'] = 'boolean';
  215. $props['bulk']['type'] = 'boolean';
  216. // Set the correct type for the date properties.
  217. foreach (array('created', 'valid_from', 'valid_until') as $key) {
  218. $props[$key]['type'] = 'date';
  219. $props[$key]['getter callback'] = 'entity_property_verbatim_date_get';
  220. }
  221. // Unpack some of the 'data' properties.
  222. unset($props['data']);
  223. $props['products'] = array(
  224. 'type' => 'list<node>',
  225. 'label' => t('Products'),
  226. 'description' => t('The applicable products for this coupon.'),
  227. 'getter callback' => 'uc_coupon_data_property_get',
  228. 'setter callback' => 'uc_coupon_data_property_set',
  229. );
  230. $props['negate_products'] = array(
  231. 'type' => 'boolean',
  232. 'label' => t('Negate applicable products'),
  233. 'description' => t('Whether or not the products list represents allowed or disallowed products'),
  234. 'getter callback' => 'uc_coupon_data_property_get',
  235. 'setter callback' => 'uc_coupon_data_property_set',
  236. );
  237. $props['base_coupon'] = array(
  238. 'type' => 'uc_coupon',
  239. 'description' => t('The coupon on which a purchased or assigned coupon is based'),
  240. 'label' => t('Base coupon'),
  241. 'getter callback' => 'uc_coupon_data_property_get',
  242. 'setter callback' => 'uc_coupon_data_property_set'
  243. );
  244. return $prop_info;
  245. }
  246. }
  247. /**
  248. * Generic getter to extract properties from the coupon data array.
  249. */
  250. function uc_coupon_data_property_get($coupon, array $options, $name, $entity_type) {
  251. switch ($name) {
  252. case 'base_coupon':
  253. return empty($coupon->data['base_cid']) ? FALSE : uc_coupon_load($coupon->data['base_cid']);
  254. break;
  255. default:
  256. if (!isset($coupon->data[$name])) {
  257. return NULL;
  258. }
  259. elseif (is_array($coupon->data[$name])) {
  260. return array_values($coupon->data[$name]);
  261. }
  262. else {
  263. return $coupon->data[$name];
  264. }
  265. }
  266. }
  267. /**
  268. * Generic setter to update properties from the coupon data array.
  269. */
  270. function uc_coupon_data_property_set($coupon, $name, $value) {
  271. switch ($name) {
  272. case 'base_coupon':
  273. $coupon->data[$name] = $value->cid;
  274. break;
  275. default:
  276. if (is_array($value)) {
  277. $coupon->data[$name] = drupal_map_assoc($value);
  278. }
  279. else {
  280. $coupon->data[$name] = $name;
  281. }
  282. }
  283. }
  284. /**
  285. * Options callback for the coupon type property.
  286. */
  287. function _uc_coupon_type_options() {
  288. return array(
  289. 'price' => t('Fixed discount'),
  290. 'percentage' => t('Percentage'),
  291. 'set_price' => t('Set product price'),
  292. 'credit' => t('Store credit')
  293. );
  294. }
  295. /**
  296. * Options callback for the coupon status property
  297. */
  298. function _uc_coupon_status_options() {
  299. return array(
  300. 0 => t('Inactive'),
  301. 1 => t('Active'),
  302. );
  303. }