<?php

/* -----------------------------------------------------------------
 * 	$Id: blog_item_edit.php 1113 2014-06-19 11:32:53Z 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_ITEMS,'canonical')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_ITEMS . " ADD COLUMN canonical varchar(250) NOT NULL DEFAULT '';");
}
if (!column_exists (TABLE_BLOG_ITEMS,'calender')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_ITEMS . " ADD COLUMN calender INT(5) NOT NULL DEFAULT '0' AFTER item_id;");
}
if (!column_exists (TABLE_BLOG_ITEMS,'datestart')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_ITEMS . " ADD COLUMN datestart DATE NOT NULL AFTER date_release;");
}
if (!column_exists (TABLE_BLOG_ITEMS,'dateend')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_ITEMS . " ADD COLUMN dateend DATE NOT NULL AFTER datestart;");
}
if (!column_exists (TABLE_BLOG_ITEMS,'slider_set')) {
	xtc_db_query("ALTER TABLE " . TABLE_BLOG_ITEMS . " ADD COLUMN slider_set INT(11) NOT NULL DEFAULT '0' AFTER categories_id;");
}
$module_smarty = new Smarty;
$module_smarty->assign('txt', $coo_text_mgr->v_section_content_array['blog']);
$module_smarty->assign('FORM', xtc_draw_form('item', FILENAME_BLOG, 'action=save_item', 'post', 'enctype="multipart/form-data"') . "\n" . xtc_draw_hidden_field('categories_id', $_GET['cat']) . "\n" . xtc_draw_hidden_field('item', $_GET['item']));
$languages = xtc_get_languages();

if ($_GET['action'] == 'new_item') {
    $module_smarty->assign('action', xtc_draw_hidden_field('action', 'insert'));
} elseif ($_GET['action'] == 'edit_item') {
    $module_smarty->assign('action', xtc_draw_hidden_field('action', 'update'));
    $module_smarty->assign('item', xtc_draw_hidden_field('item', $_GET['item']));
}

$status[] = array('id' => 1, 'text' => TABLE_FOOTER_STATUS_1);
$status[] = array('id' => 2, 'text' => TABLE_FOOTER_STATUS_2);

$categories_query = xtc_db_query("SELECT categories_id, parent_id, titel FROM " . TABLE_BLOG_CATEGORIES . " WHERE  language_id = '" . (int) $_SESSION['languages_id'] . "' ORDER BY position ASC");

while ($categories = xtc_db_fetch_array($categories_query)) {
    $select_categories[] = array('id' => $categories['categories_id'], 'text' => $categories['titel']);
}

for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
    $items = xtc_db_fetch_array(xtc_db_query("SELECT * FROM " . TABLE_BLOG_ITEMS . " WHERE language_id = '" . (int) $languages[$i]['id'] . "' AND item_id = '" . (int) $_GET['item'] . "';"));
	//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, $items['slider_set'], 'class="btn btn-default"');
	$lang_images = xtc_image(DIR_WS_LANGUAGES . $languages[$i]['directory'] . '/' . $languages[$i]['image'], $languages[$i]['name']);
    $canonical = xtc_draw_input_field('canonical[' . $languages[$i]['id'] . ']', $items['canonical'], 'size="60"');
    $fieldname = xtc_draw_input_field('title[' . $languages[$i]['id'] . ']', $items['title'], 'size="60"');
    $fieldtitle = xtc_draw_input_field('name[' . $languages[$i]['id'] . ']', $items['name'], 'size="60"');
    $fieldposition = xtc_draw_small_input_field('position[' . $languages[$i]['id'] . ']', $items['position'], '');
    $field_mtitle = xtc_draw_input_field('meta_title[' . $languages[$i]['id'] . ']', $items['meta_title'], 'size="60"');
    $field_mdesc = xtc_draw_input_field('meta_desc[' . $languages[$i]['id'] . ']', $items['meta_description'], 'size="60"');
    $field_mkeys = xtc_draw_input_field('meta_key[' . $languages[$i]['id'] . ']', $items['meta_keywords'], 'size="60"');
    $field_sdesc = xtc_draw_textarea_field('shortdesc_' . $languages[$i]['id'], '', '100', '50', $items['shortdesc'], 'class="ckeditor" name="editor1"');
    $field_ldesc = xtc_draw_textarea_field('description_' . $languages[$i]['id'], '', '100', '50', $items['description'], 'class="ckeditor" name="editor1"');
    if (USE_WYSIWYG == 'true') {
        $field_sdesc_wy = '1';
    } else {
        $field_sdesc_wy = '0';
    }

    $itemedit[$i] = array(
        'langid' => $languages[$i]['id'],
        'langname' => $languages[$i]['name'],
		'field_slider' => $slider_set,
        'lang_images' => $lang_images,
        'canonical' => $canonical,
        'fieldname' => $fieldname,
        'fieldtitle' => $fieldtitle,
        'fieldposition' => $fieldposition,
        'field_mtitle' => $field_mtitle,
        'field_mdesc' => $field_mdesc,
        'field_mkeys' => $field_mkeys,
        'field_sdesc' => $field_sdesc,
        'field_ldesc' => $field_ldesc,
        'field_sdesc_wy' => $field_sdesc_wy
    );
}
$itemstatus = xtc_db_fetch_array(xtc_db_query("SELECT * FROM " . TABLE_BLOG_ITEMS . " WHERE item_id = '" . (int) $_GET['item'] . "' "));
$module_smarty->assign('date', xtc_draw_input_field('date', $itemstatus['date'], 'size="30"'));
$module_smarty->assign('date_update', xtc_draw_input_field('date_update', $itemstatus['date_update'], 'size="30"'));
$module_smarty->assign('date_release', xtc_draw_input_field('date_release', $itemstatus['date_release'], 'size="30" class="datepickers"'));
$module_smarty->assign('date_out', xtc_draw_input_field('date_out', $itemstatus['date_out'], 'size="30" class="datepickers"'));
$module_smarty->assign('datestart', xtc_draw_input_field('datestart', $itemstatus['datestart'],'size="30"  class="datepickers"'));
$module_smarty->assign('dateend', xtc_draw_input_field('dateend', $itemstatus['dateend'],'size="30"  class="datepickers"'));
$module_smarty->assign('position', xtc_draw_small_input_field('position', $itemstatus['position'], ''));
$module_smarty->assign('calender', $itemstatus['calender']);

if (file_exists(DIR_FS_CATALOG_IMAGES . 'blog_image/thumbnail_images/' . $itemstatus['blog_image'])) {
	$module_smarty->assign('blog_image_prev', xtc_image(DIR_WS_CATALOG_IMAGES . 'blog_image/thumbnail_images/' . $itemstatus['blog_image']));
}

$module_smarty->assign('blog_image', xtc_draw_file_field('blog_image') . xtc_draw_hidden_field('blog_image_preview', $itemstatus['blog_image']));
$module_smarty->assign('blog_image_name', $itemstatus['blog_image']);
$module_smarty->assign('blog_image_delete', xtc_draw_selection_field('imageremove', 'checkbox', 'yes') . ' ' . TEXT_DELETE);
$module_smarty->assign('lenght', xtc_draw_small_input_field('lenght', $itemstatus['lenght'], ''));
$module_smarty->assign('status', xtc_draw_pull_down_menu('status', $status, $itemstatus['status'], 'class="btn btn-default"'));
$module_smarty->assign('ITEMEDIT', xtc_draw_pull_down_menu('categories_id', $select_categories, $_GET['cat'], 'class="btn btn-default"'));

if ($_GET['item'] != '') {
    $myimages = xtc_get_blog_mo_images($_GET['item']);
}

for ($i = 0; $i < PICITEM; $i++) {
    $Bildnummer = TEXT_PRODUCTS_IMAGE . ' ' . ($i + 1);
    $Bildfeld = ('ism_image_' . ($i + 1));
    $bilder[$i] = array(
        'bildfeld1' => $Bildnummer,
        'bildfeld3' => $Bildfeld
    );
}
$bilder2 = '';
$bilder2['width'] .= xtc_draw_input_field('width', 'auto', '');
$bilder2['height'] .= xtc_draw_input_field('height', 'auto', '');
$bilder2['floating'] .= xtc_draw_input_field('floating', '', '');
$bilder2['position'] .= xtc_draw_input_field('slposition', '', '');
$module_smarty->assign('BILDER2', $bilder2);

$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 group_ids, title FROM " . TABLE_BLOG_ITEMS . " WHERE item_id = '" . (int) $_GET['item'] . "' 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);
}


if (isset($_GET['item']) && $_GET['item'] != '') {
    include(DIR_WS_MODULES . 'blog_edit_item_block.php');
    $module_smarty->assign('ITEM_EDIT2', $itemedit2);
    $module_smarty->assign('ITEMEX', 'true');
} else {
    $module_smarty->assign('ITEMEX', 'false');
}
$module_smarty->assign('GRUPPE', $kundengr);
$module_smarty->assign('BILDER', $bilder);
$module_smarty->assign('ITEM_EDIT', $itemedit);
$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_item_edit.html');
$smarty->assign('MODULE_blog_categories_edit', $module);

echo '<script type="text/javascript">
    $(function() {
        $(\'.datepickers\').datepicker({
            minDate: new Date('.date('Y') . ',' . date('m') . '-1,' . date('d').'),
            buttonImage: "images/calendar.png",
            showOn: "button",
            dateFormat: \'yy-mm-dd\'});
    });
</script>';