Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace WameCms\AddToCart\Controls;
- use App\Model\Shop\Order\OrderManager;
- use App\Model\Shop\ShopSettingsRepository;
- use Nette\Database\Table\ActiveRow;
- use Nette\Utils\Html;
- use WameCms\AddToCart\Models\AddToCartModel;
- use WameCms\Component\Controls\BaseControl;
- use App\Model\Shop\ShopProductRepository;
- use App\Model\Shop\ShopOrderRepository;
- use App\Model\Shop\ShopOrderItemRepository;
- use Nette\Application\UI\Form;
- trait TAddToCartFromCategoryControl
- {
- /** @var AddToCartFromCategoryControlFactory @inject */
- public $addToCartFromCategoryControlFactory;
- protected function createComponentAddToCartFromCategory()
- {
- $control = $this->addToCartFromCategoryControlFactory->create($this->componentsSettings['addToCartFromCategory'], $this->lang);
- return $control;
- }
- }
- interface AddToCartFromCategoryControlFactory
- {
- /** @return AddToCartFromCategoryControl */
- public function create($componentSettings, $lang);
- }
- class AddToCartFromCategoryControl extends BaseControl
- {
- /** @var array */
- private $componentSettings;
- /** @var string */
- private $lang;
- /** @var AddToCartModel */
- private $addToCartModel;
- /** @var ShopSettingsRepository */
- private $shopSettingsRepository;
- /** @var ShopProductRepository */
- private $shopProductRepository;
- /** @var ShopOrderRepository */
- private $shopOrderRepository;
- /** @var ShopOrderItemRepository */
- private $shopOrderItemRepository;
- /** @var array */
- private $variants;
- /** @var int */
- private $variantId;
- /** @var ActiveRow */
- private $variant;
- private $product;
- private $possibility;
- /** @var OrderManager */
- private $orderManager;
- public function __construct(
- $componentSettings,
- $lang,
- AddToCartModel $addToCartModel,
- ShopSettingsRepository $shopSettingsRepository,
- ShopProductRepository $shopProductRepository,
- ShopOrderRepository $shopOrderRepository,
- ShopOrderItemRepository $shopOrderItemRepository,
- OrderManager $orderManager
- ) {
- $this->shopOrderRepository = $shopOrderRepository;
- $this->shopOrderItemRepository = $shopOrderItemRepository;
- $this->componentSettings = $componentSettings;
- $this->lang = $lang;
- $this->addToCartModel = $addToCartModel;
- $this->shopSettingsRepository = $shopSettingsRepository;
- $this->shopProductRepository = $shopProductRepository;
- $this->orderManager = $orderManager;
- }
- /**
- * Get quantity
- *
- * @param ActiveRow $product
- * @return array
- */
- private function getQuantity($product)
- {
- $min = 1;
- $max = null;
- if(isset($product['min_purchase'])) $min = $product['min_purchase'] == 0 ? $min : $product['min_purchase'];
- if(isset($product['max_purchase'])) $max = $product['max_purchase'] == 0 ? $max : $product['max_purchase'];
- return [$min, $max];
- }
- /**
- * Get add to cart possibility
- *
- * @return boolean
- */
- private function getPossibility()
- {
- $shopSettings = $this->shopSettingsRepository->getPairs(['lang' => $this->lang, 'name' => 'negative_stock'], 'name', 'value');
- // Ked je povoleny zaporny sklad
- if ($shopSettings['negative_stock'] == 1 && (count($this->variants) == 0 || (count($this->variants) > 0 && $this->variant))) {
- return true;
- }
- // Ked nema varianty
- if (count($this->variants) == 0) {
- // Ked je na sklade menej alebo rovne 0
- if ($this->product['stock_status'] <= 0) {
- return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, tovar nieje na sklade.'));
- }
- // Ked je stav skladu menej ako minimalne mnozstvo pre nakup
- if ($this->product['min_purchase'] > 0 && $this->product['stock_status'] < $this->product['min_purchase']) {
- return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, tovar nieje na sklade v dostatočnom množstve.'));
- }
- }
- // Ked ma varianty
- else {
- // Ked nieje zvolena varianta
- if (!$this->variant) {
- return false;
- }
- // Ked je na sklade menej alebo rovne 0
- if ($this->variant['stock_status'] <= 0) {
- return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, táto varianta je vypredaná.'));
- }
- // Ked je stav skladu menej ako minimalne mnozstvo pre nakup
- if ($this->variant['min_purchase'] > 0 && $this->variant['stock_status'] < $this->variant['min_purchase']) {
- return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, táto varianta nieje na sklade v dostatočnom množstve.'));
- }
- }
- return true;
- }
- /**
- * Get variants
- *
- * @return array
- */
- private function getVariants()
- {
- $productId = $this->product['product_id'];
- if ($this->product['descendant_product'] != 0) {
- $productId = $this->product['descendant_product'];
- }
- return $this->shopProductRepository->getObjectPairs(['descendant_product' => $productId, 'status' => 2], 'product_id', 'descendant_product_sort ASC, title ASC');
- }
- /**
- * Get product variant
- *
- * @return int|null
- */
- private function getVariant()
- {
- if ($this->product['descendant_product'] == 0) {
- return null;
- }
- $this->variantId = $this->product['product_id'];
- return $this->product;
- }
- /**
- * Set variant if is one
- */
- private function setVariant()
- {
- if (count($this->variants) == 1) {
- $this->variantId = key($this->variants);
- $this->variant = $this->variants[$this->variantId];
- }
- }
- /**
- * @return \Nette\Application\UI\Multiplier
- */
- protected function createComponentForm()
- {
- return new \Nette\Application\UI\Multiplier(function($productId) {
- $form = new Form;
- if ($productId == 'main' && count($this->variants) > 0) {
- if (isset($this->componentSettings['params']['type']) && $this->componentSettings['params']['type'] == 'select') {
- $form->addSelect('product', _('Varianta'), $this->getVariantList())
- ->setPrompt('- ' . _('Veľkosť') . ' -')
- ->setRequired(_('Vyberte si veľkosť'));
- } else {
- $form->addRadioList('product', _('Varianta'), $this->getVariantList())
- ->setRequired(_('Vyberte si variantu'));
- }
- if (isset($this->variants[$this->variantId])) {
- $form['product']->setDefaultValue($this->variantId);
- }
- }
- if ($this->componentSettings['default']['params']['quantity'] == 1) {
- $quantity = $this->getQuantity($this->product);
- $form->addText('quantity', _('Množstvo'))
- ->setType('number')
- ->setDefaultValue($quantity[0])
- ->setRequired(false)
- ->addRule(Form::INTEGER, _('Musí byť číslo'))
- ->addRule(Form::RANGE, _('Minimálne %s ks'), $quantity);
- }
- $form->addSubmit('submit', _('Pridať do košíka'));
- $form->onSuccess[] = [$this, 'formSuccess'];
- return $form;
- });
- }
- /**
- * @param Form $form
- * @param $values
- * @throws \Nette\Application\AbortException
- */
- public function formSuccess(Form $form, $values)
- {
- $presenter = $this->getPresenter();
- try {
- $orderId = $this->shopOrderRepository->getOrderId();
- if (!$orderId) {
- $orderId = $this->shopOrderRepository->create();
- $this->orderManager->add($orderId);
- }
- $productId = isset($values['product']) ? $values['product'] : $form->getName();
- $quantity = isset($values['quantity']) ? $values['quantity'] : 1;
- $this->shopOrderItemRepository->addItem($orderId, $productId, $quantity);
- $this->presenter->flashMessage(
- Html::el()
- ->addText(_('Produkt bol vložený do nákupného košíka.'))
- ->addHtml(Html::el('div')->addAttributes(['class' => 'text-center']))
- ->addHtml(Html::el('a')
- ->href($this->presenter->link('ShopOrders:contentCart'))
- ->addAttributes(['class' => 'btn btn-sm btn-primary'])
- ->setText(_('Prejsť do košíka'))
- ),
- 'success'
- );
- $presenter->redirect('this#checkout-open');
- } catch (\Exception $e) {
- if ($e instanceof \Nette\Application\AbortException) {
- throw $e;
- }
- $this->flashMessage($e->getMessage(), 'danger');
- $presenter->redirect('this');
- }
- }
- /**
- * Render method
- *
- * @param string $position
- * @param $product
- */
- public function render($position = 'default', $product)
- {
- $this->product = $product;
- $this->variant = $this->getVariant();
- $this->variants = $this->getVariants();
- $this->setVariant();
- $this->getPossibility();
- $componentSettings = $this->componentSettings[$position];
- $params = $componentSettings['params'];
- $this->template->variants = $this->variants;
- $this->template->params = $params;
- $this->template->productId = $product->product_id;
- $this->template->possibility = $this->possibility;
- $this->template->product = $this->product;
- $this->template->setFile(__DIR__ . '/' . ($componentSettings['template'] != '' ? $componentSettings['template'] : 'default.latte'));
- $this->template->render();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement