Several minor rewrites, getter GET_JACKPOT_POINTS() added
[mailer.git] / inc / mysql-manager.php
index 4996285..102a3de 100644 (file)
@@ -1682,20 +1682,37 @@ function generateMetaDescriptionCode ($mod, $wht) {
        unset($GLOBALS['ref_level']);
 }
 
-// Adds points to the jackpot
-function ADD_JACKPOT ($points) {
-       $result = SQL_QUERY("SELECT points FROM `{!_MYSQL_PREFIX!}_jackpot` WHERE ok='ok' LIMIT 1", __FUNCTION__, __LINE__);
+function GET_JACKPOT_POINTS () {
+       // Default is zero
+       $jackpot = '0.00000';
+
+       // Read them
+       $result = SQL_QUERY("SELECT `points` FROM `{!_MYSQL_PREFIX!}_jackpot` WHERE `ok`='ok' LIMIT 1", __FUNCTION__, __LINE__);
+
+       // Do we have an entry?
        if (SQL_NUMROWS($result) == 0) {
-               // Create line
-               SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_jackpot` (ok, points) VALUES ('ok','%s')", array($points), __FUNCTION__, __LINE__);
+               // No, so create line
+               SQL_QUERY("INSERT INTO `{!_MYSQL_PREFIX!}_jackpot` (`ok`, `points`) VALUES ('ok','0.00000')", __FUNCTION__, __LINE__);
        } else {
-               // Free memory
-               SQL_FREERESULT($result);
+               // Read the line
+               list($jackpot) = SQL_FETCHROW($result);
+       }
 
-               // Update points
-               SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_jackpot` SET points=points+%s WHERE ok='ok' LIMIT 1",
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return them
+       return $jackpot;
+}
+
+// Adds points to the jackpot
+function ADD_JACKPOT ($points) {
+       // Get jackpot points for dummy
+       $jackpot = GET_JACKPOT_POINTS();
+
+       // Update points
+       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_jackpot` SET `points`=`points`+%s WHERE `ok`='ok' LIMIT 1",
                array($points), __FUNCTION__, __LINE__);
-       }
 }
 
 // Subtracts points from the jackpot
@@ -1703,24 +1720,22 @@ function SUB_JACKPOT ($points) {
        // First failed
        $ret = '-1';
 
-       // Get current points
-       $result = SQL_QUERY("SELECT points FROM `{!_MYSQL_PREFIX!}_jackpot` WHERE ok='ok' LIMIT 1", __FUNCTION__, __LINE__);
-       if (SQL_NUMROWS($result) == 0) {
-               // Create line
-               SQL_QUERY("INSERT INTO `{!_MYSQL_PREFIX!}_jackpot` (ok, points) VALUES ('ok', 0.00000)", __FUNCTION__, __LINE__);
-       } else {
-               // Read points
-               list($jackpot) = SQL_FETCHROW($result);
-               if ($jackpot >= $points) {
-                       // Update points when there are enougth points in jackpot
-                       SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_jackpot` SET points=points-%s WHERE ok='ok' LIMIT 1",
+       // Get jackpot points for dummy
+       $jackpot = GET_JACKPOT_POINTS();
+
+       // Enougth points i jackpot?
+       if ($jackpot >= $points) {
+               // Update points when there are enougth points in jackpot
+               SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_jackpot` SET `points`=`points`-%s WHERE `ok`='ok' LIMIT 1",
                        array($points), __FUNCTION__, __LINE__);
-                       $ret = $jackpot - $points;
-               } // END - if
-       }
+               $ret = $jackpot - $points;
+       } // END - if
 
        // Free memory
        SQL_FREERESULT($result);
+
+       // Return the result
+       return $ret;
 }
 
 // Checks wether the extension demo is actuve and the admin login is demo (password needs to be demo, too!)
@@ -1831,7 +1846,7 @@ function updateConfiguration ($entries, $values, $updateMode='') {
 
        // Run database update
        //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "entries={$entries}");
-       SQL_QUERY("UPDATE `{!_MYSQL_PREFIX!}_config` SET ".$entries." WHERE config=0 LIMIT 1", __FUNCTION__, __LINE__);
+       SQL_QUERY("UPDATE `{!_MYSQL_PREFIX!}_config` SET ".$entries." WHERE `config`=0 LIMIT 1", __FUNCTION__, __LINE__);
 
        // Get affected rows
        $affectedRows = SQL_AFFECTEDROWS();
@@ -1861,9 +1876,12 @@ function PREPARE_SQL_HTML_HOLIDAY ($mode) {
 // "Getter" for total available receivers
 function getTotalReceivers ($mode='normal') {
        // Query database
-       $result_all = SQL_QUERY("SELECT userid
-FROM `{!_MYSQL_PREFIX!}_user_data`
-WHERE `status`='CONFIRMED' AND receive_mails > 0 ".PREPARE_SQL_HTML_HOLIDAY($mode),
+       $result_all = SQL_QUERY("SELECT
+       `userid`
+FROM
+       `{!_MYSQL_PREFIX!}_user_data`
+WHERE
+       `status`='CONFIRMED' AND `receive_mails` > 0 ".PREPARE_SQL_HTML_HOLIDAY($mode),
        __FUNCTION__, __LINE__);
 
        // Get num rows
@@ -1899,8 +1917,8 @@ function generateCategoryOptionsList ($mode) {
                        $CATS['name'][] = $content['cat'];
 
                        // Check which users are in this category
-                       $result_uids = SQL_QUERY_ESC("SELECT userid FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE cat_id=%s",
-                       array(bigintval($content['id'])), __FUNCTION__, __LINE__);
+                       $result_uids = SQL_QUERY_ESC("SELECT `userid` FROM `{!_MYSQL_PREFIX!}_user_cats` WHERE `cat_id`=%s",
+                               array(bigintval($content['id'])), __FUNCTION__, __LINE__);
 
                        // Start adding all
                        $uid_cnt = 0;
@@ -1911,7 +1929,7 @@ function generateCategoryOptionsList ($mode) {
 FROM
        `{!_MYSQL_PREFIX!}_user_data`
 WHERE
-       `userid`=%s AND `status`='CONFIRMED' AND receive_mails > 0".PREPARE_SQL_HTML_HOLIDAY($mode)."
+       `userid`=%s AND `status`='CONFIRMED' AND `receive_mails` > 0".PREPARE_SQL_HTML_HOLIDAY($mode)."
 LIMIT 1",
                                        array(bigintval($ucat)), __FUNCTION__, __LINE__);
 
@@ -1977,35 +1995,35 @@ function addBonusMailToQueue ($subject, $text, $receiverList, $points, $seconds,
 
                // Add HTML mail
                SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_bonus`
-(subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent, html_msg)
+(`subject`, `text`, `receivers`, `points`, `time`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `mails_sent`, `html_msg`)
 VALUES ('%s','%s','%s','%s','%s','NEW', UNIX_TIMESTAMP(),'%s','%s','%s','%s','%s')",
                array(
-               $subject,
-               $text,
-               $receiverList,
-               $points,
-               $seconds,
-               $url,
-               $cat,
-               $target,
-               bigintval($receiver),
-               $HTML
+                       $subject,
+                       $text,
+                       $receiverList,
+                       $points,
+                       $seconds,
+                       $url,
+                       $cat,
+                       $target,
+                       bigintval($receiver),
+                       $HTML
                ), __FUNCTION__, __LINE__);
        } else {
                // Add regular mail
                SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_bonus`
-(subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, mails_sent)
+(`subject`, `text`, `receivers`, `points`, `time`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send`, `mails_sent`)
 VALUES ('%s','%s','%s','%s','%s','NEW', UNIX_TIMESTAMP(),'%s','%s','%s','%s')",
                array(
-               $subject,
-               $text,
-               $receiverList,
-               $points,
-               $seconds,
-               $url,
-               $cat,
-               $target,
-               bigintval($receiver),
+                       $subject,
+                       $text,
+                       $receiverList,
+                       $points,
+                       $seconds,
+                       $url,
+                       $cat,
+                       $target,
+                       bigintval($receiver),
                ), __FUNCTION__, __LINE__);
        }
 }
@@ -2087,9 +2105,12 @@ function getTimestampFromUserStats ($type, $data, $uid = 0) {
        } // END - if
 
        // Try to find the entry
-       $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`inserted`) AS `stamp`
-FROM `{!_MYSQL_PREFIX!}_user_stats_data`
-WHERE `userid`=%s AND `stats_type`='%s' AND `stats_data`='%s'
+       $result = SQL_QUERY_ESC("SELECT
+       UNIX_TIMESTAMP(`inserted`) AS stamp
+FROM
+       `{!_MYSQL_PREFIX!}_user_stats_data`
+WHERE
+       `userid`=%s AND `stats_type`='%s' AND `stats_data`='%s'
 LIMIT 1",
                array(
                        bigintval($uid),
@@ -2122,7 +2143,7 @@ function insertUserStatsRecord ($uid, $type, $data) {
        if ((!getTimestampFromUserStats($type, $data, $uid)) && (!is_array($data))) {
                // Then insert it!
                SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_stats_data` (`userid`,`stats_type`,`stats_data`) VALUES (%s,'%s','%s')",
-               array(bigintval($uid), $type, $data), __FUNCTION__, __LINE__);
+                       array(bigintval($uid), $type, $data), __FUNCTION__, __LINE__);
        } elseif (is_array($data)) {
                // Invalid data!
                DEBUG_LOG(__FUNCTION__, __LINE__, "uid={$uid},type={$type},data={".gettype($data).": Invalid statistics data type!");
@@ -2142,15 +2163,25 @@ function getUserReferalPoints ($uid, $level) {
        } // END - if
 
        // Get refs from database
-       $result = SQL_QUERY_ESC("SELECT ur.id, ur.refid, ud.status, ud.last_online, ud.mails_confirmed, ud.emails_received".$add."
-FROM `{!_MYSQL_PREFIX!}_user_refs` AS ur
-LEFT JOIN `{!_MYSQL_PREFIX!}_user_points` AS up
-ON ur.refid=up.userid AND ur.level=0
-LEFT JOIN `{!_MYSQL_PREFIX!}_user_data` AS ud
-ON ur.refid=ud.userid
-WHERE ur.userid=%s AND ur.level=%s
+       $result = SQL_QUERY_ESC("SELECT
+       ur.id, ur.refid, ud.status, ud.last_online, ud.mails_confirmed, ud.emails_received".$add."
+FROM
+       `{!_MYSQL_PREFIX!}_user_refs` AS ur
+LEFT JOIN
+       `{!_MYSQL_PREFIX!}_user_points` AS up
+ON
+       ur.refid=up.userid AND ur.level=0
+LEFT JOIN
+       `{!_MYSQL_PREFIX!}_user_data` AS ud
+ON
+       ur.refid=ud.userid
+WHERE
+       ur.userid=%s AND ur.level=%s
 ORDER BY ur.refid ASC",
-       array(bigintval($uid), bigintval($level)), __FUNCTION__, __LINE__);
+               array(
+                       bigintval($uid),
+                       bigintval($level)
+               ), __FUNCTION__, __LINE__);
 
        // Are there some entries?
        if (SQL_NUMROWS($result) > 0) {
@@ -2202,7 +2233,7 @@ ORDER BY ur.refid ASC",
 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",
-       array($column, bigintval($id), $count), __FUNCTION__, __LINE__);
+               array($column, bigintval($id), $count), __FUNCTION__, __LINE__);
 
        // Are there entries?
        if (SQL_NUMROWS($result) > 0) {