<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=market.tags
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

if($item['item_id'] > 0) {
  require_once cot_incfile('catextratypes', 'plug');

  $catextratypes = array(
    'label' => '',
    'catattrs' => array(),
    'catlables' => array(),
    'list' => array(),
    'list_join' => array()
  );

  if(!empty($item['item_catextratypes'])) {
    foreach(explode(",", $item['item_catextratypes']) as $i) {
      if($i > 0) {
        $tval = $db->query("SELECT e_title FROM $db_catextratypes WHERE e_area='market' AND e_id=".$i." LIMIT 1")->fetchColumn();
        if(!empty($tval)) $catextratypes['list'] = array(
          'id' => $i,
          'name' => $tval
        );
        $catextratypes['list_join'][] = $tval;
      }
    }
  }

  if(!empty($item['item_catextratypes_other'])) {
    $tval = json_decode($item['item_catextratypes_other'], 1);

    if(count($tval) > 0) foreach($tval as $i => $val) {
      foreach($val as $ttval) {
        $catextratypes['list'] = array(
          'id' => 0,
          'name' => $ttval
        );
        $catextratypes['list_join'][] = $ttval;
      }
    }
  }

  if(count($catextratypes['list']) > 0) {
    if($item['item_catattr'] != $item['item_cat'] && $structure['market'][$item['item_catattr']]['id'] > 0) $catextratypes['catattrs'][] = $structure['market'][$item['item_catattr']]['id'];

    if($structure['market'][$item['item_catattr']]['parent'] != $item['item_catattr']) {
      $tempcatarr = $structure['market'][$item['item_catattr']]['parent'];
      $catextratypes['catattrs'][] = $structure['market'][$tempcatarr]['id'];
      if($structure['market'][$tempcatarr]['parent'] != $tempcatarr) {
        $tempcatarr = $structure['market'][$tempcatarr]['parent'];
        $catextratypes['catattrs'][] = $structure['market'][$tempcatarr]['id'];
        if($structure['market'][$tempcatarr]['parent'] != $tempcatarr) {
          $tempcatarr = $structure['market'][$tempcatarr]['parent'];
          $catextratypes['catattrs'][] = $structure['market'][$tempcatarr]['id'];
        }
      }
    }

    foreach($catextratypes['catattrs'] as $cval) {
      if($cval > 0) $catextratypes['catlables'][$cval] = '';
    }

    if(count($catextratypes['catattrs']) > 0) {
      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($catextratypes['catattrs'] as $cval) {
            if($cval > 0 && in_array($cval, $val[0])) {
              foreach($val[0] as $vv) {
                if($vv > 0 && $vv == $cval) {
                  $catextratypes['catlables'][$cval] = $val[1][0];
                }
              }
            }
          }
        }
      }
    }


    foreach($catextratypes['catlables'] as $clabel) {
      if(!empty($clabel)) $catextratypes['label'] = $clabel;
    }
    if(empty($catextratypes['label'])) $catextratypes['label'] = 'Тип';
  }

  $t->assign(array(
    "PRD_CATEXTRATIPES" => $catextratypes
  ));

}
