Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace WameCms\AddToCart\Controls;
  4.  
  5.  
  6. use App\Model\Shop\Order\OrderManager;
  7. use App\Model\Shop\ShopSettingsRepository;
  8. use Nette\Database\Table\ActiveRow;
  9. use Nette\Utils\Html;
  10. use WameCms\AddToCart\Models\AddToCartModel;
  11. use WameCms\Component\Controls\BaseControl;
  12. use App\Model\Shop\ShopProductRepository;
  13. use App\Model\Shop\ShopOrderRepository;
  14. use App\Model\Shop\ShopOrderItemRepository;
  15.  
  16. use Nette\Application\UI\Form;
  17.  
  18.  
  19. trait TAddToCartFromCategoryControl
  20. {
  21. /** @var AddToCartFromCategoryControlFactory @inject */
  22. public $addToCartFromCategoryControlFactory;
  23.  
  24. protected function createComponentAddToCartFromCategory()
  25. {
  26. $control = $this->addToCartFromCategoryControlFactory->create($this->componentsSettings['addToCartFromCategory'], $this->lang);
  27.  
  28. return $control;
  29. }
  30. }
  31.  
  32.  
  33. interface AddToCartFromCategoryControlFactory
  34. {
  35. /** @return AddToCartFromCategoryControl */
  36. public function create($componentSettings, $lang);
  37. }
  38.  
  39.  
  40. class AddToCartFromCategoryControl extends BaseControl
  41. {
  42.  
  43. /** @var array */
  44. private $componentSettings;
  45.  
  46. /** @var string */
  47. private $lang;
  48.  
  49. /** @var AddToCartModel */
  50. private $addToCartModel;
  51.  
  52. /** @var ShopSettingsRepository */
  53. private $shopSettingsRepository;
  54.  
  55. /** @var ShopProductRepository */
  56. private $shopProductRepository;
  57.  
  58. /** @var ShopOrderRepository */
  59. private $shopOrderRepository;
  60.  
  61. /** @var ShopOrderItemRepository */
  62. private $shopOrderItemRepository;
  63.  
  64. /** @var array */
  65. private $variants;
  66.  
  67. /** @var int */
  68. private $variantId;
  69.  
  70. /** @var ActiveRow */
  71. private $variant;
  72.  
  73. private $product;
  74.  
  75. private $possibility;
  76.  
  77. /** @var OrderManager */
  78. private $orderManager;
  79.  
  80.  
  81. public function __construct(
  82. $componentSettings,
  83. $lang,
  84. AddToCartModel $addToCartModel,
  85. ShopSettingsRepository $shopSettingsRepository,
  86. ShopProductRepository $shopProductRepository,
  87. ShopOrderRepository $shopOrderRepository,
  88. ShopOrderItemRepository $shopOrderItemRepository,
  89. OrderManager $orderManager
  90. ) {
  91. $this->shopOrderRepository = $shopOrderRepository;
  92. $this->shopOrderItemRepository = $shopOrderItemRepository;
  93. $this->componentSettings = $componentSettings;
  94. $this->lang = $lang;
  95. $this->addToCartModel = $addToCartModel;
  96. $this->shopSettingsRepository = $shopSettingsRepository;
  97. $this->shopProductRepository = $shopProductRepository;
  98. $this->orderManager = $orderManager;
  99. }
  100.  
  101.  
  102. /**
  103. * Get quantity
  104. *
  105. * @param ActiveRow $product
  106. * @return array
  107. */
  108. private function getQuantity($product)
  109. {
  110. $min = 1;
  111. $max = null;
  112.  
  113. if(isset($product['min_purchase'])) $min = $product['min_purchase'] == 0 ? $min : $product['min_purchase'];
  114. if(isset($product['max_purchase'])) $max = $product['max_purchase'] == 0 ? $max : $product['max_purchase'];
  115.  
  116. return [$min, $max];
  117. }
  118.  
  119. /**
  120. * Get add to cart possibility
  121. *
  122. * @return boolean
  123. */
  124. private function getPossibility()
  125. {
  126. $shopSettings = $this->shopSettingsRepository->getPairs(['lang' => $this->lang, 'name' => 'negative_stock'], 'name', 'value');
  127.  
  128. // Ked je povoleny zaporny sklad
  129. if ($shopSettings['negative_stock'] == 1 && (count($this->variants) == 0 || (count($this->variants) > 0 && $this->variant))) {
  130. return true;
  131. }
  132.  
  133. // Ked nema varianty
  134. if (count($this->variants) == 0) {
  135. // Ked je na sklade menej alebo rovne 0
  136. if ($this->product['stock_status'] <= 0) {
  137. return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, tovar nieje na sklade.'));
  138. }
  139.  
  140. // Ked je stav skladu menej ako minimalne mnozstvo pre nakup
  141. if ($this->product['min_purchase'] > 0 && $this->product['stock_status'] < $this->product['min_purchase']) {
  142. return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, tovar nieje na sklade v dostatočnom množstve.'));
  143. }
  144. }
  145. // Ked ma varianty
  146. else {
  147. // Ked nieje zvolena varianta
  148. if (!$this->variant) {
  149. return false;
  150. }
  151.  
  152. // Ked je na sklade menej alebo rovne 0
  153. if ($this->variant['stock_status'] <= 0) {
  154. return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, táto varianta je vypredaná.'));
  155. }
  156.  
  157. // Ked je stav skladu menej ako minimalne mnozstvo pre nakup
  158. if ($this->variant['min_purchase'] > 0 && $this->variant['stock_status'] < $this->variant['min_purchase']) {
  159. return Html::el('div')->addClass('alert alert-danger')->addText(_('Ľutujeme, táto varianta nieje na sklade v dostatočnom množstve.'));
  160. }
  161. }
  162.  
  163. return true;
  164. }
  165.  
  166.  
  167. /**
  168. * Get variants
  169. *
  170. * @return array
  171. */
  172. private function getVariants()
  173. {
  174. $productId = $this->product['product_id'];
  175.  
  176. if ($this->product['descendant_product'] != 0) {
  177. $productId = $this->product['descendant_product'];
  178. }
  179.  
  180. return $this->shopProductRepository->getObjectPairs(['descendant_product' => $productId, 'status' => 2], 'product_id', 'descendant_product_sort ASC, title ASC');
  181. }
  182.  
  183. /**
  184. * Get product variant
  185. *
  186. * @return int|null
  187. */
  188. private function getVariant()
  189. {
  190. if ($this->product['descendant_product'] == 0) {
  191. return null;
  192. }
  193.  
  194. $this->variantId = $this->product['product_id'];
  195.  
  196. return $this->product;
  197. }
  198.  
  199. /**
  200. * Set variant if is one
  201. */
  202. private function setVariant()
  203. {
  204. if (count($this->variants) == 1) {
  205. $this->variantId = key($this->variants);
  206. $this->variant = $this->variants[$this->variantId];
  207. }
  208. }
  209.  
  210. /**
  211. * @return \Nette\Application\UI\Multiplier
  212. */
  213. protected function createComponentForm()
  214. {
  215. return new \Nette\Application\UI\Multiplier(function($productId) {
  216. $form = new Form;
  217.  
  218. if ($productId == 'main' && count($this->variants) > 0) {
  219. if (isset($this->componentSettings['params']['type']) && $this->componentSettings['params']['type'] == 'select') {
  220. $form->addSelect('product', _('Varianta'), $this->getVariantList())
  221. ->setPrompt('- ' . _('Veľkosť') . ' -')
  222. ->setRequired(_('Vyberte si veľkosť'));
  223. } else {
  224. $form->addRadioList('product', _('Varianta'), $this->getVariantList())
  225. ->setRequired(_('Vyberte si variantu'));
  226. }
  227.  
  228. if (isset($this->variants[$this->variantId])) {
  229. $form['product']->setDefaultValue($this->variantId);
  230. }
  231. }
  232.  
  233. if ($this->componentSettings['default']['params']['quantity'] == 1) {
  234. $quantity = $this->getQuantity($this->product);
  235.  
  236. $form->addText('quantity', _('Množstvo'))
  237. ->setType('number')
  238. ->setDefaultValue($quantity[0])
  239. ->setRequired(false)
  240. ->addRule(Form::INTEGER, _('Musí byť číslo'))
  241. ->addRule(Form::RANGE, _('Minimálne %s ks'), $quantity);
  242. }
  243.  
  244. $form->addSubmit('submit', _('Pridať do košíka'));
  245.  
  246. $form->onSuccess[] = [$this, 'formSuccess'];
  247.  
  248. return $form;
  249. });
  250. }
  251.  
  252.  
  253. /**
  254. * @param Form $form
  255. * @param $values
  256. * @throws \Nette\Application\AbortException
  257. */
  258. public function formSuccess(Form $form, $values)
  259. {
  260. $presenter = $this->getPresenter();
  261.  
  262. try {
  263. $orderId = $this->shopOrderRepository->getOrderId();
  264.  
  265. if (!$orderId) {
  266. $orderId = $this->shopOrderRepository->create();
  267.  
  268. $this->orderManager->add($orderId);
  269. }
  270.  
  271. $productId = isset($values['product']) ? $values['product'] : $form->getName();
  272.  
  273. $quantity = isset($values['quantity']) ? $values['quantity'] : 1;
  274.  
  275. $this->shopOrderItemRepository->addItem($orderId, $productId, $quantity);
  276.  
  277. $this->presenter->flashMessage(
  278. Html::el()
  279. ->addText(_('Produkt bol vložený do nákupného košíka.'))
  280. ->addHtml(Html::el('div')->addAttributes(['class' => 'text-center']))
  281. ->addHtml(Html::el('a')
  282. ->href($this->presenter->link('ShopOrders:contentCart'))
  283. ->addAttributes(['class' => 'btn btn-sm btn-primary'])
  284. ->setText(_('Prejsť do košíka'))
  285. ),
  286. 'success'
  287. );
  288.  
  289. $presenter->redirect('this#checkout-open');
  290. } catch (\Exception $e) {
  291. if ($e instanceof \Nette\Application\AbortException) {
  292. throw $e;
  293. }
  294.  
  295. $this->flashMessage($e->getMessage(), 'danger');
  296. $presenter->redirect('this');
  297. }
  298. }
  299.  
  300. /**
  301. * Render method
  302. *
  303. * @param string $position
  304. * @param $product
  305. */
  306. public function render($position = 'default', $product)
  307. {
  308. $this->product = $product;
  309.  
  310. $this->variant = $this->getVariant();
  311. $this->variants = $this->getVariants();
  312. $this->setVariant();
  313. $this->getPossibility();
  314.  
  315. $componentSettings = $this->componentSettings[$position];
  316.  
  317. $params = $componentSettings['params'];
  318.  
  319. $this->template->variants = $this->variants;
  320. $this->template->params = $params;
  321. $this->template->productId = $product->product_id;
  322. $this->template->possibility = $this->possibility;
  323. $this->template->product = $this->product;
  324. $this->template->setFile(__DIR__ . '/' . ($componentSettings['template'] != '' ? $componentSettings['template'] : 'default.latte'));
  325. $this->template->render();
  326. }
  327.  
  328.  
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement