<?php defined('COT_CODE') or die('Wrong URL.');

// Table names
cot::$db->registerTable('catextratypes');
$db_catextratypes = $db_x.'catextratypes';

function get_catextratypes_cfg($catid = 0) {
  global $cfg;

  $epcfg = array(
    'cetlimit' => array(),
    'cetextra' => array(),
    'cetlabel' => array()
  );
  if(!empty($cfg['plugin']['catextratypes']['cetlimit'])) foreach(explode("\n", $cfg['plugin']['catextratypes']['cetlimit']) as $val) {
    $val = explode('=', $val);
    if(!empty($val[1])) {
      $val[0] = explode(',', $val[0]);
      foreach($val[0] as $vv) {
        if($vv > 0) {
          $epcfg['cetlimit'][$vv] = (int)$val[1];
        }
      }
    }
  }
  if(!empty($cfg['plugin']['catextratypes']['cetextra'])) foreach(explode("\n", $cfg['plugin']['catextratypes']['cetextra']) as $val) {
    $val = explode('=', $val);
    if(!empty($val[1])) {
      $val[0] = explode(',', $val[0]);
      foreach($val[0] as $vv) {
        if($vv > 0) {
          $epcfg['cetextra'][$vv] = (int)$val[1];
        }
      }
    }
  }
  if(!empty($cfg['plugin']['catextratypes']['cetlabel'])) foreach(explode("\n", $cfg['plugin']['catextratypes']['cetlabel']) as $val) {
    $val = explode('=', $val);
    if(!empty($val[1])) {
      $val[0] = explode(',', $val[0]);
      $val[1] = explode(';', $val[1]);
      foreach($val[0] as $vv) {
        if($vv > 0) {
          $epcfg['cetlabel'][$vv] = array(
            'label' => $val[1][0],
            'tooltip' => $val[1][1]
          );
        }
      }
    }
  }

  if($catid > 0) {
    $return = array(
      'cetlimit' => -1,
      'cetextra' => 0,
      'cetlabel' => array(
        'label' => 'Тип',
        'tooltip' => ''
      )
    );

    if(is_numeric($epcfg['cetlimit'][$catid])) $return['cetlimit'] = $epcfg['cetlimit'][$catid];
    if(is_numeric($epcfg['cetextra'][$catid])) $return['cetextra'] = $epcfg['cetextra'][$catid];

    $label_arr = $epcfg['cetlabel'][$catid];
    if(is_array($label_arr)) $return['cetlabel'] = $label_arr;
    $epcfg = $return;
  }

  return $epcfg;
}
