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'] == 'saveInfo') {
         $validator = new WPAM_Validation_Validator();
         $validator->addValidator('ddBountyType', new WPAM_Validation_SetValidator(array('fixed', 'percent')));
         if ($request['ddBountyType'] === 'fixed') {
             $validator->addValidator('txtBountyAmount', new WPAM_Validation_MoneyValidator());
         } else {
             if ($request['ddBountyType'] === 'percent') {
                 $validator->addValidator('txtBountyAmount', new WPAM_Validation_NumberValidator());
             }
         }
         $affiliateHelper = new WPAM_Util_AffiliateFormHelper();
         $vr = $affiliateHelper->validateForm($validator, $request, $affiliateFields);
         if ($vr->getIsValid()) {
             $model = $affiliateHelper->getNewAffiliate();
             $affiliateHelper->setModelFromForm($model, $affiliateFields, $request);
             $affiliateHelper->setPaymentFromForm($model, $request);
             //send email etc.
             $userHandler = new WPAM_Util_UserHandler();
             if (get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption) == 1) {
                 $userHandler->AutoapproveAffiliate($model, $request['ddBountyType'], $request['txtBountyAmount']);
             } else {
                 $userHandler->approveAffiliate($model, $request['ddBountyType'], $request['txtBountyAmount'], false);
             }
             return new WPAM_Pages_TemplateResponse('admin/affiliate_new_success');
         } else {
             return $this->getForm($affiliateFields, $request, $vr);
         }
     }
     return $this->getForm($affiliateFields, $request);
 }
 public function processRequest($request)
 {
     $db = new WPAM_Data_DataAccess();
     if (isset($request['viewDetail']) && is_numeric($request['viewDetail'])) {
         $affiliateFields = $db->getAffiliateFieldRepository()->loadMultipleBy(array('enabled' => true), array('order' => 'asc'));
         $id = (int) $request['viewDetail'];
         $model = $db->getAffiliateRepository()->load($id);
         if ($model == null) {
             wp_die("Invalid affiliate ID.");
         }
         if (isset($request['action']) && $request['action'] == 'saveInfo') {
             $validator = new WPAM_Validation_Validator();
             //validate bounty type & amount if they're in the appropriate status
             if (!$model->isPending() && !$model->isBlocked() && !$model->isDeclined()) {
                 $validator->addValidator('ddBountyType', new WPAM_Validation_SetValidator(array('fixed', 'percent')));
                 if ($request['ddBountyType'] === 'fixed') {
                     $validator->addValidator('txtBountyAmount', new WPAM_Validation_MoneyValidator());
                 } else {
                     if ($request['ddBountyType'] === 'percent') {
                         $validator->addValidator('txtBountyAmount', new WPAM_Validation_NumberValidator());
                     }
                 }
                 $validator->addValidator('ddPaymentMethod', new WPAM_Validation_SetValidator(array('check', 'paypal')));
                 if ($request['ddPaymentMethod'] === 'paypal') {
                     $validator->addValidator('txtPaypalEmail', new WPAM_Validation_EmailValidator());
                 }
             }
             $affiliateHelper = new WPAM_Util_AffiliateFormHelper();
             $vr = $affiliateHelper->validateForm($validator, $request, $affiliateFields, TRUE);
             if ($vr->getIsValid()) {
                 $affiliateHelper->setModelFromForm($model, $affiliateFields, $request);
                 $affiliateHelper->setPaymentFromForm($model, $request);
                 $db->getAffiliateRepository()->update($model);
             } else {
                 return $this->getDetailForm($affiliateFields, $model, $request, $vr);
             }
         }
         return $this->getDetailForm($affiliateFields, $model, $request);
     } else {
         //Show all the affiliates list.
         //Lets include the affiliates_list.php file
         $response = new WPAM_Pages_TemplateResponse('admin/affiliates_list');
     }
     return $response;
 }
 protected function doContactInfo($request, $affiliate)
 {
     add_action('wp_footer', array($this, 'onFooter'));
     $db = new WPAM_Data_DataAccess();
     $affiliateFields = $db->getAffiliateFieldRepository()->loadMultipleBy(array('enabled' => true), array('order' => 'asc'));
     $response = new WPAM_Pages_TemplateResponse('affiliate_cp_contactinfo');
     $response->viewData['affiliateFields'] = $affiliateFields;
     $affiliateHelper = new WPAM_Util_AffiliateFormHelper();
     $response->viewData['paymentMethods'] = $affiliateHelper->getPaymentMethods();
     $response->viewData['paymentMethod'] = isset($request['ddPaymentMethod']) ? $request['ddPaymentMethod'] : $affiliate->paymentMethod;
     $response->viewData['paypalEmail'] = isset($request['txtPaypalEmail']) ? $request['txtPaypalEmail'] : $affiliate->paypalEmail;
     $user = wp_get_current_user();
     if (isset($request['action']) && $request['action'] == 'saveInfo') {
         $validator = new WPAM_Validation_Validator();
         $validator->addValidator('ddPaymentMethod', new WPAM_Validation_SetValidator(array('check', 'paypal')));
         if ($request['ddPaymentMethod'] === 'paypal') {
             $validator->addValidator('txtPaypalEmail', new WPAM_Validation_EmailValidator());
         }
         $vr = $affiliateHelper->validateForm($validator, $request, $affiliateFields, TRUE);
         if ($vr->getIsValid()) {
             //#79 hackery to do the "normal" WP email approval process
             require_once ABSPATH . 'wp-admin/includes/ms.php';
             $_POST['email'] = $request['_email'];
             $_POST['user_id'] = $user->ID;
             unset($request['_email']);
             global $errors;
             //*try* to save email
             send_confirmation_on_profile_email();
             if (!empty($errors->errors)) {
                 $vr->addError(new WPAM_Validation_ValidatorError('_email', $_POST['email'] . " " . $errors->get_error_message('user_email')));
                 $response->viewData['validationResult'] = $vr;
                 $response->viewData['affiliate'] = $affiliate;
                 //save for form validation in the footer
                 $this->response = $response;
                 return $response;
             }
             $affiliateHelper->setModelFromForm($affiliate, $affiliateFields, $request);
             $affiliateHelper->setPaymentFromForm($affiliate, $request);
             $db->getAffiliateRepository()->update($affiliate);
         } else {
             $response->viewData['validationResult'] = $vr;
         }
     }
     $new_email = get_option($user->ID . '_new_email');
     if ($new_email && $new_email != $user->user_email) {
         $response->viewData['newEmail'] = $new_email;
         $response->viewData['userId'] = $user->ID;
     }
     $response->viewData['affiliate'] = $affiliate;
     //save for form validation in the footer
     $this->response = $response;
     return $response;
 }
 public function processRequest($request)
 {
     $db = new WPAM_Data_DataAccess();
     if (isset($request['viewDetail']) && is_numeric($request['viewDetail'])) {
         $affiliateFields = $db->getAffiliateFieldRepository()->loadMultipleBy(array('enabled' => true), array('order' => 'asc'));
         $id = (int) $request['viewDetail'];
         $model = $db->getAffiliateRepository()->load($id);
         if ($model == null) {
             wp_die("Invalid affiliate ID.");
         }
         if (isset($request['action']) && $request['action'] == 'saveInfo') {
             $validator = new WPAM_Validation_Validator();
             //validate bounty type & amount if they're in the appropriate status
             if (!$model->isPending() && !$model->isBlocked() && !$model->isDeclined()) {
                 $validator->addValidator('ddBountyType', new WPAM_Validation_SetValidator(array('fixed', 'percent')));
                 if ($request['ddBountyType'] === 'fixed') {
                     $validator->addValidator('txtBountyAmount', new WPAM_Validation_MoneyValidator());
                 } else {
                     if ($request['ddBountyType'] === 'percent') {
                         $validator->addValidator('txtBountyAmount', new WPAM_Validation_NumberValidator());
                     }
                 }
                 $validator->addValidator('ddPaymentMethod', new WPAM_Validation_SetValidator(array('check', 'paypal')));
                 if ($request['ddPaymentMethod'] === 'paypal') {
                     $validator->addValidator('txtPaypalEmail', new WPAM_Validation_EmailValidator());
                 }
             }
             $affiliateHelper = new WPAM_Util_AffiliateFormHelper();
             $vr = $affiliateHelper->validateForm($validator, $request, $affiliateFields, TRUE);
             if ($vr->getIsValid()) {
                 $affiliateHelper->setModelFromForm($model, $affiliateFields, $request);
                 $affiliateHelper->setPaymentFromForm($model, $request);
                 $db->getAffiliateRepository()->update($model);
             } else {
                 return $this->getDetailForm($affiliateFields, $model, $request, $vr);
             }
         }
         return $this->getDetailForm($affiliateFields, $model, $request);
     } else {
         if (isset($_GET['orderby'])) {
             $current_orderby = $_GET['orderby'];
         } else {
             $current_orderby = 'affiliateId';
         }
         if (isset($_GET['order']) && 'desc' == $_GET['order']) {
             $current_order = 'desc';
         } else {
             $current_order = 'asc';
         }
         $orderBy = array($current_orderby => $current_order);
         $filterWhere = array();
         $minPayout = NULL;
         if (!isset($request['statusFilter'])) {
             $request['statusFilter'] = 'all_active';
         }
         if ($request['statusFilter'] !== 'all') {
             switch ($request['statusFilter']) {
                 case 'all_active':
                     $filterWhere['status'] = array('NOT IN', array('declined', 'blocked', 'inactive'));
                     break;
                 case 'all':
                     break;
                 case 'active':
                 case 'applied':
                 case 'approved':
                 case 'confirmed':
                 case 'declined':
                 case 'blocked':
                 case 'inactive':
                     $filterWhere['status'] = $request['statusFilter'];
                 default:
                     break;
             }
         }
         if (isset($request['overPayout']) && $request['overPayout'] === 'true') {
             $minPayout = get_option(WPAM_PluginConfig::$MinPayoutAmountOption);
         }
         $models = $db->getAffiliateRepository()->loadAffiliateSummary($filterWhere, $minPayout, $orderBy);
         $response = new WPAM_Pages_TemplateResponse('admin/affiliates_list');
         $response->viewData['minPayoutAmount'] = get_option(WPAM_PluginConfig::$MinPayoutAmountOption);
         $response->viewData['affiliates'] = $models;
         $response->viewData['request'] = $request;
         $response->viewData['statusFilters'] = array('all_active' => __('All Active', 'wpam'), 'all' => __('All (Including Closed)', 'wpam'), '---' => '---', 'active' => __('Status: Active', 'wpam'), 'applied' => __('Status: Applied', 'wpam'), 'approved' => __('Status: Approved', 'wpam'), 'confirmed' => __('Status: Confirmed', 'wpam'), 'declined' => __('Status: Declined', 'wpam'), 'blocked' => __('Status: Blocked', 'wpam'), 'inactive' => __('Status: Inactive', 'wpam'));
         $response->viewData['current_orderby'] = $current_orderby;
         $response->viewData['current_order'] = $current_order;
     }
     return $response;
 }