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 - 2011 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 `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' AND `receiving_coupons`='Y' ORDER BY `userid` ASC",
60 // Do we have entries left?
61 if (SQL_NUMROWS($result) > 0) {
62 // Convert timestamp selections
63 $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
66 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')",
70 bigintval(postRequestElement('points')),
71 postRequestElement('coupon_description')
75 // Get the insert id from the table
76 $couponId = SQL_INSERTID();
79 while (list($userid) = SQL_FETCHROW($result)) {
80 // By default no code is unique
84 // Look for a unique id
87 * Add a coupon for this user, first we need to create a
88 * table-unique "id". The function generatePassword() can do
89 * this job for us but we want to exclude some characters.
91 $couponCode = generatePassword(30, array('-','+','_','/','.'));
93 // Is it really unique?
94 $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', true) == 0);
97 // The above loop should always set $couponCode, so we can now insert it
98 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_coupons` (`coupon_id`,`userid`,`coupon_code`) VALUES (%s,%s,'%s')",
99 array($couponId, $userid, $couponCode), __FILE__, __LINE__);
101 // Fill array with all required data
103 'coupon_id' => $couponId,
105 'points' => bigintval(postRequestElement('points')),
106 'coupon_code' => $couponCode,
107 'coupon_expired' => generateDateTime($expirationTime, '0')
110 // Load member email template
111 $mailText = loadEmailTemplate('member_coupon_code', $content, $userid);
114 sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
116 // Rember this user for the admin email
117 $adminUserids[] = $userid;
120 // Prepare content for template
122 'userids' => implode(',', $adminUserids),
123 'userid_count' => count($adminUserids),
124 'coupon_id' => $couponId,
125 'points' => bigintval(postRequestElement('points')),
126 'coupon_description' => postRequestElement('coupon_description'),
127 'coupon_expired' => generateDateTime($expirationTime, '0')
130 // Send admin notification
131 sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon_code', $content);
133 // Coupon has been sent
134 displayMessage('{--ADMIN_SEND_COUPON_DONE--}');
137 displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
141 SQL_FREERESULT($result);
144 // Prepare some template data
145 $content['coupon_default_time_selection'] = createConfigurationTimeSelections('coupon_default_time', 'WDh');
148 loadTemplate('admin_send_coupon', false, $content);