2016-12-15 17:30:05 +01:00
< ? php
if ( ! defined ( '_PS_VERSION_' )) {
exit ;
}
include_once ( _PS_ROOT_DIR_ . '/modules/privatesales/Sale.php' );
2016-12-28 14:22:31 +01:00
include_once ( _PS_ROOT_DIR_ . '/modules/categoryfamily/models/CategoryFamily.php' );
2016-12-15 17:30:05 +01:00
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' )) {
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 );
2016-12-19 18:32:06 +01:00
if ( Tools :: getValue ( 'families' )) {
2016-12-19 17:23:37 +01:00
$families = Tools :: getValue ( 'families' );
2016-12-19 18:32:06 +01:00
if ( Tools :: getValue ( 'subfamilies' )) {
$subfamilies = Tools :: getValue ( 'subfamilies' );
}
2016-12-19 17:23:37 +01:00
}
2016-12-15 17:30:05 +01:00
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 ();
2016-12-19 17:23:37 +01:00
2016-12-19 18:32:06 +01:00
if ( isset ( $families ) && isset ( $families [ $key ]) && ( int ) $families [ $key ] != 0 ) {
2016-12-21 14:31:41 +01:00
if ( isset ( $subfamilies ) && isset ( $subfamilies [ $key ]) && ( int ) $subfamilies [ $key ] != 0 ) {
CategoryFamilyCore :: addAssociations2 ( $subcategory -> id , $subfamilies [ $key ]);
2016-12-19 18:32:06 +01:00
} else {
CategoryFamilyCore :: addAssociations2 ( $subcategory -> id , $families [ $key ]);
}
2016-12-19 17:23:37 +01:00
}
2016-12-19 18:32:06 +01:00
2016-12-15 17:30:05 +01:00
}
}
}
Category :: regenerateEntireNtree ();
HelperFormBootstrap :: echoConfirmation ( 'Catégories créées' );
}
} elseif ( Tools :: isSubmit ( 'submitAddSubCategories' )) {
if ( Tools :: getValue ( 'id_parent' )) {
$id_parent = ( int ) Tools :: getValue ( 'id_parent' );
2016-12-21 14:31:41 +01:00
$date = date ( 'Y' ) . '-' . date ( 'm' );
2016-12-15 17:30:05 +01:00
if ( Tools :: getValue ( 'subcategories2' )) {
$subcategories = Tools :: getValue ( 'subcategories2' );
$subcategories = explode ( ',' , $subcategories );
2016-12-19 18:32:06 +01:00
if ( Tools :: getValue ( 'families' )) {
2016-12-19 17:23:37 +01:00
$families = Tools :: getValue ( 'families' );
2016-12-19 18:32:06 +01:00
if ( Tools :: getValue ( 'subfamilies' )) {
$subfamilies = Tools :: getValue ( 'subfamilies' );
}
2016-12-19 17:23:37 +01:00
}
2016-12-15 17:30:05 +01:00
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 ();
2016-12-19 17:23:37 +01:00
2016-12-19 18:32:06 +01:00
if ( isset ( $families ) && isset ( $families [ $key ]) && ( int ) $families [ $key ] != 0 ) {
2016-12-21 14:31:41 +01:00
if ( isset ( $subfamilies ) && isset ( $subfamilies [ $key ]) && ( int ) $subfamilies [ $key ] != 0 ) {
CategoryFamilyCore :: addAssociations2 ( $subcategory -> id , $subfamilies [ $key ]);
2016-12-19 18:32:06 +01:00
} else {
CategoryFamilyCore :: addAssociations2 ( $subcategory -> id , $families [ $key ]);
}
2016-12-19 17:23:37 +01:00
}
2016-12-15 17:30:05 +01:00
}
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
2016-12-21 14:31:41 +01:00
AND c . id_parent = 1
2016-12-15 17:30:05 +01:00
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' ,
2016-12-19 17:23:37 +01:00
'id' => 'form1' ,
2016-12-15 17:30:05 +01:00
'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' ,
2016-12-19 17:23:37 +01:00
'class-group' => 'switch' ,
2016-12-19 11:42:37 +01:00
'name' => 'family' ,
2016-12-19 17:23:37 +01:00
'title' => ' ' ,
2016-12-19 11:42:37 +01:00
),
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' ,
2016-12-19 17:23:37 +01:00
'id' => 'form2' ,
2016-12-15 17:30:05 +01:00
'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
),
2016-12-19 17:23:37 +01:00
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' => ' ' ,
),
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 17:23: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-19 17:23:37 +01:00
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 );
}
}
$ ( document ) . ready ( function () {
2016-12-19 11:42:37 +01:00
$ ( \ ' #subcategories\').tagsInput({
width : \ ' auto\ ' ,
defaultText : \ ' Nom\ ' ,
});
2016-12-19 17:23:37 +01:00
$ ( \ ' #subcategories .tag a\').click(function(event) {
$ ( \ ' #div-families\').empty();
});
2016-12-19 11:42:37 +01:00
$ ( \ ' #subcategories2\').tagsInput({
width : \ ' auto\ ' ,
defaultText : \ ' Nom\ '
});
2016-12-19 17:23:37 +01:00
$ ( \ ' #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(\'\');
}
}
});
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-19 17:23:37 +01:00
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 ;
}
2016-12-15 17:30:05 +01:00
}