public static function GetMessage($name)
 {
     $msgRepo = self::$db->getMessageRepository();
     $msg = $msgRepo->loadBy(array('name' => $name));
     if ($msg === NULL) {
         throw new Exception(sprintf(__("Attempt to load invalid message of name '%s'", 'wpam'), $name));
     }
     return $msg->content;
 }
 protected function getSettingsForm($request = NULL, $message = NULL, $vr = NULL)
 {
     //add widget_form_error js to settings page
     add_action('admin_footer', array($this, 'onFooter'));
     $response = new WPAM_Pages_TemplateResponse('admin/settings/settings');
     $db = new WPAM_Data_DataAccess();
     $response->viewData['affiliateRegisterFields'] = $db->getAffiliateFieldRepository()->loadMultipleBy(array(), array('order' => 'asc'));
     $response->viewData['messages'] = $db->getMessageRepository()->loadAll();
     if ($request !== NULL) {
         $response->viewData['request']['txtTnc'] = $request['txtTnc'];
         $response->viewData['request']['txtMinimumPayout'] = $request['txtMinimumPayout'];
         $response->viewData['request']['txtCookieExpire'] = $request['txtCookieExpire'];
         $response->viewData['request']['txtEmailName'] = $request['txtEmailName'];
         $response->viewData['request']['txtEmailAddress'] = $request['txtEmailAddress'];
         $response->viewData['request']['autoaffapprove'] = isset($request['autoaffapprove']) ? 1 : 0;
         $response->viewData['request']['affBountyType'] = $request['affBountyType'];
         $response->viewData['request']['affBountyAmount'] = $request['affBountyAmount'];
         $response->viewData['request']['affCurrencySymbol'] = $request['affCurrencySymbol'];
         $response->viewData['request']['affCurrencyCode'] = $request['affCurrencyCode'];
         $response->viewData['request']['chkImpressions'] = isset($request['chkImpressions']) ? 1 : 0;
         $response->viewData['request']['chkPayoutMethodCheck'] = isset($request['chkPayoutMethodCheck']) ? 1 : 0;
         $response->viewData['request']['chkPayoutMethodPaypal'] = isset($request['chkPayoutMethodPaypal']) ? 1 : 0;
         $response->viewData['request']['chkEnablePaypalMassPay'] = isset($request['chkEnablePaypalMassPay']) ? 1 : 0;
         $response->viewData['request']['txtPaypalAPIUser'] = $request['txtPaypalAPIUser'];
         $response->viewData['request']['txtPaypalAPIPassword'] = $request['txtPaypalAPIPassword'];
         $response->viewData['request']['txtPaypalAPISignature'] = $request['txtPaypalAPISignature'];
         $response->viewData['request']['ddPaypalAPIEndPoint'] = $request['ddPaypalAPIEndPoint'];
         $response->viewData['request']['affLoginPage'] = $request['affLoginPage'];
         $response->viewData['validationResult'] = $vr;
     } else {
         $response->viewData['request']['txtTnc'] = get_option(WPAM_PluginConfig::$TNCOptionOption);
         $response->viewData['request']['txtMinimumPayout'] = get_option(WPAM_PluginConfig::$MinPayoutAmountOption);
         $response->viewData['request']['txtCookieExpire'] = get_option(WPAM_PluginConfig::$CookieExpireOption);
         $response->viewData['request']['txtEmailName'] = get_option(WPAM_PluginConfig::$EmailNameOption);
         $response->viewData['request']['txtEmailAddress'] = get_option(WPAM_PluginConfig::$EmailAddressOption);
         $response->viewData['request']['autoaffapprove'] = get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption);
         $response->viewData['request']['affBountyType'] = get_option(WPAM_PluginConfig::$AffBountyType);
         $response->viewData['request']['affBountyAmount'] = get_option(WPAM_PluginConfig::$AffBountyAmount);
         $response->viewData['request']['affCurrencySymbol'] = get_option(WPAM_PluginConfig::$AffCurrencySymbol);
         $response->viewData['request']['affCurrencyCode'] = get_option(WPAM_PluginConfig::$AffCurrencyCode);
         $response->viewData['request']['chkImpressions'] = get_option(WPAM_PluginConfig::$AffEnableImpressions);
         $response->viewData['request']['enable_debug'] = get_option(WPAM_PluginConfig::$AffEnableDebug);
         $response->viewData['request']['chkPayoutMethodCheck'] = get_option(WPAM_PluginConfig::$PayoutMethodCheckIsEnabledOption);
         $response->viewData['request']['chkPayoutMethodPaypal'] = get_option(WPAM_PluginConfig::$PayoutMethodPaypalIsEnabledOption);
         $response->viewData['request']['chkEnablePaypalMassPay'] = get_option(WPAM_PluginConfig::$PaypalMassPayEnabledOption);
         $response->viewData['request']['txtPaypalAPIUser'] = get_option(WPAM_PluginConfig::$PaypalAPIUserOption);
         $response->viewData['request']['txtPaypalAPIPassword'] = get_option(WPAM_PluginConfig::$PaypalAPIPasswordOption);
         $response->viewData['request']['txtPaypalAPISignature'] = get_option(WPAM_PluginConfig::$PaypalAPISignatureOption);
         $response->viewData['request']['ddPaypalAPIEndPoint'] = get_option(WPAM_PluginConfig::$PaypalAPIEndPointOption);
         $response->viewData['request']['affLoginPage'] = get_option(WPAM_PluginConfig::$AffLoginPageURL);
     }
     if ($message !== NULL) {
         $response->viewData['updateMessage'] = $message;
     }
     //save for form validation in the footer
     $this->response = $response;
     return $response;
 }
 public function doFreshInstallDbDefaultData()
 {
     //Only inserts the default data if the respective tables are empty
     $affiliates_fields_table = $this->db->prefix . WPAM_Data_DataAccess::TABLE_AFFILIATES_FIELDS;
     $messages_table = $this->db->prefix . WPAM_Data_DataAccess::TABLE_MESSAGES;
     $actions_table = $this->db->prefix . WPAM_Data_DataAccess::TABLE_ACTIONS;
     $results = $this->db->get_results("SELECT * FROM " . $affiliates_fields_table, OBJECT);
     if (is_null($results) || is_array($results) && empty($results)) {
         //No record in this table
         $this->db->query("\n            INSERT INTO `{$affiliates_fields_table}`\n            VALUES\n            (1,'base','First Name',50,'string',1,'firstName',1,0),\n            (2,'base','Last Name',50,'string',1,'lastName',1,1),\n            (3,'base','E-Mail Address',0,'email',1,'email',1,3),\n            (4,'base','Address Line 1',255,'string',1,'addressLine1',1,4),\n            (5,'base','Address Line 2',255,'string',0,'addressLine2',1,5),\n            (6,'base','City',128,'string',1,'addressCity',1,6),\n            (7,'base','State',0,'stateCode',1,'addressState',1,7),\n            (8,'base','Zip Code',0,'zipCode',1,'addressZipCode',1,8),\n            (9,'base','Country',0,'countryCode',1,'addressCountry',1,10),\n            (10,'base','Company Name',50,'string',0,'companyName',1,11),\n            (11,'base','Website URL',255,'string',0,'websiteUrl',1,12),\n            (12,'base','Phone Number',0,'phoneNumber',1,'phoneNumber',1,2)\n            ");
     }
     $results = $this->db->get_results("SELECT * FROM " . $actions_table);
     if (is_null($results) || is_array($results) && empty($results)) {
         //No record in this table
         $this->db->query("INSERT INTO `{$actions_table}`\n            (name, description)\n            VALUES\n            ('visit', 'New visitor'),\n            ('purchase', 'User confirmed purchase')");
     }
     $results = $this->db->get_results("SELECT * FROM " . $messages_table);
     if (is_null($results) || is_array($results) && empty($results)) {
         //No record in this table
         $this->db->query("\n            INSERT INTO `{$messages_table}`\n            VALUES\n            (1,'affiliate_application_approved','Displayed to user at logon if affiliate STATUS = APPROVED','Congratulations, the administrator has <strong>approved</strong> your application.  You have one more step to complete before you can begin publishing for this store and generating revenue! The store owner has specified the terms of your agreement, which you will need to review and agree to.<br /><br/>','web'),\n            (2,'affiliate_application_declined','Displayed to user at logon if affiliate STATUS = DECLINED','I\\'m sorry, your application was declined.','web'),\n            (3,'affiliate_application_pending','Displayed to user at logon if affiliate STATUS = PENDING','Your application is still being reviewed. Please check back later!','web'),\n            (4,'affiliate_application_submitted','Displayed to user after successfully submitting the affiliate registration form','Your application has been submitted.  The store owner will be contacting you soon.<br />\r\n<br />\r\nThank you!<br />\r\n','web'),\n            (5,'affiliate_application_submitted_email','Body of e-mail sent to the affiliate immediately after submitting their application.','Your application will be reviewed and you will be hearing from us soon!','email')\n            ");
         $db = new WPAM_Data_DataAccess();
         $msgRepo = $db->getMessageRepository();
         $msg = new WPAM_Data_Models_MessageModel();
         $msg->content = "I'm sorry, your application was declined.";
         $msg->name = 'affiliate_application_declined_email';
         $msg->type = 'email';
         $msg->use = 'Body of e-mail sent to the affiliate immediately following their application being declined.';
         $msgRepo->insert($msg);
         $db = new WPAM_Data_DataAccess();
         $msgRepo = $db->getMessageRepository();
         $msg = new WPAM_Data_Models_MessageModel();
         $msg->content = "Thank you. Your registration is now complete. You can log into the affiliate area and begin promoting.";
         $msg->name = 'aff_app_submitted_auto_approved';
         $msg->type = 'web';
         $msg->use = 'Displayed to a newly registered affiliate if automatic affiliate approval option is enabled.';
         $msgRepo->insert($msg);
     }
     //Add the default message insert code below (it will only add it if it doesn't exist already)
     $db = new WPAM_Data_DataAccess();
     $msgRepo = $db->getMessageRepository();
     $message = $msgRepo->loadBy(array('name' => 'affiliate_application_approved_email'));
     if ($message === NULL) {
         $msg = new WPAM_Data_Models_MessageModel();
         $msg->content = "Your affiliate account for {blogname} has been approved!. \n\nUsername: {affusername} \nPassword: {affpassword} \nLogin URL: {affloginurl} \n\nPlease log into your account to get referral code.";
         $msg->name = 'affiliate_application_approved_email';
         $msg->type = 'email';
         $msg->use = 'Body of e-mail sent to a newly registered affiliate immediately following their application being approved.';
         $msgRepo->insert($msg);
     }
     //Add other options below
     //add a new creative (default)
     $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
     $db = new WPAM_Data_DataAccess();
     $creativesRepo = $db->getCreativesRepository();
     $create_new_creative = false;
     if (empty($default_creative_id)) {
         $create_new_creative = true;
     } else {
         if (!$creativesRepo->existsBy(array('creativeId' => $default_creative_id, 'status' => 'active'))) {
             $create_new_creative = true;
         }
     }
     if ($create_new_creative) {
         $model = new WPAM_Data_Models_CreativeModel();
         $model->dateCreated = time();
         $model->status = 'active';
         $model->type === 'text';
         $model->linkText = 'default affiliate link';
         $model->altText = '';
         $model->slug = site_url('/');
         $model->name = 'default creative';
         $id = $creativesRepo->insert($model);
         update_option(WPAM_PluginConfig::$DefaultCreativeId, $id);
         //Save the ID of the deafult creative
     }
 }