Exemplo n.º 1
0
 /**
  * Execute a command and return a response. Does not render
  * @param IRequest $Request
  * @throws RequestException
  * @throws \CPath\Request\Validation\Exceptions\ValidationException
  * @throws \Exception
  * @return IResponse the execution response
  */
 function execute(IRequest $Request)
 {
     $SessionRequest = $Request;
     if (!$SessionRequest instanceof ISessionRequest) {
         throw new \Exception("Session required");
     }
     $AccountEntry = AccountEntry::get($this->id);
     $Account = $AccountEntry->getAccount();
     $AffiliateQuery = AccountAffiliationEntry::queryAccountAffiliates($Account->getID());
     $AffiliateListTable = new HTMLPDOQueryTable($AffiliateQuery);
     $AffiliateListTable->addColumn('affiliate');
     $AffiliateListTable->addColumn('type');
     $Form = new HTMLForm(self::FORM_METHOD, $Request->getPath(), self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLHeaderScript(__DIR__ . '/assets/account.js'), new HTMLHeaderStyleSheet(__DIR__ . '/assets/account.css'), new HTMLElement('fieldset', 'fieldset-manage inline', new HTMLElement('legend', 'legend-manage', self::TITLE), new HTMLInputField(self::PARAM_ID, $this->id, 'hidden'), new HTMLInputField(self::PARAM_ACCOUNT_TYPE, $Account->getTypeName(), 'hidden'), new HTMLElement('label', null, "Status<br/>", $SelectStatus = new HTMLSelectField(self::PARAM_ACCOUNT_STATUS, AccountEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", $Account->getFieldSet($Request), "<br/><br/>", new HTMLButton(self::PARAM_SUBMIT, 'Update', 'update'), new HTMLButton(self::PARAM_SUBMIT, 'Delete', 'delete')), new HTMLElement('fieldset', 'fieldset-affiliates inline', new HTMLElement('legend', 'legend-affiliates', "Add affiliation"), "Account ID:<br/>", new HTMLInputField(self::PARAM_AFFILIATE_ID), "<br/><br/>Affiliation Type:<br/>", $TypeSelect = new HTMLSelectField(self::PARAM_AFFILIATE_TYPE, array("Request New Affiliate" => AccountAffiliationEntry::TYPE_REQUEST_AFFILIATE, "Request New Reseller" => AccountAffiliationEntry::TYPE_REQUEST_RESELLER, "Request New Processor" => AccountAffiliationEntry::TYPE_REQUEST_PROCESSOR)), "<br/><br/>", new HTMLButton(self::PARAM_SUBMIT, 'Request', 'request')), new HTMLElement('fieldset', 'fieldset-affiliate-list inline', new HTMLElement('legend', 'legend-affiliate-list', "Affiliates"), $AffiliateListTable), new HTMLElement('fieldset', 'fieldset-affiliate-approve inline', new HTMLElement('legend', 'legend-affiliate-approve', "Approve Affiliates"), $ApproveSelect = new HTMLSelectField(self::PARAM_APPROVE_AFFILIATE_ID, array('Pending affiliate approvals' => null)), "<br/><br/>", new HTMLButton(self::PARAM_SUBMIT, 'Approve', 'approve')));
     $SelectStatus->setInputValue($AccountEntry->getStatus());
     $Account = AbstractAccountType::loadFromSession($SessionRequest);
     if ($Account instanceof AdministratorAccount) {
         $TypeSelect->setOptions(AccountAffiliationEntry::$TypeOptions);
     }
     $AffiliateQuery->addRowCallback(function (AccountAffiliationEntry $Affiliation) use($Account, $ApproveSelect) {
         if ($Affiliation->getType() & $Affiliation::TYPE_REQUEST_RESELLER && $Affiliation->getAffiliateID() === $Account->getID()) {
             $ApproveSelect->addOption($Affiliation->getAffiliateID(), "Approve '" . $Affiliation->getTypeText() . "' - " . $Affiliation->getAffiliateID());
         }
     });
     if (!$Request instanceof IFormRequest) {
         return $Form;
     }
     $submit = $Request[self::PARAM_SUBMIT];
     switch ($submit) {
         case 'update':
             $status = $Form->validateField($Request, self::PARAM_ACCOUNT_STATUS);
             $AccountEntry->update($Request, $Account, $status);
             return new RedirectResponse(ManageAccount::getRequestURL($this->getAccountID()), "Account updated successfully. Redirecting...", 5);
         case 'delete':
             AccountEntry::delete($Request, $this->getAccountID());
             return new RedirectResponse(SearchAccounts::getRequestURL(), "Account deleted successfully. Redirecting...", 5);
         case 'approve':
             $affiliateID = $Form->validateField($Request, self::PARAM_APPROVE_AFFILIATE_ID);
             AccountAffiliationEntry::approveAffiliation($Request, $this->getAccountID(), $affiliateID);
             return new RedirectResponse(SearchAccounts::getRequestURL(), "Account deleted successfully. Redirecting...", 5);
         case 'request':
             $affiliateID = $Form->validateField($Request, self::PARAM_AFFILIATE_ID);
             $type = $Form->validateField($Request, self::PARAM_AFFILIATE_TYPE);
             AccountAffiliationEntry::setAffiliate($Request, $this->getAccountID(), $affiliateID, $type);
             return new RedirectResponse(ManageAccount::getRequestURL($this->getAccountID()), "Affiliation requested successfully. Redirecting...", 5);
     }
     throw new \InvalidArgumentException($submit);
 }
 static function setAffiliate(IRequest $Request, $accountID, $affiliateID, $type = self::TYPE_AFFILIATE, $orUpdate = false)
 {
     AccountEntry::get($accountID);
     AccountEntry::get($affiliateID);
     $Table = self::table();
     $Insert = $Table->insert(AccountAffiliationTable::COLUMN_ACCOUNT_ID, AccountAffiliationTable::COLUMN_AFFILIATE_ID, AccountAffiliationTable::COLUMN_TYPE)->values($accountID, $affiliateID, $type);
     if ($orUpdate) {
         $Insert->onDuplicateKeyUpdate(AccountAffiliationTable::COLUMN_TYPE, $type);
     }
     $Insert->execute($Request);
 }