This should display the data?
[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 - 2011 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 `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' AND `receiving_coupons`='Y' ORDER BY `userid` ASC",
58                         __FILE__, __LINE__);
59
60                 // Do we have entries left?
61                 if (SQL_NUMROWS($result) > 0) {
62                         // Convert timestamp selections
63                         $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
64
65                         // Insert coupon data
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')",
67                                 array(
68                                         $expirationTime,
69                                         SQL_NUMROWS($result),
70                                         bigintval(postRequestElement('points')),
71                                         postRequestElement('coupon_description')
72                                 ), __FILE__, __LINE__
73                         );
74
75                         // Get the insert id from the table
76                         $couponId = SQL_INSERTID();
77
78                         // Load entry
79                         while (list($userid) = SQL_FETCHROW($result)) {
80                                 // By default no code is unique
81                                 $isUnique = false;
82                                 $couponCode = '';
83
84                                 // Look for a unique id
85                                 while (!$isUnique) {
86                                         /*
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.
90                                          */
91                                         $couponCode = generatePassword(30, array('-','+','_','/','.'));
92
93                                         // Is it really unique?
94                                         $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', true) == 0);
95                                 } // END - while
96
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__);
100
101                                 // Fill array with all required data
102                                 $content = array(
103                                         'coupon_id'      => $couponId,
104                                         'userid'         => $userid,
105                                         'points'         => bigintval(postRequestElement('points')),
106                                         'coupon_code'    => $couponCode,
107                                         'coupon_expired' => generateDateTime($expirationTime, '0')
108                                 );
109
110                                 // Load member email template
111                                 $mailText = loadEmailTemplate('member_coupon_code', $content, $userid);
112
113                                 // And deliver it
114                                 sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
115
116                                 // Rember this user for the admin email
117                                 $adminUserids[] = $userid;
118                         } // END - while
119
120                         // Prepare content for template
121                         $content = array(
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')
128                         );
129
130                         // Send admin notification
131                         sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon_code', $content);
132
133                         // Coupon has been sent
134                         displayMessage('{--ADMIN_SEND_COUPON_DONE--}');
135                 } else {
136                         // No user was found
137                         displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
138                 }
139
140                 // Free result
141                 SQL_FREERESULT($result);
142         }
143 } else {
144         // Prepare some template data
145         $content['coupon_default_time_selection'] = createConfigurationTimeSelections('coupon_default_time', 'WDh');
146
147         // Load template
148         loadTemplate('admin_send_coupon', false, $content);
149 }
150
151 // [EOF]
152 ?>