uc_coupon.admin.inc 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. <?php
  2. /**
  3. * @file
  4. * Discount Coupons administration pages.
  5. */
  6. /**
  7. * Store-wide coupon settings form.
  8. */
  9. function uc_coupon_settings_form($form, &$form_state) {
  10. global $base_url;
  11. $form['uc_coupon_line_item_format'] = array(
  12. '#type' => 'textfield',
  13. '#title' => t('Default line item title format'),
  14. '#description' => t('Controls the way coupons appear in the cart and order summary. This setting be overridden by individual coupons.'),
  15. '#default_value' => variable_get('uc_coupon_line_item_format', t('Coupon !code', array('!code' => '[uc_coupon:code]'))),
  16. );
  17. if (module_exists('token')) {
  18. $form['uc_coupon_line_item_format']['#description'] .= ' ' . t('You may use any of the following replacement patterns.');
  19. $form['uc_coupon_line_item_format']['#suffix'] = theme('token_tree', array('token_types' => array('uc_coupon')));
  20. }
  21. $form['uc_coupon_show_in_cart'] = array(
  22. '#type' => 'checkbox',
  23. '#title' => t('Show coupons as an item in the cart.'),
  24. '#default_value' => variable_get('uc_coupon_show_in_cart', TRUE),
  25. );
  26. $options = array();
  27. for ($i = -10; $i <= 10; $i++) {
  28. $options[$i] = $i;
  29. }
  30. $form['uc_li_coupon_weight'] = array(
  31. '#type' => 'select',
  32. '#title' => t('Line item weight'),
  33. '#description' => t('Controls the placement of the coupon line item in the order summary. Heavier items appear lower in the list.'),
  34. '#options' => $options,
  35. '#default_value' => variable_get('uc_li_coupon_weight', 0),
  36. );
  37. $form['uc_coupon_form_components'] = array(
  38. '#title' => t('Coupon submission form components'),
  39. '#description' => t('Select what to display on the coupon submission form (this appears in the coupon block, cart and checkout panes).
  40. Note that you must enable the coupon code entry field in order for customers to submit coupons. Also, if coupons are not displayed
  41. in the cart (see above), you must enable either the active coupons table or the active coupons list in order for custmers to be able
  42. to remove coupons.'),
  43. '#type' => 'checkboxes',
  44. '#options' => array(
  45. 'entry' => t('Coupon code entry field'),
  46. 'table' => t('Active coupons (table)'),
  47. 'list' => t('Active coupons (list)'),
  48. ),
  49. '#default_value' => variable_get('uc_coupon_form_components',
  50. drupal_map_assoc(variable_get('uc_coupon_allow_multiple', FALSE) ? array('entry') : array('entry', 'list'))),
  51. );
  52. $form['uc_coupon_allow_multiple'] = array(
  53. '#type' => 'checkbox',
  54. '#title' => t('Allow multiple coupons to apply to a single order.'),
  55. '#default_value' => variable_get('uc_coupon_allow_multiple', FALSE),
  56. '#description' => t('Note, you must enable this feature if you have configured multiple automatic coupons,
  57. even if the restrictions for those coupons would prevent them ever applying to the same order.'),
  58. );
  59. $form['uc_coupon_default_usage'] = array(
  60. '#type' => 'radios',
  61. '#title' => t('Default redemption setting for new coupons'),
  62. '#options' => array(
  63. 'single' => t('Single use per code.'),
  64. 'multi' => t('Multiple uses per code.'),
  65. ),
  66. '#default_value' => variable_get('uc_coupon_default_usage', 'single'),
  67. );
  68. $form['uc_coupon_used_order_status'] = array(
  69. '#type' => 'select',
  70. '#title' => t('Order status to mark a coupon used'),
  71. '#options' => uc_order_status_options_list(),
  72. '#default_value' => variable_get('uc_coupon_used_order_status', 'processing'),
  73. '#description' => t('A coupon will be considered used when it has been applied to an order that reaches this status
  74. (or a later one). If the order status subsequenty reverts to an earlier status, the coupon use will also be reverted.'),
  75. );
  76. $form['uc_coupon_querystring'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Query string parameter'),
  79. '#description' => t('If set, coupon codes can be applied by including them in any URL. Leave blank to disable this feature.<br />Example: if set to "coupon", visiting !base_url?coupon=CODE will apply the CODE coupon.', array('!base_url' => $base_url)),
  80. '#default_value' => variable_get('uc_coupon_querystring', ''),
  81. '#size' => 15,
  82. );
  83. $form['uc_coupon_expiry'] = array(
  84. '#title' => t('Coupon activation/expiration time'),
  85. '#type' => 'item',
  86. '#description' => t('Date-restricted coupons will activate and expire at this hour on the dates specified for the
  87. individual coupon. Note that changing this setting will not effect coupons which have already been created. To
  88. update the activation/expiration times of existing coupons, edit and save them.'),
  89. );
  90. $form['uc_coupon_expiry']['uc_coupon_expiry_hour'] = array(
  91. '#type' => 'select',
  92. '#options' => drupal_map_assoc(range(0, 23, 1), '_uc_coupon_format_hour'),
  93. '#default_value' => variable_get('uc_coupon_expiry_hour', 0),
  94. );
  95. $form['uc_coupon_expiry']['uc_coupon_expiry_timezone'] = array(
  96. '#type' => 'radios',
  97. '#options' => array( 0 => 'UTC', 1 => 'Local'),
  98. '#default_value' => variable_get('uc_coupon_expiry_timezone', 0),
  99. );
  100. return system_settings_form($form);
  101. }
  102. /**
  103. * Display a brief overview of system coupons
  104. *
  105. * @param $view_type
  106. * 'active' or 'inactive'
  107. */
  108. function uc_coupon_display($view_type = 'active') {
  109. _uc_coupon_paypal_check();
  110. $header = array(
  111. array('data' => t('Actions')),
  112. array(
  113. 'data' => t('Name'),
  114. 'field' => 'name',
  115. ),
  116. array(
  117. 'data' => t('Code'),
  118. 'field' => 'code',
  119. 'sort' => 'asc',
  120. ),
  121. array(
  122. 'data' => t('Value'),
  123. 'field' => 'value',
  124. ),
  125. array(
  126. 'data' => t('Created'),
  127. 'field' => 'created',
  128. ),
  129. array(
  130. 'data' => t('Valid from'),
  131. 'field' => 'valid_from',
  132. ),
  133. array(
  134. 'data' => t('Valid until'),
  135. 'field' => 'valid_until',
  136. ),
  137. );
  138. $query = db_select('uc_coupons', 'c')
  139. ->extend('TableSort')
  140. ->extend('PagerDefault')
  141. ->orderByHeader($header);
  142. $query->condition('c.status', $view_type == 'inactive' ? 0 : 1)->fields('c');
  143. $rows = array();
  144. $result = $query->execute();
  145. foreach ($result as $coupon) {
  146. $coupon->data = $coupon->data ? unserialize($coupon->data) : array();
  147. $rows[] = array(
  148. theme('uc_coupon_actions', array('coupon' => $coupon)),
  149. check_plain($coupon->name),
  150. check_plain($coupon->code) . ($coupon->bulk ? '* ' . t('(bulk)') : ''),
  151. theme('uc_coupon_discount', array('coupon' => $coupon)),
  152. _uc_coupon_format_date($coupon->created, variable_get('date_format_uc_store', 'm/d/Y')),
  153. $coupon->valid_from ? _uc_coupon_format_date($coupon->valid_from, variable_get('date_format_uc_store', 'm/d/Y H:iT')) : '-',
  154. $coupon->valid_until ? _uc_coupon_format_date($coupon->valid_until, variable_get('date_format_uc_store', 'm/d/Y H:iT')) : '-',
  155. );
  156. }
  157. if (count($rows)) {
  158. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('width' => '100%')));
  159. $output .= theme('pager', array('tags' => NULL));
  160. }
  161. else {
  162. switch ($view_type) {
  163. case 'active':
  164. $output = '<p>' . t('There are currently no active coupons in the system.') . '</p>';
  165. break;
  166. case 'inactive':
  167. $output = '<p>' . t('There are currently no inactive coupons in the system.') . '</p>';
  168. break;
  169. }
  170. }
  171. $output .= '<p>' . l(t('Add a new coupon.'), 'admin/store/coupons/add') . '</p>';
  172. return $output;
  173. }
  174. /**
  175. * View coupon details.
  176. *
  177. * @param $coupon object
  178. * Coupon to view.
  179. */
  180. function uc_coupon_view($coupon) {
  181. return entity_view('uc_coupon', array($coupon));
  182. }
  183. /**
  184. * Print a coupon, or set of bulk coupons.
  185. */
  186. function uc_coupon_print($coupon, $code = NULL, $op = 'view') {
  187. if ($code) {
  188. $codes = array($code);
  189. }
  190. elseif (!$coupon->bulk) {
  191. $codes = array($coupon->code);
  192. }
  193. else {
  194. $codes = array();
  195. for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
  196. $codes[] = uc_coupon_get_bulk_code($coupon, $id);
  197. }
  198. }
  199. $output = '';
  200. foreach ($codes as $code) {
  201. $output .= theme('uc_coupon_certificate', array('coupon' => $coupon, 'code' => $code));
  202. }
  203. drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.css');
  204. if ($op == 'print') {
  205. echo theme('uc_coupon_page', array('content' => $output));
  206. exit;
  207. }
  208. return $output;
  209. }
  210. /**
  211. * Coupon add/edit form.
  212. *
  213. * @param $coupon object
  214. * Coupon object, or NULL to add a new coupon.
  215. */
  216. function uc_coupon_add_form($form, &$form_state, $coupon = NULL) {
  217. _uc_coupon_paypal_check();
  218. drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.css');
  219. drupal_add_js(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.admin.js');
  220. if ($coupon) {
  221. $value = $coupon;
  222. $usage = uc_coupon_count_usage($coupon->cid);
  223. $used = array_sum($usage['codes']);
  224. $form['#uc_coupon_cid'] = $value->cid;
  225. $form['#uc_coupon'] = $value;
  226. $form['#uc_coupon_used'] = $used;
  227. $use_validity = $value->valid_until;
  228. }
  229. else {
  230. $use_validity = FALSE;
  231. $value = new stdClass;
  232. $value->minimum_order = 0;
  233. $value->data['apply_to'] = 'subtotal';
  234. $value->max_uses = variable_get('uc_coupon_default_usage', 'single') == 'single' ? 1 : 0;
  235. $used = 0;
  236. }
  237. if (!$use_validity) {
  238. $value->valid_from = REQUEST_TIME;
  239. $value->valid_until = REQUEST_TIME;
  240. }
  241. // Handle AJAX functionality.
  242. $ajax_fields = array(
  243. 'products' => 'admin/store/coupons/autocomplete/node',
  244. 'skus' => '',
  245. 'terms' => 'admin/store/coupons/autocomplete/term',
  246. 'users' => 'admin/store/coupons/autocomplete/user',
  247. 'roles' => 'admin/store/coupons/autocomplete/role',
  248. );
  249. if (empty($form_state['ajax_submission'])) {
  250. // Set the initial field count.
  251. foreach (array_keys($ajax_fields) as $field) {
  252. $form_state['storage']['field_count'][$field] = isset($value->data[$field]) ? (count($value->data[$field]) + 3) : 3;
  253. }
  254. }
  255. else {
  256. // Use data from the AJAX submission.
  257. $value->data = $form_state['coupon_data'];
  258. // Increase the count for the selected field.
  259. $form_state['storage']['field_count'][$form_state['ajax_submission']] += 3;
  260. // Reset form action, otherwise validation errors go wrong.
  261. $form['#action'] = $form_state['action'];
  262. }
  263. foreach (array_keys($ajax_fields) as $field) {
  264. if (!isset($value->data[$field])) {
  265. $value->data[$field] = array();
  266. }
  267. }
  268. $form['name'] = array(
  269. '#type' => 'textfield',
  270. '#title' => t('Coupon name'),
  271. '#default_value' => isset($value->name) ? $value->name : '',
  272. '#required' => TRUE,
  273. '#weight' => -15,
  274. );
  275. $form['code'] = array(
  276. '#type' => 'textfield',
  277. '#title' => t('Coupon code'),
  278. '#description' => t('Coupon codes cannot be changed once they have been used in an order.'),
  279. '#default_value' => isset($value->code) ? $value->code : '',
  280. '#size' => 25,
  281. '#required' => !$used,
  282. '#maxlength' => 50,
  283. '#disabled' => $used,
  284. '#weight' => -10,
  285. );
  286. $form['bulk'] = array(
  287. '#type' => 'fieldset',
  288. '#title' => t('Bulk coupon codes'),
  289. '#description' => t('The coupon code entered above will be used to prefix each generated code.'),
  290. '#collapsible' => TRUE,
  291. '#collapsed' => isset($value->bulk) ? !$value->bulk : TRUE,
  292. '#weight' => -5,
  293. );
  294. $form['bulk']['bulk_generate'] = array(
  295. '#type' => 'checkbox',
  296. '#title' => t('Enable bulk generation of coupon codes.'),
  297. '#default_value' => isset($value->bulk) ? $value->bulk : FALSE,
  298. '#disabled' => $used,
  299. );
  300. $form['bulk']['bulk_number'] = array(
  301. '#type' => 'textfield',
  302. '#title' => t('Number of codes to generate'),
  303. '#default_value' => isset($value->data['bulk_number']) ? $value->data['bulk_number'] : NULL,
  304. '#size' => 10,
  305. '#maxlength' => 10,
  306. '#description' => $used ? t('You cannot decrease this number, because this coupon has already been used.') : '',
  307. );
  308. $form['bulk']['bulk_length'] = array(
  309. '#type' => 'select',
  310. '#title' => t('Code length'),
  311. '#description' => t('The number of characters selected here will be appended to the code entered above when generating bulk coupons.'),
  312. '#default_value' => isset($value->data['bulk_length']) ? $value->data['bulk_length'] : NULL,
  313. '#options' => drupal_map_assoc(range(8, 30)),
  314. '#disabled' => $used,
  315. );
  316. $form['status'] = array(
  317. '#type' => 'checkbox',
  318. '#title' => t('Coupon is active.'),
  319. '#description' => t('Only active coupons can be used at checkout.'),
  320. '#default_value' => isset($value->status) ? $value->status : FALSE,
  321. );
  322. $form['discount'] = array(
  323. '#type' => 'textfield',
  324. '#title' => t('Discount'),
  325. '#default_value' => isset($coupon->value) ? _uc_coupon_format_discount($value, FALSE) : '',
  326. '#size' => 6,
  327. '#description' => t('Four discount types are possible:') . theme('item_list', array('items' => array(
  328. t('Fixed value discount; enter "15" for @p off.', array('@p' => uc_currency_format(15))),
  329. t('Store credit coupons; enter "15" and check the box below for store credit of @p.', array('@p' => uc_currency_format(15))),
  330. t('Percentage discount; enter "15%" for 15% off.'),
  331. t('Set product price; enter "=15" to make all matching products @p after discount.', array('@p' => uc_currency_format(15))),
  332. ))),
  333. '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
  334. '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  335. '#required' => TRUE,
  336. );
  337. $form['store_credit'] = array(
  338. '#type' => 'checkbox',
  339. '#title' => t('Store Credit'),
  340. '#default_value' => (!empty($value->type) && $value->type === 'credit') ? 1 : 0,
  341. '#description' => t('When checked, the coupon value is applied cumulatively across multiple orders,
  342. and the coupon is valid until this has been exhausted. There are no additional limits
  343. on the number of times the coupon can be used. Usage is always reckoned per-code,
  344. (i.e., each bulk code is good for the full face value of the coupon). '),
  345. '#prefix' => '<div id="store-credit-wrapper">',
  346. '#suffix' => '</div>'
  347. );
  348. $form['apply_to'] = array(
  349. '#type' => 'radios',
  350. '#title' => t('Apply discount to'),
  351. '#options' => array(
  352. 'subtotal' => t('Order subtotal.'),
  353. 'products_total' => t('Total of all matching products and line items.'),
  354. 'products' => t('All matching products and line items. Face value will be applied to each.'),
  355. 'cheapest' => t('The cheapest matching products and all matching line items. Face value will be applied to each.'),
  356. 'expensive' => t('The most expensive matching products and all matching line items. Face value will be applied to each.'),
  357. ),
  358. '#description' => t('Coupons with no restrictions (as specified below) will match all products. However, you must explicitly specify any line items you wish to discount.'),
  359. '#default_value' => $value->data['apply_to'],
  360. );
  361. $form['apply_count'] = array(
  362. '#type' => 'textfield',
  363. '#title' => t('Maximum number of products to discount'),
  364. '#description' => t('Note: this number applies only to products. Applicable line items (as specified below) will always be discounted.'),
  365. '#default_value' => isset($value->data['apply_count']) ? $value->data['apply_count'] : '',
  366. '#size' => 5,
  367. );
  368. $form['require_match_all'] = array(
  369. '#type' => 'checkbox',
  370. '#title' => t('Require all products to match'),
  371. '#default_value' => isset($value->data['require_match_all']) ? TRUE : FALSE,
  372. '#description' => t('If checked, the coupon will be rejected if an order contains any non-applicable products as configured below.'),
  373. );
  374. $form['date_restrictions'] = array(
  375. '#type' => 'fieldset',
  376. '#title' => t('Date restrictions'),
  377. '#collapsible' => TRUE,
  378. '#collapsed' => $use_validity == 0,
  379. );
  380. $form['date_restrictions']['use_validity'] = array(
  381. '#type' => 'checkbox',
  382. '#title' => t('Restrict coupon by date.'),
  383. '#default_value' => $use_validity != 0,
  384. );
  385. $zone = variable_get('uc_coupon_expiry_timezone', 0) ? '(local time)' : '(UTC)';
  386. $hour = _uc_coupon_format_hour(variable_get('uc_coupon_expiry_hour', 0));
  387. $link = l('store-wide coupon settings page', 'admin/store/settings/coupon', array('query' => array('destination' => $_GET['q'])));
  388. $form['date_restrictions']['valid_from'] = array(
  389. '#type' => 'date',
  390. '#title' => t('Start date'),
  391. '#default_value' => array(
  392. 'year' => _uc_coupon_format_date($value->valid_from, 'Y'),
  393. 'month' => _uc_coupon_format_date($value->valid_from, 'n'),
  394. 'day' => _uc_coupon_format_date($value->valid_from, 'j'),
  395. ),
  396. '#description' => t('Coupon will activate at @hour @zone on the specified date. The time/zone may be changed on the !link',
  397. array('@hour' => $hour, '@zone' => $zone, '!link' => $link)),
  398. '#after_build' => array('_uc_coupon_date_range'),
  399. );
  400. $form['date_restrictions']['valid_until'] = array(
  401. '#type' => 'date',
  402. '#title' => t('Expiry date'),
  403. '#default_value' => array(
  404. 'year' => _uc_coupon_format_date($value->valid_until, 'Y'),
  405. 'month' => _uc_coupon_format_date($value->valid_until, 'n'),
  406. 'day' => _uc_coupon_format_date($value->valid_until, 'j'),
  407. ),
  408. '#description' => t('Coupon will expire at @hour @zone on the specified date. The time/zone may be changed on the !link',
  409. array('@hour' => $hour, '@zone' => $zone, '!link' => $link)),
  410. '#after_build' => array('_uc_coupon_date_range'),
  411. );
  412. $form['usage_limits'] = array(
  413. '#type' => 'fieldset',
  414. '#collapsible' => TRUE,
  415. '#collapsed' => empty($value->max_uses) && empty($value->data['max_uses_per_user']),
  416. '#title' => t('Coupon usage limits'),
  417. '#tree' => FALSE,
  418. );
  419. $form['usage_limits']['max_uses'] = array(
  420. '#type' => 'textfield',
  421. '#title' => t('Maximum number of redemptions (per code)'),
  422. '#default_value' => $value->max_uses ? $value->max_uses : '',
  423. '#description' => t('If set, the coupon can only be used this number of times. For bulk coupons, this applies to each available code.'),
  424. '#size' => 5,
  425. );
  426. $form['usage_limits']['max_uses_per_user'] = array(
  427. '#type' => 'textfield',
  428. '#title' => t('Maximum number of redemptions (per user)'),
  429. '#default_value' => isset($value->data['max_uses_per_user']) ? $value->data['max_uses_per_user'] : '',
  430. '#description' => t('If set, each user can only use the coupon this number of times.'),
  431. '#size' => 5,
  432. );
  433. $form['order'] = array(
  434. '#type' => 'fieldset',
  435. '#title' => t('Minimum order limits'),
  436. '#collapsible' => TRUE,
  437. '#collapsed' => $value->minimum_order < 0.01 && !isset($value->data['minimum_qty']),
  438. );
  439. $form['order']['minimum_order'] = array(
  440. '#type' => 'textfield',
  441. '#title' => t('Minimum order total'),
  442. '#default_value' => $value->minimum_order < 0.01 ? '' : $value->minimum_order,
  443. '#size' => 6,
  444. '#description' => t('If set, the subtotal of products must be at least this amount for the coupon to be accepted.'),
  445. '#field_prefix' => variable_get('uc_sign_after_amount', FALSE) ? '' : variable_get('uc_currency_sign', '$'),
  446. '#field_suffix' => variable_get('uc_sign_after_amount', FALSE) ? variable_get('uc_currency_sign', '$') : '',
  447. );
  448. $form['order']['minimum_qty'] = array(
  449. '#type' => 'textfield',
  450. '#title' => t('Minimum order quantity'),
  451. '#default_value' => isset($value->data['minimum_qty']) ? $value->data['minimum_qty'] : '',
  452. '#size' => 5,
  453. '#description' => t('If set, at least this many products must be in the cart for the coupon to be accepted.'),
  454. );
  455. $form['order']['minimum_qty_restrict'] = array(
  456. '#type' => 'checkbox',
  457. '#title' => t('Only count applicable products'),
  458. '#default_value' => isset($value->data['minimum_qty_restrict']) ? TRUE : FALSE,
  459. '#description' => t('If checked, only applicable products as configured below will count towards the minimum quantity or total.'),
  460. );
  461. $form['line_items'] = array(
  462. '#type' => 'fieldset',
  463. '#title' => t('Applicable line items'),
  464. '#description' => t('Select any line items to which this coupon should apply.'),
  465. '#collapsible' => TRUE,
  466. '#collapsed' => empty($value->data['line_items']),
  467. );
  468. $options = array();
  469. foreach (_uc_line_item_list() as $id => $line_item) {
  470. if (!in_array($id, array('subtotal', 'tax_subtotal', 'total', 'tax_display', 'coupon', 'tax'))) {
  471. $options[$id] = $line_item['title'];
  472. }
  473. }
  474. $form['line_items']['line_items'] = array(
  475. '#type' => 'checkboxes',
  476. '#options' => $options,
  477. '#default_value' => isset($value->data['line_items']) ? $value->data['line_items'] : array(),
  478. );
  479. $form['product_types'] = array(
  480. '#type' => 'fieldset',
  481. '#title' => t('Applicable product classes'),
  482. '#description' => t('Selecting a class will apply this coupon to <strong>all</strong> products of the class, overriding other restrictions except for products or terms explicitly excluded below.'),
  483. '#collapsible' => TRUE,
  484. '#collapsed' => empty($value->data['product_types']),
  485. );
  486. $form['product_types']['product_types'] = array(
  487. '#type' => 'checkboxes',
  488. '#options' => array_map('check_plain', array_intersect_key(node_type_get_names(), drupal_map_assoc(uc_product_types()))),
  489. '#default_value' => isset($value->data['product_types']) ? $value->data['product_types'] : array(),
  490. );
  491. $form['products'] = array(
  492. '#type' => 'fieldset',
  493. '#title' => t('Applicable products'),
  494. '#description' => t('Enter one or more products below to restrict this coupon to a set of products, regardless of any product attributes. Discounts will apply to each matching product.'),
  495. );
  496. $form['products']['negate_products'] = array(
  497. '#type' => 'radios',
  498. '#default_value' => isset($value->data['negate_products']) ? 1 : 0,
  499. '#options' => array(
  500. 0 => t('Apply coupon to products listed below.'),
  501. 1 => t('Apply coupon to all products except those listed below.'),
  502. ),
  503. '#tree' => FALSE,
  504. );
  505. foreach ($value->data['products'] as $nid) {
  506. $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  507. $form['products'][] = array(
  508. '#type' => 'textfield',
  509. '#default_value' => $title . ' [nid:' . $nid . ']',
  510. '#autocomplete_path' => 'admin/store/coupons/autocomplete/node',
  511. '#maxlength' => 300,
  512. );
  513. }
  514. $form['skus'] = array(
  515. '#type' => 'fieldset',
  516. '#title' => t('Applicable SKUs'),
  517. '#description' => t('Enter one or more SKUs below to restrict this coupon to a set of SKUs, allowing coupons to apply to specific products or attribute options. Discounts will apply to matching SKUs. Wildcards are supported, e.g. "E*" will match all products with SKUs beginning with E.'),
  518. );
  519. foreach ($value->data['skus'] as $sku) {
  520. $form['skus'][] = array(
  521. '#type' => 'textfield',
  522. '#default_value' => $sku,
  523. );
  524. }
  525. $form['terms'] = array(
  526. '#type' => 'fieldset',
  527. '#title' => t('Applicable taxonomy terms'),
  528. '#description' => t('Enter one or more taxonomy terms (categories) below to restrict this coupon to a set of products. Discounts will apply to all matching products with these terms.'),
  529. '#maxlength' => 300,
  530. );
  531. $form['terms']['negate_terms'] = array(
  532. '#type' => 'radios',
  533. '#default_value' => isset($value->data['negate_terms']) ? 1 : 0,
  534. '#options' => array(
  535. 0 => t('Apply coupon to products with terms listed below.'),
  536. 1 => t('Apply coupon to all products except those with terms listed below.'),
  537. ),
  538. '#tree' => FALSE,
  539. );
  540. foreach ($value->data['terms'] as $tid) {
  541. $name = db_query('SELECT name FROM {taxonomy_term_data} WHERE tid = :tid', array(':tid' => $tid))->fetchField();
  542. $form['terms'][] = array(
  543. '#type' => 'textfield',
  544. '#default_value' => $name . ' [tid:' . $tid . ']',
  545. '#autocomplete_path' => 'admin/store/coupons/autocomplete/term',
  546. );
  547. }
  548. $form['users'] = array(
  549. '#type' => 'fieldset',
  550. '#title' => t('User restrictions'),
  551. '#description' => t('Enter one or more user names and/or "anonymous users" below to restrict this coupon by user.'),
  552. );
  553. $form['users']['negate_users'] = array(
  554. '#type' => 'radios',
  555. '#default_value' => isset($value->data['negate_users']) ? 1 : 0,
  556. '#options' => array(
  557. 0 => t('Allow this coupon only for users listed below.'),
  558. 1 => t('Allow this coupon for all users except those listed below.'),
  559. ),
  560. '#tree' => FALSE,
  561. );
  562. foreach ($value->data['users'] as $uid) {
  563. $username = $uid ? db_query('SELECT name FROM {users} WHERE uid = :uid', array(':uid' => $uid))->fetchField() : t('anonymous users');
  564. $form['users'][] = array(
  565. '#type' => 'textfield',
  566. '#default_value' => $username . ' [uid:' . $uid . ']',
  567. '#autocomplete_path' => 'admin/store/coupons/autocomplete/user',
  568. );
  569. }
  570. $form['roles'] = array(
  571. '#type' => 'fieldset',
  572. '#title' => t('Role restrictions'),
  573. '#description' => t('Enter one or more role names below to restrict this coupon based on a user\'s roles.'),
  574. );
  575. $form['roles']['negate_roles'] = array(
  576. '#type' => 'radios',
  577. '#default_value' => isset($value->data['negate_roles']) ? 1 : 0,
  578. '#options' => array(
  579. 0 => t('Allow this coupon only for users with the roles listed below.'),
  580. 1 => t('Allow this coupon for users with all roles except those listed below.'),
  581. ),
  582. '#tree' => FALSE,
  583. );
  584. foreach ($value->data['roles'] as $role) {
  585. $form['roles'][] = array(
  586. '#type' => 'textfield',
  587. '#default_value' => $role,
  588. '#autocomplete_path' => 'admin/store/coupons/autocomplete/role',
  589. );
  590. }
  591. $form['combinations'] = array(
  592. '#type' => 'fieldset',
  593. '#title' => t('Combinations'),
  594. '#description' => t('Restrict the way this coupon may be combined with other coupons.'),
  595. '#collapsible' => TRUE,
  596. '#collapsed' => empty($value->data['combinations']) && !isset($value->data['negate_combinations']),
  597. );
  598. $form['combinations']['negate_combinations'] = array(
  599. '#type' => 'radios',
  600. '#default_value' => isset($value->data['negate_combinations']) ? 1 : 0,
  601. '#options' => array(
  602. 0 => t('Allow this coupon to be combined only with those selected below. If you do not select any coupons, this coupon may not be combined.'),
  603. 1 => t('Allow this coupon to be combined with all coupons except those selected below.'),
  604. ),
  605. '#tree' => FALSE,
  606. );
  607. $form['combinations']['combinations'] = array(
  608. '#type' => 'select',
  609. '#multiple' => 'true',
  610. '#options' => db_query('SELECT cid, name FROM {uc_coupons}')->fetchAllKeyed(),
  611. '#default_value' => isset($value->data['combinations']) ? $value->data['combinations'] : array(),
  612. );
  613. // Add common settings and ajax functionality to restriction fieldsets.
  614. foreach ($ajax_fields as $field => $path) {
  615. $form[$field] = array_merge($form[$field], array(
  616. '#tree' => TRUE,
  617. '#collapsible' => TRUE,
  618. '#collapsed' => empty($value->data[$field]) && empty($form_state['ajax_submission']),
  619. '#prefix' => '<div id="ajax-' . $field . '">',
  620. '#suffix' => '</div>',
  621. 'more_' . $field => array(
  622. '#type' => 'submit',
  623. '#value' => t('Add more items'),
  624. '#weight' => 1,
  625. '#name' => 'op_' . $field,
  626. '#limit_validation_errors' => array(),
  627. '#submit' => array('uc_coupon_add_form_submit'),
  628. '#ajax' => array(
  629. 'callback' => 'uc_coupon_ajax_callback',
  630. 'wrapper' => 'ajax-' . $field,
  631. 'effect' => 'fade',
  632. ),
  633. '#tree' => FALSE,
  634. ),
  635. ));
  636. // Add blank fields if needed.
  637. for ($i = count($value->data[$field]); $i < $form_state['storage']['field_count'][$field]; $i++) {
  638. $form[$field][] = array(
  639. '#type' => 'textfield',
  640. '#autocomplete_path' => $path ? $path : NULL,
  641. '#maxlength' => ($field == 'products' || $field == 'terms') ? 300 : 128,
  642. );
  643. }
  644. }
  645. $form['display'] = array(
  646. '#type' => 'fieldset',
  647. '#title' => t('Display options'),
  648. '#description' => t('Options governing the way this coupon appears to users.'),
  649. '#collapsible' => TRUE,
  650. '#collapsed' => empty($value->data['line_item_format']) || empty($value->data['apply_message']),
  651. );
  652. $form['display']['line_item_format'] = array(
  653. '#type' => 'textfield',
  654. '#size' => 60,
  655. '#title' => t('Line item title format.'),
  656. '#description' => t('The title for this coupon when displayed in the cart and as a line-item.
  657. Leave blank to use the default format as specified on the main coupon settings page.'),
  658. '#default_value' => empty($value->data['line_item_format']) ? '' : $value->data['line_item_format'],
  659. );
  660. $form['display']['apply_message'] = array(
  661. '#type' => 'textarea',
  662. '#rows' => 3,
  663. '#title' => t('Message'),
  664. '#description' => t('The message to be displayed when this coupon is applied. Leave blank for a default message.'),
  665. '#default_value' => empty($value->data['apply_message']) ? '' : $value->data['apply_message'],
  666. );
  667. if (module_exists('token')) {
  668. $form['display']['replacement_patterns'] = array(
  669. '#type' => 'item',
  670. '#title' => t('Replacement Patterns'),
  671. '#markup' => theme('token_tree', array('token_types' => array('uc_coupon'))),
  672. );
  673. }
  674. $form['fields'] = array(
  675. '#type' => 'fieldset',
  676. '#title' => t('Custom fields'),
  677. '#description' => t('Custom fields can be configured at !link.', array(
  678. '!link' => l('admin/store/settings/coupon/fields', 'admin/store/settings/coupon/fields'),
  679. )),
  680. '#collapsible' => TRUE,
  681. '#collapsed' => FALSE,
  682. );
  683. if (count(field_info_instances('uc_coupon', 'uc_coupon')) == 0) {
  684. $form['fields']['#description'] = t('There are no custom fields defined.') . ' ' . $form['fields']['#description'];
  685. $form['fields']['#collapsed'] = TRUE;
  686. }
  687. field_attach_form('uc_coupon', $coupon, $form['fields'], $form_state);
  688. $form['submit'] = array(
  689. '#type' => 'submit',
  690. '#value' => t('Save coupon'),
  691. '#weight' => 10,
  692. );
  693. return $form;
  694. }
  695. function _uc_coupon_format_hour($hour) {
  696. return sprintf("%02d:00", $hour);
  697. }
  698. function _uc_coupon_date_range($form_element) {
  699. $form_element['year']['#options'] = drupal_map_assoc(range(2008, 2020));
  700. return $form_element;
  701. }
  702. /**
  703. * Ajax form element selector callback for "Add more items" buttons in restrictions fieldsets.
  704. */
  705. function uc_coupon_ajax_callback($form, $form_state) {
  706. return $form[$form_state['ajax_submission']];
  707. }
  708. /**
  709. * Autocomplete callback for product restrictions.
  710. */
  711. function uc_coupon_autocomplete_node($string) {
  712. $matches = array();
  713. $product_types = uc_product_types();
  714. $result = db_query("SELECT nid, title FROM {node} WHERE type IN (:types) AND title LIKE :title", array( ':types' => $product_types, ':title' => "%$string%"));
  715. foreach ($result as $row) {
  716. $title = check_plain($row->title);
  717. $matches[$title . ' [nid:' . $row->nid . ']'] = $title;
  718. }
  719. print drupal_json_encode($matches);
  720. exit;
  721. }
  722. /**
  723. * Autocomplete callback for term restrictions.
  724. */
  725. function uc_coupon_autocomplete_term($string) {
  726. $matches = array();
  727. $result = db_query("SELECT tid, name FROM {taxonomy_term_data} WHERE name LIKE :name", array(':name' => "%$string%"));
  728. foreach ($result as $row) {
  729. $matches[$row->name . ' [tid:' . $row->tid . ']'] = $row->name;
  730. }
  731. print drupal_json_encode($matches);
  732. exit;
  733. }
  734. /**
  735. * Autocomplete callback for user restrictions.
  736. */
  737. function uc_coupon_autocomplete_user($string) {
  738. $matches = array();
  739. $anonymous = t('anonymous users');
  740. if (strpos($anonymous, $string) !== FALSE) {
  741. $matches[$anonymous . ' [uid:0]'] = $anonymous;
  742. }
  743. $result = db_query("SELECT uid, name FROM {users} WHERE name LIKE :name", array( ':name' => "%$string%"));
  744. foreach ($result as $row) {
  745. $matches[$row->name . ' [uid:' . $row->uid . ']'] = $row->name;
  746. }
  747. print drupal_json_encode($matches);
  748. exit;
  749. }
  750. /**
  751. * Autocomplete callback for role restrictions.
  752. */
  753. function uc_coupon_autocomplete_role($string) {
  754. $matches = array();
  755. $result = db_query("SELECT name FROM {role} WHERE name LIKE :name", array(':name' => "%$string%"));
  756. foreach ($result as $row) {
  757. $matches[$row->name] = $row->name;
  758. }
  759. print drupal_json_encode($matches);
  760. exit;
  761. }
  762. /**
  763. * Coupon form validate handler.
  764. */
  765. function uc_coupon_add_form_validate($form, &$form_state) {
  766. drupal_add_css(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.css');
  767. drupal_add_js(drupal_get_path('module', 'uc_coupon') . '/uc_coupon.admin.js');
  768. $name = db_query("SELECT name FROM {uc_coupons} WHERE code = :code AND cid <> :cid", array(':code' => strtoupper($form_state['values']['code']), ':cid' => isset($form['#uc_coupon_cid']) ? $form['#uc_coupon_cid'] : 0))->fetchField();
  769. if ($name) {
  770. form_set_error('code', t('Coupon code already used by %name.', array('%name' => $name)));
  771. }
  772. if (isset($form['#uc_coupon_used']) && $form['#uc_coupon_used']
  773. && isset($form_state['values']['bulk_number']) && isset($form['#uc_coupon']->data['bulk_number'])
  774. && $form_state['values']['bulk_number'] < $form['#uc_coupon']->data['bulk_number']) {
  775. form_set_error('bulk_number', t('You cannot decrease the number of bulk codes for a coupon that has been used.'));
  776. }
  777. if (!preg_match('/^(=(?!.*%))?\d+(\.\d+)?%?$/', $form_state['values']['discount'])) {
  778. form_set_error('discount', t('Invalid discount.'));
  779. }
  780. if (substr($form_state['values']['discount'], 0, 1) == '=' &&
  781. ($form_state['values']['apply_to'] == 'subtotal' || $form_state['values']['apply_to'] == 'products_total')) {
  782. form_set_error('apply_to', t('Set prices cannot be applied to the order subtotal or matching products total.'));
  783. }
  784. if ($form_state['values']['apply_to'] == 'cheapest' || $form_state['values']['apply_to'] == 'expensive') {
  785. if (!preg_match('/^[1-9]\d*$/', $form_state['values']['apply_count'])) {
  786. form_set_error('apply_count', t('You must specify the maximum number of products to discount.'));
  787. }
  788. }
  789. foreach ($form_state['values']['products'] as $key => $product) {
  790. if ($product && !preg_match('/\[nid:(\d+)\]$/', $product)) {
  791. form_set_error('products][' . $key, t('Products must include the node ID.'));
  792. }
  793. }
  794. foreach ($form_state['values']['users'] as $key => $user) {
  795. if ($user && !preg_match('/\[uid:(\d+)\]$/', $user)) {
  796. form_set_error('users][' . $key, t('User names must include the user ID.'));
  797. }
  798. }
  799. if ( (!isset($form['#uc_coupon_used']) || $form['#uc_coupon_used'] == 0)
  800. && $form_state['values']['bulk_generate']
  801. && intval($form_state['values']['bulk_number']) <= 0) {
  802. form_set_error('bulk_number', t('You must specify the number of codes to generate.'));
  803. }
  804. if ($form_state['values']['use_validity']) {
  805. $valid_from = _uc_coupon_mktime($form_state['values']['valid_from']['month'], $form_state['values']['valid_from']['day'], $form_state['values']['valid_from']['year']);
  806. $valid_until = _uc_coupon_mktime($form_state['values']['valid_until']['month'], $form_state['values']['valid_until']['day'], $form_state['values']['valid_until']['year']);
  807. if ($valid_from > $valid_until) {
  808. form_set_error('valid_from', t('The coupon start date must be before the expiration date.'));
  809. }
  810. }
  811. }
  812. /**
  813. * Coupon form submit handler.
  814. */
  815. function uc_coupon_add_form_submit($form, &$form_state) {
  816. // If the coupon was previously used, reset disabled textfields to their original values.
  817. if (isset($form['#uc_coupon_used']) && $form['#uc_coupon_used'] != 0) {
  818. $form_state['values']['code'] = $form['#uc_coupon']->code;
  819. }
  820. // Ensure all fields of original coupon object propagate to presave hook.
  821. $coupon = (isset($form['#uc_coupon'])) ? $form['#uc_coupon'] : new stdClass;
  822. if (isset($form['#uc_coupon_cid'])) {
  823. $coupon->cid = $form['#uc_coupon_cid'];
  824. }
  825. // Set basic coupon information.
  826. $coupon->name = $form_state['values']['name'];
  827. $coupon->code = strtoupper($form_state['values']['code']);
  828. $coupon->bulk = $form_state['values']['bulk_generate'];
  829. $coupon->data['bulk_number'] = $form_state['values']['bulk_generate'] ? $form_state['values']['bulk_number'] : 0;
  830. $coupon->data['bulk_length'] = $form_state['values']['bulk_length'];
  831. if ($form_state['values']['use_validity']) {
  832. $coupon->valid_from = _uc_coupon_mktime($form_state['values']['valid_from']['month'], $form_state['values']['valid_from']['day'], $form_state['values']['valid_from']['year']);
  833. $coupon->valid_until = _uc_coupon_mktime($form_state['values']['valid_until']['month'], $form_state['values']['valid_until']['day'], $form_state['values']['valid_until']['year']);
  834. }
  835. else {
  836. $coupon->valid_from = $coupon->valid_until = 0;
  837. }
  838. $coupon->status = $form_state['values']['status'];
  839. if (substr(trim($form_state['values']['discount']), -1) == '%') {
  840. $coupon->type = 'percentage';
  841. }
  842. elseif (substr(trim($form_state['values']['discount']), 0, 1) == '=') {
  843. $coupon->type = 'set_price';
  844. }
  845. elseif (!empty($form_state['values']['store_credit'])) {
  846. $coupon->type = 'credit';
  847. }
  848. else {
  849. $coupon->type = 'price';
  850. }
  851. $coupon->value = str_replace(array('%', '='), '', $form_state['values']['discount']);
  852. $coupon->minimum_order = $form_state['values']['minimum_order'] ? $form_state['values']['minimum_order'] : 0;
  853. $coupon->data['minimum_qty'] = $form_state['values']['minimum_qty'];
  854. $coupon->data['minimum_qty_restrict'] = $form_state['values']['minimum_qty_restrict'];
  855. $coupon->data['require_match_all'] = $form_state['values']['require_match_all'];
  856. $coupon->max_uses = $form_state['values']['max_uses'] ? $form_state['values']['max_uses'] : 0;
  857. $coupon->data['max_uses_per_user'] = $form_state['values']['max_uses_per_user'];
  858. $coupon->data['apply_to'] = $form_state['values']['apply_to'];
  859. if ($form_state['values']['apply_to'] == 'cheapest' || $form_state['values']['apply_to'] == 'expensive') {
  860. $coupon->data['apply_count'] = $form_state['values']['apply_count'];
  861. }
  862. // Map restrictions back to their IDs.
  863. array_walk($form_state['values']['products'], '_uc_coupon_map_restriction', 'nid');
  864. array_walk($form_state['values']['terms'], '_uc_coupon_map_restriction', 'tid');
  865. array_walk($form_state['values']['users'], '_uc_coupon_map_restriction', 'uid');
  866. // Set coupon restrictions.
  867. $coupon->data['line_items'] = drupal_map_assoc(array_filter($form_state['values']['line_items']));
  868. $coupon->data['product_types'] = drupal_map_assoc(array_filter($form_state['values']['product_types']));
  869. $coupon->data['negate_products'] = $form_state['values']['negate_products'];
  870. $coupon->data['products'] = drupal_map_assoc(array_filter($form_state['values']['products']));
  871. $coupon->data['skus'] = drupal_map_assoc(array_filter($form_state['values']['skus']));
  872. $coupon->data['negate_terms'] = $form_state['values']['negate_terms'];
  873. $coupon->data['terms'] = drupal_map_assoc(array_filter($form_state['values']['terms']));
  874. $coupon->data['negate_users'] = $form_state['values']['negate_users'];
  875. $coupon->data['users'] = drupal_map_assoc(array_filter($form_state['values']['users'], 'is_numeric'));
  876. $coupon->data['negate_roles'] = $form_state['values']['negate_roles'];
  877. $coupon->data['roles'] = drupal_map_assoc(array_filter($form_state['values']['roles']));
  878. $coupon->data['negate_combinations'] = $form_state['values']['negate_combinations'];
  879. $coupon->data['combinations'] = drupal_map_assoc(array_filter($form_state['values']['combinations']));
  880. // Set coupon display info
  881. $coupon->data['apply_message'] = trim($form_state['values']['apply_message']);
  882. $coupon->data['line_item_format'] = trim($form_state['values']['line_item_format']);
  883. // Remove zero values and empty arrays.
  884. $coupon->data = array_filter($coupon->data);
  885. // Handle AJAX submissions.
  886. if (!empty($form_state['triggering_element']) && substr($elem = $form_state['triggering_element']['#name'], 0, 3) === 'op_') {
  887. $form_state['ajax_submission'] = substr($elem, 3);
  888. $form_state['coupon_data'] = $coupon->data;
  889. $form_state['rebuild'] = TRUE;
  890. }
  891. else {
  892. field_attach_submit('uc_coupon', $coupon, $form, $form_state);
  893. // Invoke all specified builders for copying form values to entity properties.
  894. // !TODO - Make full use of the EntityUIController class for uc_coupon
  895. if (isset($form['#entity_builders'])) {
  896. foreach ($form['#entity_builders'] as $function) {
  897. $function('uc_coupon', $coupon, $form, $form_state);
  898. }
  899. }
  900. uc_coupon_save($coupon, $form_state['values']);
  901. drupal_set_message(t('Coupon %name has been saved.', array('%name' => $coupon->name)));
  902. $form_state['redirect'] = 'admin/store/coupons' . ($coupon->status ? '' : '/inactive');
  903. }
  904. }
  905. function _uc_coupon_map_restriction(&$value, $key, $prefix) {
  906. if ($value && preg_match('/\[' . $prefix . ':(\d+)\]$/', $value, $matches)) {
  907. $value = $matches[1];
  908. }
  909. else {
  910. $value = FALSE;
  911. }
  912. }
  913. /**
  914. * Delete coupon confirm form
  915. *
  916. * @param $cid int
  917. * Coupon ID.
  918. *
  919. * @return $confirm
  920. * Return a drupal confirm form.
  921. */
  922. function uc_coupon_delete_confirm($form, &$form_state, $coupon) {
  923. $form['#uc_coupon'] = $coupon;
  924. return confirm_form($form, t('Are you sure you want to delete coupon %name with code %code?', array('%name' => $coupon->name, '%code' => $coupon->code)), 'admin/store/coupons', t('This action cannot be undone. Deleting this coupon will remove all records of past uses as well.'), t('Delete'));
  925. }
  926. /**
  927. * Delete coupon confirm form submit handler.
  928. */
  929. function uc_coupon_delete_confirm_submit($form, &$form_state) {
  930. $coupon = $form['#uc_coupon'];
  931. uc_coupon_delete($coupon->cid);
  932. drupal_set_message(t('Coupon %name has been deleted.', array('%name' => $coupon->name)));
  933. $form_state['redirect'] = 'admin/store/coupons' . ($coupon->status ? '' : '/inactive');
  934. }
  935. /**
  936. * Generate a list of bulk coupon codes.
  937. */
  938. function uc_coupon_codes_csv($coupon) {
  939. if (!$coupon->bulk) {
  940. drupal_not_found();
  941. return;
  942. }
  943. header('Content-Type: application/octet-stream');
  944. header('Content-Disposition: attachment; filename="' . $coupon->code . '.csv";');
  945. for ($id = 0; $id < $coupon->data['bulk_number']; $id++) {
  946. echo uc_coupon_get_bulk_code($coupon, $id) . "\n";
  947. }
  948. exit;
  949. }
  950. /**
  951. * Show the actions a user may perform on a coupon.
  952. */
  953. function theme_uc_coupon_actions($variables) {
  954. $coupon = $variables['coupon'];
  955. $actions = array();
  956. foreach (module_invoke_all('uc_coupon_actions', $coupon) as $action) {
  957. $icon = theme('image', array('path' => $action['icon'], 'alt' => $action['title']));
  958. $actions[] = l($icon, $action['url'], array('attributes' => array('title' => $action['title']), 'html' => TRUE));
  959. }
  960. return implode(' ', $actions);
  961. }
  962. /**
  963. * Show coupon code in Views.
  964. *
  965. * @param $coupon
  966. * The coupon object for which this code is to be formatted.
  967. * $coupon->usage can optionally be set.
  968. * For bulk coupons, $coupon->code should be set to the specific code to display,.
  969. */
  970. function theme_uc_coupon_code($variables) {
  971. $coupon = $variables['coupon'];
  972. $code = $coupon->code;
  973. $class = "uc-coupon-code";
  974. if (isset($coupon->usage)) {
  975. $uses = $coupon->usage['codes'][$coupon->code];
  976. if ($coupon->max_uses == 0 || $coupon->max_uses > $uses) {
  977. $class .= ' uc-coupon-code-available';
  978. if (arg(0) == 'user') {
  979. $code = l($code, "user/" . arg(1) . '/coupons/' . $coupon->cid . "/view/$code");
  980. }
  981. }
  982. else {
  983. $class .= ' uc-coupon-code-used';
  984. }
  985. }
  986. return '<span class="' . $class . '">' . $code . '</span>';
  987. }
  988. function _uc_coupon_format_date($date, $format) {
  989. $func = variable_get('uc_coupon_expiry_timezone', 0) ? 'date' : 'gmdate';
  990. return $func($format, $date);
  991. }
  992. function _uc_coupon_mktime($month, $day, $year) {
  993. $func = variable_get('uc_coupon_expiry_timezone', 0) ? 'mktime' : 'gmmktime';
  994. $hour = variable_get('uc_coupon_expiry_hour', 0);
  995. $time = $func($hour, 0, 0, $month, $day, $year);
  996. return $func($hour, 0, 0, $month, $day, $year);
  997. }