2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 06/28/2011 *
4 * =================== Last change: 06/28/2011 *
6 * -------------------------------------------------------------------- *
7 * File : what-cash_coupon.php *
8 * -------------------------------------------------------------------- *
9 * Short description : To cash a coupon *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Einen Gutschein einloesen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
41 } elseif (!isMember()) {
42 redirectToIndexMemberOnlyModule();
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
48 // Continue only if the proper extension is active (admins can always continue)
49 if ((!isExtensionActive('coupon')) && (!isAdmin())) {
50 displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=coupon%}');
56 // Is the coupon code entered?
57 if (isPostRequestElementSet('coupon_code')) {
58 // Search for the coupon
59 $result = SQL_QUERY_ESC("SELECT
63 UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
64 UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
69 d.`coupon_description`
71 `{?_MYSQL_PREFIX?}_user_coupons` AS c
73 `{?_MYSQL_PREFIX?}_coupon_data` AS d
77 c.`coupon_code`='%s' AND
79 c.`cashed_on` IS NULL AND
80 UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
83 postRequestElement('coupon_code'),
85 ), __FILE__, __LINE__);
87 // Do we have an entry?
88 if (SQL_NUMROWS($result) == 1) {
90 $content = SQL_FETCHARRAY($result);
93 $content = runFilterChain('pre_cash_coupon', $content);
95 // Mark it as "cashed"
97 `{?_MYSQL_PREFIX?}_user_coupons`
102 `coupon_code`='%s' AND
106 postRequestElement('coupon_code'),
108 ), __FILE__, __LINE__);
111 if (SQL_HASZEROAFFECTED()) {
112 // Abort it here, it always must work
113 debug_report_bug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
116 // Update count as well
117 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_coupon_data` SET `total_cashed`=`total_cashed`+1 WHERE `id`=%s AND `total_cashed` < `total_created` LIMIT 1",
118 array(bigintval($content['coupon_id'])), __FILE__, __LINE__);
121 if (SQL_HASZEROAFFECTED()) {
122 // Abort it here, it always must work
123 debug_report_bug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
126 // Run post filter chain
127 $content = runFilterChain('post_cash_coupon', $content);
130 displayMessage('{%message,MEMBER_COUPON_CODE_CASHED=' . $content['points'] . '%}');
133 displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
137 SQL_FREERESULT($result);
140 displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
142 } elseif (isFormSent('change')) {
143 // Change receive status, depending on its current state
144 if (isUserDataEnabled('receiving_coupons')) {
145 // Unsubscribe from coupons
146 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_UNSUBSCRIBED--}';
149 // Subscribe to coupons
150 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
154 // And change it in the database
155 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receiving_coupons`='%s' WHERE `userid`=%s LIMIT 1",
156 array($newStatus, getMemberId()), __FILE__, __LINE__);
158 // Did something change?
159 if (!SQL_HASZEROAFFECTED()) {
160 // Records have been updated
161 displayMessage($message);
164 displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
167 // Load form template
168 loadTemplate('member_cash_coupon', false);