<?php

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

$out['subtitle'] = (!empty($cfg['market']['cat___default']['metatitle'])) ? $cfg['market']['cat___default']['metatitle'] : $L['market'];
$out['desc'] = $cfg['market']['cat___default']['metadesc'];
$out['keywords'] = $cfg['market']['cat___default']['keywords'];

$out['canonical_uri'] = cot_url('market', 'm=list');

$t = new XTemplate(cot_tplfile(array('market', 'list_lvl_0')));

$parsejj=0;
$jj=0;
foreach($structure['market'] as $code => $cat) {
  if(mb_substr_count($cat['rpath'], ".") == 0) {
    $jj++;
    $cat['subcnt'] = 0;
    foreach($structure['market'] as $subcode => $subcat) {
      if($subcat['parent'] == $code && $subcat['parent'] != $subcode) {
        $subcat['subcnt'] = 0;
        foreach($structure['market'] as $subsubcode => $subsubcat) {
          if($subsubcat['parent'] == $subcode && $subsubcat['parent'] != $subsubcode) {
            $subcat['subcnt']++;

            $t->assign(array(
              'SUBSUBCAT_ROW_ID' => $subsubcat['id'],
              'SUBSUBCAT_ROW_TITLE' => $subsubcat['title'],
              'SUBSUBCAT_ROW_CODE' => $subsubcode,
              'SUBSUBCAT_ROW_URL' => cot_url('market', 'c='.$subsubcode),
            ));
            $t->parse('MAIN.CATBLOCK.LVL1_ROW.LVL2_ROW.LVL3_ROW');
          }
        }

        $cat['subcnt']++;
        $t->assign(array(
          'SUBCAT_ROW_ID' => $subcat['id'],
          'SUBCAT_ROW_TITLE' => $subcat['title'],
          'SUBCAT_ROW_CODE' => $subcode,
          'SUBCAT_ROW_URL' => cot_url('market', 'c='.$subcode),
          'SUBCAT_ROW_SUBCNT' => $subcat['subcnt'],
        ));

        $t->parse('MAIN.CATBLOCK.LVL1_ROW.LVL2_ROW');
      }
    }
    $t->assign(array(
      'CAT_ROW_ID' => $cat['id'],
      'CAT_ROW_TITLE' => $cat['title'],
      'CAT_ROW_CODE' => $code,
      'CAT_ROW_URL' => cot_url('market', 'c='.$code),
      'CAT_ROW_SUBCNT' => $cat['subcnt'],
    ));

    $t->parse('MAIN.CATBLOCK.LVL1_ROW');

    if(($jj % 2) == 0) {
      $parsejj += 2;
      $t->parse('MAIN.CATBLOCK');
    }
  }
}

if($jj > $parsejj) {
  $t->parse('MAIN.CATBLOCK');
}

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