Source of file AddToCartForm.php
Size: 11,816 Bytes - Last Modified: 2021-12-24T06:46:07+00:00
/var/www/docs.ssmods.com/process/src/src/Form/AddToCartForm.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 | <?php namespace Dynamic\Foxy\Form; use Dynamic\Foxy\Extension\Shippable; use Dynamic\Foxy\Model\FoxyHelper; use Dynamic\Foxy\Model\OptionType; use Dynamic\Foxy\Model\ProductOption; use Dynamic\Foxy\Model\Variation; use Dynamic\Foxy\Model\VariationType; use Dynamic\Foxy\ProductExpirationHelper; use Dynamic\Products\Page\Product; use SilverStripe\CMS\Model\VirtualPage; use SilverStripe\Forms\CompositeField; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HeaderField; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\GroupedList; use SilverStripe\ORM\HasManyList; /** * Class AddToCartForm * @package Dynamic\Foxy\Form */ class AddToCartForm extends Form { /** * @var */ protected $helper; /** * @var */ private $product; /** * @var ProductExpirationHelper */ private $expiration_helper; /** * @param $helper * * @return $this */ public function setFoxyHelper($helper) { $helper = $helper === null ? FoxyHelper::create() : $helper; if ($helper instanceof FoxyHelper) { $this->helper = $helper; return $this; } throw new \InvalidArgumentException('$helper needs to be an instance of FoxyHelper.'); } /** * @return FoxyHelper */ public function getFoxyHelper() { if (!$this->helper) { $this->setFoxyHelper(FoxyHelper::create()); } return $this->helper; } /** * @param $product * * @return $this */ public function setProduct($product) { if ($product instanceof VirtualPage) { $product = $this->getFoxyHelper()->getProducts()->filter('ID', $product->CopyContentFromID)->first(); } if ($product->isProduct()) { $this->product = $product; return $this; } throw new \InvalidArgumentException('$product needs to implement a Foxy DataExtension.'); } /** * @return Product */ public function getProduct() { return $this->product; } /** * @return $this */ protected function setExpirationHelper() { $this->expiration_helper = ProductExpirationHelper::create(); return $this; } /** * @return ProductExpirationHelper */ public function getExpirationHelper() { if (!$this->expiration_helper) { $this->setExpirationHelper(); } return $this->expiration_helper; } /** * AddToCartForm constructor. * @param $controller * @param $name * @param FieldList|null $fields * @param FieldList|null $actions * @param null $validator * @param null $product * @param null $helper * @throws \SilverStripe\ORM\ValidationException */ public function __construct( $controller, $name, FieldList $fields = null, FieldList $actions = null, $validator = null, $product = null, $helper = null ) { $this->setProduct($product); $this->setFoxyHelper($helper); $fields = ($fields != null && $fields->exists()) ? $this->getProductFields($fields) : $this->getProductFields(FieldList::create()); $actions = ($actions != null && $actions->exists()) ? $this->getProductActions($actions) : $this->getProductActions(FieldList::create()); $validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); parent::__construct($controller, $name, $fields, $actions, $validator); //have to call after parent::__construct() $this->setAttribute('action', FoxyHelper::FormActionURL()); $this->disableSecurityToken(); $this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); } /** * @param FieldList $fields * * @return FieldList */ protected function getProductFields(FieldList $fields) { $hiddenTitle = ($this->product->ReceiptTitle) ? htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title); $code = $this->product->Code; if ($this->product->isAvailable()) { $fields->push( HiddenField::create('name') ->setValue( self::getGeneratedValue($code, 'name', $hiddenTitle, 'value') ) ); $fields->push( HiddenField::create('category') ->setValue( self::getGeneratedValue($code, 'category', $this->product->FoxyCategory()->Code, 'value') ) ); $fields->push( HiddenField::create('code') ->setValue( self::getGeneratedValue($code, 'code', $this->product->Code, 'value') ) ); $fields->push( HiddenField::create('product_id') ->setValue( self::getGeneratedValue($code, 'product_id', $this->product->ID, 'value') ) ->setName('h:product_id') ); $fields->push( HiddenField::create('price') ->setValue( self::getGeneratedValue($code, 'price', $this->product->Price, 'value') ) ); if ($this->product->hasMethod('AbsoluteLink')) { $fields->push( HiddenField::create('url') ->setValue( self::getGeneratedValue($code, 'url', $this->product->AbsoluteLink(), 'value') ) ); } if ($this->product->hasExtension(Shippable::class)) { if ($this->product->Weight > 0) { $fields->push( HiddenField::create('weight') ->setValue( self::getGeneratedValue($code, 'weight', $this->product->Weight, 'value') ) ); } } $image = null; if ($this->product->hasMethod('getImage')) { if ($this->product->getImage()) { $image = $this->product->getImage()->CMSThumbnail()->absoluteURL; } if ($image) { $fields->push( HiddenField::create('image') ->setValue( self::getGeneratedValue($code, 'image', $image, 'value') ) ); } } if ($this->product->QuantityMax > 0) { $fields->push( HiddenField::create('quantity_max') ->setValue(self::getGeneratedValue($code, 'quantity_max', $this->product->QuantityMax, 'value')) ); } $fields->push($this->getProductVariations()); if ($this->product->QuantityMax != 1) { $fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1)); } $fields->push( HiddenField::create('quantity') ->setValue( self::getGeneratedValue($code, 'quantity', 1, 'value') ) ); $fields->push( HeaderField::create('submitPrice', '$' . $this->product->Price, 4) ->addExtraClass('submit-price') ); } $this->extend('updateProductFields', $fields); if ($this->getExpirationHelper()->getExpiration() !== null && $this->getExpirationHelper()->getExpiration() > 0) { $fields->push( HiddenField::create('expires') ->setValue( AddToCartForm::getGeneratedValue( $this->getProduct()->Code, 'expires', $this->getExpirationHelper()->getExpiration(), 'value' ) ) ); } return $fields; } /** * @param FieldList $actions * * @return FieldList */ protected function getProductActions(FieldList $actions) { if (!empty(trim($this->helper->getStoreCartURL())) && $this->product->isAvailable()) { $actions->push( FormAction::create( 'x:submit', _t(__CLASS__ . '.AddToCart', 'Add to Cart') ) ->addExtraClass('fs-add-to-cart-button') ->setName('x:submit') ); } $this->extend('updateProductActions', $actions); return $actions; } /** * @param null $productCode * @param null $optionName * @param null $optionValue * @param string $method * @param bool $output * @param bool $urlEncode * * @return null|string */ // todo - Purchasable Extension or AddToCartForm? protected in Form public static function getGeneratedValue( $productCode = null, $optionName = null, $optionValue = null, $method = 'name', $output = false, $urlEncode = false ) { $optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName; $helper = FoxyHelper::create(); return $helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode); } /** * @return CompositeField */ protected function getProductVariations() { $types = VariationType::get(); $variationsField = CompositeField::create(); foreach ($types as $type) { if (($variations = $type->Variations()->filter('ProductID', $this->product->ID)) && $variations->count()) { $variationsField->push($this->createVariationField($type, $variations)); } } $variationsField->addExtraClass('foxycartOptionsContainer'); return $variationsField; } /** * @param VariationType $type * @param HasManyList $variations * @return DropdownField */ protected function createVariationField(VariationType $type, HasManyList $variations) { $disabled = []; $list = []; $variationField = DropdownField::create(preg_replace('/\s/', '_', $type->Title)); /** @var Variation $variation */ foreach ($variations as $variation) { $name = self::getGeneratedValue( $this->product->Code, $type->Title, $variation->getGeneratedValue(), 'value' ); $list[$name] = $variation->getGeneratedTitle(); if (!$variation->getAvailability()) { array_push($disabled, $name); } } $variationField->setSource($list) ->setTitle($type->Title) ->addExtraClass("product-options"); if (!empty($disabled)) { $variationField->setDisabledItems($disabled); } return $variationField; } } |