<?php

/**
 * EasyAdmin for Giglance
 *
 * @package Categories manage
 * @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']);

$t = new XTemplate(cot_tplfile('eadmin.categories', 'module'));

if (empty($n)) {
    cot::$out['title'] = $L['EA_admin_catmanage'];
    cot::$out['adminpath'][] = array(cot_url('eadmin', 'em=categories'), $L['EA_admin_cats']);
    cot::$out['adminpath'][] = $L['EA_admin_manage'];
    if (is_array($extension_structure) && count($extension_structure) > 0) {
        foreach ($extension_structure as $code) {
            $parse = false;
            if (cot_plugin_active($code)) {
                $is_module = false;
                $parse = true;
            }
            if (cot_module_active($code)) {
                $is_module = true;
                $parse = true;
            }
            if ($parse) {
                require_once cot_incfile('eadmin', 'module', 'functions');
                $ext_info = ea_get_extensionparams($code, $is_module);
                $t->assign(array(
                    'ADMIN_STRUCTURE_EXT_URL' => cot_url('eadmin', 'em=categories&n=' . $code),
                    'ADMIN_STRUCTURE_EXT_ICO' => $ext_info['icon'],
                    'ADMIN_STRUCTURE_EXT_NAME' => $ext_info['name']
                ));
                $t->parse('MAIN.MODLIST.ADMIN_STRUCTURE_EXT');
            }
        }
    } else {
        $t->parse('MAIN.MODLIST.ADMIN_STRUCTURE_EMPTY');
    }

    $t->assign(array(
        'ADMIN_STRUCTURE_EXFLDS_URL' => cot_url('admin', 'm=extrafields')
    ));

    $t->parse('MAIN.MODLIST');
} else {
    $cats_list = $cats_class->clist();
    if ($id > 0) {
        $t->assign(array(
            'CAT_EDIT' => $cats_class->edit($id, $n),
        ));
        $t->parse('MAIN.EDIT');
    } else {

        global $cot_modules, $cot_extrafields, $db_structure, $structure, $L, $R;

        cot::$out['title'] = $L['EA_admin_catmanage'];
        cot::$out['small_title'] = $cot_modules[$n]['title'];

        cot::$out['adminpath'][] = array(cot_url('eadmin', 'em=categories'), $L['EA_admin_cats']);
        cot::$out['adminpath'][] = array(cot_url('eadmin', 'em=categories&n=' . $n), $cot_modules[$n]['title']);
        cot::$out['adminpath'][] = $L['EA_admin_manage'];

        $t->assign(array(
            'ADMIN_STRUCTURE_URL_FORM_ADD' => cot_url('eadmin', 'em=categories&n=' . $n . '&a=add'),
            'ADMIN_STRUCTURE_CODE' => cot_inputbox('text', 'rstructurecode', null, 'class="form-control"'),
            'ADMIN_STRUCTURE_PARENT' => $cats_class->parent_cat($parentid, 'rstructureparent'),
            'ADMIN_STRUCTURE_TITLE' => cot_inputbox('text', 'rstructuretitle', null, 'class="form-control" maxlength="100"'),
            'ADMIN_STRUCTURE_DESC' => cot_inputbox('text', 'rstructuredesc', null, 'class="form-control" maxlength="255"'),
            'ADMIN_STRUCTURE_ICON' => cot_inputbox('text', 'rstructureicon', null, 'class="form-control" maxlength="128"'),
            'ADMIN_STRUCTURE_LOCKED' => cot_checkbox(null, 'rstructurelocked'),
            'ADMIN_STRUCTURE_TPL' => cot_inputbox('text', 'rstructuretpl', null, 'class="form-control" maxlength="255"'),
        ));

        // Extra fields
        foreach ($cot_extrafields[$db_structure] as $exfld) {
            $exfld_val = cot_build_extrafields('rstructure' . $exfld['field_name'], $exfld, null);
            $exfld_title = isset($L['structure_' . $exfld['field_name'] . '_title']) ? $L['structure_' . $exfld['field_name'] . '_title'] : $exfld['field_description'];
            $t->assign(array(
                'ADMIN_STRUCTURE_' . strtoupper($exfld['field_name']) => $exfld_val,
                'ADMIN_STRUCTURE_' . strtoupper($exfld['field_name']) . '_TITLE' => $exfld_title,
                'ADMIN_STRUCTURE_EXTRAFLD' => $exfld_val,
                'ADMIN_STRUCTURE_EXTRAFLD_TITLE' => $exfld_title
            ));
            $t->parse('MAIN.CREATE.EXTRAFLD');
        }
        $t->parse('MAIN.CREATE');

        $t->assign(array(
            'RESYNC_URL' => cot_url('eadmin', 'em=categories&n=' . $n . '&a=sync&' . cot_xg()),
            'CATEGORIES_LIST' => $cats_list,
        ));
        $t->parse('MAIN.CATS');
    }
}

cot_display_messages($t);
$t->parse('MAIN');