From 1972cc4b6538e319712a70421f1d311decfc8bf9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 12 Jul 2011 20:56:48 +0000 Subject: [PATCH] ext-refback is now longer essential for the referal system, please make sure that gen_refback.php is execute (yes, from ext-user) --- DOCS/TODOs.txt | 1 - inc/extensions/ext-refback.php | 41 ++-- inc/extensions/ext-sql_patches.php | 3 - inc/extensions/ext-user.php | 25 ++- inc/gen_refback.php | 13 +- inc/libs/refback_functions.php | 186 ++---------------- inc/modules/member/what-refback.php | 6 +- inc/referal-functions.php | 175 +++++++++++++++- templates/de/emails/admin/admin_refback.tpl | 2 +- templates/de/emails/member/member_refback.tpl | 2 +- .../de/html/member/member_refback_edit.tpl | 2 +- .../html/member/member_refback_list_row.tpl | 4 +- .../member_refback_list_row_deleted.tpl | 4 +- 13 files changed, 238 insertions(+), 226 deletions(-) diff --git a/DOCS/TODOs.txt b/DOCS/TODOs.txt index 4f49ddab99..d12268f137 100644 --- a/DOCS/TODOs.txt +++ b/DOCS/TODOs.txt @@ -177,7 +177,6 @@ ./inc/mysql-manager.php:371: // @TODO Try to rewrite this to one or more functions ./inc/mysql-manager.php:44:// @TODO Can we cache this? ./inc/purge/purge-inact.php:55: // @TODO Rewrite these if() blocks to a filter -./inc/referal-functions.php:308: // @TODO Rewrite this to a filter ./inc/revision-functions.php:169:// @TODO This function does also set and get in 'cache_array' ./inc/template-functions.php:1069: // @TODO Deprecate this thing ./inc/template-functions.php:1080: // @TODO Deprecate this thing diff --git a/inc/extensions/ext-refback.php b/inc/extensions/ext-refback.php index 1c65b0cd5c..0d6b0c1741 100644 --- a/inc/extensions/ext-refback.php +++ b/inc/extensions/ext-refback.php @@ -41,27 +41,14 @@ if (!defined('__SECURITY')) { } // END - if // Version number -setThisExtensionVersion('0.0.3'); +setThisExtensionVersion('0.0.4'); // Version history array (add more with , '0.0.1' and so on) -setExtensionVersionHistory(array('0.0.0','0.0.1', '0.0.2', '0.0.3')); +setExtensionVersionHistory(array('0.0.0','0.0.1', '0.0.2', '0.0.3', '0.0.4')); switch (getExtensionMode()) { case 'register': // Do stuff when installation is running - // SQL commands to run - addDropTableSql('user_refs'); - addCreateTableSql('user_refs', " -`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, -`userid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, -`level` smallINT(6) UNSIGNED NOT NULL DEFAULT 0, -`refid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, -`refback` float(4,1) NOT NULL DEFAULT 0.0, -`points` FLOAT(20,5) DEFAULT 0.00000 NOT NULL, -PRIMARY KEY (`id`), -UNIQUE `user_refid` (`userid`, `level`, `refid`), -INDEX (`level`), -INDEX (`refid`)", - 'User Referals With Refback'); + // Member menu addMemberMenuSql('main', 'refback', 'Ref-Back einstellen', 4); // Execute this special file on registration @@ -73,7 +60,6 @@ INDEX (`refid`)", addDropTableSql('user_refs'); addExtensionSql("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_member_menu` WHERE `what`='refback'"); addExtensionSql("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_admin_menu` WHERE `what` IN('config_refback','list_refback')"); - addExtensionSql("UPDATE `{?_MYSQL_PREFIX?}_refsystem` SET level=level-1"); break; case 'activate': // Do stuff when admin activates this extension @@ -100,13 +86,8 @@ INDEX (`refid`)", break; case '0.0.2': // SQL queries for v0.0.2 - addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` CHANGE `userid` `userid` BIGINT(20) UNSIGNED NULL DEFAULT NULL'); - addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` CHANGE `level` `level` SMALLINT(6) UNSIGNED NULL DEFAULT NULL'); - addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` CHANGE `refid` `refid` BIGINT(20) UNSIGNED NULL DEFAULT NULL'); - addExtensionSql('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid` IS NULL OR `refid` IS NULL OR `level` IS NULL'); - // Update notes (these will be set as task text!) - setExtensionUpdateNotes("Updates von ext-user hier her verschoben."); + setExtensionUpdateNotes("Ungütiges Update seit Revision 246x."); break; case '0.0.3': // SQL queries for v0.0.3 @@ -115,6 +96,20 @@ INDEX (`refid`)", // This depends on ext-sql_patches addExtensionDependency('sql_patches'); + // Update notes + setExtensionUpdateNotes("Refback-Gutschriften werden nun über die Tabelle {OPEN_CONFIG}_MYSQL_PREFIX{CLOSE_CONFIG}_points_data verwaltet."); + break; + + case '0.0.4': // SQL queries for v0.0.4 + // This extension update depends on ext-sql_patches + addExtensionDependency('user'); + + // SQL commands to run - drop old first, add new next + addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` DROP `refback`'); + addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` DROP `points`'); + addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` ADD `refback_percents` FLOAT(4,1) NOT NULL DEFAULT 0.0'); + addExtensionSql('ALTER TABLE `{?_MYSQL_PREFIX?}_user_refs` ADD `refback_points` FLOAT(20,5) NOT NULL DEFAULT 0.00000'); + // Update notes setExtensionUpdateNotes("Refback-Gutschriften werden nun über die Tabelle {OPEN_CONFIG}_MYSQL_PREFIX{CLOSE_CONFIG}_points_data verwaltet."); break; diff --git a/inc/extensions/ext-sql_patches.php b/inc/extensions/ext-sql_patches.php index fcd19bfb74..8cc9bbc63a 100644 --- a/inc/extensions/ext-sql_patches.php +++ b/inc/extensions/ext-sql_patches.php @@ -586,9 +586,6 @@ INDEX (`admin_id`)", case '0.5.4': // SQL queries for v0.5.4 addMemberMenuSql('main', 'reflist', 'Ref-Übersicht', 5); - // Depends on refback extension - addExtensionDependency('refback'); - // Update notes (these will be set as task text!) setExtensionUpdateNotes("Ref-Übersicht eingebaut. Diese hängt von der Erweiterung refback ab."); break; diff --git a/inc/extensions/ext-user.php b/inc/extensions/ext-user.php index bb56d0cf02..b34c6bdd89 100644 --- a/inc/extensions/ext-user.php +++ b/inc/extensions/ext-user.php @@ -41,10 +41,10 @@ if (!defined('__SECURITY')) { } // END - if // Version number -setThisExtensionVersion('0.4.3'); +setThisExtensionVersion('0.4.4'); // Version history array (add more with , '0.0.1' and so on) -setExtensionVersionHistory(array('0.0.0', '0.1.0', '0.1.1', '0.1.2', '0.1.3', '0.1.4', '0.1.5', '0.1.6', '0.1.7', '0.1.8', '0.1.9', '0.2.0', '0.2.1', '0.2.2', '0.2.3', '0.2.4', '0.2.5', '0.2.6', '0.2.7', '0.2.8', '0.2.9', '0.3.0', '0.3.1', '0.3.2', '0.3.3', '0.3.4', '0.3.5', '0.3.6', '0.3.7', '0.3.8', '0.3.9', '0.4.0', '0.4.1', '0.4.2', '0.4.3')); +setExtensionVersionHistory(array('0.0.0', '0.1.0', '0.1.1', '0.1.2', '0.1.3', '0.1.4', '0.1.5', '0.1.6', '0.1.7', '0.1.8', '0.1.9', '0.2.0', '0.2.1', '0.2.2', '0.2.3', '0.2.4', '0.2.5', '0.2.6', '0.2.7', '0.2.8', '0.2.9', '0.3.0', '0.3.1', '0.3.2', '0.3.3', '0.3.4', '0.3.5', '0.3.6', '0.3.7', '0.3.8', '0.3.9', '0.4.0', '0.4.1', '0.4.2', '0.4.3', '0.4.4')); // Keep this extension always active! setExtensionAlwaysActive('Y'); @@ -453,6 +453,27 @@ INDEX (`userid`)", // Update notes (these will be set as task text!) setExtensionUpdateNotes("Überflüssiges Menü chk_regs entfernt, dies kann mit list_user übernommen werden."); break; + + case '0.4.4': // SQL queries for v0.4.4 + // SQL commands to run + addDropTableSql('user_refs'); + addCreateTableSql('user_refs', " +`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, +`userid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, +`level` smallINT(6) UNSIGNED NOT NULL DEFAULT 0, +`refid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, +PRIMARY KEY (`id`), +UNIQUE `user_refid` (`userid`, `level`, `refid`), +INDEX (`level`), +INDEX (`refid`)", + 'User referals'); + + // Update notes (these will be set as task text!) + setExtensionUpdateNotes("Neues Tabellenlayout und verschoben von ext-refback."); + + // Load ext-refback's special include to regenerate the referal table + addIncludeToPool('extension', 'inc/gen_refback.php'); + break; } // END - switch break; diff --git a/inc/gen_refback.php b/inc/gen_refback.php index 9f137f0bfd..fa4a874aca 100644 --- a/inc/gen_refback.php +++ b/inc/gen_refback.php @@ -40,9 +40,6 @@ if (!defined('__SECURITY')) { die(); } // END - if -// Make sure our functions are there -loadIncludeOnce('inc/libs/refback_functions.php'); - // Sanity-check: Do we have ref level 0? $result = SQL_QUERY('SELECT `id` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `level`=0 OR `level` IS NULL', __FILE__, __LINE__); if (!SQL_HASZERONUMS($result)) { @@ -67,13 +64,13 @@ SQL_QUERY('TRUNCATE TABLE `{?_MYSQL_PREFIX?}_user_refs`', __FILE__, __LINE__); // Begin with all users in level one $result_direct = SQL_QUERY('SELECT - u.userid + `userid` FROM - `{?_MYSQL_PREFIX?}_user_data` AS u + `{?_MYSQL_PREFIX?}_user_data` WHERE - u.refid=0 + `refid`=0 ORDER BY - u.userid ASC', __FILE__, __LINE__); + `userid` ASC', __FILE__, __LINE__); // Do we have entries? if (!SQL_HASZERONUMS($result_direct)) { @@ -81,7 +78,7 @@ if (!SQL_HASZERONUMS($result_direct)) { while ($content = SQL_FETCHARRAY($result_direct)) { // Update refback table //* DEBUG: */ debugOutput('userid=' . $content['userid']); - updateRefbackTable($content['userid']); + updateReferalTable($content['userid']); } // END - while } // END - if diff --git a/inc/libs/refback_functions.php b/inc/libs/refback_functions.php index 9cb0ac3734..fce722d012 100644 --- a/inc/libs/refback_functions.php +++ b/inc/libs/refback_functions.php @@ -59,7 +59,7 @@ function addRefbackPoints ($userid, $ref, $points, $ref_points) { initReferalSystem(); // "Walk" through all refids - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ',level=' . $content['level'] . ',percents=' . $content['percents'] . ',points=' . $points . ' - LOOP START!'); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . $ref . ',level=' . $content['level'] . ',percents=' . $content['refback_percents'] . ',points=' . $points . ' - LOOP START!'); foreach (getArrayFromRefbackLevel($userid, $content['level']) as $refid) { // Skip level zero or if both are the same if ($userid == $refid) { @@ -72,15 +72,15 @@ function addRefbackPoints ($userid, $ref, $points, $ref_points) { // Some percents given? if ($percents > 0) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',[percents]=' . $content['percents'] . ',percents=' . $percents . ',level=' . $content['level']); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'points=' . $points . ',[percents]=' . $content['refback_percents'] . ',percents=' . $percents . ',level=' . $content['level']); // Calculate points for refback - $refback = $points * ($content['percents'] / 100) * ($percents / 100); + $refback = $points * ($content['refback_percents'] / 100) * ($percents / 100); // Update refback table ('refid' and 'userid' must be exchanged!) SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET - `points`=`points`+%s + `refback_points`=`refback_points`+%s WHERE `userid`=%s AND `refid`=%s @@ -104,7 +104,7 @@ LIMIT 1", // Reduce points if refid is found if ($refid == $ref) { // Reduce points here! - $return = $points * ($content['percents'] / 100) - $refback; + $return = $points * ($content['refback_percents'] / 100) - $refback; //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'return=' . $return . ' - REDUCED!'); } // END - if } // END - if @@ -136,7 +136,7 @@ function getRefbackPercents ($userid, $ref) { $GLOBALS['refback_percents'][$userid][$ref] = '0'; // Get percents from database - $result = SQL_QUERY_ESC("SELECT `refback` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1", + $result = SQL_QUERY_ESC("SELECT `refback_percents` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `refid`=%s LIMIT 1", array($ref, $userid), __FUNCTION__, __LINE__); // Entry found? (Should be!) @@ -170,7 +170,8 @@ function getArrayFromRefbackLevel ($refid, $level) { FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE - `refid`=%s AND `level`=%s + `refid`=%s AND + `level`=%s ORDER BY `userid` ASC", array($refid, $level), __FUNCTION__, __LINE__); @@ -193,163 +194,6 @@ ORDER BY return $userIds; } -// Update "refback table" -function updateRefbackTable ($userid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); - // Load all referals - loadRefbackTable($userid); - - // Add missing level > 1 - addMissingRefbackLevels($userid); - - // The last step is to flush all userid's entries to the database - flushRefbackTableToDatabase($userid); - - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); -} - -// Loads all referals for given userid -function loadRefbackTable ($userid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); - // Init array - $GLOBALS['refback_refid'][$userid] = array(); - - // Get all level entries from the refsystem table - $result = SQL_QUERY_ESC('SELECT `level` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `userid`=%s ORDER BY `level` ASC', - array($userid), __FUNCTION__, __LINE__); - - // Do we have entries? - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_NUMROWS()=' . SQL_NUMROWS($result)); - if (SQL_NUMROWS($result) > 0) { - // Then walk through all levels - while (list($level) = SQL_FETCHROW($result)) { - // Init array - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level); - $GLOBALS['refback_refid'][$userid][$level] = array(); - - // Level is = 1? - if ($level == 1) { - // Load all referals of this user - $result_refs = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC', - array($userid), __FUNCTION__, __LINE__); - - // Do we have entries? - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',SQL_NUMROWS()=' . SQL_NUMROWS($result_refs)); - if (SQL_NUMROWS($result_refs) > 0) { - // Then again walk through all - while (list($refid) = SQL_FETCHROW($result_refs)) { - // Add this refid - $GLOBALS['refback_refid'][$userid][$level][] = $refid; - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); - // Load the refid's array as well - loadRefbackTable($refid); - } // END - while - } // END - if - - // Free result - SQL_FREERESULT($result_refs); - } // END - if - } // END - while - } // END - if - - // Free result - SQL_FREERESULT($result); - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); -} - -// Adds missing refback levels to the array -function addMissingRefbackLevels ($userid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); - // If the array is gone, you have called this function without calling loadRefbackTable() - if (!isset($GLOBALS['refback_refid'][$userid])) { - // Please fix your code - debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadRefbackTable() before! userid=' . $userid); - } // END - if - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['refback_refid'][$userid])); - - // Now walk through the array, first levels - foreach ($GLOBALS['refback_refid'][$userid] as $level=>$levelArray) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); - // Next are the users - foreach ($levelArray as $refid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); - // Does the refid have an array? - if (isset($GLOBALS['refback_refid'][$refid])) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',count()=' . count($GLOBALS['refback_refid'][$refid])); - // Okay, then walk through here, too - foreach ($GLOBALS['refback_refid'][$refid] as $refLevel=>$refArray) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',count()=' . count($refArray)); - // Also walk through this one - foreach ($refArray as $refRefid) { - // Calculate new level - $newLevel = $level + $refLevel; - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel); - // Is the refRefid not in? - if ((!isset($GLOBALS['refback_refid'][$userid][$newLevel])) || (!in_array($refRefid, $GLOBALS['refback_refid'][$userid][$newLevel]))) { - // Then we must add this ref's refid to the userid's next level - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',newLevel=' . $newLevel . ',refRefid=' . $refRefid . ' - ADDED!'); - $GLOBALS['refback_refid'][$userid][$newLevel][] = $refRefid; - } // END - if - } // END - foreach - } // END - foreach - } // END - foreach - } // END - foreach - } // END - foreach - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); -} - -// Flush all entries for given userid to database -function flushRefbackTableToDatabase ($userid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); - // If the array is gone, you have called this function without calling loadRefbackTable() - if (!isset($GLOBALS['refback_refid'][$userid])) { - // Please fix your code - debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadRefbackTable() before! userid=' . $userid); - } // END - if - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['refback_refid'][$userid])); - - // If no entries are there, skip this whole step - if (count($GLOBALS['refback_refid'][$userid]) == 0) { - // No entries found - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ABORTING...'); - return; - } // END - if - - // Prepare SQL - $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES '; - - // Now walk through the array, first levels - foreach ($GLOBALS['refback_refid'][$userid] as $level=>$levelArray) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); - // Next are the users - foreach ($levelArray as $refid) { - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); - // Query the user_refs table - list($count) = SQL_FETCHROW(SQL_QUERY_ESC('SELECT COUNT(`id`) AS `count` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`=%s AND `refid`=%s LIMIT 1', - array( - $userid, - $level, - $refid - ), __FUNCTION__, __LINE__)); - - // Do we have no entry? - if ($count == 0) { - // Then add it to the SQL - $SQL .= '(' . $userid . ',' . $level . ',' . $refid . '),'; - } // END - if - } // END - foreach - } // END - foreach - - // Remove last comma from SQL - $SQL = substr($SQL, 0, -1); - - // And run it - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL); - SQL_QUERY($SQL, __FUNCTION__, __LINE__); - - //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); -} - // "Getter" for array for user refs in given level function getArrayFromRefbackUserRefs ($userid, $level) { //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ' - ENTERED!'); @@ -360,8 +204,8 @@ function getArrayFromRefbackUserRefs ($userid, $level) { $result = SQL_QUERY_ESC("SELECT r.`id`, r.`refid`, - r.`refback`, - r.`points`, + r.`refback_percents`, + r.`refback_points`, d.`status`, d.`joined`, d.`mails_confirmed`, @@ -416,7 +260,7 @@ function getArrayFromUserRefbackData ($id) { $id = bigintval($id); // Get entry from database - $result = SQL_QUERY_ESC("SELECT `id`, `userid`, `refid`, `refback`, `level`, `points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1", + $result = SQL_QUERY_ESC("SELECT `id`, `userid`, `refid`, `refback_percents`, `level`, `refback_points` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `id`=%s AND `userid`=%s LIMIT 1", array($id, getMemberId()), __FUNCTION__, __LINE__); // Is there an entry? @@ -469,7 +313,11 @@ function updateMemberRefbackPercents ($id, $percents) { // Update entry SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_refs` SET `refback`='%s' WHERE `id`=%s AND `userid`=%s LIMIT 1", - array($percents, $id, getMemberId()), __FUNCTION__, __LINE__); + array( + $percents, + $id, + getMemberId() + ), __FUNCTION__, __LINE__); // Entry updated? if (SQL_HASZEROAFFECTED()) { @@ -479,7 +327,7 @@ function updateMemberRefbackPercents ($id, $percents) { } // END - if // Prepare email content - $dummy['percents'] = $percents; + $dummy['refback_percents'] = $percents; // Load member email template $mail = loadEmailTemplate('member_refback', $dummy, $dummy['refid']); diff --git a/inc/modules/member/what-refback.php b/inc/modules/member/what-refback.php index fb506673a8..4c098a910c 100644 --- a/inc/modules/member/what-refback.php +++ b/inc/modules/member/what-refback.php @@ -60,14 +60,14 @@ if ((!isExtensionActive('refback')) && (!isAdmin())) { // Was the form submitted? if ((isFormSent('edit')) && (isPostRequestParameterSet('id'))) { // Okay, has the user entered some values? - if (isPostRequestParameterSet('percents')) { + if (isPostRequestParameterSet('refback_percents')) { // Revert german commta for testing - $percents = convertCommaToDot(postRequestParameter('percents')); + $percents = convertCommaToDot(postRequestParameter('refback_percents')); // Validate percents if ((($percents >= getConfig('refback_min_perc')) || (round($percents) == 0)) && ($percents <= getConfig('refback_max_perc'))) { // Change ref-back for this direct id - $status = updateMemberRefbackPercents(postRequestParameter('id'), postRequestParameter('percents')); + $status = updateMemberRefbackPercents(postRequestParameter('id'), postRequestParameter('refback_percents')); // Check status if (isset($status['ok'])) { diff --git a/inc/referal-functions.php b/inc/referal-functions.php index 89142bf61c..57b00298b1 100644 --- a/inc/referal-functions.php +++ b/inc/referal-functions.php @@ -304,11 +304,9 @@ function updateReferalCounter ($userid) { rebuildCache('refsystem', 'refsystem'); } - // Handle refback here if extension is installed - // @TODO Rewrite this to a filter - if (isExtensionActive('refback')) { - updateRefbackTable($userid); - } // END - if + // Update the referal table + updateReferalTable($userid); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',ref=' . makeZeroToNull($ref) . ',level=' . makeZeroToNull($GLOBALS['cache_array']['ref_level'][$ref]) . ' - EXIT!'); } @@ -346,14 +344,14 @@ function getUserReferalPoints ($userid, $level) { $result = SQL_QUERY_ESC("SELECT ur.`id`, ur.`refid`, ud.`status`, ud.`last_online`, ud.`mails_confirmed`, ud.`emails_received` FROM - `{?_MYSQL_PREFIX?}_user_refs` AS ur + `{?_MYSQL_PREFIX?}_user_refs` AS `ur` LEFT JOIN - `{?_MYSQL_PREFIX?}_user_points` AS up + `{?_MYSQL_PREFIX?}_user_points` AS `up` ON - ur.refid=up.userid AND - (ur.level=0 OR ur.level IS NULL) + ur.`refid`=up.`userid` AND + (ur.`level`=0 OR ur.`level` IS NULL) LEFT JOIN - `{?_MYSQL_PREFIX?}_user_data` AS ud + `{?_MYSQL_PREFIX?}_user_data` AS `ud` ON ur.`refid`=ud.`userid` WHERE @@ -502,5 +500,162 @@ function isPaymentRecipientNotificationEnabled ($subject) { return $isEnabled; } +// Update "referal table" +function updateReferalTable ($userid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); + // Load all referals + loadReferalTable($userid); + + // Add missing level > 1 + addMissingReferalLevels($userid); + + // The last step is to flush all userid's entries to the database + flushReferalTableToDatabase($userid); + + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); +} + +// Loads all referals for given userid +function loadReferalTable ($userid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); + // Init array + $GLOBALS['referal_refid'][$userid] = array(); + + // Get all level entries from the refsystem table + $result = SQL_QUERY_ESC('SELECT `level` FROM `{?_MYSQL_PREFIX?}_refsystem` WHERE `userid`=%s ORDER BY `level` ASC', + array($userid), __FUNCTION__, __LINE__); + + // Do we have entries? + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL_NUMROWS()=' . SQL_NUMROWS($result)); + if (SQL_NUMROWS($result) > 0) { + // Then walk through all levels + while (list($level) = SQL_FETCHROW($result)) { + // Init array + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level); + $GLOBALS['referal_refid'][$userid][$level] = array(); + + // Level is = 1? + if ($level == 1) { + // Load all referals of this user + $result_refs = SQL_QUERY_ESC('SELECT `userid` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `refid`=%s ORDER BY `userid` ASC', + array($userid), __FUNCTION__, __LINE__); + + // Do we have entries? + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',SQL_NUMROWS()=' . SQL_NUMROWS($result_refs)); + if (SQL_NUMROWS($result_refs) > 0) { + // Then again walk through all + while (list($refid) = SQL_FETCHROW($result_refs)) { + // Add this refid + $GLOBALS['referal_refid'][$userid][$level][] = $refid; + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); + // Load the refid's array as well + loadReferalTable($refid); + } // END - while + } // END - if + + // Free result + SQL_FREERESULT($result_refs); + } // END - if + } // END - while + } // END - if + + // Free result + SQL_FREERESULT($result); + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); +} + +// Adds missing referal levels to the array +function addMissingReferalLevels ($userid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); + // If the array is gone, you have called this function without calling loadReferalTable() + if (!isset($GLOBALS['referal_refid'][$userid])) { + // Please fix your code + debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadReferalTable() before! userid=' . $userid); + } // END - if + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referal_refid'][$userid])); + + // Now walk through the array, first levels + foreach ($GLOBALS['referal_refid'][$userid] as $level=>$levelArray) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); + // Next are the users + foreach ($levelArray as $refid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); + // Does the refid have an array? + if (isset($GLOBALS['referal_refid'][$refid])) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',count()=' . count($GLOBALS['referal_refid'][$refid])); + // Okay, then walk through here, too + foreach ($GLOBALS['referal_refid'][$refid] as $refLevel=>$refArray) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',count()=' . count($refArray)); + // Also walk through this one + foreach ($refArray as $refRefid) { + // Calculate new level + $newLevel = $level + $refLevel; + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid . ',refLevel=' . $refLevel . ',refRefid=' . $refRefid . ',newLevel=' . $newLevel); + // Is the refRefid not in? + if ((!isset($GLOBALS['referal_refid'][$userid][$newLevel])) || (!in_array($refRefid, $GLOBALS['referal_refid'][$userid][$newLevel]))) { + // Then we must add this ref's refid to the userid's next level + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',newLevel=' . $newLevel . ',refRefid=' . $refRefid . ' - ADDED!'); + $GLOBALS['referal_refid'][$userid][$newLevel][] = $refRefid; + } // END - if + } // END - foreach + } // END - foreach + } // END - foreach + } // END - foreach + } // END - foreach + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); +} + +// Flush all entries for given userid to database +function flushReferalTableToDatabase ($userid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ENTERED!'); + // If the array is gone, you have called this function without calling loadReferalTable() + if (!isset($GLOBALS['referal_refid'][$userid])) { + // Please fix your code + debug_report_bug(__FUNCTION__, __LINE__, 'Called without calling loadReferalTable() before! userid=' . $userid); + } // END - if + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',count()=' . count($GLOBALS['referal_refid'][$userid])); + + // If no entries are there, skip this whole step + if (count($GLOBALS['referal_refid'][$userid]) == 0) { + // No entries found + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - ABORTING...'); + return; + } // END - if + + // Prepare SQL + $SQL = 'INSERT INTO `{?_MYSQL_PREFIX?}_user_refs` (`userid`,`level`,`refid`) VALUES '; + + // Now walk through the array, first levels + foreach ($GLOBALS['referal_refid'][$userid] as $level=>$levelArray) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',count()=' . count($levelArray)); + // Next are the users + foreach ($levelArray as $refid) { + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',level=' . $level . ',refid=' . $refid); + // Query the user_refs table + list($count) = SQL_FETCHROW(SQL_QUERY_ESC('SELECT COUNT(`id`) AS `count` FROM `{?_MYSQL_PREFIX?}_user_refs` WHERE `userid`=%s AND `level`=%s AND `refid`=%s LIMIT 1', + array( + $userid, + $level, + $refid + ), __FUNCTION__, __LINE__)); + + // Do we have no entry? + if ($count == 0) { + // Then add it to the SQL + $SQL .= '(' . $userid . ',' . $level . ',' . $refid . '),'; + } // END - if + } // END - foreach + } // END - foreach + + // Remove last comma from SQL + $SQL = substr($SQL, 0, -1); + + // And run it + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',SQL=' . $SQL); + SQL_QUERY($SQL, __FUNCTION__, __LINE__); + + //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ' - EXIT!'); +} + // [EOF] ?> diff --git a/templates/de/emails/admin/admin_refback.tpl b/templates/de/emails/admin/admin_refback.tpl index c0fa399c28..8e87a8646d 100644 --- a/templates/de/emails/admin/admin_refback.tpl +++ b/templates/de/emails/admin/admin_refback.tpl @@ -13,7 +13,7 @@ Email-Adresse: {%user,email=$userid%} ----------------------------------- Ref-Back: {%pipe,translateComma=$content[percents]%}% ----------------------------------- -Bereits erhalten: {%pipe,translateComma=$content[points]%} {?POINTS?} +Bereits erhalten: {%pipe,translateComma=$content[refback_points]%} {?POINTS?} ----------------------------------- Mit freundlichen Grüßen, diff --git a/templates/de/emails/member/member_refback.tpl b/templates/de/emails/member/member_refback.tpl index 821fcba93a..6f51b5a788 100644 --- a/templates/de/emails/member/member_refback.tpl +++ b/templates/de/emails/member/member_refback.tpl @@ -8,7 +8,7 @@ Ihr Mitglieder-Id ist: $userid Ref-Back: {%pipe,translateComma=$content[percents]%}% ------------------------------ -Sie haben bereits {%pipe,translateComma=$content[points]%} {?POINTS?} Ref-Back-Vergütung an das Mitglied abgegeben. +Sie haben bereits {%pipe,translateComma=$content[refback_points]%} {?POINTS?} Ref-Back-Vergütung an das Mitglied abgegeben. Mit freundlichen Grüßen, Ihr {?MAIN_TITLE?} Team diff --git a/templates/de/html/member/member_refback_edit.tpl b/templates/de/html/member/member_refback_edit.tpl index d214d1e087..89d3985917 100644 --- a/templates/de/html/member/member_refback_edit.tpl +++ b/templates/de/html/member/member_refback_edit.tpl @@ -8,7 +8,7 @@
{--MEMBER_REFBACK_PERCENTS--}: - % + %