2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 26/06/2011 *
4 * =================== Last change: 26/06/2011 *
6 * -------------------------------------------------------------------- *
7 * File : what-send_coupon.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Send out coupons *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Versenden von Gutscheinen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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')) || (!isAdmin())) {
43 // Add description as navigation point
44 addYouAreHereLink('admin', __FILE__);
48 // Are all required fields set?
49 if (!isPostRequestElementSet('points')) {
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--}');
56 // All fields are set, so we can start looking for users who wants to receive coupons
57 $result = SQL_QUERY("SELECT
60 `{?_MYSQL_PREFIX?}_user_data`
62 `status`='CONFIRMED' AND
63 `receiving_coupons`='Y'
64 " . runFilterChain('user_exclusion_sql', ' ') . "
69 // Are there entries left?
70 if (!SQL_HASZERONUMS($result)) {
71 // Convert timestamp selections
72 $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
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')",
79 bigintval(postRequestElement('points')),
80 postRequestElement('coupon_description')
84 // Get the insert id from the table
85 $couponId = SQL_INSERTID();
88 while (list($userid) = SQL_FETCHROW($result)) {
89 // By default no code is unique
93 // Look for a unique id
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.
100 $couponCode = generatePassword(30, array('-', '+', '_', '/', '.'));
102 // Is it really unique?
103 $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', true) == 0);
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')",
112 ), __FILE__, __LINE__);
114 // Fill array with all required data
116 'coupon_id' => $couponId,
118 'points' => bigintval(postRequestElement('points')),
119 'coupon_code' => $couponCode,
120 'coupon_expired' => generateDateTime($expirationTime, '0')
123 // Load member email template
124 $mailText = loadEmailTemplate('member_coupon_code', $content, $userid);
127 sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
129 // Rember this user for the admin email
130 array_push($adminUserids, $userid);
133 // Prepare content for template
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')
143 // Send admin notification
144 sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon_code', $content);
146 // Coupon has been sent
147 displayMessage('{--ADMIN_SEND_COUPON_DONE--}');
150 displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
154 SQL_FREERESULT($result);
157 // Prepare some template data
158 $content['coupon_default_time_selection'] = createConfigurationTimeSelections('coupon_default_time', 'WDh');
161 loadTemplate('admin_send_coupon', false, $content);