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://www.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 (isPostRequestParameterSet('coupon_code')) {
58 // Search for the coupon
59 $result = SQL_QUERY_ESC("SELECT
61 UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
62 UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
67 d.`coupon_description`
69 `{?_MYSQL_PREFIX?}_user_coupons` AS c
71 `{?_MYSQL_PREFIX?}_coupon_data` AS d
75 c.`coupon_code`='%s' AND
77 c.`cashed_on` IS NULL AND
78 UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
81 postRequestParameter('coupon_code'),
83 ), __FILE__, __LINE__);
85 // Do we have an entry?
86 if (SQL_NUMROWS($result) == 1) {
88 $content = SQL_FETCHARRAY($result);
90 // Mark it as "cashed"
92 `{?_MYSQL_PREFIX?}_user_coupons`
97 `coupon_code`='%s' AND
101 postRequestParameter('coupon_code'),
103 ), __FILE__, __LINE__);
106 if (SQL_HASZEROAFFECTED()) {
107 // Abort it here, it always must work
108 debug_report_bug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
111 // Update count as well
112 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_coupon_data` SET `total_cashed`=`total_cashed`+1 WHERE `id`=%s AND `total_cashed` < `total_created` LIMIT 1",
113 array(bigintval($content['coupon_id'])), __FILE__, __LINE__);
116 if (SQL_HASZEROAFFECTED()) {
117 // Abort it here, it always must work
118 debug_report_bug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
121 // Now we just need to book that points on the user's account
123 addPointsThroughReferalSystem('coupon_cashed', getMemberId(), $content['points']);
125 // Subtract points from member account if the admin has selected one
126 if (isValidUserId(getConfig('coupon_userid'))) {
127 // Subtract from this account
128 subtractPoints('cashed_coupon', getConfig('coupon_userid'), $content['points']);
131 // Translate some data
132 $content['coupon_expired'] = generateDateTime($content['coupon_expired'], '0');
133 $content['coupon_created'] = generateDateTime($content['coupon_created'], '0');
135 // Send admin notification
136 sendAdminNotification('{--ADMIN_COUPON_CASHED_SUBJECT--}', 'admin_coupon_cashed', $content, getMemberId());
139 displayMessage('{%message,MEMBER_COUPON_CODE_CASHED=' . $content['points'] . '%}');
142 displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
146 SQL_FREERESULT($result);
149 displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
151 } elseif (isFormSent('change')) {
152 // Change receive status, depending on its current state
153 if (isUserDataEnabled('receiving_coupons')) {
154 // Unsubscribe from coupons
155 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_UNSUBSCRIBED--}';
158 // Subscribe to coupons
159 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
163 // And change it in the database
164 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receiving_coupons`='%s' WHERE `userid`=%s LIMIT 1",
165 array($newStatus, getMemberId()), __FILE__, __LINE__);
167 // Did something change?
168 if (!SQL_HASZEROAFFECTED()) {
169 // Records have been updated
170 displayMessage($message);
173 displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
176 // Load form template
177 loadTemplate('member_cash_coupon', false);