bebeboutik/modules/ant_creationcategories/AdminAntCreationcategories.php
Marion Muszynski f886f5c02e fix
2017-03-27 18:10:05 +02:00

455 lines
23 KiB
PHP

<?php
if(!defined('_PS_VERSION_')) {
exit;
}
include_once(_PS_ROOT_DIR_.'/modules/privatesales/Sale.php');
include_once(_PS_ROOT_DIR_.'/modules/categoryfamily/models/CategoryFamily.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperFormBootstrap.php');
require_once(PS_ADMIN_DIR . '/helpers/HelperList.php');
class AdminAntCreationcategories extends AdminTab
{
public function postProcess() {
global $cookie;
if(Tools::isSubmit('submitAddCategories')) {
if(Tools::getValue('category')) {
$groups = array();
foreach (Group::getGroups((int)($cookie->id_lang)) AS $row){
$groups[] = $row['id_group'];
}
$date = date('Y') . '-' . date('m');
$name = Tools::getValue('category');
$meta_name = 'Vente privée '.$name;
$category = new Category();
$category->id_parent = 1;
$category->active = 1;
$category->name[2] = $name;
$category->name[3] = $name;
$category->description[2] = $date;
$category->description[3] = $date;
$category->meta_title[2] = $meta_name;
$category->meta_title[3] = $meta_name;
$category->meta_keywords[2] = $meta_name;
$category->meta_keywords[3] = $meta_name;
$category->meta_description[2] = $meta_name;
$category->meta_description[3] = $meta_name;
$category->link_rewrite[2] = $this->str2url($name);
$category->link_rewrite[3] = $category->link_rewrite[2];
$category->groupBox = $groups;
if($category->add()) {
if (isset($_FILES['image_thumb']) && $_FILES['image_thumb']['name'] != NULL) {
copy($_FILES['image_thumb']['tmp_name'], _PS_CAT_IMG_DIR_.$category->id.'_thumb_vp.jpg');
}
if (isset($_FILES['image_category']) && $_FILES['image_category']['name'] != NULL) {
copy($_FILES['image_category']['tmp_name'], _PS_CAT_IMG_DIR_.$category->id.'.jpg');
if (file_exists(_PS_CAT_IMG_DIR_.$category->id.'.jpg')) {
$imagesTypes = ImageType::getImagesTypes('categories');
foreach ($imagesTypes AS $k => $imageType) {
imageResize(_PS_CAT_IMG_DIR_.$category->id.'.jpg', _PS_CAT_IMG_DIR_.$category->id.'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']));
}
}
}
if(Tools::getValue('subcategories')) {
$subcategories = Tools::getValue('subcategories');
$subcategories = explode(';',$subcategories);
if(Tools::getValue('families')) {
$families = Tools::getValue('families');
if(Tools::getValue('subfamilies')) {
$subfamilies = Tools::getValue('subfamilies');
}
}
foreach ($subcategories as $key => $name) {
$meta_name = 'Vente privée '.$name;
$subcategory = new Category();
$subcategory->id_parent = $category->id;
$subcategory->active = 1;
$subcategory->name[2] = $name;
$subcategory->name[3] = $name;
$subcategory->description[2] = $date;
$subcategory->description[3] = $date;
$subcategory->meta_title[2] = $meta_name;
$subcategory->meta_title[3] = $meta_name;
$subcategory->meta_keywords[2] = $meta_name;
$subcategory->meta_keywords[3] = $meta_name;
$subcategory->meta_description[2] = $meta_name;
$subcategory->meta_description[3] = $meta_name;
$subcategory->link_rewrite[2] = $this->str2url($name);
$subcategory->link_rewrite[3] = $subcategory->link_rewrite[2];
$subcategory->groupBox = $groups;
$subcategory->add();
if(isset($families) && isset($families[$key]) && (int)$families[$key] != 0) {
if(isset($subfamilies) && isset($subfamilies[$key]) && (int)$subfamilies[$key] != 0) {
CategoryFamilyCore::addAssociations2($subcategory->id, $subfamilies[$key]);
} else {
CategoryFamilyCore::addAssociations2($subcategory->id, $families[$key]);
}
}
}
}
}
Category::regenerateEntireNtree();
HelperFormBootstrap::echoConfirmation('Catégories créées');
}
} elseif(Tools::isSubmit('submitAddSubCategories')) {
if(Tools::getValue('id_parent')) {
$groups = array();
foreach (Group::getGroups((int)($cookie->id_lang)) AS $row){
$groups[] = $row['id_group'];
}
$id_parent = (int) Tools::getValue('id_parent');
if(Tools::getValue('category_child')) {
$id_parent = (int) Tools::getValue('category_child');
}
$date = date('Y') . '-' . date('m');
if(Tools::getValue('subcategories2')) {
$subcategories = Tools::getValue('subcategories2');
$subcategories = explode(';',$subcategories);
if(Tools::getValue('families')) {
$families = Tools::getValue('families');
if(Tools::getValue('subfamilies')) {
$subfamilies = Tools::getValue('subfamilies');
}
}
foreach ($subcategories as $key => $name) {
$meta_name = 'Vente privée '.$name;
$subcategory = new Category();
$subcategory->id_parent = (int)$id_parent;
$subcategory->active = 1;
$subcategory->name[2] = $name;
$subcategory->name[3] = $name;
$subcategory->description[2] = $date;
$subcategory->description[3] = $date;
$subcategory->meta_title[2] = $meta_name;
$subcategory->meta_title[3] = $meta_name;
$subcategory->meta_keywords[2] = $meta_name;
$subcategory->meta_keywords[3] = $meta_name;
$subcategory->meta_description[2] = $meta_name;
$subcategory->meta_description[3] = $meta_name;
$subcategory->link_rewrite[2] = $this->str2url($name);
$subcategory->link_rewrite[3] = $subcategory->link_rewrite[2];
$subcategory->groupBox = $groups;
$subcategory->add();
if(isset($families) && isset($families[$key]) && (int)$families[$key] != 0) {
if(isset($subfamilies) && isset($subfamilies[$key]) && (int)$subfamilies[$key] != 0) {
CategoryFamilyCore::addAssociations2($subcategory->id, $subfamilies[$key]);
} else {
CategoryFamilyCore::addAssociations2($subcategory->id, $families[$key]);
}
}
}
Category::regenerateEntireNtree();
HelperFormBootstrap::echoConfirmation('Sous catégories créées');
} else {
HelperFormBootstrap::echoError('Vous devez renseigner au moins une sous catégorie');
}
} else {
HelperFormBootstrap::echoError('Vous devez selectionner une catégorie parente');
}
}
}
public function display() {
global $cookie, $currentIndex;
$form = '';
$base_link = $currentIndex . '&token='.Tools::getAdminTokenLite('AdminAntCreationcategories');
$helperForm = new HelperFormBootstrap();
$helperForm->_inputTag = true;
$helperForm->_select2 = true;
$helperForm->_inputSwitch = true;
$id_categories = array();
foreach(Db::getInstance()->ExecuteS('
SELECT c.`id_category`, cl.`name`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category`)
WHERE cl.`id_lang` = '.$cookie->id_lang.'
AND c.active = 1
AND c.id_parent = 1
ORDER BY c.`id_category` DESC
') as $row) {
$id_categories[] = array(
'label' => '#'.$row['id_category'].' - '.$row['name'],
'value' => (int) $row['id_category']
);
}
$helperForm->_forms = array(
array(
'action' => $base_link,
'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(
array(
'inputs' => array(
array(
'type' => 'simpleText',
'label' => $this->l('Catégorie : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'category',
),
array(
'type' => 'uploadImage',
'label' => $this->l('Logo : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'image_thumb',
'id' => 'preview_thumb',
),
array(
'type' => 'uploadImage',
'label' => $this->l('Image Latérale : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'image_category',
'id' => 'preview_category',
),
array(
'type' => 'tag',
'label' => $this->l('Sous Catégorie : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'subcategories',
'id_tag' => 'tag_subcategories',
'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' => ' ',
),
),
),
),
'actions' => array(
array(
'type' => 'submit',
'class' => 'btn-primary',
'name' => 'submitAddCategories',
'value' => $this->l('Ajouter les catégories')
)
),
'actions-class' => 'text-right',
),
array(
'action' => $base_link,
'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(
array(
'inputs' => array(
array(
'type' => 'select2',
'label' => $this->l('Catégorie Parente'),
'select-class' => 'col-md-6',
'label-class' => 'col-md-2',
'options' => $id_categories,
'name' => 'id_parent',
),
array(
'type' => 'tag',
'label' => $this->l('Sous Catégorie : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'subcategories2',
'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' => ' ',
),
),
),
),
'actions' => array(
array(
'type' => 'submit',
'class' => 'btn-primary',
'name' => 'submitAddSubCategories',
'value' => $this->l('Ajouter les sous catégories')
)
),
'actions-class' => 'text-right',
)
);
$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) {
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);
}
}
function changeSelectParent(){
var id_parent = $(\'#id_parent\').val();
if(id_parent != 0) {
$.ajax({
url : \'/modules/ant_creationcategories/ajax.php\',
type : \'POST\',
data : \'id_parent=\'+id_parent,
dataType : \'json\',
success : function(json, statut){
if (json.errors == false) {
if($(\'#id_parent\').parent().children(".cat-children").length > 0) {
$(\'#id_parent\').parent().children(".cat-children").remove();
}
$(\'#id_parent\').parent().append(json.data);
}
}
});
}
}
$(document).ready(function() {
$(\'#subcategories\').tagsInput({
width: \'auto\',
defaultText:\'Nom\',
delimiter: [\';\']
});
$(\'#subcategories .tag a\').click(function(event) {
$(\'#div-families\').empty();
});
$(\'#subcategories2\').tagsInput({
width: \'auto\',
defaultText:\'Nom\',
delimiter: [\';\']
});
changeSelectParent();
$(\'#id_parent\').change(function(){
changeSelectParent();
})
$(\'#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();
$form .= '<div class="row">'.$helperForm->renderForm(false, NULL, NULL, true).'</div>';
$form .= $helperForm->renderScript();
echo $form;
}
public function str2url($string, $slug = '-', $extra = null)
{
return strtolower(trim(preg_replace('~[^0-9a-z' . preg_quote($extra, '~') . ']+~i', $slug, $this->unaccent($string)), $slug));
}
public function unaccent($string) // normalizes (romanization) accented chars
{
if (strpos($string = htmlentities($string, ENT_QUOTES, 'UTF-8'), '&') !== false) {
$string = html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|tilde|uml);~i', '$1', $string), ENT_QUOTES, 'UTF-8');
}
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;
}
}