Now total received mails are reduced if no entry in user_stats_data is found
[mailer.git] / inc / mysql-manager.php
index 0e41c93d78987f21a3ee3f218903e500c4e88200..c9fad71c38bd45b68868552f4ecc139b956bb10c 100644 (file)
@@ -2083,16 +2083,29 @@ ORDER BY ur.refid ASC",
 // Recuce the amount of received emails for the receipients for given email
 function reduceRecipientReceivedMails ($column, $id, $count) {
        // Search for mail in database
-       $result = SQL_QUERY_ESC("SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `%s`=%s ORDER BY `userid` ASC LIMIT %s",
+       $result = SQL_QUERY_ESC("SELECT * FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `%s`=%s ORDER BY `userid` ASC LIMIT %s",
                array($column, bigintval($id), $count), __FUNCTION__, __LINE__);
 
        // Are there entries?
        if (SQL_NUMROWS($result) > 0) {
                // Now load all userids for one big query!
-               // @TODO This can be somehow rewritten
                $userids = array();
-               while (list($userid) = SQL_FETCHROW($result)) {
-                       $userids[$userid] = $userid;
+               while ($data = SQL_FETCHARRAY($result)) {
+                       // By default we want to reduce and have no mails found
+                       $num = 0;
+
+                       // We must now look if he has already confirmed this mail, so might sound double, but it may resolve problems
+                       // @TODO Rewrite this to a filter
+                       if ((isset($data['stats_id'])) && ($data['stats_id'] > 0)) {
+                               // User email
+                               $num = countSumTotalData($data['userid'], 'user_stats_data', 'id', 'userid', true, sprintf(" AND `stats_type`='mailid' AND `stats_data`=%s", bigintval($data['stats_id'])));
+                       } elseif ((isset($data['bonus_id'])) && ($data['bonus_id'] > 0)) {
+                               // Bonus mail
+                               $num = countSumTotalData($data['userid'], 'user_stats_data', 'id', 'userid', true, sprintf(" AND `stats_type`='bonusid' AND `stats_data`=%s", bigintval($data['bonus_id'])));
+                       }
+
+                       // Reduce this users total received emails?
+                       if ($num === 0) $userids[$data['userid']] = $data['userid'];
                } // END - while
 
                // Now update all user accounts