<?php defined('COT_CODE') or die('Wrong URL.');
/**
 * [BEGIN_COT_EXT]
 * Hooks=tools
 * [END_COT_EXT]
 */
defined('COT_CODE') or die('Wrong URL.');

require_once cot_incfile('catextratypes', 'plug');

$a = cot_import('a', 'G', 'TXT');
$area = cot_import('area', 'G', 'TXT');
if(!$area) $area = 'market';

if($a == 'add')
{
 $ecatid = cot_import('ecatid', 'P', 'INT');
 $earea = cot_import('earea', 'P', 'TXT');
 $etitle = cot_import('etitle', 'P', 'TXT');

 cot_check(empty($ecatid), 'Укажите категорию');
 cot_check(empty($earea), 'Не указан модуль для выборки категорий');
 cot_check(empty($etitle), 'Укажите название типа');

 if(!cot_error_found())
 {
   $db->insert($db_catextratypes, array('e_catid' => $ecatid, 'e_area' => $earea, 'e_title' => $etitle));
 }
 cot_redirect(cot_url('admin', 'm=other&p=catextratypes', '', true));
}
elseif($a == 'del')
{
 $id = cot_import('id', 'G', 'INT');
 $db->delete($db_catextratypes, 'e_id='.$id);
 cot_redirect(cot_url('admin', 'm=other&p=catextratypes', '', true));
}

$t = new XTemplate(cot_tplfile('catextratypes.admin', 'plug', true));

$epcfg = get_catextratypes_cfg();

$epArr = array();
$sql = $db->query("SELECT * FROM $db_catextratypes WHERE e_area='" . $db->prep($area) . "' ORDER BY e_catid ASC, e_title ASC")->fetchAll();
foreach($sql as $val) {
  $epArr[$val['e_catid']][$val['e_id']] = $val['e_title'];
}

$catsArr = array();

$sql = $db->query("SELECT * FROM $db_structure WHERE structure_area='" . $db->prep($area) . "' ORDER BY structure_path ASC, structure_code ASC");
$totalitems = $db->query("SELECT COUNT(*) FROM $db_structure WHERE structure_area='" . $db->prep($area) . "'")->fetchColumn();

require_once cot_incfile('forms');
$values = array();
foreach ($sql->fetchAll() as $row) {
    $count = mb_substr_count($row['structure_path'], '.');
    $sosub = '';
    for ($i = 0; $i < $count - 1; $i++) {
        $sosub .= "° ";
    }
    if ($count > 0) {
        $sosub .="° ";
    }

    $values[$row['structure_id']] = $sosub . $row['structure_title'];

    $cntEP = count($epArr[$row['structure_id']]);
    if($cntEP > 0) {
      $fEP = array();
      $jj = 0;
      foreach($epArr[$row['structure_id']] as $epID => $ep) {
        if($jj == 0) {
          $fEP = array(
            'id' => $epID,
            'title' => $ep
          );
        } else {
          $t->assign(array(
          	'EP_LIST_ID' => $epID,
            'EP_LIST_TITLE' => $ep
          ));
          $t->parse('MAIN.EP_ROW.EP_LIST');
        }
        $jj++;
      }

      $row['label_arr'] = $epcfg['cetlabel'][$row['structure_id']];
      $row['label_arr'] = (is_array($row['label_arr']) ? $row['label_arr'] : array('label' => '', 'tooltip' => ''));

      $t->assign(array(
        'EP_ROW_CAT_ID' => $row['structure_id'],
      	'EP_ROW_CAT' => $row['structure_title'],
        'EP_ROW_CNT' => $cntEP,
        'EP_ROW_CETLIMIT' => (is_numeric($epcfg['cetlimit'][$row['structure_id']]) ? $epcfg['cetlimit'][$row['structure_id']] : 'Все'),
        'EP_ROW_CETEXTRA' => (is_numeric($epcfg['cetextra'][$row['structure_id']]) ? $epcfg['cetextra'][$row['structure_id']] : 'Нет'),
        'EP_ROW_CETLABEL_L' => $row['label_arr']['label'],
        'EP_ROW_CETLABEL_T' => $row['label_arr']['tooltip'],
      	'EP_LIST_ID' => $fEP['id'],
        'EP_LIST_TITLE' => $fEP['title']
      ));

      $t->parse('MAIN.EP_ROW');
    }
}

$t->assign(array(
	'ADMIN_STRUCTURE_PARENT' => cot_selectbox('', 'ecatid', array_keys($values), array_values($values), false, '')
));

cot_display_messages($t);

$t->parse('MAIN');
$adminmain = $t->text('MAIN');