Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / pool / pool-bonus.php
index 8951eafdcc2f2e563d3dadf5b91512bf86f79a53..eb263b9905241feda5c5aa0995d6539756be13b6 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
+ * 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 *
@@ -43,32 +43,59 @@ if (!defined('__SECURITY')) {
 // Don't run on daily reset
 if ((isResetModeEnabled()) || (!isHtmlOutputMode())) {
        // Skip here
-       return false;
-} elseif (!isExtensionActive('bonus')) {
-       // Abort if extension bonus is not active
-       return false;
+       return FALSE;
+} elseif ((!isExtensionActive('bonus')) || (!isExtensionActive('other'))) {
+       // Abort if extension bonus/other are not active
+       return FALSE;
 }
 
-// Need this here
 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
 if ($GLOBALS['pool_cnt'] < getMaxSend()) {
-       // Do we need to send out bonus mails?
+       // Default is ext-html_mail is gone
+       $add = '';
+
+       // Send out bonus mails?
        if (isExtensionActive('html_mail')) {
-               //                                 0        1        2          3          4        5          6            7         8        9            10           11
-               $result_bonus = SQL_QUERY("SELECT `id`,`subject`,`text`,`receivers`,`points`,`time`,`data_type`,`timestamp`,`url`,`cat_id`,`target_send`,`html_msg` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='NEW' ORDER BY `timestamp` DESC", __FILE__, __LINE__);
-       } else {
-               //                                 0        1        2          3          4        5          6            7         8        9            10
-               $result_bonus = SQL_QUERY("SELECT `id`,`subject`,`text`,`receivers`,`points`,`time`,`data_type`,`timestamp`,`url`,`cat_id`,`target_send` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='NEW' ORDER BY `timestamp` DESC", __FILE__, __LINE__);
-       }
-
-       if (!SQL_HASZERONUMS($result_bonus)) {
+               //  With HTML support
+               $add = ', `html_msg`';
+       } // END - if
+
+       // Run query
+       $result_main = sqlQuery("SELECT
+       `id`,
+       `subject`,
+       `text`,
+       `receivers`,
+       `points`,
+       `time`,
+       `data_type`,
+       `timestamp`,
+       `url`,
+       `cat_id`,
+       `target_send`
+       " . $add . "
+FROM
+       `{?_MYSQL_PREFIX?}_bonus`
+WHERE
+       `data_type`='NEW'
+ORDER BY
+       `timestamp` DESC", __FILE__, __LINE__);
+
+       // Some mails left?
+       if (!ifSqlHasZeroNumRows($result_main)) {
+               // Init SQLs here
+               initSqls();
+
                // Send these mails away...
                $count2 = '';
-               while ($mailData = SQL_FETCHARRAY($result_bonus)) {
+               while ($mailData = sqlFetchArray($result_main)) {
                        // Message is active in queue
-                       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
+                       sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
                                array(bigintval($mailData['id'])), __FILE__, __LINE__);
 
+                       // Default is no users left
+                       $receivers = array();
+
                        // "Explode" all receivers into an array
                        if (isInString(';', $mailData['receivers'])) {
                                // There's more than one receiver in the list...
@@ -76,14 +103,13 @@ if ($GLOBALS['pool_cnt'] < getMaxSend()) {
                        } elseif (!empty($mailData['points'])) {
                                // Only one user left
                                $receivers = array($mailData['receivers']);
-                       } else {
-                               // No users left
-                               $receivers = array(0);
                        }
                        $temporaryReceivers = $receivers;
 
-                       // Now, if we are good little boys and girls Santa Claus left us some user-ids.
-                       // We can now send mails to them...
+                       /*
+                        * Now, if we are good little boys and girls Santa Claus left us
+                        * some user-ids and send out mails to them.
+                        */
                        foreach ($receivers as $key => $userid) {
                                // Load personal data
                                if (fetchUserData($userid)) {
@@ -91,7 +117,7 @@ if ($GLOBALS['pool_cnt'] < getMaxSend()) {
                                        //* DEBUG: */ debugOutput('OK!/L:'.__LINE__);
 
                                        // Remove receiver from list
-                                       $status = removeReceiver($temporaryReceivers, $key, $userid, $mailData['id'], $mailData['id'], true);
+                                       $status = removeReceiver($temporaryReceivers, $key, $userid, $mailData['id'], $mailData['id'], TRUE);
 
                                        // Did it work?
                                        switch ($status) {
@@ -111,16 +137,17 @@ if ($GLOBALS['pool_cnt'] < getMaxSend()) {
                                                        // Count one up and remove entry from dummy array
                                                        $GLOBALS['pool_cnt']++; unset($temporaryReceivers[$key]);
 
+                                                       // Is ext-user installed?
                                                        if (isExtensionInstalledAndNewer('user', '0.1.4')) {
+                                                               // Debug message
+                                                               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',mailData[id]=' . $mailData['id']);
                                                                // Update mails received for receiver
-                                                               SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
-                                                               array(bigintval($userid)), __FILE__, __LINE__);
+                                                               addSql(sprintf("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1", bigintval($userid)));
                                                        } // END - if
 
-                                                       // Do we have send maximum mails?
+                                                       // Is the size of pool reached or nothing to sent left?
                                                        if (($GLOBALS['pool_cnt'] >= getMaxSend()) || (countSelection($temporaryReceivers) == 0)) {
-                                                               // Yes, we have
-                                                               //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__);
+                                                               // Yes, it is. So abort here
                                                                break;
                                                        } // END - if
                                                        break;
@@ -132,6 +159,9 @@ if ($GLOBALS['pool_cnt'] < getMaxSend()) {
                                } // END - if
                        } // END - foreach
 
+                       // Run all SQLs
+                       runFilterChain('run_sqls');
+
                        // Update mediadata if version is 0.0.4 or higher
                        if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
                                // Update entry (or add missing
@@ -145,7 +175,7 @@ if ($GLOBALS['pool_cnt'] < getMaxSend()) {
                        //* DEBUG: */ debugOutput('-L:'.__LINE__.'/'.countSelection($temporaryReceivers).'-');
                        if (countSelection($temporaryReceivers) == 0) {
                                // Queue reached!
-                               SQL_QUERY_ESC("UPDATE
+                               sqlQueryEscaped("UPDATE
        `{?_MYSQL_PREFIX?}_bonus`
 SET
        `data_type`='SEND',
@@ -165,7 +195,7 @@ LIMIT 1",
                                } // END - if
                        } elseif ($GLOBALS['pool_cnt'] >= getMaxSend()) {
                                // Update bonus pool
-                               SQL_QUERY_ESC("UPDATE
+                               sqlQueryEscaped("UPDATE
        `{?_MYSQL_PREFIX?}_bonus`
 SET
        `data_type`='NEW',
@@ -179,14 +209,17 @@ LIMIT 1",
                                                implode(';', $temporaryReceivers),
                                                bigintval($mailData['id'])
                                        ), __FILE__, __LINE__);
-                               //* DEBUG: */ debugOutput('*L:'.__LINE__.'<pre>'.print_r($temporaryReceivers, true).'</pre>!!!');
+                               //* DEBUG: */ debugOutput('*L:'.__LINE__.'<pre>'.print_r($temporaryReceivers, TRUE).'</pre>!!!');
+
+                               // Run any SQLs
+                               runFilterChain('run_sqls');
                                break;
                        }
                } // END - while
        } // END - if
 
        // Free memory
-       SQL_FREERESULT($result_bonus);
+       sqlFreeResult($result_main);
 
        // Remove variable
        unset($mailText);