public static function create_from_arid($user, $arid)
 {
     $referer = User::find_by_affiliate_code($arid);
     $referral = new AffiliateReferral();
     $referral->user_id = $user->id;
     $referral->referer_id = $referer->id;
     $result = $referral->save();
     return $result;
 }
 public function index($nickname = null)
 {
     $user = $this->load_user($nickname);
     $page = 1;
     if ($this->GetData('page')) {
         $page = $this->GetData('page');
     }
     $id = mysql_real_escape_string($user->id);
     $referrals = AffiliateReferral::paginate("referer.id = '{$id}'", '', $page, 25);
     $this->assign('user', $user);
     $this->assign('referrals', $referrals);
     $this->title = "{$user->nickname} :: Referrals";
     $this->render("affiliate_referral/index.tpl");
 }
 public function index()
 {
     $page = 1;
     if ($this->GetData('page')) {
         $page = $this->GetData('page');
     }
     $user = Site::CurrentUser();
     $id = mysql_real_escape_string($user->id);
     $referrals = AffiliateReferral::paginate("referer.id = '{$id}'", "affiliate_referrals.created_at DESC, affiliate_referrals.id DESC", $page, 25);
     $this->assign('user', $user);
     $this->assign('referrals', $referrals);
     $this->title = 'Referrals';
     $this->render('affiliate_referral/index.tpl');
 }
 public function referrals($reload = false)
 {
     if ($reload or !$this->referrals_cache) {
         $id = mysql_real_escape_string($this->id);
         $this->referrals_cache = AffiliateReferral::find_all("referer.id = '{$id}'");
     }
     return $this->referrals_cache;
 }
<?php

require "init.php";
$script = Script::find_by_code('loyalty_referrals');
$script->start();
ob_start();
$time = date("H:i:s");
echo "[{$time}] Referral Loyalty Points\r\n\r\n";
$page = AffiliateReferral::paginate("users.activated = 1 AND event_signups.paid = 1 AND event_tickets.participant = 1 AND affiliate_referrals.processed = 0 AND events.startdate <= NOW()", "users.created_at ASC", 1, 50);
$count = count($page->collection);
echo "    Processing {$count} / {$page->total} referrals\r\n";
foreach ($page->collection as $obj) {
    mysql_query('BEGIN');
    $loyalty = $obj->referer->award_loyalty(500, "Referral: {$obj->user->nickname}");
    $script->addlog("Awarded {$obj->achievement->points_value}pts to [{$obj->referer}] {$obj->referer->nickname} for referring [{$obj->user}] {$obj->user->nickname}");
    $obj->processed = true;
    $ua = $obj->save();
    if ($loyalty && $ua) {
        mysql_query('COMMIT');
    } else {
        echo "Error processing [{$obj}]\r\n";
        print_r($obj);
        mysql_query('ROLLBACK');
        $script->addlog("Error processing [{$obj}]", 'Critical');
    }
}
$script = Script::find_by_code('loyalty_referrals');
$output = ob_get_clean();
$script->finish($output);
 public function signup()
 {
     global $config;
     global $site;
     $user = new User();
     $user->requiresContactData = true;
     $arid = '';
     if (isset($_SESSION['affiliate_referral'])) {
         $arid = $_SESSION['affiliate_referral'];
     }
     $user->referral_type = "none";
     if ($this->post) {
         $user->nickname = $this->PostData('nickname');
         $user->email = $this->PostData('email');
         $user->password = $this->PostData('password');
         $user->password_confirmation = $this->PostData('password_confirmation');
         $user->firstname = $this->PostData('firstname');
         $user->surname = $this->PostData('surname');
         $user->clan = $this->PostData('clan');
         $user->address1 = $this->PostData('address1');
         $user->address2 = $this->PostData('address2');
         $user->towncity = $this->PostData('towncity');
         $user->county = $this->PostData('county');
         $user->country_id = $this->PostData('country_id');
         $user->postcode = $this->PostData('postcode');
         $user->phone = $this->PostData('phone');
         $user->set_dateofbirth($this->PostData('dateofbirth'));
         $user->terms = $this->PostData('terms');
         $user->allow_emails = $this->PostData('allow_emails');
         $user->referral_type = $this->PostData('referral_type');
         $arid = $this->PostData('arid');
         $valid = $user->validate();
         $validCaptcha = Recaptcha::validate($this->PostData('g-recaptcha-response'), Site::RemoteIP());
         if (!$validCaptcha) {
             $this->assign("failedcaptcha", true);
         } elseif ($valid) {
             $user->save();
             // This was a referral, make a note.
             if ($arid) {
                 AffiliateReferral::create_from_arid($user, $arid);
             }
             Email::send_user_signup($user);
             Redirect("signup/complete");
         }
     }
     $this->assign("affiliate_referral_id", $arid);
     $referral_types = array_merge(array("none" => "Please choose..."), $user->referral_types);
     $this->assign("referral_types", $referral_types);
     $terms = Content::find_by_permalink("signup-terms");
     $countries = Utils::FormOptions(Country::find_all("", "countries.name ASC"));
     $this->assign('countries', $countries);
     $this->assign("user", $user);
     $this->assign("site", $site);
     $this->assign("terms", $terms);
     $this->assign('arid', $arid);
     global $config;
     $this->assign('recaptcha', $config['recaptcha']['public']);
     $this->title = "Signup";
     $this->render("user/signup.tpl");
 }