]> git.mxchange.org Git - mailer.git/blob - inc/modules/member/what-cash_coupon.php
A lot has been rewritten, ext-teams added, ext-forced continued:
[mailer.git] / inc / modules / member / what-cash_coupon.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 06/28/2011 *
4  * ===================                          Last change: 06/28/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-cash_coupon.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : To cash a coupon                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Einen Gutschein einloesen                        *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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                  *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } elseif (!isMember()) {
42         redirectToIndexMemberOnlyModule();
43 }
44
45 // Add description as navigation point
46 addYouAreHereLink('member', __FILE__);
47
48 // Continue only if the proper extension is active (admins can always continue)
49 if ((!isExtensionActive('coupon')) && (!isAdmin())) {
50         displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=coupon%}');
51         return;
52 } // END - if
53
54 // Is the form sent?
55 if (isFormSent()) {
56         // Is the coupon code entered?
57         if (isPostRequestParameterSet('coupon_code')) {
58                 // Search for the coupon
59                 $result = SQL_QUERY_ESC("SELECT
60         c.`coupon_id`,
61         UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
62         UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
63         d.`coupon_type`,
64         d.`total_created`,
65         d.`total_cashed`,
66         d.`points`,
67         d.`coupon_description`
68 FROM
69         `{?_MYSQL_PREFIX?}_user_coupons` AS c
70 INNER JOIN
71         `{?_MYSQL_PREFIX?}_coupon_data` AS d
72 ON
73         c.`coupon_id`=d.`id`
74 WHERE
75         c.`coupon_code`='%s' AND
76         c.`userid`=%s AND
77         c.`cashed_on` IS NULL AND
78         UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
79 LIMIT 1",
80                         array(
81                                 postRequestParameter('coupon_code'),
82                                 getMemberId()
83                         ), __FILE__, __LINE__);
84
85                 // Do we have an entry?
86                 if (SQL_NUMROWS($result) == 1) {
87                         // Load data
88                         $content = SQL_FETCHARRAY($result);
89
90                         // Mark it as "cashed"
91                         SQL_QUERY_ESC("UPDATE
92         `{?_MYSQL_PREFIX?}_user_coupons`
93 SET
94         `cashed_on`=NOW(),
95         `coupon_code`=NULL
96 WHERE
97         `coupon_code`='%s' AND
98         `userid`=%s
99 LIMIT 1",
100                                 array(
101                                         postRequestParameter('coupon_code'),
102                                         getMemberId()
103                                 ), __FILE__, __LINE__);
104
105                         // Does it work?
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']);
109                         } // END - if
110
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__);
114
115                         // Does it work?
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']);
119                         } // END - if
120
121                         // Now we just need to book that points on the user's account
122                         initReferalSystem();
123                         addPointsThroughReferalSystem('coupon_cashed', getMemberId(), $content['points']);
124
125                         // Subtract points from member account if the admin has selected one
126                         if (isValidUserId(getConfig('coupon_userid'))) {
127                                 // Subtract from this account and ignore return status
128                                 subtractPoints('cashed_coupon', getConfig('coupon_userid'), $content['points']);
129                         } // END - if
130
131                         // Translate some data
132                         $content['coupon_expired'] = generateDateTime($content['coupon_expired'], '0');
133                         $content['coupon_created'] = generateDateTime($content['coupon_created'], '0');
134
135                         // Send admin notification
136                         sendAdminNotification('{--ADMIN_COUPON_CASHED_SUBJECT--}', 'admin_coupon_cashed', $content, getMemberId());
137
138                         // Display message
139                         displayMessage('{%message,MEMBER_COUPON_CODE_CASHED=' . $content['points'] . '%}');
140                 } else {
141                         // No coupon found
142                         displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
143                 }
144
145                 // Free the result
146                 SQL_FREERESULT($result);
147         } else {
148                 // No code entered!
149                 displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
150         }
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--}';
156                 $newStatus = 'N';
157         } else {
158                 // Subscribe to coupons
159                 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
160                 $newStatus = 'Y';
161         }
162
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__);
166
167         // Did something change?
168         if (!SQL_HASZEROAFFECTED()) {
169                 // Records have been updated
170                 displayMessage($message);
171         } else {
172                 // Nothing changed!
173                 displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
174         }
175 } else {
176         // Load form template
177         loadTemplate('member_cash_coupon', false);
178 }
179
180 // [EOF]
181 ?>