]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
More fixes for points booking and surfbar
[mailer.git] / inc / mysql-manager.php
index 61a98d58c70fcdd46e45dae4a10e07575dcdd792..ade9cabcd67dffc5535110075814da747b30a4b3 100644 (file)
@@ -1044,6 +1044,9 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
 {
        global $DEPTH, $_CONFIG, $DATA, $link;
 
+       // Debug message
+       //DEBUG_LOG(__FUNCTION__.": uid={$uid},points={$points}");
+
        // When $uid = 0 add points to jackpot
        if ($uid == "0") {
                // Add points to jackpot
@@ -1067,48 +1070,65 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
        // Which points, locked or normal?
        $data = "points"; if ($locked) $data = "locked_points";
 
+       // Check user account
        $result_user = SQL_QUERY_ESC("SELECT refid, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
         array(bigintval($uid)), __FILE__, __LINE__);
+
        //* DEBUG */ echo "+".SQL_NUMROWS($result_user).":".$points."+<br />\n";
        if (SQL_NUMROWS($result_user) == 1) {
                // This is the user and his ref
                list ($ref, $email) = SQL_FETCHROW($result_user);
-               SQL_FREERESULT($result_user);
 
-               $result = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE level='%s' LIMIT 1",
+               // Debug message
+               //DEBUG_LOG(__FUNCTION__.": ref={$ref},email={$email},DEPTH={$DEPTH}");
+
+               // Get referal data
+               $result_lvl = SQL_QUERY_ESC("SELECT %s FROM "._MYSQL_PREFIX."_%s WHERE level='%s' LIMIT 1",
                 array($percents, $table, bigintval($DEPTH)), __FILE__, __LINE__);
                //* DEBUG */ echo "DEPTH:".$DEPTH."<br />\n";
-               if (SQL_NUMROWS($result) == 1) {
-                       list($per) = SQL_FETCHROW($result);
-                       SQL_FREERESULT($result);
-                       $P = $points * $per / 100;
-                       //* DEBUG */ echo "ADD:".$P."<br />\n";
+               if (SQL_NUMROWS($result_lvl) == 1) {
+                       // Get percents
+                       list($per) = SQL_FETCHROW($result_lvl);
+
+                       // Calculate new points
+                       $ref_points = $points * $per / 100;
+
+                       // Debug message
+                       //DEBUG_LOG(__FUNCTION__.": percent={$per},ref_points={$ref_points}");
 
                        // Update points...
-                       $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET %s=%s+%s WHERE userid=%s AND ref_depth=%s LIMIT 1",
-                        array($data, $data, $P, bigintval($uid), bigintval($DEPTH)), __FILE__, __LINE__);
-                       if (SQL_AFFECTEDROWS($link, __FILE__, __LINE__) == 0) {
+                       SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET %s=%s+%s WHERE userid=%s AND ref_depth=%d LIMIT 1",
+                        array($data, $data, $ref_points, bigintval($uid), bigintval($DEPTH)), __FILE__, __LINE__);
+
+                       // Debug log
+                       //DEBUG_LOG(__FUNCTION__.": affectedRows=".SQL_AFFECTEDROWS().",DEPTH={$DEPTH}");
+
+                       // No entry updated?
+                       if (SQL_AFFECTEDROWS() == 0) {
                                // First ref in this level! :-)
-                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_points (userid, ref_depth, %s) VALUES (%s, %s, %s)",
-                                array($data, bigintval($uid), bigintval($DEPTH), $P), __FILE__, __LINE__);
-                       }
+                               $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_user_points (userid, ref_depth, %s) VALUES (%s, %d, %s)",
+                                array($data, bigintval($uid), bigintval($DEPTH), $ref_points), __FILE__, __LINE__);
+
+                               // Debug log
+                               //DEBUG_LOG(__FUNCTION__.": insertedRows=".SQL_AFFECTEDROWS()."");
+                       } // END - if
 
                        // Update mediadata as well
                        if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
                                // Update database
-                               MEDIA_UPDATE_ENTRY(array("total_points"), "add", $P);
-                       }
+                               MEDIA_UPDATE_ENTRY(array("total_points"), "add", $ref_points);
+                       } // END - if
 
                        // Points updated, maybe I shall send him an email?
                        if (($send_notify) && ($ref > 0) && (!$locked)) {
                                //              0                1      2              3
-                               $DATA = array($per, bigintval($DEPTH), $P, bigintval($ref));
+                               $DATA = array($per, bigintval($DEPTH), $ref_points, bigintval($ref));
                                $msg = LOAD_EMAIL_TEMPLATE("confirm-referral", "", bigintval($uid));
 
                                SEND_EMAIL($email, THANX_REFERRAL_ONE, $msg);
                        } elseif (($send_notify) && ($ref == 0) && (!$locked) && ($add_mode == "direct") && (!defined('__POINTS_VALUE'))) {
                                // Direct payment shall be notified about
-                               define('__POINTS_VALUE', $P);
+                               define('__POINTS_VALUE', $ref_points);
 
                                // Load message
                                $msg = LOAD_EMAIL_TEMPLATE("add-points", REASON_DIRECT_PAYMENT, $uid);
@@ -1124,7 +1144,13 @@ function ADD_POINTS_REFSYSTEM($uid, $points, $send_notify=false, $rid="0", $lock
                                ADD_POINTS_REFSYSTEM($ref, $points, $send_notify, $ref, $locked);
                        }
                }
+
+               // Free result
+               SQL_FREERESULT($result_lvl);
        }
+
+       // Free result
+       SQL_FREERESULT($result_user);
 }
 //
 function UPDATE_REF_COUNTER($uid)
@@ -1635,7 +1661,7 @@ function UPDATE_CONFIG ($entries, $values, $updateMode="") {
        }
 
        // Run database update
-       //* DEBUG: */ DEBUG_LOG(__FUNCTION__.":entries={$entries}");
+       //DEBUG_LOG(__FUNCTION__.":entries={$entries}");
        SQL_QUERY("UPDATE "._MYSQL_PREFIX."_config SET ".$entries." WHERE config=0 LIMIT 1", __FILE__, __LINE__);
 
        // Destroy cache