* @copyright Copyright (c) 2011-2014 Qualiteam software Ltd . All rights reserved * @license http://www.x-cart.com/license-agreement.html X-Cart 5 License Agreement * @link http://www.x-cart.com/ */ namespace XLite\Module\Webkulsoftware\PreorderSales\Controller\Admin; /** * Product preorders page controller (Product modify section) */ class Product extends \XLite\Controller\Admin\Product implements \XLite\Base\IDecorator { /** * Page key */ const PAGE_VARI = 'preorders'; /** * Multiple attributes * * @var array */ protected $multipleAttributes; /** * PreorderSales attribute ids * * @var array */ protected $preordersAttributeIds; /** * Possible preorders count * * @var integer */ protected $possiblePreorderSalesCount; /** * Get multiple attributes * * @return array */ // protected function doActionUpdate() // { // $list = new \XLite\Module\Webkulsoftware\PreorderSales\View\ItemsList\Model\Preorders; // $list->processQuick(); // } public function getMultipleAttributes() { if (!isset($this->multipleAttributes)) { $this->multipleAttributes = $this->getProduct()->getMultipleAttributes(); } return $this->multipleAttributes; } /** * Get preorders attribute ids * * @return array */ public function getPreorderSalesAttributeIds() { if (!isset($this->preordersAttributeIds)) { $this->preordersAttributeIds = array(); foreach ($this->getPreorderSalesAttributes() as $va) { $this->preordersAttributeIds[$va->getId()] = $va->getId(); } } return $this->preordersAttributeIds; } /** * Get preorders attributes * * @return array */ public function getPreorderSalesAttributes() { return $this->getProduct()->getPreorderSalesAttributes()->toArray(); } /** * Get possible preorders count * * @return integer */ public function getPossiblePreorderSalesCount() { if (!isset($this->possiblePreorderSalesCount)) { $this->possiblePreorderSalesCount = 1; foreach ($this->getMultipleAttributes() as $a) { if (in_array($a->getId(), $this->getPreorderSalesAttributeIds())) { $this->possiblePreorderSalesCount *= count($a->getAttributeValue($this->getProduct())); } } } return $this->possiblePreorderSalesCount; } /** * Check - add preorder or no * * @return boolean */ public function isAllowVaraintAdd() { return $this->getPossiblePreorderSalesCount() > $this->getProduct()->getPreorderSales(); } /** * Get pages * * @return array */ public function getPages() { $list = parent::getPages(); if (!$this->isNew()) { $list = array_merge( array_slice($list, 0, 5), array(static::PAGE_VARI => static::t('PreorderSales')), array_slice($list, 5) ); } return $list; } /** * Return page template * * @return string */ public function getPageTemplate() { return defined('static::PAGE_WHOLESALE_PRICING') && static::PAGE_WHOLESALE_PRICING == $this->getPage() && $this->getProduct()->mustHavePreorderSales() ? 'modules/Webkulsoftware/PreorderSales/pricing/body.twig' : parent::getPageTemplate(); } /** * Get pages templates * * @return array */ protected function getPageTemplates() { $list = parent::getPageTemplates(); if (!$this->isNew()) { $list = array_merge( array_slice($list, 0,5), array(static::PAGE_VARI => 'modules/Webkulsoftware/PreorderSales/preorders/body.twig'), array_slice($list, 5) ); } return $list; } /** * Update preorders attributes * * @return void */ protected function updatePreorderSalesAttributes() { $attr = \XLite\Core\Request::getInstance()->attr; $product = $this->getProduct(); $product->getPreorderSalesAttributes()->clear(); if ($attr) { $attributes = \XLite\Core\Database::getRepo('XLite\Model\Attribute')->findByIds($attr); foreach ($attributes as $a) { $product->addPreorderSalesAttributes($a); $a->addPreorderSalesProduct($product); } } $product->checkPreorderSales(); \XLite\Core\Database::getEM()->flush(); } /** * Do create preorders * * @return void */ protected function doActionCreatePreorderSales() { $this->updatePreorderSalesAttributes(); $product = $this->getProduct(); $preorders = array(); foreach ($this->getPreorderSalesAttributes() as $a) { $_preorders = $preorders; $preorders = array(); foreach ($a->getAttributeValue($this->getProduct()) as $attributeValue) { $val = array(array($attributeValue, $a->getType())); if ($_preorders) { foreach ($_preorders as $v) { $preorders[] = array_merge($val, $v); } } else { $preorders[] = $val; } } } if ($preorders) { foreach ($preorders as $attributeValues) { $preorder = new \XLite\Module\Webkulsoftware\PreorderSales\Model\Preorders(); foreach ($attributeValues as $attributeValue) { $method = 'addAttributeValue' . $attributeValue[1]; $attributeValue = $attributeValue[0]; $preorder->$method($attributeValue); $attributeValue->addPreorderSales($preorder); } $preorder->setProduct($product); $product->addPreorderSales($preorder); \XLite\Core\Database::getEM()->persist($preorder); } } \XLite\Core\Database::getEM()->flush(); $this->getProduct()->checkPreorderSales(); \XLite\Core\TopMessage::addInfo('PreorderSales have been created successfully'); } /** * Do discard preorders * * @return void */ protected function doActionDiscardPreorderSales() { if ($this->getProduct()->getPreorderSales()) { $repo = \XLite\Core\Database::getRepo('\XLite\Module\Webkulsoftware\PreorderSales\Model\Preorders'); foreach ($this->getProduct()->getPreorderSales() as $v) { $repo->delete($v, false); } \XLite\Core\Database::getEM()->flush(); \XLite\Core\TopMessage::addInfo('PreorderSales have been discarded successfully'); } } /** * Do update preorders * * @return void */ protected function doActionUpdatePreorderSales() { $list = new \XLite\Module\Webkulsoftware\PreorderSales\View\ItemsList\Model\Preorders; $list->processQuick(); $this->getProduct()->checkPreorderSales(); \XLite\Core\TopMessage::addInfo('PreorderSales have been updated successfully'); } /** * Do update preorders attributes * * @return void */ protected function doActionUpdatePreorderSalesAttributes() { $this->updatePreorderSalesAttributes(); } /** * Do action delete * * @return void */ protected function doActionDeletePreorderSales() { $select = \XLite\Core\Request::getInstance()->select; if ( $select && is_array($select) && $this->getProduct()->getPreorderSales() ) { $selectedKeys = array_keys($select); $repo = \XLite\Core\Database::getRepo('\XLite\Module\Webkulsoftware\PreorderSales\Model\Preorders'); foreach ($this->getProduct()->getPreorderSales() as $v) { if (in_array($v->getId(), $selectedKeys)) { $repo->delete($v, false); } } \XLite\Core\Database::getEM()->flush(); \XLite\Core\TopMessage::addInfo('PreorderSales have been successfully deleted'); } else { \XLite\Core\TopMessage::addWarning('Please select the products first'); } } }