Source of file CartResponse.php
Size: 6,648 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Control/CartResponse.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 | <?php namespace Sunnysideup\Ecommerce\Control; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\FieldType\DBField; use SilverStripe\View\SSViewer; use Sunnysideup\Ecommerce\Api\ShoppingCart; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Pages\Product; /** * @description: returns the cart as JSON * * @authors: Nicolaas [at] Sunny Side Up .co.nz * @package: ecommerce * @sub-package: control */ class CartResponse extends EcommerceResponse { /** * Should the page be reloaded rather than using AJAX? * * @var bool */ protected $includeHeaders = true; /** * Should the page be reloaded rather than using AJAX? * * @var bool */ private static $force_reload = false; /** * @var array */ private static $cart_responses_required = [ 'SideBarCartID' => 'Sidebar_Cart_Inner', ]; /** * Sets the $force_reload to true;. */ public static function set_force_reload() { self::$force_reload = true; } /** * turn the json headers on or off... * useful if you want to use the json data * but not the associated header. */ public function setIncludeHeaders(bool $b): self { $this->includeHeaders = $b; return $this; } /** * Builds json object to be returned via ajax. * * @param array $messages (Type, Message) * @param string $status * * @return string HEADER + JSON */ public function ReturnCartData(array $messages = [], array $additionalData = null, $status = 'success') { //add header if ($this->includeHeaders) { $this->addHeader('Content-Type', 'application/json'); } Config::modify()->update(SSViewer::class, 'source_file_comments', false); //merge messages $messagesImploded = ''; if (is_array($messages) && count($messages)) { foreach ($messages as $messageArray) { $messagesImploded .= '<span class="' . $messageArray['Type'] . '">' . $messageArray['Message'] . '</span>'; } } //bad status if ('success' !== $status) { $this->setStatusCode(400, $messagesImploded); } //init Order - IMPORTANT $currentOrder = ShoppingCart::current_order(); //THIS LINE TAKES UP MOST OF THE TIME OF THE RESPONSE!!! $currentOrder->calculateOrderAttributes($force = false); $ajaxObject = $currentOrder->AJAXDefinitions(); // populate Javascript $js = []; //must be first if (isset($_REQUEST['loadingindex'])) { $js[] = [ 't' => 'loadingindex', 'v' => $_REQUEST['loadingindex'], ]; } //order items $inCartArray = []; $items = $currentOrder->Items(); if ($items->exists()) { foreach ($items as $item) { $js = $item->updateForAjax($js); $buyable = $item->getBuyableCached(true); if ($buyable) { //products in cart $inCartArray[] = $buyable->AJAXDefinitions()->UniqueIdentifier(); //HACK TO INCLUDE PRODUCT IN PRODUCT VARIATION $inCartArray[] = $buyable->Product()->AJAXDefinitions()->UniqueIdentifier(); } } } //in cart items $js[] = [ 't' => 'replaceclass', 's' => $inCartArray, 'p' => $currentOrder->AJAXDefinitions()->ProductListItemClassName(), 'v' => $currentOrder->AJAXDefinitions()->ProductListItemInCartClassName(), 'without' => $currentOrder->AJAXDefinitions()->ProductListItemNotInCartClassName(), ]; //order modifiers $modifiers = $currentOrder->Modifiers(); if ($modifiers->exists()) { foreach ($modifiers as $modifier) { $js = $modifier->updateForAjax($js); } } //order $js = $currentOrder->updateForAjax($js); //messages if (is_array($messages)) { $js[] = [ 't' => 'id', 's' => $ajaxObject->TableMessageID(), 'p' => 'innerHTML', 'v' => $messagesImploded, 'isOrderMessage' => true, ]; $js[] = [ 't' => 'id', 's' => $ajaxObject->TableMessageID(), 'p' => 'hide', 'v' => 0, ]; } else { $js[] = [ 't' => 'id', 's' => $ajaxObject->TableMessageID(), 'p' => 'hide', 'v' => 1, ]; } //TO DO: set it up in such a way that it specifically requests one of these $templates = EcommerceConfig::get(CartResponse::class, 'cart_responses_required'); foreach ($templates as $idMethod => $template) { $selector = $ajaxObject->{$idMethod}(); $classOrID = 'id'; if (false !== stripos($selector, 'class')) { $classOrID = 'class'; } $js[] = [ 't' => $classOrID, 's' => $selector, 'p' => 'innerHTML', //note the space is a hack to return something! 'v' => ' ' . $currentOrder->RenderWith($template), ]; } //now can check if it needs to be reloaded if (self::$force_reload) { $js = [ 'reload' => 1, ]; } else { $js[] = [ 'reload' => 0, ]; } //merge and return if (is_array($additionalData) && count($additionalData)) { $js = array_merge($js, $additionalData); } //TODO: remove doubles? //turn HTMLText (et al.) objects into text foreach ($js as $key => $node) { if (isset($node['v'])) { if ($node['v'] instanceof DBField) { $js[$key]['v'] = $node['v']->forTemplate(); } } } $json = json_encode($js); $json = str_replace('\t', ' ', $json); $json = str_replace('\r', ' ', $json); $json = str_replace('\n', ' ', $json); $json = preg_replace('#\s\s+#', ' ', $json); if (Director::isDev()) { $json = str_replace('{', "\r\n{", $json); } return $json; } } |