public function afterSaveUsersModelsUsers($event) { $identity = $event->getArgument('model'); // if the active status of the user record from the DB (grabbed in beforeSave above) // differs from the active status of the $user, and $user->active = true // grant the affiliate a referral credit if (!empty($this->db_user)) { if ($affiliate_id = $this->db_user->{'affiliates.validation_triggers_referral'}) { // Make the user into a referral for affiliate_id // \Dsc\System::addMessage('Making you into a referral for an Affiliate ID'); try { $referral = \Affiliates\Models\Referrals::isUser($identity->id); if (empty($referral->id)) { // is this a valid affiliate? $affiliate = (new \Users\Models\Users())->load(array('_id' => new \MongoId((string) $affiliate_id))); if (!empty($affiliate->id)) { // make them into a referral $referral = new \Affiliates\Models\Referrals(); $referral->bind(array('referral_user_id' => $identity->id, 'referral_name' => $identity->fullName(), 'referral_email' => $identity->email, 'referral_fingerprints' => (array) $identity->{'affiliates.fingerprints'}, 'affiliate_fingerprints' => (array) $affiliate->{'affiliates.fingerprints'}, 'affiliate_id' => $affiliate_id))->save(); /* \Affiliates\Models\Referrals::createCommission($referral->id); */ \Dsc\Queue::task('\\Affiliates\\Models\\Referrals::createCommission', array('id' => $referral->id), array('title' => 'Verify and create commission for referral: ' . $referral->referral_email)); } } // referral created, clear it from the user $identity->{'affiliates.validation_triggers_referral'} = null; $identity->{'affiliates.validation_triggers_referral_invite'} = null; $identity = $identity->store(); } catch (\Exception $e) { // Log the failure in the system logger $identity->log('Could not create referral for affiliate ' . $affiliate_id . ' (' . $affiliate->email . ') for referral of user ' . $identity->id . ' (' . $identity->email . ') because of error ' . $e->getMessage()); $identity->{'affiliates.validation_triggers_referral'} = null; $identity->{'affiliates.validation_triggers_referral_invite'} = null; $identity = $identity->store(); } } elseif ($invite_id = $this->db_user->{'affiliates.validation_triggers_referral_invite'}) { try { if ($invite = \Affiliates\Models\Invites::idValid($invite_id)) { $affiliate = (new \Users\Models\Users())->load(array('_id' => new \MongoId((string) $invite->affiliate_id))); if (!empty($affiliate->id)) { // make them into a referral $referral = new \Affiliates\Models\Referrals(); $referral->bind(array('referral_user_id' => $identity->id, 'referral_name' => $identity->fullName(), 'referral_email' => $invite->recipient_email, 'referral_fingerprints' => (array) $identity->{'affiliates.fingerprints'}, 'affiliate_fingerprints' => (array) $affiliate->{'affiliates.fingerprints'}, 'affiliate_id' => $invite->affiliate_id, 'affiliate_email' => $affiliate->email, 'invite_id' => $invite_id))->save(); /* \Affiliates\Models\Referrals::createCommission($referral->id); */ \Dsc\Queue::task('\\Affiliates\\Models\\Referrals::createCommission', array('id' => $referral->id), array('title' => 'Verify and create commission for referral: ' . $referral->referral_email)); } // referral created, clear it from the user $identity->{'affiliates.validation_triggers_referral'} = null; $identity->{'affiliates.validation_triggers_referral_invite'} = null; $identity = $identity->store(); } } catch (\Exception $e) { // Log the failure in the system logger $identity->log('Could not create referral for invite ' . $invite_id . ' for referral of user ' . $identity->id . ' (' . $identity->email . ') because of error ' . $e->getMessage()); $identity->{'affiliates.validation_triggers_referral'} = null; $identity->{'affiliates.validation_triggers_referral_invite'} = null; $identity = $identity->store(); } } } }
/** * This handles the affiliate tracking for an invite_id, * placing a cookie when necessary, * otherwise putting the referral in the database * */ public static function handleInviteId() { $app = \Base::instance(); $identity = \Dsc\System::instance()->get('auth')->getIdentity(); $request_invite_id = \Dsc\System::instance()->get('input')->get('invite_id'); $cookie_invite_id = \Dsc\Cookie::get('invite_id'); // is there an invite ID in the request? if (!empty($request_invite_id)) { // \Dsc\System::addMessage('Tracking Invite ID in the request: ' . $request_invite_id); // If the user is not logged in, set a cookie. if (empty($identity->id)) { // Validate the $request_invite_id if (\Affiliates\Models\Invites::idValid($request_invite_id)) { \Dsc\Cookie::set('invite_id', $request_invite_id, 2592000 / 60); // == 30 days == (86400*30) return true; } } elseif (static::isUser($identity->id)) { \Dsc\Cookie::forget('invite_id'); return false; } else { \Dsc\Cookie::forget('invite_id'); return false; } } elseif (empty($identity->id) && !empty($cookie_invite_id)) { // Extend the life of the cookie // \Dsc\System::addMessage('Extending the life of the cookie of the Invite ID'); \Dsc\Cookie::set('invite_id', $cookie_invite_id, 2592000 / 60); // == 30 days == (86400*30) return true; } elseif (!empty($identity->id) && !empty($cookie_invite_id)) { \Dsc\Cookie::forget('invite_id'); return false; } \Dsc\Cookie::forget('invite_id'); return true; }