Rewrote 'we' word a little, rewrote mail order to use SQL_INSERTID() instead of anoth...
[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 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://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         exit();
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 (isPostRequestElementSet('coupon_code')) {
58                 // Search for the coupon
59                 $result = SQL_QUERY_ESC("SELECT
60         c.`coupon_id`,
61         c.`userid`,
62         c.`coupon_code`,
63         UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
64         UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
65         d.`coupon_type`,
66         d.`total_created`,
67         d.`total_cashed`,
68         d.`points`,
69         d.`coupon_description`
70 FROM
71         `{?_MYSQL_PREFIX?}_user_coupons` AS c
72 INNER JOIN
73         `{?_MYSQL_PREFIX?}_coupon_data` AS d
74 ON
75         c.`coupon_id`=d.`id`
76 WHERE
77         c.`coupon_code`='%s' AND
78         c.`userid`=%s AND
79         c.`cashed_on` IS NULL AND
80         UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
81 LIMIT 1",
82                         array(
83                                 postRequestElement('coupon_code'),
84                                 getMemberId()
85                         ), __FILE__, __LINE__);
86
87                 // Is there an entry?
88                 if (SQL_NUMROWS($result) == 1) {
89                         // Load data
90                         $content = SQL_FETCHARRAY($result);
91
92                         // Run pre-filter
93                         $content = runFilterChain('pre_cash_coupon', $content);
94
95                         // Mark it as "cashed"
96                         SQL_QUERY_ESC("UPDATE
97         `{?_MYSQL_PREFIX?}_user_coupons`
98 SET
99         `cashed_on`=NOW(),
100         `coupon_code`=NULL
101 WHERE
102         `coupon_code`='%s' AND
103         `userid`=%s
104 LIMIT 1",
105                                 array(
106                                         postRequestElement('coupon_code'),
107                                         getMemberId()
108                                 ), __FILE__, __LINE__);
109
110                         // Does it work?
111                         if (SQL_HASZEROAFFECTED()) {
112                                 // Abort it here, it always must work
113                                 reportBug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
114                         } // END - if
115
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__);
119
120                         // Does it work?
121                         if (SQL_HASZEROAFFECTED()) {
122                                 // Abort it here, it always must work
123                                 reportBug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
124                         } // END - if
125
126                         // Run post filter chain
127                         $content = runFilterChain('post_cash_coupon', $content);
128
129                         // Display message
130                         displayMessage('{%message,MEMBER_COUPON_CODE_CASHED=' . $content['points'] . '%}');
131                 } else {
132                         // No coupon found
133                         displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
134                 }
135
136                 // Free the result
137                 SQL_FREERESULT($result);
138         } else {
139                 // No code entered!
140                 displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
141         }
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--}';
147                 $newStatus = 'N';
148         } else {
149                 // Subscribe to coupons
150                 $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
151                 $newStatus = 'Y';
152         }
153
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__);
157
158         // Did something change?
159         if (!SQL_HASZEROAFFECTED()) {
160                 // Records have been updated
161                 displayMessage($message);
162         } else {
163                 // Nothing changed!
164                 displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
165         }
166 } else {
167         // Load form template
168         loadTemplate('member_cash_coupon', false);
169 }
170
171 // [EOF]
172 ?>