Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / mails / bonus_mails.php
index 67d20ee247e3a66a0a14bfd637a3d764b4b3eaf3..6fb1ef0d1d171748e5a77c97c80d9aed8faafcbc 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /************************************************************************
- * MXChange v0.2.1                                    Start: 11/22/2005 *
- * ===============                              Last change: 11/22/2005 *
+ * Mailer v0.2.1-FINAL                                Start: 11/22/2005 *
+ * ===================                          Last change: 11/22/2005 *
  *                                                                      *
  * -------------------------------------------------------------------- *
  * File              : bonus_mails.php                                  *
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : Versendet Erinnerungsmails                       *
  * -------------------------------------------------------------------- *
- * $Revision:: 856                                                    $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
- * -------------------------------------------------------------------- *
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *
- * For more information visit: http://www.mxchange.org                  *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2015 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 *
 
 // Some security stuff...
 if (!defined('__SECURITY')) {
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
-       require($INC);
-} elseif (!EXT_IS_ACTIVE("bonus")) {
+       die();
+} elseif ((!isExtensionActive('bonus')) || (isExtensionInstalledAndOlder('bonus', '0.9.2'))) {
+       // Do not execute script on missing/out-dated extension ext-bonus
+       return;
+} elseif (!isHtmlOutputMode()) {
+       // Do not execute script if not in HTML mode
        return;
 }
 
-// Do not execute when script is in CSS mode
-if ($GLOBALS['output_mode'] == 1) return;
-
 // Create timemark from saved month
-$mark = mktime(0, 0, 0, getConfig('last_month'), date("d", time()), date('Y', time()));
-$sql = ""; $MODE = "";
+$mark = mktime(0, 0, 0, getLastMonthly(), getDay(), getYear());
+$sql = ''; $mode = '';
 
 // Shall I sent activation or deactivation mail?
-$sql = "SELECT userid, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE (bonus_ral_notify ";
-switch (getConfig('bonus_active'))
-{
-case "Y": // Active rallye is activated
-       if (getConfig('bonus_en_notify') == "Y") {
-               // Okay, let's check for member accounts
-               $sql .= "= 0 OR (bonus_ral_notify > 0 AND bonus_ral_en_notify < bonus_ral_di_notify)";
-               $MODE = "en";
-       } else {
-               // Do not notify!
-               $sql = "";
-       }
-       break;
-
-case "N": // Active rallye is deactivated
-       if (getConfig('bonus_di_notify') == "Y") {
-               // Okay, let's check for member accounts
-               $sql .= " > 0 AND bonus_ral_di_notify < bonus_ral_en_notify";
-               $MODE = "di";
-       } else {
-               // Do not notify!
-               $sql = "";
-       }
-       break;
-}
+$sql = "SELECT `d`.`userid`, `d`.`email` FROM `{?_MYSQL_PREFIX?}_user_data` AS `d` WHERE (`d`.`bonus_rallye_enable_notify` ";
+switch (getBonusActive()) {
+       case 'Y': // Active rallye is activated
+               if (getBonusEnableNotify() == 'Y') {
+                       // Okay, let's check for member accounts
+                       $sql .= '= 0 OR (`d`.`bonus_rallye_enable_notify` > 0 AND `d`.`bonus_rallye_enable_notify` < `d`.`bonus_rallye_disable_notify`)';
+                       $mode = 'enable';
+               } else {
+                       // Do not notify!
+                       $sql = '';
+               }
+               break;
+
+       case 'N': // Active rallye is deactivated
+               if (getBonusDisableNotify() == 'Y') {
+                       // Okay, let's check for member accounts
+                       $sql .= ' > 0 AND `d`.`bonus_rallye_disable_notify` < `d`.`bonus_rallye_enable_notify`';
+                       $mode = 'disable';
+               } else {
+                       // Do not notify!
+                       $sql = '';
+               }
+               break;
+} // END - switch
 
 if (!empty($sql)) {
        // The SQL command needs to be finisched here (only confirmed accounts!)
-       $sql .= ") AND `status`='CONFIRMED' ORDER BY last_online ASC";
+       $sql .= ')' . runFilterChain('user_exclusion_sql', " AND `d`.`status`='CONFIRMED'") . ' ORDER BY `d`.`last_online` ASC';
 
        // Normal notification mails or bonus mails?
-       $sentBonusMails = ((getConfig('bonus_notify_points') > 0) && ($MODE == "en") && (EXT_IS_ACTIVE("bonus")));
-
-       // Generate subject line
-       $SUBJECT = constant('BONUS_RALLYE_'.strtoupper($MODE).'_NOTIFY');
+       $sentBonusMails = ((getBonusNotifyPoints() > 0) && ($mode == 'enable') && (isExtensionActive('bonus')));
 
        // Load message body for bonus mails
-       $MSG = LOAD_EMAIL_TEMPLATE("bonus_en_notify_body", "", "{PER}uid{PER}");
-       $RECEIVER = ""; $UIDs = array();
+       $message = loadEmailTemplate('bonus_enable_notify_body', '', '{PER}userid{PER}');
+       $receiver = ''; $userids = array();
 
        // Check for accounts to be notified
-       $result_main = SQL_QUERY($sql, __FILE__, __LINE__);
-       if (SQL_NUMROWS($result_main) > 0) {
+       $result_main = sqlQuery($sql, __FILE__, __LINE__);
+
+       if (!ifSqlHasZeroNumRows($result_main)) {
                // Okay lets notify all users!
-               while ($content = SQL_FETCHARRAY($result_main)) {
+               while ($content = sqlFetchArray($result_main)) {
                        // Update account
-                       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data`
-SET bonus_ral_notify='%s', bonus_ral_%s_notify='%s'
-WHERE userid=%s
+                       sqlQueryEscaped("UPDATE
+       `{?_MYSQL_PREFIX?}_user_data`
+SET
+       `bonus_rallye_%s_notify`=UNIX_TIMESTAMP()
+WHERE
+       `userid`=%s
 LIMIT 1",
-                               array(time(), $MODE, time(), $content['userid']), __FILE__, __LINE__);
+                               array(
+                                       $mode,
+                                       $content['userid']
+                               ), __FILE__, __LINE__);
 
                        // Load email template and send it to the user!
-                       if ($sentBonusMails === true) {
+                       if ($sentBonusMails === TRUE) {
                                // Add userid to queue
-                               $UIDs[] = $content['userid'];
+                               array_push($userids, $content['userid']);
                        } else {
                                // Send normal notification mail to the members
-                               $MSG = LOAD_EMAIL_TEMPLATE("bonus_".$MODE."_notify", array(), $content['userid']);
-                               SEND_EMAIL($content['email'], $SUBJECT, $MSG);
+                               $message = loadEmailTemplate('bonus_' . $mode . '_notify', $content, $content['userid']);
+                               sendEmail($content['userid'], '{--MEMBER_BONUS_RALLYE_' . strtoupper($mode) . '_SUBJECT--}', $message);
                        }
                } // END - while
 
                // Shall I send out bonus mails?
-               if ($sentBonusMails === true) {
+               if ($sentBonusMails === TRUE) {
                        // Okay, make array to string
-                       $RECEIVER = implode(";", $UIDs);
+                       $receiver = implode(';', $userids);
 
                        // Prepare URL
-                       $URL = "modules.php?module=index&amp;what=login";
+                       $url = 'modules.php?module=index&amp;what=login';
 
                        // Insert mail
-                       ADD_BONUS_MAIL_TO_QUEUE($SUBJECT, $MSG, $RECEIVER, getConfig('bonus_notify_points'), getConfig('bonus_notify_wait'), $URL, 0, "normal", SQL_NUMROWS($result_main));
+                       addBonusMailToQueue('{--MEMBER_BONUS_RALLYE_' . strtoupper($mode) . '_SUBJECT--}', $message, $receiver, getBonusNotifyPoints(), getConfig('bonus_notify_wait'), $url, 0, 'normal', sqlNumRows($result_main));
                } // END - if
        } // END - if
 
        // Free memory
-       SQL_FREERESULT($result_main);
+       sqlFreeResult($result_main);
 } // END - if
 
 //