Rewrote 'we' word a little, rewrote mail order to use SQL_INSERTID() instead of anoth...
[mailer.git] / inc / modules / admin / what-send_coupon.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 26/06/2011 *
4  * ===================                          Last change: 26/06/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-send_coupon.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Send out coupons                                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Versenden von Gutscheinen                        *
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')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
45
46 // Is the form sent?
47 if (isFormSent()) {
48         // Are all required fields set?
49         if (!isPostRequestElementSet('points')) {
50                 // Points not entered
51                 displayMessage('{--ADMIN_SEND_COUPON_POINTS_NOT_ENTERED--}');
52         } elseif (!isPostRequestElementSet('coupon_description')) {
53                 // Description not entered
54                 displayMessage('{--ADMIN_SEND_COUPON_DESCRIPTION_NOT_ENTERED--}');
55         } else {
56                 // All fields are set, so we can start looking for users who wants to receive coupons
57                 $result = SQL_QUERY("SELECT
58         `userid`
59 FROM
60         `{?_MYSQL_PREFIX?}_user_data`
61 WHERE
62         `status`='CONFIRMED' AND
63         `receiving_coupons`='Y'
64         " . runFilterChain('user_exclusion_sql', ' ') . "
65 ORDER BY
66         `userid` ASC",
67                         __FILE__, __LINE__);
68
69                 // Are there entries left?
70                 if (!SQL_HASZERONUMS($result)) {
71                         // Convert timestamp selections
72                         $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
73
74                         // Insert coupon data
75                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_coupon_data` (`coupon_expired`,`total_created`,`points`,`coupon_description`) VALUES ('{%%pipe,SQL_EPOCHE_TO_TIMESTAMP=%s%%}', %s,%s,'%s')",
76                                 array(
77                                         $expirationTime,
78                                         SQL_NUMROWS($result),
79                                         bigintval(postRequestElement('points')),
80                                         postRequestElement('coupon_description')
81                                 ), __FILE__, __LINE__
82                         );
83
84                         // Get the insert id from the table
85                         $couponId = SQL_INSERTID();
86
87                         // Load entry
88                         while (list($userid) = SQL_FETCHROW($result)) {
89                                 // By default no code is unique
90                                 $isUnique = false;
91                                 $couponCode = '';
92
93                                 // Look for a unique id
94                                 while (!$isUnique) {
95                                         /*
96                                          * Add a coupon for this user, first we need to create a
97                                          * table-unique "id". The function generatePassword() can do
98                                          * this job for us but we want to exclude some characters.
99                                          */
100                                         $couponCode = generatePassword(30, array('-', '+', '_', '/', '.'));
101
102                                         // Is it really unique?
103                                         $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', true) == 0);
104                                 } // END - while
105
106                                 // The above loop should always set $couponCode, so we can now insert it
107                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_coupons` (`coupon_id`,`userid`,`coupon_code`) VALUES (%s,%s,'%s')",
108                                         array(
109                                                 $couponId,
110                                                 $userid,
111                                                 $couponCode
112                                         ), __FILE__, __LINE__);
113
114                                 // Fill array with all required data
115                                 $content = array(
116                                         'coupon_id'      => $couponId,
117                                         'userid'         => $userid,
118                                         'points'         => bigintval(postRequestElement('points')),
119                                         'coupon_code'    => $couponCode,
120                                         'coupon_expired' => generateDateTime($expirationTime, '0')
121                                 );
122
123                                 // Load member email template
124                                 $mailText = loadEmailTemplate('member_coupon_code', $content, $userid);
125
126                                 // And deliver it
127                                 sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
128
129                                 // Rember this user for the admin email
130                                 array_push($adminUserids, $userid);
131                         } // END - while
132
133                         // Prepare content for template
134                         $content = array(
135                                 'userids'            => implode(',', $adminUserids),
136                                 'userid_count'       => count($adminUserids),
137                                 'coupon_id'          => $couponId,
138                                 'points'             => bigintval(postRequestElement('points')),
139                                 'coupon_description' => postRequestElement('coupon_description'),
140                                 'coupon_expired'     => generateDateTime($expirationTime, '0')
141                         );
142
143                         // Send admin notification
144                         sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon_code', $content);
145
146                         // Coupon has been sent
147                         displayMessage('{--ADMIN_SEND_COUPON_DONE--}');
148                 } else {
149                         // No user was found
150                         displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
151                 }
152
153                 // Free result
154                 SQL_FREERESULT($result);
155         }
156 } else {
157         // Prepare some template data
158         $content['coupon_default_time_selection'] = createConfigurationTimeSelections('coupon_default_time', 'WDh');
159
160         // Load template
161         loadTemplate('admin_send_coupon', false, $content);
162 }
163
164 // [EOF]
165 ?>