<?php

/**
 * EasyAdmin for Giglance
 *
 * @package Categories add
 * @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, $cot_extrafields, $structure;

$rstructure['structure_code'] = preg_replace('#[^\w\p{L}\-]#u', '', cot_import('rstructurecode', 'P', 'TXT'));
$rstructure['structure_title'] = cot_import('rstructuretitle', 'P', 'TXT');
$rstructure['structure_path'] = cot_import('rstructurepath', 'P', 'TXT');
$rstructure['structure_desc'] = cot_import('rstructuredesc', 'P', 'TXT');
$rstructure['structure_icon'] = cot_import('rstructureicon', 'P', 'TXT');
$rstructure['structure_locked'] = (cot_import('rstructurelocked', 'P', 'BOL')) ? 1 : 0;
$rstructure['structure_area'] = $n;
$rstructure['structure_tpl'] = cot_import('rstructuretpl', 'P', 'TXT');
$parent = cot_import('rstructureparent', 'P', 'TXT');

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

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');
$rstructure['structure_code'] || cot_error('adm_structure_code_required', 'rstructurecode');
$rstructure['structure_path'] || cot_error('adm_structure_path_required', 'rstructurepath');

/* === Hook === */
foreach (cot_getextplugins('admin.structure.add.first') as $pl) {
    include $pl;
}
/* ===== */

if (!cot_error_found()) {
    $res = cot_structure_add($n, $rstructure, $is_module);
    if ($res === true) {
        cot_extrafield_movefiles();
        /* === Hook === */
        foreach (cot_getextplugins('admin.structure.add.done') as $pl) {
            include $pl;
        }
        cot_message('Ea_cat_added');
    } elseif (is_array($res)) {
        cot_error($res[0], $res[1]);
    } else {
        cot_error('Error');
    }
}

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