Extension ext-coupon continued, a lot improvements applied:
[mailer.git] / inc / modules / admin / what-send_coupon.php
index 2e4d66a2fcd9ab44b721d01aeb34b033c1f02a7b..a3329ae3e3614f4a1518b46e324d44ed775fbece 100644 (file)
@@ -54,6 +54,88 @@ if (isFormSent()) {
                displayMessage('{--ADMIN_SEND_COUPON_DESCRIPTION_NOT_ENTERED--}');
        } else {
                // All fields are set, so we can start looking for users who wants to receive coupons
+               $result = SQL_QUERY("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `status`='CONFIRMED' AND `receiving_coupons`='Y' ORDER BY `userid` ASC",
+                       __FILE__, __LINE__);
+
+               // Do we have entries left?
+               if (SQL_NUMROWS($result) > 0) {
+                       // Convert timestamp selections
+                       $expirationTime = time() + createEpocheTimeFromSelections('coupon_default_time', postRequestArray());
+
+                       // Insert coupon data
+                       SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_coupon_data` (`timestamp_expired`, `total_created`, `points`, `coupon_description`) VALUES('{%%pipe,SQL_EPOCHE_TO_TIMESTAMP=%s%%}', %s,%s,'%s')",
+                               array(
+                                       $expirationTime,
+                                       SQL_NUMROWS($result),
+                                       bigintval(postRequestParameter('points')),
+                                       postRequestParameter('description')
+                               ), __FILE__, __LINE__
+                       );
+
+                       // Get the insert id from the table
+                       $couponId = SQL_INSERTID();
+
+                       // Load entry
+                       while (list($userid) = SQL_FETCHROW($result)) {
+                               // By default no code is unique
+                               $isUnique = false;
+                               $couponCode = '';
+
+                               // Look for a unique id
+                               while (!$isUnique) {
+                                       /**
+                                        * Add a coupon for this user, first we need to create a
+                                        * table-unique "id". The function generatePassword() can do
+                                        * this job for us but we want to exclude some characters.
+                                        */
+                                       $couponCode = generatePassword(30, array('-','+','_','/','.'));
+
+                                       // Is it really unique?
+                                       $isUnique = (countSumTotalData($couponCode, 'user_coupons', 'id', 'coupon_code', true) == 0);
+                               } // END - while
+
+                               // The above loop should always set $couponCode, so we can now insert it
+                               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_coupons` (`coupon_id`, `userid`, `coupon_code`) VALUES(%s,%s,'%s')",
+                                       array($couponId, $userid, $couponCode), __FILE__, __LINE__);
+
+                               // Fill array with all required data
+                               $content = array(
+                                       'coupon_id'      => $couponId,
+                                       'userid'         => $userid,
+                                       'points'         => bigintval(postRequestParameter('points')),
+                                       'coupon_code'    => $couponCode,
+                                       'coupon_expires' => generateDateTime($expirationTime, '0')
+                               );
+
+                               // Load member email template
+                               $mailText = loadEmailTemplate('member_coupon', $content, $userid);
+
+                               // And deliver it
+                               sendEmail($userid, '{--MEMBER_COUPON_SUBJECT--}', $mailText);
+
+                               // Rember this user for the admin email
+                               $adminUserids[] = $userid;
+                       } // END - while
+               } else {
+                       // No user was found
+                       displayMessage('{--ADMIN_SEND_COUPON_USER_404--}');
+               }
+
+               // Prepare content for template
+               $content = array(
+                       'userids'        => implode(',', $adminUserids),
+                       'userid_count'   => count($adminUserids),
+                       'coupon_id'      => $couponId,
+                       'points'         => bigintval(postRequestParameter('points')),
+                       'description'    => postRequestParameter('description'),
+                       'coupon_expires' => generateDateTime($expirationTime, '0')
+               );
+
+               // Send admin notification
+               sendAdminNotification('{--ADMIN_COUPON_SUBJECT--}', 'admin_coupon', $content);
+
+               // Free result
+               SQL_FREERESULT($result);
        }
 } else {
        // Prepare some template data