<?php

/**
 * EasyAdmin for Giglance
 *
 * @package Categories update
 * @version 2.6.9
 * @author CoTEMPLATE
 * @copyright Copyright (c) CoTEMPLATE.com
 */
(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('admin', 'a');
cot_block($usr['isadmin']);

global $db, $db_structure, $db_config, $cot_extrafields, $structure;
require_once cot_incfile('structure');
$editconfig = cot_import('editconfig', 'P', 'TXT');
if (!empty($editconfig))
{
	require_once cot_incfile('configuration');
	
	$optionslist = cot_config_list($is_module ? 'module' : 'plug', $n, $editconfig);
	foreach ($optionslist as $key => $val)
	{
		$data = cot_import($key, 'P', sizeof($cot_import_filters[$key]) ? $key : 'NOC');
		if ($optionslist[$key]['config_value'] != $data)
		{
			if (is_null($optionslist[$key]['config_subdefault']))
			{
				$optionslist[$key]['config_value'] = $data;
				$optionslist[$key]['config_subcat'] = $editconfig;
				$db->insert($db_config, $optionslist[$key]);
			}
			else
			{
				$db->update($db_config, array('config_value' => $data), "config_name = ? AND config_owner = ?
					AND config_cat = ?  AND config_subcat = ?", array($key, ($is_module ? 'module' : 'plug'), $n, $editconfig));
			}
		}
	}
}



$oldrow = $db->query("SELECT * FROM $db_structure WHERE structure_id=".(int)$id)->fetch();
$rstructure['structure_code'] = preg_replace('#[^\w\p{L}\-]#u', '', cot_import('rstructurecode', 'P', 'TXT'));
$rstructure['structure_path'] = cot_import('rstructurepath', 'P', 'TXT');
$rstructure['structure_title'] = cot_import('rstructuretitle', 'P', 'TXT');
$rstructure['structure_desc'] = cot_import('rstructuredesc', 'P', 'TXT');
$rstructure['structure_icon'] = cot_import('rstructureicon', 'P', 'TXT');
$rstructure['structure_tpl'] = cot_import('rstructuretpl', 'P', 'TXT');
$parent = cot_import('rstructureparent', 'P', 'TXT');

$rstructure['structure_locked'] = (cot_import('rstructurelocked', 'P', 'BOL')) ? 1 : 0;

foreach ($cot_extrafields[$db_structure] as $exfld)
{
	$rstructure['structure_'.$exfld['field_name']] = cot_import_extrafields('rstructure'.$exfld['field_name'], $exfld, 'P', $oldrow['structure_'.$exfld['field_name']]);
}

if (empty($rstructure['structure_code']))
	{
		$rstructure['structure_code'] = $cats_class->cat_code($rstructure['structure_title']);
	}
	if(empty($rstructure['structure_path']))
	{
		$rstructure['structure_path'] = $cats_class->newpath($parent);
	}
	
($rstructure['structure_code'] != 'all') || cot_error('adm_structure_code_reserved', 'rstructurecode');
$rstructure['structure_title'] || cot_error('adm_structure_title_required', 'rstructuretitle');

if (!cot_error_found())
{
	

	$res = cot_structure_update($n, $id, $oldrow, $rstructure, $is_module);
	if (is_array($res))
	{
		cot_error($res[0], $res[1]);
	}
}

cot_extrafield_movefiles();
cot_auth_clear('all');

if ($cache)
{
	$cache->clear();
}

if (!cot_error_found())
{
	cot_message('Updated');
}
else
{
	cot_error('adm_structure_somenotupdated');
}

cot_redirect(cot_url('eadmin', 'em=categories&n='.$n.'&id='.$id, '', true));