bebeboutik/modules/ant_creationcategories/AdminAntCreationcategories.php

275 lines
13 KiB
PHP
Raw Normal View History

2016-12-15 17:30:05 +01:00
<?php
if(!defined('_PS_VERSION_')) {
exit;
}
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');
2016-12-19 11:42:37 +01:00
//require_once '..'._MODULE_DIR_.'categoryfamily/models/CategoryFamily.php';
2016-12-15 17:30:05 +01:00
class AdminAntCreationcategories extends AdminTab
{
public function postProcess() {
global $cookie;
if(Tools::isSubmit('submitAddCategories')) {
if(Tools::getValue('category')) {
2016-12-16 15:59:35 +01:00
$groups = array();
foreach (Group::getGroups((int)($cookie->id_lang)) AS $row){
$groups[] = $row['id_group'];
}
2016-12-15 17:30:05 +01:00
$date = date('Y') . '-' . date('m');
$name = Tools::getValue('category');
2016-12-16 15:59:35 +01:00
$meta_name = 'Vente privée '.$name;
2016-12-15 17:30:05 +01:00
$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;
2016-12-16 15:59:35 +01:00
$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;
2016-12-15 17:30:05 +01:00
if($category->add()) {
2016-12-16 15:59:35 +01:00
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');
}
2016-12-15 17:30:05 +01:00
if(Tools::getValue('subcategories')) {
$subcategories = Tools::getValue('subcategories');
$subcategories = explode(',',$subcategories);
foreach ($subcategories as $key => $name) {
2016-12-16 15:59:35 +01:00
$meta_name = 'Vente privée '.$name;
2016-12-15 17:30:05 +01:00
$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;
2016-12-16 15:59:35 +01:00
$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;
2016-12-15 17:30:05 +01:00
$subcategory->add();
}
}
}
Category::regenerateEntireNtree();
HelperFormBootstrap::echoConfirmation('Catégories créées');
}
} elseif(Tools::isSubmit('submitAddSubCategories')) {
if(Tools::getValue('id_parent')) {
$id_parent = (int) Tools::getValue('id_parent');
if(Tools::getValue('subcategories2')) {
$subcategories = Tools::getValue('subcategories2');
$subcategories = explode(',',$subcategories);
foreach ($subcategories as $key => $name) {
2016-12-16 15:59:35 +01:00
$meta_name = 'Vente privée '.$name;
2016-12-15 17:30:05 +01:00
$subcategory = new Category();
2016-12-16 15:59:35 +01:00
$subcategory->id_parent = (int)$id_parent;
2016-12-15 17:30:05 +01:00
$subcategory->active = 1;
$subcategory->name[2] = $name;
$subcategory->name[3] = $name;
$subcategory->description[2] = $date;
$subcategory->description[3] = $date;
2016-12-16 15:59:35 +01:00
$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;
2016-12-15 17:30:05 +01:00
$subcategory->add();
}
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();
2016-12-19 11:42:37 +01:00
$helperForm->_inputTag = true;
$helperForm->_select2 = true;
$helperForm->_inputSwitch = true;
2016-12-15 17:30:05 +01:00
$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
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',
'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',
),
2016-12-16 15:59:35 +01:00
array(
'type' => 'uploadImage',
'label' => $this->l('Logo : '),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'image_thumb',
),
2016-12-15 17:30:05 +01:00
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',
2016-12-15 17:48:34 +01:00
'info' => 'Renseigner un nom et ajouter en appuyant sur Entrer<br>Ajouter autant de nom que de catégories souhaitées'
2016-12-15 17:30:05 +01:00
),
2016-12-19 11:42:37 +01:00
array(
'type' => 'switch',
'label' => $this->l('Ajouter les familles'),
'label-class' => 'col-md-2',
'input-class' => 'col-md-6',
'name' => 'family',
'title' => 'Activer',
),
2016-12-15 17:30:05 +01:00
),
),
),
'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',
'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',
2016-12-15 17:48:34 +01:00
'info' => 'Renseigner un nom et ajouter en appuyant sur Entrer<br>Ajouter autant de nom que de catégories souhaitées'
2016-12-15 17:30:05 +01:00
),
),
),
),
'actions' => array(
array(
'type' => 'submit',
'class' => 'btn-primary',
'name' => 'submitAddSubCategories',
'value' => $this->l('Ajouter les sous catégories')
)
),
'actions-class' => 'text-right',
)
);
2016-12-19 11:42:37 +01:00
// $families = array();
// foreach(CategoryFamilyCore::getParentFamily($cookie->id_lang) as $row) {
// $families[] = array(
// 'label' => $row['name'],
// 'value' => (int) $row['id_category_family']
// );
// }
2016-12-15 17:30:05 +01:00
2016-12-17 20:12:09 +01:00
$helperForm->_js .='
<script>
2016-12-15 17:30:05 +01:00
$(document).ready(function() {
2016-12-19 11:42:37 +01:00
$(\'#subcategories\').tagsInput({
width: \'auto\',
defaultText:\'Nom\',
});
$(\'#subcategories2\').tagsInput({
width: \'auto\',
defaultText:\'Nom\'
});
2016-12-15 17:30:05 +01:00
});
</script>';
$form .= $helperForm->renderStyle();
$form .= '<div class="row">'.$helperForm->renderForm(false, NULL, NULL, true).'</div>';
$form .= $helperForm->renderScript();
echo $form;
}
2016-12-16 15:59:35 +01:00
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;
}
2016-12-15 17:30:05 +01:00
}