Index: branches/5.2.x/units/affiliates/affiliates_event_handler.php =================================================================== diff -u -N -r14475 -r14594 --- branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 14475) +++ branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 14594) @@ -1,6 +1,6 @@ Array ('self' => true), - 'OnChangePaymentType' => Array ('self' => true), - 'OnBecomeAffiliate' => Array ('self' => true), ); + $this->permMapping = array_merge($this->permMapping, $permissions); } /** + * Checks user permission to execute given $event + * + * @param kEvent $event + * @return bool + * @access public + */ + public function CheckPermission(&$event) + { + if ( $event->Name == 'OnBecomeAffiliate' || $event->Name == 'OnChangePaymentType' ) { + return $this->Application->LoggedIn() && $this->Application->ConfigValue('Comm_RegisterAsAffiliate'); + } + + return parent::CheckPermission($event); + } + + /** + * Allows to get ID of affiliate record, associated with currently logged-in user + * + * @param kEvent $event + * @return int + */ + function getPassedID(&$event) + { + if ( $event->Special == 'user' ) { + $event->setEventParam('raise_warnings', 0); + + $sql = 'SELECT ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . ' + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE PortalUserId = ' . (int)$this->Application->RecallVar('user_id'); + $id = $this->Conn->GetOne($sql); + + if ( $id ) { + return $id; + } + } + + return parent::getPassedID($event); + } + + /** * Generate new affiliate code * * @param kEvent $event @@ -56,11 +96,11 @@ } $ret = strtoupper($ret); - $idfield = $this->Application->getUnitOption($event->Prefix, 'IDField'); + $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); $sql = 'SELECT %s FROM %s WHERE AffiliateCode = %s'; - $code_found = $this->Conn->GetOne( sprintf($sql, $idfield, $table, $this->Conn->qstr($ret) ) ); + $code_found = $this->Conn->GetOne( sprintf($sql, $id_field, $table, $this->Conn->qstr($ret) ) ); if($code_found) return $this->generateAffiliateCode($event); return $ret; @@ -70,14 +110,51 @@ * Creates new affiliate code when new affiliate is created * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeItemCreate(&$event) + protected function OnBeforeItemCreate(&$event) { - $object =& $event->getObject( Array('skip_autoload'=>true) ); - $object->SetDBField('AffiliateCode', $this->generateAffiliateCode($event) ); + parent::OnBeforeItemCreate($event); + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $object->SetDBField('AffiliateCode', $this->generateAffiliateCode($event)); + + if ( $object->getFormName() == 'registration' ) { + if ( $this->Application->LoggedIn() ) { + $object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id')); + } + + $object->SetDBField('AffiliatePlanId', $this->_getPrimaryAffiliatePlan()); + } } /** + * Ensures, that user can only update his affiliate record + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnBeforeItemUpdate(&$event) + { + parent::OnBeforeItemUpdate($event); + + if ( !$this->Application->isAdmin ) { + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $object->SetDBField('PortalUserId', $object->GetOriginalField('PortalUserId')); + + if ( $object->GetDBField('PortalUserId') != $this->Application->RecallVar('user_id') ) { + $object->SetError('PortalUserId', 'not_owner'); + } + } + } + + /** * Stores affiliate id using method from Config (session or cookie) if correct code is present in url * * @param kEvent $event @@ -208,156 +285,179 @@ } } - function OnCheckAffiliateAgreement(&$event) + /** + * [HOOK] Validates affiliate fields on user registration form + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnValidateAffiliate(&$event) { - $user_object =& $this->Application->recallObject('u', null, Array('skip_autoload' => true)); - - $items_info = $this->Application->GetVar('u'); - if($items_info) - { - list($id,$field_values) = each($items_info); - $user_object->SetFieldsFromHash($field_values); - $user_object->setID($id); + if ( $this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS ) { + return; } + + $object =& $event->getObject( Array('form_name' => 'registration', 'skip_autoload' => true) ); + /* @var $object kDBItem */ - $require_affiliate = ($this->Application->GetVar('RegisterAsAffiliate') == 'on'); + $field_values = $this->getSubmittedFields($event); + $object->SetFieldsFromHash($field_values); + $object->setID(0); - if($require_affiliate && !$this->Application->GetVar('AgreeToAffiliateTerms') ) - { - $this->Application->SetVar('MustAgreeToTerms', 1); - $event->MasterEvent->status = kEvent::erFATAL; - } + if ( !$object->Validate() ) { + $user =& $event->MasterEvent->getObject(); + /* @var $user kDBItem */ - if($require_affiliate && !$this->Application->GetVar('SSN') ) - { - $this->Application->SetVar('SSNRequiredError', 1); - $event->MasterEvent->status = kEvent::erFATAL; - } + $user->Validate(); - if( ($event->MasterEvent->status == kEvent::erFATAL) && $items_info ) $user_object->Validate(); + $event->MasterEvent->status = kEvent::erFAIL; + } } /** * [AFTER HOOK] to u:OnCreate * * @param kEvent $event */ - function OnRegisterAsAffiliate(&$event) + function OnRegisterAffiliate(&$event) { - if($this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS) - { + if ( $this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS ) { return; } - $object =& $event->getObject( Array('skip_autoload' => true) ); - $sql = 'SELECT AffiliatePlanId FROM '.$this->Application->getUnitOption('ap', 'TableName').' - WHERE IsPrimary = 1'; - $affiliate_plan = $this->Conn->GetOne($sql); - $object->SetDBField('PortalUserId', $this->Application->GetVar('u.current_id')); - $object->SetDBField('Status', 2); - $object->SetDBField('AffiliatePlanId', $affiliate_plan); - $object->SetDBField('SSN', $this->Application->GetVar('SSN')); - $object->SetDBField('Comments', $this->Application->GetVar('Comments')); - $object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId')); + $object =& $event->getObject(); + /* @var $object kDBItem */ - $object->Create(); + $user =& $event->MasterEvent->getObject(); + /* @var $user UsersItem */ - $email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTER', $this->Application->GetVar('u.current_id')); - $email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTER'); + $object->SetDBField('PortalUserId', $user->GetID()); + + if ( $object->Create() ) { + $this->Application->EmailEventUser('AFFILIATE.REGISTER', $user->GetID()); + $this->Application->EmailEventAdmin('AFFILIATE.REGISTER'); + } } - function OnBecomeAffiliate(&$event) + /** + * Returns primary affiliate plan + * + * @return int + * @access protected + */ + protected function _getPrimaryAffiliatePlan() { - if(!$this->Application->GetVar('AgreeToAffiliateTerms')) - { - $this->Application->SetVar('MustAgreeToTerms', 1); - $event->status = kEvent::erFATAL; - } - if(!$this->Application->GetVar('SSN')) - { - $this->Application->SetVar('SSNRequiredError', 1); - $event->status = kEvent::erFATAL; - } - if($event->status == kEvent::erFATAL) - { - return; - } - $object =& $event->getObject( Array('skip_autoload' => true) ); - $sql = 'SELECT AffiliatePlanId FROM '.$this->Application->getUnitOption('ap', 'TableName').' + $sql = 'SELECT AffiliatePlanId + FROM ' . $this->Application->getUnitOption('ap', 'TableName') . ' WHERE IsPrimary = 1'; - $affiliate_plan = $this->Conn->GetOne($sql); - $object->SetDBField('PortalUserId', $this->Application->RecallVar('user_id')); - $object->SetDBField('Status', 2); - $object->SetDBField('AffiliatePlanId', $affiliate_plan); - $object->SetDBField('SSN', $this->Application->GetVar('SSN')); - $object->SetDBField('Comments', $this->Application->GetVar('Comments')); - $object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId')); - $object->Create(); - if($next_template = $this->Application->GetVar('next_template')) - { - $event->redirect = $next_template; - } + return (int)$this->Conn->GetOne($sql); + } - $email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTER', $this->Application->RecallVar('user_id')); - $email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTER'); + /** + * Creates affiliate record for logged-in user + * + * @param kEvent $event + */ + function OnBecomeAffiliate(&$event) + { + $object =& $event->getObject( Array('form_name' => 'registration', 'skip_autoload' => true) ); + /* @var $object UsersItem */ + + $event->CallSubEvent('OnCreate'); + + if ( $event->status == kEvent::erSUCCESS ) { + $event->SetRedirectParam('opener', 's'); + + $next_template = $this->Application->GetVar('next_template'); + + if ( $next_template ) { + $event->redirect = $next_template; + } + } } - function OnChangePaymentType(&$event) + /** + * Change affiliate payment type of affiliate record associated with logged-in user + * + * @param kEvent $event + * @return void + * @access protected + */ + protected function OnChangePaymentType(&$event) { - $user_id = $this->Application->RecallVar('user_id'); + $event->CallSubEvent('OnUpdate'); - $object =& $event->getObject( Array('skip_autoload' => true) ); - $object->Load( Array('PortalUserId' => $user_id) ); - $object->SetDBField('Comments', $this->Application->GetVar('Comments')); - $object->SetDBField('PaymentTypeId', $this->Application->GetVar('PaymentTypeId')); - $object->Update(); + if ( $event->status == kEvent::erSUCCESS ) { + $object =& $event->getObject(); + /* @var $object kDBItem */ - $email_event_user =& $this->Application->EmailEventUser('AFFILIATE.PAYMENT.TYPE.CHANGED', $user_id); - $email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT.TYPE.CHANGED'); + $this->Application->EmailEventUser('AFFILIATE.PAYMENT.TYPE.CHANGED', $object->GetDBField('PortalUserId')); + $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT.TYPE.CHANGED'); - $event->redirect = $this->Application->GetVar('next_template'); - $event->status = kEvent::erSUCCESS; + $next_template = $this->Application->GetVar('next_template'); + + if ( $next_template ) { + $event->redirect = $this->Application->GetVar('next_template'); + } + + $event->SetRedirectParam('opener', 's'); + } } /** * If new payments made, then send email about that * * @param kEvent $event + * @return void + * @access protected */ - function OnBeforeDeleteFromLive(&$event) + protected function OnBeforeDeleteFromLive(&$event) { - $payment_object =& $this->Application->recallObject('apayments', 'apayments', Array('skip_autoload' => true) ); + parent::OnBeforeDeleteFromLive($event); + $payment_object =& $this->Application->recallObject('apayments', 'apayments', Array ('skip_autoload' => true)); + /* @var $payment_object kDBItem */ + $id = $event->getEventParam('id'); $ap_table = $this->Application->getUnitOption('apayments', 'TableName'); - $sql = 'SELECT AffiliatePaymentId FROM '.$ap_table.' WHERE AffiliateId = '.$id; + $sql = 'SELECT AffiliatePaymentId + FROM ' . $ap_table . ' + WHERE AffiliateId = ' . $id; $live_ids = $this->Conn->GetCol($sql); - $sql = 'SELECT AffiliatePaymentId FROM '.$payment_object->TableName.' WHERE AffiliateId = '.$id; + $sql = 'SELECT AffiliatePaymentId + FROM ' . $payment_object->TableName . ' + WHERE AffiliateId = ' . $id; $temp_ids = $this->Conn->GetCol($sql); $new_ids = array_diff($temp_ids, $live_ids); - foreach($new_ids as $payment_id) - { + + foreach ($new_ids as $payment_id) { $payment_object->Load($payment_id); - $email_event_user =& $this->Application->EmailEventUser('AFFILIATE.PAYMENT', $payment_object->GetDBField('PortalUserId')); - $email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT'); + $this->Application->EmailEventUser('AFFILIATE.PAYMENT', $payment_object->GetDBField('PortalUserId')); + $this->Application->EmailEventAdmin('AFFILIATE.PAYMENT'); } $object =& $event->getObject(); + /* @var $object kDBItem */ + $passed_id = $event->getEventParam('id'); - if( $object->GetID() != $passed_id ) - { + + if ( $object->GetID() != $passed_id ) { $object->Load($passed_id); } - $sql = 'SELECT Status FROM '.$this->Application->getUnitOption( $event->Prefix, 'TableName' ).' WHERE '.$object->IDField.' = '.$object->GetID(); + + $sql = 'SELECT Status + FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' + WHERE ' . $object->IDField . ' = ' . $object->GetID(); $old_status = $this->Conn->GetOne($sql); - if( $old_status == 2 && $object->GetDBField('Status') == 1 ) - { - $email_event_user =& $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId')); - $email_event_admin =& $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED'); + + if ( $old_status == 2 && $object->GetDBField('Status') == 1 ) { + $this->Application->EmailEventUser('AFFILIATE.REGISTRATION.APPROVED', $object->GetDBField('PortalUserId')); + $this->Application->EmailEventAdmin('AFFILIATE.REGISTRATION.APPROVED'); } }