]> git.mxchange.org Git - mailer.git/blobdiff - inc/modules/member/what-cash_coupon.php
mailer project continued:
[mailer.git] / inc / modules / member / what-cash_coupon.php
index 2984ec788dcf755dd9cdcc11c4fc297514455573..fedbe03e8431d1d81f0b9ad30efd6eff0a85ec02 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * Mailer v0.2.1-FINAL                                Start: 28/06/2011 *
- * ===================                          Last change: 28/06/2011 *
+ * Mailer v0.2.1-FINAL                                Start: 06/28/2011 *
+ * ===================                          Last change: 06/28/2011 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : what-cash_coupon.php                             *
@@ -16,8 +16,8 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
  * it under the terms of the GNU General Public License as published by *
@@ -37,7 +37,7 @@
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       die();
+       exit();
 } elseif (!isMember()) {
        redirectToIndexMemberOnlyModule();
 }
@@ -47,13 +47,122 @@ addYouAreHereLink('member', __FILE__);
 
 // Continue only if the proper extension is active (admins can always continue)
 if ((!isExtensionActive('coupon')) && (!isAdmin())) {
-       displayMessage(generateExtensionInactiveNotInstalledMessage('coupon'));
+       displayMessage('{%pipe,generateExtensionInactiveNotInstalledMessage=coupon%}');
        return;
 } // END - if
 
 // Is the form sent?
 if (isFormSent()) {
-       // @TODO
+       // Is the coupon code entered?
+       if (isPostRequestElementSet('coupon_code')) {
+               // Search for the coupon
+               $result = SQL_QUERY_ESC("SELECT
+       c.`coupon_id`,
+       c.`userid`,
+       c.`coupon_code`,
+       UNIX_TIMESTAMP(d.`coupon_created`) AS `coupon_created`,
+       UNIX_TIMESTAMP(d.`coupon_expired`) AS `coupon_expired`,
+       d.`coupon_type`,
+       d.`total_created`,
+       d.`total_cashed`,
+       d.`points`,
+       d.`coupon_description`
+FROM
+       `{?_MYSQL_PREFIX?}_user_coupons` AS c
+INNER JOIN
+       `{?_MYSQL_PREFIX?}_coupon_data` AS d
+ON
+       c.`coupon_id`=d.`id`
+WHERE
+       c.`coupon_code`='%s' AND
+       c.`userid`=%s AND
+       c.`cashed_on` IS NULL AND
+       UNIX_TIMESTAMP(d.`coupon_expired`) > UNIX_TIMESTAMP()
+LIMIT 1",
+                       array(
+                               postRequestElement('coupon_code'),
+                               getMemberId()
+                       ), __FILE__, __LINE__);
+
+               // Do we have an entry?
+               if (SQL_NUMROWS($result) == 1) {
+                       // Load data
+                       $content = SQL_FETCHARRAY($result);
+
+                       // Run pre-filter
+                       $content = runFilterChain('pre_cash_coupon', $content);
+
+                       // Mark it as "cashed"
+                       SQL_QUERY_ESC("UPDATE
+       `{?_MYSQL_PREFIX?}_user_coupons`
+SET
+       `cashed_on`=NOW(),
+       `coupon_code`=NULL
+WHERE
+       `coupon_code`='%s' AND
+       `userid`=%s
+LIMIT 1",
+                               array(
+                                       postRequestElement('coupon_code'),
+                                       getMemberId()
+                               ), __FILE__, __LINE__);
+
+                       // Does it work?
+                       if (SQL_HASZEROAFFECTED()) {
+                               // Abort it here, it always must work
+                               reportBug(__FILE__, __LINE__, 'User coupon cannot be updated! coupon_id=' . $content['coupon_id']);
+                       } // END - if
+
+                       // Update count as well
+                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_coupon_data` SET `total_cashed`=`total_cashed`+1 WHERE `id`=%s AND `total_cashed` < `total_created` LIMIT 1",
+                               array(bigintval($content['coupon_id'])), __FILE__, __LINE__);
+
+                       // Does it work?
+                       if (SQL_HASZEROAFFECTED()) {
+                               // Abort it here, it always must work
+                               reportBug(__FILE__, __LINE__, 'Coupon data cannot be updated! coupon_id=' . $content['coupon_id']);
+                       } // END - if
+
+                       // Run post filter chain
+                       $content = runFilterChain('post_cash_coupon', $content);
+
+                       // Display message
+                       displayMessage('{%message,MEMBER_COUPON_CODE_CASHED=' . $content['points'] . '%}');
+               } else {
+                       // No coupon found
+                       displayMessage('{--MEMBER_COUPON_CODE_ALREADY_CASHED--}');
+               }
+
+               // Free the result
+               SQL_FREERESULT($result);
+       } else {
+               // No code entered!
+               displayMessage('{--MEMBER_COUPON_CODE_EMPTY--}');
+       }
+} elseif (isFormSent('change')) {
+       // Change receive status, depending on its current state
+       if (isUserDataEnabled('receiving_coupons')) {
+               // Unsubscribe from coupons
+               $message = '{--MEMBER_COUPON_RECEIVE_STATUS_UNSUBSCRIBED--}';
+               $newStatus = 'N';
+       } else {
+               // Subscribe to coupons
+               $message = '{--MEMBER_COUPON_RECEIVE_STATUS_SUBSCRIBED--}';
+               $newStatus = 'Y';
+       }
+
+       // And change it in the database
+       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `receiving_coupons`='%s' WHERE `userid`=%s LIMIT 1",
+               array($newStatus, getMemberId()), __FILE__, __LINE__);
+
+       // Did something change?
+       if (!SQL_HASZEROAFFECTED()) {
+               // Records have been updated
+               displayMessage($message);
+       } else {
+               // Nothing changed!
+               displayMessage('{--MEMBER_COUPON_RECEIVE_STATUS_UNCHANGED--}');
+       }
 } else {
        // Load form template
        loadTemplate('member_cash_coupon', false);