156 lines
3.5 KiB
PHP

<?php
/**
* Description of AdminCartRuleProductPackController
*
* @company Antadis
*/
include_once(_PS_MODULE_DIR_.'antadis_productpack/models/ProductPack.php');
class AdminProductPackController extends ModuleAdminController {
public function __construct()
{
$this->bootstrap = true;
$this->table = 'productpack';
$this->identifier = 'id_productpack';
$this->className = 'ProductPack';
$this->explicitSelect = true;
$this->lang = false;
$this->context = Context::getContext();
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->_select .= 'a.`id_product_root`, pl.`name`, p.`reference` product_ref, count(pa.`id_product_associated`) nb_products';
$this->_join .= '
JOIN `'._DB_PREFIX_.'product` p ON (a.`id_product_root` = p.`id_product`)
JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.$this->context->language->id.')
LEFT JOIN `'._DB_PREFIX_.'productpack_association` pa ON (a.`id_productpack` = pa.`id_productpack`)';
$this->_group .= 'GROUP BY a.`id_product_root`, pl.`name`, product_ref';
parent::__construct();
}
public function init()
{
parent::init();
}
public function renderList()
{
$this->fields_list = array(
'id_productpack' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 20,
),
'name' => array(
'title' => $this->l('Product pack'),
'width' => 40,
),
'product_ref' => array(
'title' => $this->l('Ref'),
'width' => 40,
),
'nb_products' => array(
'title' => $this->l('Nb products in pack'),
'width' => 40,
),
);
return parent::renderList();
}
public function processDelete()
{
$object = $this->loadObject();
if (Validate::isLoadedObject($object)) {
$object->deleteAssociations();
}
return parent::processDelete();
}
public function postProcess()
{
if ($this->display == 'edit' || $this->display == 'add')
{
$this->addjQueryPlugin(array(
'autocomplete',
'tablednd'
));
$this->addJS(array('/modules/antadis_productpack/js/antadis_productpack.js',));
}
parent::postProcess();
}
public function renderForm()
{
$this->display = 'edit';
$this->initToolbar();
$productpack = $this->object;
$this->fields_form = array(
'multilang'=> false,
'legend' => array(
'title' => $this->l('Product pack')
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('ID Product of the pack :'),
'name' => 'id_product_root',
'id' => 'id_product_root',
'size' => 40,
'lang' => false,
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
$this->context->smarty->assign('products', $productpack->getAssociations($this->context->language->id));
return parent::renderForm();
}
public function processSave()
{
$save = parent::processSave();
if(Validate::isLoadedObject($this->object)) {
$productpack = $this->object;
}
else{
$productpack = new ProductPack();
$productpack->id_product_root = Tools::getValue('id_product_root');
$productpack->add();
}
$this->updateProductAssociations($productpack);
return $save;
}
public function updateProductAssociations($productpack)
{
$productpack->deleteAssociations();
if ($product_associations = Tools::getValue('inputProducts'))
{
$product_id = array_unique(explode('-', $product_associations));
if (count($product_id))
{
array_pop($product_id);
$productpack->changeAssociations($product_id);
}
}
}
}