addition of section of family catégory

This commit is contained in:
Marion Muszynski 2016-12-19 17:23:37 +01:00
parent 6bc6d54b50
commit d0db786f0b
5 changed files with 188 additions and 22 deletions

View File

@ -107,7 +107,7 @@ class HelperFormBootstrap{
<div class="clearfix"></div>
</div>
<div class="panel-content">
<form action="'.$action.'" method="'.$method.'" enctype="'.$enctype.'" class="'.$class.'" style="'.$css.'">';
<form action="'.$action.'" method="'.$method.'" enctype="'.$enctype.'" class="'.$class.'" id="'.$id.'" style="'.$css.'">';
if(isset($form['information'])) {
$this->_html .= '<p>'.$form['information'].'</p>';
}
@ -389,7 +389,7 @@ class HelperFormBootstrap{
public function inputSwitch($p = array()) {
$this->_html .='
<div class="form-group">
<div class="form-group '.(isset($p['class-group'])?$p['class-group']:'').'">
'.(isset($p['label'])?'<label class="control-label '.(isset($p['label-class']) ? $p['label-class'] : '').'">'.$p['label'].'</label>':'').'
<div class="'.(isset($p['input-class']) ? $p['input-class'] : '').'">
<div class="checkbox input-switch">
@ -398,16 +398,7 @@ class HelperFormBootstrap{
</label>
</div>
</div>
</div>
<script>
$(document).ready(function() {
var elems = Array.prototype.slice.call(document.querySelectorAll(\'.js-switch\'));
elems.forEach(function(html) {
var switchery = new Switchery(html, { size: \'small\', color: \'#504d8b\' });
});
});
</script>';
</div>';
}
public function inputText($p = array()){

View File

@ -54,4 +54,10 @@ $(function() {
}
reader.readAsDataURL(file);
});
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function(html) {
var switchery = new Switchery(html, { size: 'small', color: '#504d8b' });
});
});

View File

@ -5,7 +5,7 @@ if(!defined('_PS_VERSION_')) {
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperList.php');
//require_once '..'._MODULE_DIR_.'categoryfamily/models/CategoryFamily.php';
require_once(_PS_ROOT_DIR_.'/modules/categoryfamily/models/CategoryFamily.php');
class AdminAntCreationcategories extends AdminTab
{
@ -47,6 +47,10 @@ class AdminAntCreationcategories extends AdminTab
if(Tools::getValue('subcategories')) {
$subcategories = Tools::getValue('subcategories');
$subcategories = explode(',',$subcategories);
if(Tools::getValue('subfamilies') && Tools::getValue('families')) {
$families = Tools::getValue('families');
$subfamilies = Tools::getValue('subfamilies');
}
foreach ($subcategories as $key => $name) {
$meta_name = 'Vente privée '.$name;
$subcategory = new Category();
@ -66,6 +70,12 @@ class AdminAntCreationcategories extends AdminTab
$subcategory->link_rewrite[3] = $subcategory->link_rewrite[2];
$subcategory->groupBox = $groups;
$subcategory->add();
if(isset($subfamilies) && isset($subfamilies[$key]))){
CategoryFamilyCore::addAssociations2($subcategory->id, $subfamilies[$key]);
} elseif (isset($families) && isset($families[$key]))){
CategoryFamilyCore::addAssociations2($subcategory->id, $families[$key]);
}
}
}
}
@ -78,6 +88,10 @@ class AdminAntCreationcategories extends AdminTab
if(Tools::getValue('subcategories2')) {
$subcategories = Tools::getValue('subcategories2');
$subcategories = explode(',',$subcategories);
if(Tools::getValue('subfamilies') && Tools::getValue('families')) {
$families = Tools::getValue('families');
$subfamilies = Tools::getValue('subfamilies');
}
foreach ($subcategories as $key => $name) {
$meta_name = 'Vente privée '.$name;
$subcategory = new Category();
@ -97,6 +111,12 @@ class AdminAntCreationcategories extends AdminTab
$subcategory->link_rewrite[3] = $subcategory->link_rewrite[2];
$subcategory->groupBox = $groups;
$subcategory->add();
if(isset($subfamilies) && isset($subfamilies[$key])){
CategoryFamilyCore::addAssociations2($subcategory->id, $subfamilies[$key]);
} elseif (isset($families) && isset($families[$key]))){
CategoryFamilyCore::addAssociations2($subcategory->id, $families[$key]);
}
}
Category::regenerateEntireNtree();
HelperFormBootstrap::echoConfirmation('Sous catégories créées');
@ -140,6 +160,7 @@ class AdminAntCreationcategories extends AdminTab
'title' => $this->l('Creation Catégories'),
'icon' => '<span class="glyphicon glyphicon-list"></span> ',
'class' => 'form-horizontal',
'id' => 'form1',
'class_div' => 'col-md-12',
'information' => 'Création de catégories multiples',
'sections' => array(
@ -173,8 +194,9 @@ class AdminAntCreationcategories extends AdminTab
'label' => $this->l('Ajouter les familles'),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'class-group' => 'switch',
'name' => 'family',
'title' => 'Activer',
'title' => ' ',
),
),
),
@ -194,6 +216,7 @@ class AdminAntCreationcategories extends AdminTab
'title' => $this->l('Creation Sous Catégories'),
'icon' => '<span class="glyphicon glyphicon-list"></span> ',
'class' => 'form-horizontal',
'id' => 'form2',
'class_div' => 'col-md-12',
'information' => 'Création de sous-catégories ayant un parent existant',
'sections' => array(
@ -216,6 +239,15 @@ class AdminAntCreationcategories extends AdminTab
'id_tag' => 'tag_subcategories2',
'info' => 'Renseigner un nom et ajouter en appuyant sur Entrer<br>Ajouter autant de nom que de catégories souhaitées'
),
array(
'type' => 'switch',
'label' => $this->l('Ajouter les familles'),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'class-group' => 'switch',
'name' => 'family',
'title' => ' ',
),
),
),
),
@ -231,25 +263,103 @@ class AdminAntCreationcategories extends AdminTab
)
);
// $families = array();
// foreach(CategoryFamilyCore::getParentFamily($cookie->id_lang) as $row) {
// $families[] = array(
// 'label' => $row['name'],
// 'value' => (int) $row['id_category_family']
// );
// }
$families = array();
foreach(CategoryFamilyCore::getParentFamily($cookie->id_lang) as $row) {
$families[] = array(
'label' => $row['name'],
'value' => (int) $row['id_category_family']
);
}
$helperForm->_js .='
<script>
function resetFilter(obj) {
obj.html(\'\');
}
function changeSelect(obj) {
var value = obj.val();
var id_select = obj.data(\'id-family\');
if (value != 0) {
$.ajax({
url : \'/modules/categoryfamily/ajax_2.php\',
type : \'POST\',
data : \'id_select=\'+id_select+\'&id_parent=\'+value,
dataType : \'json\',
success : function(json, statut){
if (json.errors == false) {
console.log(obj.next(".sub-cat").length);
if(obj.next(".sub-cat").length == 1) {
obj.next(".sub-cat").remove();
}
obj.after(json.data);
}
}
});
} else {
resetFilter(obj);
}
}
function refresh(obj,form,id){
obj.html(\'<a onclick="refresh(\'+obj+\',\'+form+\',\'+id+\');" style="margin-top:10px;" class="col-md-12" id="a-refresh\'+ id +\'"><i class="glyphicon glyphicon-refresh"></i> Refresh</a>\');
var nb_elements = $(\'#\'+form+\' div.tagsinput span.tag\').length;
for (i=0; i < nb_elements; i++) {
var html = \'<div class="form-group col-md-4"><h4>\'+ $(\'#\'+form+\' div.tagsinput span.tag:eq(\'+i+\')\').text().substring(0,$(\'#\'+form+\' div.tagsinput span.tag:eq(\'+i+\')\').text().length - 1) +\'</h4>'.$this->generateSelectFamilies($families).'</div>\';
obj.append(html);
}
}
$(document).ready(function() {
$(\'#subcategories\').tagsInput({
width: \'auto\',
defaultText:\'Nom\',
});
$(\'#subcategories .tag a\').click(function(event) {
$(\'#div-families\').empty();
});
$(\'#subcategories2\').tagsInput({
width: \'auto\',
defaultText:\'Nom\'
});
$(\'#form1 .js-switch\').change(function(){
if($(this).is(\':checked\')) {
var nb_elements = $(\'#form1 div.tagsinput span.tag\').length;
if($(\'#div-families\').length == 0) {
$(\'#form1 .form-group.switch\').append(\'<div style="margin-top:10px;" class="col-md-12" id="div-families"></div>\');
}
if($(\'#a-refresh1\').length == 0) {
$(\'#div-families\').append(\'<a onclick="refresh($("#div-families"),"form1",1);" style="margin-top:10px;" class="col-md-12" id="a-refresh1"><i class="glyphicon glyphicon-refresh"></i> Refresh</a>\');
}
if($(\'#div-families .form-group\').length == 0) {
for (i=0; i < nb_elements; i++) {
var html = \'<div class="form-group col-md-4"><h4>\'+ $(\'#form1 div.tagsinput span.tag:eq(\'+i+\')\').text().substring(0,$(\'#form1 div.tagsinput span.tag:eq(\'+i+\')\').text().length - 1) +\'</h4>'.$this->generateSelectFamilies($families).'</div>\';
$(\'#div-families\').append(html);
}
} else {
$(\'#div-families\').html(\'\');
}
}
});
$(\'#form2 .js-switch\').change(function(){
if($(this).is(\':checked\')) {
var nb_elements = $(\'#form2 div.tagsinput span.tag\').length;
if($(\'#div-families2\').length == 0) {
$(\'#form2 .form-group.switch\').append(\'<div style="margin-top:10px;" class="col-md-12" id="div-families2"></div>\');
}
if($(\'#a-refresh2\').length == 0) {
$(\'#div-families2\').append(\'<a onclick="refresh($("#div-families2"),"form2",2);" style="margin-top:10px;" class="col-md-12" id="a-refresh2"><i class="glyphicon glyphicon-refresh"></i> Refresh</a>\');
}
if($(\'#div-families2 .form-group\').length == 0) {
for (i=0; i < nb_elements; i++) {
var html = \'<div class="form-group col-md-4"><h4>\'+ $(\'#form2 div.tagsinput span.tag:eq(\'+i+\')\').text().substring(0,$(\'#form2 div.tagsinput span.tag:eq(\'+i+\')\').text().length - 1) +\'</h4>'.$this->generateSelectFamilies($families).'</div>\';
$(\'#div-families2\').append(html);
}
} else {
$(\'#div-families2\').html(\'\');
}
}
});
});
</script>';
$form .= $helperForm->renderStyle();
@ -272,4 +382,13 @@ class AdminAntCreationcategories extends AdminTab
return $string;
}
public function generateSelectFamilies($families){
$selectFamily = '<div class=""><select onchange="changeSelect($(this));" class="form-control select-families" data-id-family="1" name="families[]" tabindex="-1">';
foreach($families as $opt) {
$selectFamily .= '<option value="'.$opt['value'].'">'.$opt['label'].'</option>';
}
$selectFamily .='</select></div>';
return $selectFamily;
}
}

View File

@ -0,0 +1,32 @@
<?php
include_once('../../config/config.inc.php');
include_once(dirname(__FILE__).'/models/CategoryFamily.php');
$data = array();
$data['errors'] = FALSE;
$id_parent = Tools::getValue('id_parent', 1);
$id_select = Tools::getValue('id_select', 1);;
$id_lang = 2;
$select_upd = (int)$id_select + 1;
$childrens = CategoryFamilyCore::getAll($id_lang, $id_parent);
if ($childrens) {
$select='<div class="sub-cat" style="margin-bottom:10px;margin-top:10px;">
<select class="form-control select-families" name="subfamilies[]" data-id-family="'.$select_upd.'">
<option value="0">-- Choisir -- </option>';
foreach ($childrens as $key => $children) {
$select.='<option value="'.$children['id_category_family'].'">'.$children['name'].'</option>';
}
$select.= '</select>
</div>';
} else {
$select = '';
}
$data['data'] = $select;
$data['id_before'] = (int)$id_select;
$data['id_select'] = (int)$select_upd;
die(json_encode($data));

View File

@ -133,6 +133,24 @@ class CategoryFamilyCore extends ObjectModel {
return TRUE;
}
public static function addAssociations2($id_category, $family) {
self::deleteAssociations($id_category);
if (isset($family)) {
Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'category_family_association`
(`id_category_family`, `id_category`)
VALUES (
'.(int)$family.',
'.(int)$id_category.'
)
');
}
return true;
}
public static function loadAssociations($id_category, $id_lang) {
global $cookie, $smarty;
$category_family = array();