/** * Execute a command and return a response. Does not render * @param IRequest $Request * @throws \Exception * @return IResponse the execution response */ function execute(IRequest $Request) { $SessionRequest = $Request; if (!$SessionRequest instanceof ISessionRequest) { throw new \Exception("Session required"); } $Account = AbstractAccountType::loadFromSession($SessionRequest); if (!$Account instanceof MerchantAccount) { throw new RequestException("Only merchants may create a new Product"); } $productOptions = array("Choose a Product Type" => null); /** @var AbstractProductType[] $ProductTypes */ $ProductTypes = array(); $ProductForms = array(); foreach (Types\AbstractProductType::loadAllProductTypes() as $ProductType) { $ProductTypes[$ProductType->getTypeName()] = $ProductType; $FieldSet = $ProductType->getConfigFieldSet($Request); $FieldSet->setAttribute('disabled', 'disabled'); $ProductForms[] = $FieldSet; // $FieldSet = $ProductType->getRatesFieldSet($Request); // $FieldSet->setAttribute('disabled', 'disabled'); // $ProductForms[] = $FieldSet; $productOptions[$ProductType->getTypeDescription()] = $ProductType->getTypeName(); } $Form = new HTMLForm(self::FORM_METHOD, $Request->getPath(), self::FORM_NAME, new HTMLMetaTag(HTMLMetaTag::META_TITLE, self::TITLE), new HTMLHeaderScript(__DIR__ . '/assets/product.js'), new HTMLHeaderStyleSheet(__DIR__ . '/assets/product.css'), new HTMLElement('fieldset', 'fieldset-create-product', new HTMLElement('legend', 'legend-product', self::TITLE), new HTMLElement('label', null, "Status<br/>", new HTMLSelectField(self::PARAM_PRODUCT_STATUS, ProductEntry::$StatusOptions, new RequiredValidation())), "<br/><br/>", new HTMLElement('label', null, "Choose a Product Type<br/>", new HTMLSelectField(self::PARAM_PRODUCT_TYPE, $productOptions, new RequiredValidation())), "<br/><br/>", $ProductForms, "<br/><br/>Submit:<br/>", new HTMLButton('submit', 'Submit', 'submit')), "<br/>"); if (!$Request instanceof IFormRequest) { return $Form; } $status = $Form->validateField($Request, self::PARAM_PRODUCT_STATUS); $productType = $Form->validateField($Request, self::PARAM_PRODUCT_TYPE); // $sourceID = $Form->validateField($Request, self::PARAM_PAYMENT_SOURCE_TYPE); $ChosenProduct = $ProductTypes[$productType]; $ChosenProduct->validateConfigRequest($Request, $Form); $accountID = 'default'; if ($Account) { $accountID = $Account->getID(); } $id = ProductEntry::create($Request, $ChosenProduct, $accountID, $status); return new RedirectResponse(ManageProduct::getRequestURL($id), "Product created successfully. Redirecting...", 5); }