public function processRequest($request)
 {
     $db = new WPAM_Data_DataAccess();
     $affiliateFields = $db->getAffiliateFieldRepository()->loadMultipleBy(array('enabled' => true), array('order' => 'asc'));
     if (isset($request['action']) && $request['action'] == 'submit') {
         $form_validated = false;
         $affiliateHelper = new WPAM_Util_AffiliateFormHelper();
         $vr = $affiliateHelper->validateForm(new WPAM_Validation_Validator(), $request, $affiliateFields);
         if ($vr->getIsValid()) {
             $form_validated = true;
         }
         $output = apply_filters('wpam_validate_registration_form_submission', '', $request);
         if (!empty($output)) {
             $form_validated = false;
         }
         if ($form_validated) {
             $model = $affiliateHelper->getNewAffiliate();
             $affiliateHelper->setModelFromForm($model, $affiliateFields, $request);
             //Fire the action hook
             do_action('wpam_front_end_registration_form_submitted', $model, $request);
             //Check if automatic affiliate approval option is enabled
             if (get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption) == 1) {
                 $userHandler = new WPAM_Util_UserHandler();
                 $userHandler->AutoapproveAffiliate($model);
                 return new WPAM_Pages_TemplateResponse('aff_app_submitted_auto_approved');
             }
             //Do the non auto approval process
             $db = new WPAM_Data_DataAccess();
             $id = $db->getAffiliateRepository()->insert($model);
             if ($id == 0) {
                 if (WPAM_DEBUG) {
                     echo '<pre>', var_export($model, true), '</pre>';
                 }
                 wp_die(__('Error submitting your details to the database. This is a bug, and your application was not submitted.', 'affiliates-manager'));
             }
             $mailer = new WPAM_Util_EmailHandler();
             //Notify admin that affiliate has registered
             $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
             $message = sprintf(__('New affiliate registration on your site %s:', 'affiliates-manager'), $blogname) . "\r\n\r\n";
             $message .= sprintf(__('Name: %s %s', 'affiliates-manager'), $request['_firstName'], $request['_lastName']) . "\r\n";
             $message .= sprintf(__('Email: %s', 'affiliates-manager'), $request['_email']) . "\r\n";
             $message .= sprintf(__('Company: %s', 'affiliates-manager'), $request['_companyName']) . "\r\n";
             $message .= sprintf(__('Website: %s', 'affiliates-manager'), $request['_websiteUrl']) . "\r\n\r\n";
             $message .= sprintf(__('View Application: %s', 'affiliates-manager'), admin_url('admin.php?page=wpam-affiliates&viewDetail=' . $id)) . "\r\n";
             $mailer->mailAffiliate(get_option('admin_email'), __('New Affiliate Registration', 'affiliates-manager'), $message);
             //Notify affiliate of their application
             $affsubj = sprintf(__('Affiliate application for %s', 'affiliates-manager'), $blogname);
             $affmessage = WPAM_MessageHelper::GetMessage('affiliate_application_submitted_email');
             $mailer->mailAffiliate($request['_email'], $affsubj, $affmessage);
             return new WPAM_Pages_TemplateResponse('affiliate_application_submitted');
         } else {
             return $this->getForm($affiliateFields, $request, $vr);
         }
     }
     //else
     return $this->getForm($affiliateFields);
 }
 public function declineApplication($affiliateId)
 {
     if (!wp_get_current_user()->has_cap(WPAM_PluginConfig::$AdminCap)) {
         throw new Exception(__('Access denied.', 'affiliates-manager'));
     }
     $affiliateId = (int) $affiliateId;
     $db = new WPAM_Data_DataAccess();
     $affiliate = $db->getAffiliateRepository()->load($affiliateId);
     if ($affiliate === null) {
         throw new Exception(__('Invalid affiliate', 'affiliates-manager'));
     }
     if ($affiliate->isPending() || $affiliate->isBlocked()) {
         $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
         if ($affiliate->isPending()) {
             $mailer = new WPAM_Util_EmailHandler();
             $mailer->mailAffiliate($affiliate->email, sprintf(__('Affiliate Application for %s', 'affiliates-manager'), $blogname), WPAM_MessageHelper::GetMessage('affiliate_application_declined_email'));
         }
         $affiliate->decline();
         $db->getAffiliateRepository()->update($affiliate);
         return new JsonResponse(JsonResponse::STATUS_OK);
     } else {
         throw new Exception(__('Invalid state transition.', 'affiliates-manager'));
     }
 }
 public function AutoapproveAffiliate($affiliate, $bountyType = '', $bountyAmount = '')
 {
     $sendEmail = true;
     $mailer = new WPAM_Util_EmailHandler();
     $db = new WPAM_Data_DataAccess();
     //Create Affiliate account in WP (1.1.2 if they don't have one)
     //and send them an email telling them they're approved
     $userLogin = sanitize_user($affiliate->email);
     $userEmail = apply_filters('user_registration_email', $affiliate->email);
     $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
     $message = sprintf(__('New affiliate registration for %s: has been approved!', 'wpam'), $blogname) . "\r\n\r\n";
     if (username_exists($userLogin)) {
         $user = new WP_User(get_userdatabylogin($userLogin)->ID);
         if ($user->has_cap(WPAM_PluginConfig::$AffiliateCap)) {
             throw new Exception(__('User already has an account and is already an affiliate', 'wpam'));
         } else {
             $user->add_cap(WPAM_PluginConfig::$AffiliateCap);
             $message .= __('Log into the site with your existing account and get started.', 'wpam') . "\r\n";
             $userId = $user->ID;
         }
     } elseif (email_exists($userEmail)) {
         $user = new WP_User(get_user_by('email', $userEmail)->ID);
         if ($db->getAffiliateRepository()->existsBy(array('userId' => $user->ID))) {
             throw new Exception(__('User already has an account and is already an affiliate', 'wpam'));
         } else {
             $user->add_cap(WPAM_PluginConfig::$AffiliateCap);
             $message .= __('Log into the site with your existing account and get started.', 'wpam') . "\r\n";
             $userId = $user->ID;
         }
     } else {
         //user not found by email address as username and no account with that email address exists
         //create new user using email address as username
         $userPass = wp_generate_password();
         $userId = wp_create_user($userLogin, $userPass, $userEmail);
         if (is_wp_error($userId)) {
             throw new Exception($userId->get_error_message());
         }
         //$mailer->mailNewAffiliate( $userId, $userPass );
         $mailer->mailNewApproveAffiliate($userId, $userPass);
         $sendEmail = false;
         $user = new WP_User($userId);
         $user->add_cap(WPAM_PluginConfig::$AffiliateCap);
     }
     $affiliate->activate();
     $affiliate->userId = $userId;
     if (isset($bountyType) && !empty($bountyType)) {
         $affiliate->bountyType = $bountyType;
     } else {
         $affiliate->bountyType = get_option(WPAM_PluginConfig::$AffBountyType);
     }
     if (isset($bountyAmount) && !empty($bountyAmount)) {
         $affiliate->bountyAmount = $bountyAmount;
     } else {
         $affiliate->bountyAmount = get_option(WPAM_PluginConfig::$AffBountyAmount);
     }
     $id = $db->getAffiliateRepository()->insert($affiliate);
     if ($id == 0) {
         if (WPAM_DEBUG) {
             echo '<pre>', var_export($model, true), '</pre>';
         }
         wp_die(__('Error submitting your details to the database. This is a bug, and your application was not submitted.', 'wpam'));
     }
     //Notify admin that affiliate has registered
     $admin_message = sprintf(__('New affiliate registration on your site %s:', 'wpam'), $blogname) . "\r\n\r\n";
     $admin_message .= sprintf(__('Name: %s %s', 'wpam'), $affiliate->firstName, $affiliate->lastName) . "\r\n";
     $admin_message .= sprintf(__('Email: %s', 'wpam'), $affiliate->email) . "\r\n";
     $admin_message .= sprintf(__('Company: %s', 'wpam'), $affiliate->companyName) . "\r\n";
     $admin_message .= sprintf(__('Website: %s', 'wpam'), $affiliate->websiteUrl) . "\r\n\r\n";
     $admin_message .= sprintf(__('View Application: %s', 'wpam'), admin_url('admin.php?page=wpam-affiliates&viewDetail=' . $id)) . "\r\n";
     $mailer->mailAffiliate(get_option('admin_email'), __('New Affiliate Registration', 'wpam'), $admin_message);
     //Send user email indicating they're approved
     if ($sendEmail) {
         $mailer->mailAffiliate($userEmail, sprintf(__('Affiliate Application for %s', 'wpam'), $blogname), $message);
     }
 }