Mod Title: Referrals System

Mod Version: 2.2

Mod Author: FunForum

Update BY: Daniel Gouveia

Mod Description:
This modification will add a "Referred by" option on registration page for people to select the user who referred them.
This modification will add a "Referred by" option onto the "Edit Profile" page for people to select the user who referred them (if they haven't already selected one).
This modification will add a 'block' into member profiles to show who referred them and who they referred.
Once a member is deleted, the people they referred become referral-less (They will be set to No One) to prevent dead links in member profiles.
One can give out registration links like: member.php?ref=USERNAME or member.php?refid=UID. 

Mod Copyright:  2011 The XMB Group. All rights reserved &&  2006 FunForum. All rights reserved.

Mod Compatibility: XMB 1.9.11 - Updated by Daniel Gouveia and miqrogroove (Robert Chapin).

Mod Install Note: Before adding this mod to your forum, you should back up all files related to this mod.

Mod License Note: This mod is released under the GPL v3 License. A copy is provided with this software.

Mod Author Note:
This modification is developed and released for use with XMB 1.9.11 which is provided by The XMB Group.

For Support Please Contact http://forums.xmbforum.com/
=======================================================================================================================================================
=======
Step 1:
=======

===================================
Go To Admin Panel -> Insert Raw SQL
===================================

ALTER TABLE $table_members ADD referredby smallint(6) DEFAULT '0' NOT NULL;

=======================================================================================================================================
=======
Step 2:
=======

================================
Edit File: header.php
================================

==========
Find Code:
==========

/* Set Up HTML Templates and Themes */

===============
Add Code Above:
===============

// Assert Referrals System Translation
if (!isset($lang['ref_o06'])) {
    require_once(ROOT.'include/translation.inc.php');
    setNewLangValue('ref_o01', 'No One');
    setNewLangValue('ref_o02', 'Referred by:');
    setNewLangValue('ref_o03', 'If you havent been referred by anyone, please select the \"No One\" option at the top of the list.');
    setNewLangValue('ref_o04', 'Referral');
    setNewLangValue('ref_o05', 'Referrals');
    setNewLangValue('ref_o06', 'Amount of Referrals:');
    loadLang($langfile);
}

=======================================================================================================================================
=======
Step 3:
=======

================================
Edit File: cp.php
================================

==========
Find Code:
==========
                $db->query("UPDATE ".X_PREFIX."whosonline SET username='xguest123' WHERE username='$dbname'");
				
===============
Add Code Below:
===============

                $db->query("UPDATE ".X_PREFIX."members SET referredby='-1' WHERE referredby='$delete'");
=======================================================================================================================================
=======
Step 4:
=======

================================
Edit File: member.php
================================

==========
Find Code:
==========

smcwcache();

eval('$css = "'.template('css').'";');

$action = postedVar('action', '', FALSE, FALSE, FALSE, 'g');
				
===============
Add Code Below:
===============

$refid = getInt('refid');
$ref = postedVar('ref', '', TRUE, FALSE, TRUE, 'g');
if($SETTINGS['coppa'] == 'on' && noSubmit('coppasubmit') && empty($action)) {
    $action = "coppa";
} elseif (empty($action)) {
    $action = "reg";
}
$ref_addon = '';
if (!empty($ref)) {
    $ref_addon = '<input type="hidden" name="ref" value="'.$ref.'" />';
} elseif ($refid != 0) {
    $ref_addon = '<input type="hidden" name="refid" value="'.$refid.'" />';
}

==========
Find Code:
==========

            if (onSubmit('coppasubmit')) {
                redirect($full_url.'member.php?action=reg', 0);
            } else {
                eval('$memberpage = "'.template('member_coppa').'";');
            }
		
===============
Replace With:
===============

            $coppa_url = ($refid > 0 ? $full_url.'member.php?refid='.$refid : (!empty($ref) ? $full_url.'member.php?ref='.recodeOut($ref) : $full_url.'member.php?action=reg'));
            if (onSubmit('coppasubmit')) {
                redirect($coppa_url, 0);
            } else {
                eval('$memberpage = "'.template('member_coppa').'";');
            }

==========
Find Code:
==========

            if ($SETTINGS['bbrules'] == 'on' && noSubmit('rulesubmit')) {
                $SETTINGS['bbrulestxt'] = nl2br($SETTINGS['bbrulestxt']);
                eval('$memberpage = "'.template('member_reg_rules').'";');

===============
Replace With:
===============

            if ($SETTINGS['bbrules'] == 'on' && noSubmit('rulesubmit')) {
                $SETTINGS['bbrulestxt'] = nl2br($SETTINGS['bbrulestxt']);
                $bbrules_url = ($refid > 0 ? $full_url.'member.php?refid='.$refid : (!empty($ref) ? $full_url.'member.php?ref='.recodeOut($ref) : $full_url.'member.php?action=reg'));
                eval('$memberpage = "'.template('member_reg_rules').'";');
				
==========
Find Code:
==========

                eval('$memberpage = "'.template('member_reg').'";');

===============
Add Code Above:
===============				

                if (empty($ref) && $refid == 0) {
                    $ref_addon = '<select name="refer">';
                    $ref_addon .= '<option value="-1">'.$lang['ref_o01'].'</option>';
                    $ref_addon .= '<option value="0" selected="selected"></option>';
                    $query = $db->query("SELECT uid, username FROM ".X_PREFIX."members ORDER BY username");
                    while($ref_mem = $db->fetch_array($query)) {
                        $ref_addon .= '<option value="'.$ref_mem['uid'].'">'.$ref_mem['username'].'</option>';
                    }
                    $ref_addon .= "</select>";
                } else {
                    if ($refid > 0) {
                        $query = $db->query("SELECT username FROM ".X_PREFIX."members WHERE uid='$refid'");
                        $ref = ($db->num_rows($query) == 1 ? $db->result($query, 0) : $lang['ref_o01']);
                    } elseif (!empty($ref)) {
                        $dbref = $db->escape_var($ref);
                        $query = $db->query("SELECT uid FROM ".X_PREFIX."members WHERE username='$dbref'");
                        $refid = ($db->num_rows($query) == 1 ? $db->result($query, 0) : -1);
                    } else {
                        $ref = $lang['ref_o01'];
                        $refid = -1;
                    }
                    $ref_addon = '<input type="hidden" name="refer" value="'.$refid.'" /><input type="text" readonly name="troep" value="'.$ref.'" />';
                }
				
==========
Find Code:
==========

            $password = md5($password);
				
===============
Add Code Below:
===============	

            $refer = formInt('refer');

==============================
Find Code On First Occurrence:
==============================

                $db->query("INSERT INTO ".X_PREFIX."members

=================================
Add before ) VALUES ( in same line:
=================================

, referredby

================================
Add to end of line BEFORE )"); :
================================

, '$refer'

===============================
Find Code On Second Occurrence:
===============================

                $db->query("INSERT INTO ".X_PREFIX."members

=================================
Add before ) VALUES ( in same line:
=================================

, referredby

================================
Add to end of line BEFORE )"); :
================================

, '$refer'

==========
Find Code:
==========

        eval('$memberpage = "'.template('member_profile').'";');

===============
Add Code Above:
===============	

        $referred_by = '';
        if ($memberinfo['referredby'] == '-1') {
            $referred_by = $lang['ref_o01'];
        } elseif ($memberinfo['referredby'] > '0') {
            $query = $db->query('SELECT username FROM '.X_PREFIX.'members WHERE uid='.$memberinfo['referredby']);
            $referred_by = $db->result($query, 0);
            $referredname = recodeOut($referred_by);
            $referred_by = '<a href="member.php?action=viewpro&member='.$referredname.'">'.$referred_by.'</a>';
        }
        $referred_count = 0;
        $referred_list = $comma = '';
        $query = $db->query('SELECT username FROM '.X_PREFIX.'members WHERE referredby='.$memberinfo['uid']);
        while($ref_mem = $db->fetch_array($query)) {
            $referrednamelist = recodeOut($ref_mem['username']);
            $referred_list .= $comma.'<a href="member.php?action=viewpro&member='.$referrednamelist.'">'.$ref_mem['username'].'</a>';
            $referred_count++;
            $comma = ', ';
        }
		
=======================================================================================================================================
=======
Step 5:
=======

================================
Edit File: memcp.php
================================

==========
Find Code:
==========

        eval('$mempage = "'.template('memcp_profile').'";');

===============
Add Code Above:
===============	

        // Referral Hack Begin
        $referral_box = '';
        if (intval($member['referredby']) <= 0) {
            $referral_box = '<tr><td colspan="2" class="category"><font color="'.$cattext.'"><strong>'.$lang['texteditpro'].' - '.$lang['ref_o04'].'</strong></font></td>';
            $referral_box .= '</tr>';
            $referral_box .= '<tr class="tablerow">';
            $referral_box .= '<td bgcolor="'.$altbg1.'">'.$lang['ref_o02'].'</td>';
            $referral_box .= '<td bgcolor="'.$altbg2.'">';
            $referral_box .= '<select name="refer">';
            $referral_box .= '<option value="-1">' . $lang['ref_o01'] . '</option>';
            $referral_box .= '<option value="0" selected="selected"> </option>';
            $query = $db->query('SELECT uid, username FROM '.X_PREFIX.'members WHERE uid !='.$member['uid'].' ORDER BY username');
            while($ref_mem = $db->fetch_array($query)) {
               $referral_box .= '<option value="'.$ref_mem['uid'].'">'.$ref_mem['username'].'</option>';
            }
            $referral_box .= '</select>';
            $referral_box .= '</td>';
            $referral_box .= '</tr>';
            $referral_box .= '<tr class="tablerow">';
            $referral_box .= '<td colspan="2" bgcolor="'.$altbg2.'">'.$lang['ref_o03'].'</td>';
            $referral_box .= '</tr>';
        }
        // Referral Hack End
		
==========
Find Code:
==========

        $location = postedVar('newlocation', 'javascript', TRUE, TRUE, TRUE);
		
===============
Add Code Above:
===============	

        // Referral Hack Begin
        $ref_sql = '';
        $refer = formInt('refer');
        if ( $refer > 0 ) {
            $ref_sql = ", referredby='$refer' ";
        }
        // Referral Hack End
		
==========
Find Code:
==========

    $db->query("UPDATE ".X_PREFIX."members SET $pwtxt email='$email',

==================================================
Find Code In-Line At End Of Above Query Statement:
==================================================

 WHERE username='$xmbuser'");

==================
Replace Code With:
==================

 $ref_sql WHERE username='$xmbuser'");
		
=======================================================================================================================================
=======
Step 6:
=======

========================================
Go to admin panel -> templates -> member_coppa
========================================

=========================
Find Code:
=========================

<form method="post" action="member.php?action=reg">

===============
Replace With:
===============

<form method="post" action="$coppa_url">
$ref_addon

=======================================================================================================================================
=======
Step 7:
=======

========================================
Go to admin panel -> templates -> member_reg_rules
========================================

=========================
Find Code:
=========================

<form method="post" action="member.php?action=reg">

===============
Replace With:
===============

<form method="post" action="$bbrules_url">
$ref_addon

=======================================================================================================================================
=======
Step 8:
=======

========================================
Go to admin panel -> templates -> member_reg
========================================

=========================
Find Code:
=========================

<tr class="ctrtablerow">
<td colspan="2" bgcolor="$altbg2"><input type="submit" class="submit" name="regsubmit" value="$lang[textregister]" /></td>
</tr>

===============
Add Code Above:
===============

<tr>
<td colspan="2" class="category"><font color="$cattext"><strong>$lang[textregister] - $lang[ref_o04]</strong></font></td>
</tr>
<tr class="tablerow">
<td bgcolor="$altbg1" width="22%">$lang[ref_o02]</td>
<td bgcolor="$altbg2">$ref_addon</td>
</tr>
<tr class="tablerow">
<td colspan="2" bgcolor="$altbg1"><font color="$cattext"><strong>$lang[ref_o03]</strong></font></td>
</tr>

=======================================================================================================================================
=======
Step 9:
=======

========================================
Go to admin panel -> templates -> memcp_profile
========================================

=========================
Find Code:
=========================

<tr>
<td class="ctrtablerow" bgcolor="$altbg2" colspan="2"><input type="submit" class="submit" name="editsubmit" value="$lang[texteditpro]" /></td>
</tr>

===============
Add Code Above:
===============

$referral_box

=======================================================================================================================================
=======
Step 10:
=======

========================================
Go to admin panel -> templates -> member_profile
========================================

=========================
Find Code:
=========================

<tr class="tablerow">
<td bgcolor="$altbg1" valign="top">$lang[textproflastpost]</td>
<td bgcolor="$altbg2">$lastpost</td>
</tr>
</table>
</td>
</tr>
</table>

===============
Add Code Below:
===============

<br />
<table cellspacing="0" cellpadding="0" border="0" width="$tablewidth" align="center">
<tr>
<td bgcolor="$bordercolor"><table border="0" cellspacing="{$THEME['borderwidth']}" cellpadding="$tablespace" width="100%">
<tr>
<td colspan="2" class="category"><font color="$cattext"><strong>$lang[ref_o05]</strong></font></td>
</tr>
<tr class="tablerow">
<td bgcolor="$altbg1" valign="top" width="22%">$lang[ref_o02]</td>
<td bgcolor="$altbg2">$referred_by</td>
</tr>
<tr class="tablerow">
<td bgcolor="$altbg1" valign="top">$lang[ref_o06]</td>
<td bgcolor="$altbg2">$referred_count</td>
</tr>
<tr class="tablerow">
<td bgcolor="$altbg1" valign="top">$lang[ref_o05]</td>
<td bgcolor="$altbg2">$referred_list</td>
</tr>
</table>
</td>
</tr>
</table>

=======================================================================================================================================
Enjoy!