<?php

/* -----------------------------------------------------------------
 * 	$Id: blog_categories_edit.php 943 2014-04-08 13:26:37Z akausch $
 * 	Copyright (c) 2011-2021 commerce:SEO by Webdesign Erfurt
 * Copyright by H&S eCom 
 * @author little Pit(S.B.)
 * 	http://www.commerce-seo.de : www.indiv-style.de
 * ------------------------------------------------------------------
 * 	based on:
 * 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
 * 	(c) 2002-2003 osCommerce - www.oscommerce.com
 * 	(c) 2003     nextcommerce - www.nextcommerce.org
 * 	(c) 2005     xt:Commerce - www.xt-commerce.com
 * 	Released under the GNU General Public License
 * --------------------------------------------------------------- */

defined("_VALID_XTC") or die("Direct access to this location isn't allowed.");

if (!column_exists (TABLE_BLOG_CATEGORIES,'itemsort')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_CATEGORIES . " ADD COLUMN itemsort int(11) NOT NULL DEFAULT '1' AFTER sort_order;");
}
if (!column_exists (TABLE_BLOG_CATEGORIES,'itemsortart')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_CATEGORIES . " ADD COLUMN itemsortart int(11) NOT NULL DEFAULT '1' AFTER sort_order;");
}
if (!column_exists (TABLE_BLOG_CATEGORIES,'slider_set')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_CATEGORIES . " ADD COLUMN slider_set int(11) NOT NULL DEFAULT '0';");
}
if (!column_exists (TABLE_BLOG_CATEGORIES,'canonical')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_CATEGORIES . " ADD COLUMN canonical varchar(250) NOT NULL DEFAULT '';");
}

$module_smarty = new Smarty;
$module_smarty->assign('txt', $coo_text_mgr->v_section_content_array['blog']);
$module_smarty->assign('FORM', xtc_draw_form('categorie', FILENAME_BLOG, 'action=save_category', 'post', 'enctype="multipart/form-data"'));

$select_categories = array();
$categories_query = xtc_db_query("SELECT categories_id, parent_id, titel FROM " . TABLE_BLOG_CATEGORIES . " WHERE categories_id != '" . (int) $_GET['cat'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "' ORDER BY position ASC");
$select_categories[] = array('id' => 0, 'text' => TABLE_FOOTER_STATUS_0);
while ($categories = xtc_db_fetch_array($categories_query)) {
    $select_categories[] = array('id' => $categories['categories_id'], 'text' => $categories['titel'], 'parent' => $categories['parent_id']);
}

$languages = xtc_get_languages();

for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
    $edit_categorie = xtc_db_fetch_array(xtc_db_query("SELECT *
											FROM " . TABLE_BLOG_CATEGORIES . "
											WHERE categories_id = '" . (int) $_GET['cat'] . "'
											AND language_id = '" . (int) $languages[$i]['id'] . "'"));
	//Slider
	$slider_set_sql = xtc_db_query("SELECT slider_id AS id, slider_title AS text FROM cseo_slider_gallery where language_id = '" . (int) $languages[$i]['id'] . "' GROUP BY slider_title");
	$slider_set_array = array(array('id' => 0, 'text' => TEXT_SELECT));
	while ($slider_set = xtc_db_fetch_array($slider_set_sql)) {
		 $slider_set_array[] = array('id' => $slider_set['id'], 'text' => $slider_set['text']);
	}
	$slider_set = xtc_draw_pull_down_menu('slider_set[' . $languages[$i]['id'] . ']', $slider_set_array, $edit_categorie['slider_set'], 'class="btn btn-default" style="width:400px;"');
    
    $lang_images = xtc_image(DIR_WS_LANGUAGES . $languages[$i]['directory'] . '/' . $languages[$i]['image'], $languages[$i]['name']);
    $fieldname = xtc_draw_input_field('titel[' . $languages[$i]['id'] . ']', $edit_categorie['titel'], 'class="inputmeta"');
    $fieldposition = xtc_draw_small_input_field('position[' . $languages[$i]['id'] . ']', $edit_categorie['position'], 'class="inputmeta"');
    $field_mtitle = xtc_draw_input_field('meta_title[' . $languages[$i]['id'] . ']', $edit_categorie['meta_title'], 'class="inputmeta"');
    $field_mdesc = xtc_draw_input_field('meta_desc[' . $languages[$i]['id'] . ']', $edit_categorie['meta_desc'], 'class="inputmeta"');
    $canonical = xtc_draw_input_field('canonical[' . $languages[$i]['id'] . ']', $edit_categorie['canonical'], 'class="inputmeta"');
    $field_mkeys = xtc_draw_input_field('meta_key[' . $languages[$i]['id'] . ']', $edit_categorie['meta_key'], 'class="inputmeta"');
    $field_sdesc = xtc_draw_textarea_field('short_description_' . $languages[$i]['id'], '', '100', '50', $edit_categorie['short_description'], 'class="ckeditor" name="editor1"');
    $field_ldesc = xtc_draw_textarea_field('description_' . $languages[$i]['id'], '', '100', '50', $edit_categorie['description'], 'class="ckeditor" name="editor1"');

    $catedit[$i] = array(
        'langid' => $languages[$i]['id'],
		'field_slider' => $slider_set,
        'langname' => $languages[$i]['name'],
        'lang_images' => $lang_images,
        'fieldname' => $fieldname,
        'fieldposition' => $fieldposition,
        'canonical' => $canonical,
        'field_mtitle' => $field_mtitle,
        'field_mdesc' => $field_mdesc,
        'field_mkeys' => $field_mkeys,
        'field_sdesc' => $field_sdesc,
        'field_ldesc' => $field_ldesc
    );
}

if (USE_WYSIWYG == 'true') {
    $field_sdesc_wy = '1';
} else {
    $field_sdesc_wy = '0';
}
$module_smarty->assign('field_sdesc_wy', $field_sdesc_wy);

if ($_GET['cat'] != '') {
    $myimages = xtc_get_blogcat_mo_images($_GET['cat']);
}
for ($i = 0; $i < PICCAT; $i++) {
    $Bildnummer = TEXT_PRODUCTS_IMAGE . ' ' . ($i + 1);
    $bildda = $myimages[$i];
    $Bild = xtc_image(DIR_WS_CATALOG_IMAGES . 'blog_image/thumbnail_images/' . $myimages[$i]['image'], 'is_images_' . ($i + 1));
    $Bildfeld = xtc_draw_file_field('ism_image_' . ($i + 1)) . '<br />' . $myimages[$i]['image'] . xtc_draw_hidden_field('ismv_image_' . ($i + 1), $myimages[$i]['image']);
    $Bildfeld2 = xtc_draw_selection_field('imageremove[]', 'checkbox', $myimages[$i]['image']) . ' ' . TEXT_DELETE;
    $Bildfeld3 = xtc_draw_file_field('ism_image_' . ($i + 1)) . xtc_draw_hidden_field('ismv_image_' . ($i + 1), $myimages[$i]['image']);

    $bilder[$i] = array(
        'bildfeld0' => $bildda,
        'bildfeld1' => $Bildnummer,
        'bildfeld2' => $Bild,
        'bildfeld3' => $Bildfeld,
        'bildfeld4' => $Bildfeld2,
        'bildfeld5' => $Bildfeld3
    );
}


$group_ids = '';

$customers_statuses_array = xtc_get_customers_statuses();

if (strstr($group_ids, 'c_all_group')) {
    $group_ids = 'c_all_group,';
    foreach ($customers_statuses_array AS $t_gm_key => $t_gm_value) {
        $group_ids .='c_' . $t_gm_value['id'] . '_group,';
    }
}
$mygroup = xtc_db_fetch_array(xtc_db_query("SELECT * FROM " . TABLE_BLOG_CATEGORIES . " WHERE categories_id = '" . (int) $_GET['cat'] . "' AND language_id = '" . (int) $_SESSION['languages_id'] . "' "));
if (GROUP_CHECK == 'true') {
    $customers_statuses_array = xtc_get_customers_statuses();
    $customers_statuses_array = array_merge(array(array('id' => 'all', 'text' => TXT_ALL)), $customers_statuses_array);

    for ($i = 0; $i < count($customers_statuses_array); $i++) {
        if (strstr($mygroup['group_ids'], 'c_' . $customers_statuses_array[$i]['id'] . '_group')) {
            $checked = 'checked ';
        } else {
            $checked = '';
        }
        $kundengruppe = '<input type="checkbox" name="groups[]" value="' . $customers_statuses_array[$i]['id'] . '"' . $checked . '> ' . $customers_statuses_array[$i]['text'] . '<br />';
        $kundengr[$i] = array(
            'KGRUPPE' => $kundengruppe
        );
    }
    $module_smarty->assign('KGRUPPE', ENTRY_CUSTOMERS_STATUS);
}
$status[] = array('id' => 1, 'text' => TABLE_FOOTER_STATUS_1);
$status[] = array('id' => 2, 'text' => TABLE_FOOTER_STATUS_2);

$module_smarty->assign('ITEMSOR', $mygroup['itemsort']);
$module_smarty->assign('ITEMSORART', $mygroup['itemsortart']);
$module_smarty->assign('status', xtc_draw_pull_down_menu('status', $status, $mygroup['status'], 'class="btn btn-default"'));
//Topmenü
$select_categories2 = array();
$category_query2 = xtc_db_query("SELECT * FROM " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
						WHERE	c.categories_id	= cd.categories_id
						AND	c.categories_status	= 1
						AND cd.language_id = " . $_SESSION['languages_id'] . "
");
$select_categories2[] = array('id' => '0', 'text' => TABLE_FOOTER_STATUS_0);
while ($categories2 = xtc_db_fetch_array($category_query2)) {
    $select_categories2[] = array('id' => $categories2['categories_id'], 'text' => $categories2['categories_name'], 'parent' => $categories2['parent_id']);
}

if (isset($_GET['cat'])) {
    $parentid = xtc_db_fetch_array(xtc_db_query("select * FROM " . TABLE_BLOG_CATEGORIES . " WHERE categories_id = " . $_GET['cat'] . ""));
    if ($parentid['tmselect'] == 'true') {
        $checked = 'checked="checked"';
        $parentid2 = xtc_db_fetch_array(xtc_db_query("select categories_id FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_blogs = '" . $_GET['cat'] . "' LIMIT 1 "));
        $module_smarty->assign('TMCATID', xtc_draw_hidden_field('tmcatid', $parentid2['categories_id']));
        $module_smarty->assign('TM_SELECT', xtc_draw_hidden_field('tm_cat', 'true'));
    } else {
        $checked = '';
        $module_smarty->assign('TM_SELECT', xtc_draw_hidden_field('tm_cat', 'false'));
    }
} else {
    $module_smarty->assign('TM_SELECT', xtc_draw_hidden_field('tm_cat', 'false'));
}
$module_smarty->assign('TMPOS', xtc_draw_input_field('tmpos', $parentid['sort_order']));
$module_smarty->assign('TMSELECT', xtc_draw_checkbox_field('tmcat', 'true', $checked));
$module_smarty->assign('CATEDIT2', xtc_draw_pull_down_menu('parent_id2', $select_categories2, $parentid['tmid'], 'class="btn btn-default" style="width:400px;"'));
$module_smarty->assign('CATEDIT', xtc_draw_pull_down_menu('parent_id', $select_categories, $parentid['parent_id'], 'class="btn btn-default" style="width:400px;"'));
if (isset($_GET['cat'])) {
    $module_smarty->assign('action', xtc_draw_hidden_field('action', 'update'));
    $module_smarty->assign('catid', xtc_draw_hidden_field('categorie_id', $_GET['cat']));
} else {
    $module_smarty->assign('action', xtc_draw_hidden_field('action', 'insert'));
}

$module_smarty->assign('TITLE', $mygroup['titel']);
$module_smarty->assign('BILDER', $bilder);
$module_smarty->assign('GRUPPE', $kundengr);
$module_smarty->assign('CAT_EDIT', $catedit);
$module_smarty->assign('FORM_END', '</form>');
$module_smarty->assign('language', $_SESSION['language']);
$module_smarty->caching = false;
$module_smarty->template_dir = DIR_FS_CATALOG . 'admin/templates/';
$module_smarty->compile_dir = DIR_FS_CATALOG . 'admin/templates_c';
$module = $module_smarty->fetch(CURRENT_ADMIN_TEMPLATE . '/blog_categories_edit.html');
$smarty->assign('MODULE_blog_categories_edit', $module);
