ce1538a3aef07901f9e55439e3abb08ea8a55b12
[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 - 2015 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 = sqlQuery("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 (!ifSqlHasZeroNums($result)) {
71                         // Convert timestamp selections
72                         $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
73
74                         // Insert coupon data
75                         sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_coupon_data` (`coupon_expired`, `total_created`, `points`, `coupon_description`) VALUES ('{%%pipe,getSqlTimestampFromUnix=%s%%}', %s,%s,'%s')",
76                                 array(
77                                         $expirationTime,
78                                         sqlNumRows($result),
79                                         bigintval(postRequestElement('points')),
80                                         postRequestElement('coupon_description')
81                                 ), __FILE__, __LINE__
82                         );
83
84                         // Get the insert id from the table
85                         $couponId = getSqlInsertId();
86
87                         // Init userid array
88                         $adminUserids = array();
89
90                         // Load entry
91                         while (list($userid) = sqlFetchRow($result)) {
92                                 // Debug message
93                                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'NUMS=' . sqlNumRows($result) . ',userid=' . $userid);
94
95                                 // By default no code is unique
96                                 $isUnique = FALSE;
97                                 $couponCode = '';
98
99                                 // Look for a unique id
100                                 while (!$isUnique) {
101                                         /*
102                                          * Add a coupon for this user, first generate a
103                                          * "table-unique" id number. generatePassword() can do this
104                                          * but with some characters excluded.
105                                          */
106                                         $couponCode = generatePassword(30, array('-', '+', '_', '/', '.'));
107
108                                         // Is it really unique?
109                                         $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', TRUE) == 0);
110                                 } // END - while
111
112                                 // The above loop should always set $couponCode, so we can now insert it
113                                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_coupons` (`coupon_id`, `userid`, `coupon_code`) VALUES (%s,%s,'%s')",
114                                         array(
115                                                 $couponId,
116                                                 $userid,
117                                                 $couponCode
118                                         ), __FILE__, __LINE__);
119
120                                 // Fill array with all required data
121                                 $content = array(
122                                         'coupon_id'      => $couponId,
123                                         'userid'         => $userid,
124                                         'points'         => bigintval(postRequestElement('points')),
125                                         'coupon_code'    => $couponCode,
126                                         'coupon_expired' => generateDateTime($expirationTime, '0')
127                                 );
128
129                                 // Load member email template
130                                 $mailText = loadEmailTemplate('member_coupon_code', $content, $userid);
131
132                                 // And deliver it
133                                 sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
134
135                                 // Rember this user for the admin email
136                                 array_push($adminUserids, $userid);
137                         } // END - while
138
139                         // Prepare content for template
140                         $content = array(
141                                 'userids'            => implode(',', $adminUserids),
142                                 'userid_count'       => count($adminUserids),
143                                 'coupon_id'          => $couponId,
144                                 'points'             => bigintval(postRequestElement('points')),
145                                 'coupon_description' => postRequestElement('coupon_description'),
146                                 'coupon_expired'     => generateDateTime($expirationTime, '0')
147                         );
148
149                         // Send admin notification
150                         sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon_code', $content);
151
152                         // Coupon has been sent
153                         displayMessage('{--ADMIN_SEND_COUPON_DONE--}');
154                 } else {
155                         // No user was found
156                         displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
157                 }
158
159                 // Free result
160                 sqlFreeResult($result);
161         }
162 } else {
163         // Prepare some template data
164         $content['coupon_default_time_selection'] = createConfigurationTimeSelections('coupon_default_time', 'WDh');
165
166         // Load template
167         loadTemplate('admin_send_coupon', FALSE, $content);
168 }
169
170 // [EOF]
171 ?>