Source of file ProductController.php
Size: 6,993 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Pages/ProductController.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 | <?php namespace Sunnysideup\Ecommerce\Pages; use PageController; use SilverStripe\Control\Director; use SilverStripe\Control\HTTPRequest; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\RequiredFields; use SilverStripe\ORM\DataList; use SilverStripe\Security\Security; use SilverStripe\View\Requirements; use Sunnysideup\Ecommerce\Api\SetThemed; use Sunnysideup\Ecommerce\Api\ShoppingCart; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Forms\Fields\EcomQuantityField; class ProductController extends PageController { /** * is this the current version? * * @var bool */ protected $isCurrentVersion = true; private static $allowed_actions = [ 'viewversion', 'ajaxview', 'addproductfromform', 'debug' => 'ADMIN', ]; /** * view earlier version of a product * returns error or changes datarecord to earlier version * if the ID does not match the Page then we look for the variation. */ public function viewversion(HTTPRequest $request) { $version = (int) $request->param('ID') - 0; $currentVersion = $this->Version; if ($currentVersion !== $version) { $record = $this->getVersionOfBuyable($this->ID, $version); if ($record) { //we check again, because we may actually get the same version back... if ($record->Version !== $this->Version) { $this->record = $record; $this->dataRecord->AllowPurchase = false; $this->AllowPurchase = false; $this->isCurrentVersion = false; $this->Title .= _t('Product.OLDERVERSION', ' - Older Version'); $this->MetaTitle .= _t('Product.OLDERVERSION', ' - Older Version'); } } else { return $this->httpError(404); } } return []; } /** * Standard SS method * Returns a snippet when requested by ajax. */ public function ajaxview(HTTPRequest $request) { SetThemed::start(); $html = $this->RenderWith('Sunnysideup\Ecommerce\Includes\ProductGroupItemMoreDetail'); SetThemed::end(); return $html; } /** * returns a form for adding products to cart. * * @return Form|string */ public function AddProductForm() { if ($this->canPurchase()) { $farray = []; $fields = new FieldList($farray); $fields->push(new NumericField('Quantity', 'Quantity', 1)); //TODO: perhaps use a dropdown instead (elimiates need to use keyboard) $actions = new FieldList( new FormAction('addproductfromform', _t('Product.ADDLINK', 'Add this item to cart')) ); $requiredFields = ['Quantity']; $validator = new RequiredFields($requiredFields); return new Form($this, 'AddProductForm', $fields, $actions, $validator); } return _t('Product.PRODUCTNOTFORSALE', 'Product not for sale'); } /** * executes the AddProductForm. */ public function addproductfromform(array $data, Form $form) { if (! $this->IsInCart()) { $quantity = round($data['Quantity'], $this->QuantityDecimals()); if (! $quantity) { $quantity = 1; } $product = Product::get_by_id($this->ID); if ($product) { ShoppingCart::singleton()->addBuyable($product, $quantity); } if ($this->IsInCart()) { $msg = _t('Order.SUCCESSFULLYADDED', 'Added to cart.'); $status = 'good'; } else { $msg = _t('Order.NOTADDEDTOCART', 'Not added to cart.'); $status = 'bad'; } if (Director::is_ajax()) { return ShoppingCart::singleton()->setMessageAndReturn($msg, $status); } $form->sessionMessage($msg, $status); $this->redirectBack(); } else { return EcomQuantityField::create($this); } } /** * Is this an older version? */ public function IsOlderVersion(): bool { return ! $this->isCurrentVersion; } /** * This method can be extended to show products in the side bar. */ public function SidebarProducts(): ?DataList { return null; } /** * This method can be extended to show products in the side bar. * * @return null|Product */ public function NextProduct() { $array = $this->getListOfIDs(); foreach ($array as $key => $id) { $id = (int) $id; if ($id === $this->ID) { if (isset($array[$key + 1])) { return Product::get_by_id((int) $array[$key + 1]); } } } return null; } /** * This method can be extended to show products in the side bar. * * @return null|Product */ public function PreviousProduct() { $array = $this->getListOfIDs(); $previousID = 0; foreach ($array as $id) { $id = (int) $id; if ($id === $this->ID) { return Product::get_by_id($previousID); } $previousID = $id; } return null; } /** * This method can be extended to show products in the side bar. */ public function HasPreviousOrNextProduct(): bool { return $this->PreviousProduct() || $this->NextProduct(); } public function debug() { $member = Security::getCurrentUser(); if (! $member || ! $member->IsShopAdmin()) { $messages = [ 'default' => 'You must login as an admin to access debug functions.', ]; Security::permissionFailure($this, $messages); } return $this->dataRecord->debug(); } /** * Standard SS method. */ protected function init() { parent::init(); Requirements::themedCSS('client/css/Product'); Requirements::javascript('sunnysideup/ecommerce: client/javascript/EcomProducts.js'); } /** * returns an array of product IDs, as saved in the last * ProductGroup view (saved using session). * * @return array */ protected function getListOfIDs() { $listOfIDs = $this->getRequest()->getSession()->get(EcommerceConfig::get(ProductGroup::class, 'session_name_for_product_array')); if ($listOfIDs) { $arrayOfIDs = explode(',', $listOfIDs); if (is_array($arrayOfIDs)) { return $arrayOfIDs; } } return []; } } |