]> git.mxchange.org Git - mailer.git/blobdiff - inc/mysql-manager.php
Referal system refactured (and some parts fixed), wrapper function introduced:
[mailer.git] / inc / mysql-manager.php
index 177da67eb57a0f78f35cc46720a4d8c639d89caf..9ffbb0297cb7c67fdbc5d356251cdc2ab18c7919 100644 (file)
@@ -1035,31 +1035,39 @@ function initReferalSystem () {
  *
  * Dynamic referal and points system, can also send mails!
  *
- * subject     = Subject line, write in lower-case letters and underscore is allowed
- * userid      = Referal id wich should receive...
- * points      = ... xxx points
- * sendNotify  = shall I send the referal an email or not?
- * refid       = inc/modules/guest/what-confirm.php need this
- * locked      = Shall I pay it to normal (false) or locked (true) points ammount?
- * add_mode    = Add points only to $userid or also refs? (WARNING! Changing 'REFERAL' to 'DIRECT'
- *               for default value will cause no referal will get points ever!!!)
+ * subject       = Subject line, write in lower-case letters and underscore is allowed
+ * userid        = Referal id wich should receive...
+ * points        = ... xxx points
+ * refid         = inc/modules/guest/what-confirm.php need this
+ * locked        = Shall I pay it to normal (false) or locked (true) points ammount?
+ * paymentMethod = Deprecated parameter, the payment method
  */
-function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify = false, $refid = '0', $addMode = 'REFERAL') {
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',addMode=' . $addMode . ' - ENTERED!');
+function addPointsThroughReferalSystem ($subject, $userid, $points, $refid = '0', $paymentMethod = 'INVALID') {
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - ENTERED!');
        // By default nothing has been added
        $added = false;
 
        // Convert mode to upper-case
-       $addMode = strtoupper($addMode);
+       $paymentMethod = strtoupper($paymentMethod);
+
+       // Is it not invalid?
+       if ($paymentMethod != 'INVALID') {
+               // We are not using this anymore
+               debug_report_bug(__FUNCTION__, __LINE__, 'subject=' . $subject . ',paymentMethod=' . $paymentMethod . ' - deprecated parameter used.');
+       } // END - if
+
+       // Determine payment method and notification
+       $paymentMethod = getPaymentMethodFromSubject($subject);
+       $sendNotify    = isPaymentRecipientNotificationEnabled($subject);
 
        // When $userid = '0' add points to jackpot
-       if (($userid == '0') && ($addMode == 'DIRECT') && (isExtensionActive('jackpot'))) {
+       if (($userid == '0') && ($paymentMethod == 'DIRECT') && (isExtensionActive('jackpot'))) {
                // Add points to jackpot only in DIRECT mode
                return addPointsToJackpot($points);
        } // END - if
 
        // Check user account
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points);
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',points=' . $points);
        if (fetchUserData($userid)) {
                // Determine wether the user has some mails to click before he/she gets the points
                $locked = ifUserPointsLocked($userid);
@@ -1072,7 +1080,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
 
                // Get percents
                $per = getReferalLevelPercents($GLOBALS['ref_level']);
-               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$addMode);
+               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',depth='.$GLOBALS['ref_level'].',per='.$per.',mode='.$paymentMethod);
 
                // Some percents found?
                if ($per > 0) {
@@ -1081,7 +1089,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                        /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',points='.$points.',per='.$per.',depth='.$GLOBALS['ref_level'].',ref_points='.$ref_points);
 
                        // Pay refback here if level > 0 and in ref-mode
-                       if ((isExtensionActive('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($addMode == 'REFERAL') && (isset($GLOBALS['cache_array']['add_userid'][$userid]))) {
+                       if ((isExtensionActive('refback')) && ($GLOBALS['ref_level'] > 0) && ($per < 100) && ($paymentMethod == 'REFERAL') && (isset($GLOBALS['cache_array']['add_userid'][$userid]))) {
                                /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - BEFORE!');
                                $ref_points = addRefbackPoints($GLOBALS['cache_array']['add_userid'][$userid], $userid, $points, $ref_points);
                                /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid='.$userid.',data='.$GLOBALS['cache_array']['add_userid'][$userid].',ref_points='.$ref_points.',depth='.$GLOBALS['ref_level'].' - AFTER!');
@@ -1108,7 +1116,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                                                bigintval($GLOBALS['ref_level'])
                                        ), __FUNCTION__, __LINE__);
                        }
-                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
+                       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'pointsColumn='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - UPDATE! ('.SQL_AFFECTEDROWS().')');
 
                        // No entry updated?
                        if (SQL_HASZEROAFFECTED()) {
@@ -1120,7 +1128,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                                                makeZeroToNull($GLOBALS['ref_level']),
                                                $ref_points
                                        ), __FUNCTION__, __LINE__);
-                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$addMode.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
+                               /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'data='.$pointsColumn.',ref_points='.$ref_points.',userid='.$userid.',depth='.$GLOBALS['ref_level'].',mode='.$paymentMethod.' - INSERTED! ('.SQL_AFFECTEDROWS().')');
                        } // END - if
 
                        // Check affected rows
@@ -1138,7 +1146,7 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                                'refid'      => $refid,
                                'locked'     => $locked,
                                'mode'       => 'add',
-                               'add_mode'   => $addMode,
+                               'add_mode'   => $paymentMethod,
                                'added'      => $added
                        );
 
@@ -1163,11 +1171,12 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
 
                                // Send email
                                sendEmail($userid, '{--THANX_REFERAL_ONE_SUBJECT--}', $message);
-                       } elseif (($sendNotify === true) && (!isValidUserId(getUserData('refid'))) && ($locked === false) && ($addMode == 'DIRECT')) {
+                       } elseif (($sendNotify === true) && (!isValidUserId(getUserData('refid'))) && ($locked === false) && ($paymentMethod == 'DIRECT')) {
                                // Prepare content
                                $content = array(
-                                       'reason' => '{--REASON_DIRECT_PAYMENT--}',
-                                       'points' => $ref_points
+                                       'reason'  => '{--REASON_DIRECT_PAYMENT--}',
+                                       'subject' => $subject,
+                                       'points'  => $ref_points
                                );
 
                                // Load message
@@ -1186,15 +1195,15 @@ function addPointsThroughReferalSystem ($subject, $userid, $points, $sendNotify
                        /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ' Referal level increased, ref_level=' . $GLOBALS['ref_level']);
 
                        // Maybe there's another ref?
-                       if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($addMode == 'REFERAL')) {
+                       if ((isValidUserId(getUserData('refid'))) && ($points > 0) && (getUserData('refid') != $userid) && ($paymentMethod == 'REFERAL')) {
                                // Then let's credit him here...
                                /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'userid=' . $userid . ',refid=' . getUserData('refid') . ',points=' . $points . ',ref_points=' . $ref_points . ' - ADVANCE!');
-                               $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, $sendNotify, getUserData('refid')));
+                               $added = ($added && addPointsThroughReferalSystem(sprintf("%s_ref:%s", $subject, $GLOBALS['ref_level']), getUserData('refid'), $points, getUserData('refid')));
                        } // END - if
                } // END - if
        } // END - if
 
-       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',addMode=' . $addMode . ' - EXIT!');
+       /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'subject=' . $subject . ',userid=' . $userid . ',points=' . $points . ',sendNotify=' . intval($sendNotify) . ',refid=' . $refid . ',paymentMethod=' . $paymentMethod . ' - EXIT!');
        return $added;
 }
 
@@ -2051,51 +2060,57 @@ function updateLastActivity($userid) {
 }
 
 // Get points data for given extension's name
-function getPointsDataArrayFromExtensionName ($ext_name) {
+function getPointsDataArrayFromSubject ($subject) {
+       // Extension sql_patches must be up-to-date
+       if (isExtensionInstalledAndOlder('sql_patches', '0.8.2')) {
+               // Please update ext-sql_patches
+               debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.2 to continue. subject=' . $subject);
+       } // END - if
+
+       // Remove any double-dot from it
+       $subjectArray = explode(':', $subject);
+       $subject = $subjectArray[0];
+       unset($subjectArray);
+
        // If we have cache, shortcut it here
-       if (isset($GLOBALS['cache_array']['points_data'][$ext_name])) {
+       if (isset($GLOBALS['cache_array']['points_data'][$subject])) {
                // Return it
-               return $GLOBALS['cache_array']['points_data'][$ext_name];
+               return $GLOBALS['cache_array']['points_data'][$subject];
        } // END - if
 
        // Now checkout the entry in database table
-       $result = SQL_QUERY_ESC("SELECT `id`, `ext_name`, `column_name`, `locked_mode`, `payment_method` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `ext_name`='%s' LIMIT 1",
-               array($ext_name), __FUNCTION__, __LINE__);
+       $result = SQL_QUERY_ESC("SELECT `id`, `subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient` FROM `{?_MYSQL_PREFIX?}_points_data` WHERE `subject`='%s' LIMIT 1",
+               array($subject), __FUNCTION__, __LINE__);
 
        // Do we have an entry?
        if (SQL_NUMROWS($result) == 1) {
                // Then load it
                $pointsData = SQL_FETCHARRAY($result);
 
-               // Add all remaining entries
+               // Add all entries to our cache array
                foreach ($pointsData as $key=>$value) {
-                       $GLOBALS['cache_array']['points_data'][$ext_name][$key] = $value;
+                       $GLOBALS['cache_array']['points_data'][$subject][$key] = $value;
                } // END - foreach
        } else {
-               /*
-                * Having no entry is not bad but it means that all points will go to
-                * the general account which the user can let payout.
-                */
-               logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - No entry found, switching to general points account.');
+               // Register this automatically
+               SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_points_data` (`subject`, `column_name`, `locked_mode`, `payment_method`, `notify_recipient`) VALUES ('%s','points','LOCKED','REFERAL','N')",
+                       array($subject), __FUNCTION__, __LINE__);
+
+               // Re-request it
+               return getPointsDataArrayFromSubject($subject);
        }
 
        // Free result
        SQL_FREERESULT($result);
 
        // Return it
-       return $GLOBALS['cache_array']['points_data'][$ext_name];
+       return $GLOBALS['cache_array']['points_data'][$subject];
 }
 
-// Determines the right points column name for given extension and 'locked'
-function getPointsColumnNameFromExtensionNameLocked ($ext_name, $isLocked) {
-       // Extension sql_patches must be up-to-date
-       if (isExtensionInstalledAndOlder('sql_patches', '0.8.0')) {
-               // Please update ext-sql_patches
-               debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.0 to continue. ext_name=' . $ext_name . ',isLocked=' . intval($isLocked));
-       } // END - if
-
+// Determines the right points column name for given subject and 'locked'
+function getPointsColumnNameFromSubjectLocked ($subject, $isLocked) {
        // Get the points_data entry
-       $pointsData = getPointsDataArrayFromExtensionName($ext_name);
+       $pointsData = getPointsDataArrayFromSubject($subject);
 
        // Regular points by default
        $columnName = $pointsData['column_name'];
@@ -2111,15 +2126,9 @@ function getPointsColumnNameFromExtensionNameLocked ($ext_name, $isLocked) {
 }
 
 // Determines the payment method for given extension and 'locked'
-function getPaymentMethodFromExtensionName ($ext_name) {
-       // Extension sql_patches must be up-to-date
-       if (isExtensionInstalledAndOlder('sql_patches', '0.8.0')) {
-               // Please update ext-sql_patches
-               debug_report_bug(__FUNCTION__, __LINE__, 'sql_patches is out-dated. Please update to at least 0.8.0 to continue. ext_name=' . $ext_name . ',isLocked=' . intval($isLocked));
-       } // END - if
-
+function getPaymentMethodFromSubject ($subject) {
        // Get the points_data entry
-       $pointsData = getPointsDataArrayFromExtensionName($ext_name);
+       $pointsData = getPointsDataArrayFromSubject($subject);
 
        // Regular points by default
        $paymentMethod = $pointsData['payment_method'];
@@ -2128,5 +2137,17 @@ function getPaymentMethodFromExtensionName ($ext_name) {
        return $paymentMethod;
 }
 
+// Checks wether notification of points recipient is enabled
+function isPaymentRecipientNotificationEnabled ($subject) {
+       // Get the points_data entry
+       $pointsData = getPointsDataArrayFromSubject($subject);
+
+       // Is it enabled?
+       $isEnabled = ($pointsData['notify_recipient'] == 'Y');
+
+       // Return the result
+       return $isEnabled;
+}
+
 // [EOF]
 ?>