<?
if (isset($ID)) {
  $category = $sql -> fetch('SELECT * FROM `shop_category` WHERE `id` = ' . $ID);
  $system['meta_keyword'] = text($category['keywords']);
}

if (isset($category['id'])) { 
  $WHERE = " WHERE `id_category` LIKE '%%/$category[id]/' ";
} else {
  $WHERE = "WHERE `id_category` = '/'";
}

if (isset($category['id'])) {
  $exp = explode("/", $category['id_category']);
  foreach($exp AS $atr => $int){
    if (is_numeric($int)) {
      $_ids[] = $int;
    }
  }

  $LINKS = '';
  if (isset($_ids)){
    foreach ($_ids AS $anut => $ids){
      $cate = $sql -> fetch('SELECT * FROM `shop_category` WHERE `id` = ' . $ids);
      $LINKS .= '<li><a href="/shop/' . $cate['links'] . '">' . text($cate['name']) . '</a></li>';
    } 
  }
  $LINKS .= '<li class="active">' . text($category['name']) . '</li>';  
} else {
  $LINKS = '<li class="active">Магазин</li>';
}

$system['title'] = 'Магазин';
require SYS . 'header.php';

?>
<div class="home_menu">
<? require '_src/search.php'; ?>
</div>
<?

if (isset($category['id']) && $category['goods'] == 1) {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `shop_goods` WHERE `id_category` = '$category[id]' AND `active` != '2' AND `active` != '0' AND `unlink` = '0'");
  $k_page = k_page($k_post, $system['page_str']);
  $page = page($k_page);
  $start = $system['page_str'] * $page - $system['page_str'];

  if ($k_post == 0) {
?>
    <div class="alert" style="margin: 2px;">Список товаров пуст</div>
<?
  } else {
    $array = $sql -> select("SELECT * FROM `shop_goods` WHERE `id_category` = '$category[id]' AND `active` != '2' AND `active` != '0' AND `unlink` = '0' ORDER BY `id` DESC LIMIT $start, $system[page_str]");

    foreach ($array AS $post) {
      if ($post['screens']) {
        $screen = unserialize($post['screens']);
        if (is_file(SRC . 'screen/shop/' . $screen[0] . '.png')) {
          $_src = '/_src/img.php?link=' . base64_encode(SRC.'screen/shop/' . $screen[0] . '.png') . '&type=1';
        }
      } else {
        $_src = '/_src/img.php?link=' . base64_encode(ROOT.'_images/images/no_image.png') . '&type=1';
      }
?>
      <div class="media <?= ($num % 2 ? "nav1" : "nav2")?>">
        <img src="<?= $_src?>" class="img-polaroid pull-left" /> 
        <div class="media-body"><a href="/goods/<?= $post['id']?>" class="media-heading"><?= text($post['name'])?></a> <br />
        <span class="on"><b><?= $post['wmr']?></b> ₽</span>
        </div>
      </div>
<?
    }
    
    if ($k_page > 1) { 
      str('/shop/'.$category['links'].'&amp;', $k_page, $page);
    }
  }

  if ($level > 0) {
?>
    <div class="foot">
    &raquo; <a href="/?func=shop.goodsedit&amp;create=<?= (isset($category['id']) ? $category['id'] : '')?>">Добавить товар</a>
    </div>
<? 
  } 
} else {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `shop_category` $WHERE");

  if ($k_post == 0) {
    ?>
    <div class="alert" style="margin: 2px;">Каталог пуст</div>
    <?
  } else {
    $array = $sql -> select("SELECT * FROM `shop_category` $WHERE ORDER BY `id` ASC");

    foreach ($array AS $post) {
      $count = $sql -> fetch("
SELECT COUNT(*) AS goods,
(SELECT COUNT(*) FROM `shop_goods` WHERE `time` > '" . mktime(0, 0, 0) . "' AND `active` != '2' AND `dir` like '%%/$post[id]/%%' AND `unlink` = '0') AS 'goods_new'
FROM `shop_goods` WHERE `dir` like '%%/$post[id]/%%' AND `active` != '2' AND `unlink` = '0'");
    ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <? if ($level == 4) { ?>
          <a class="close" href="/?func=shop.categorydelete&amp;id=<?= $post['id']?>"><i class="icon-remove"></i></a>  
          <a class="close" href="/?func=shop.categoryedit&amp;id=<?= $post['id']?>"><i class="icon-pencil"></i></a> 
      <? } ?>
     <a href="/shop/<?= $post['links']?>"><i class="ic-dir"></i><?= text($post['name'])?></a> 
      (<?= $count['goods']?>) <?= ($count['goods_new'] > 0 ? '<span class="off">+' . $count['goods_new'] . '</span>' : '')?><br />
      <?= text_out($post['msg'])?>
      </div>
    <?
    }
  }

  if ($level == 4) {
  ?>
    <div class="foot">
    &raquo; <a href="/?func=shop.categoryedit&amp;create=<?= (isset($category['id']) ? $category['id'] : '')?>">Создать папку</a>
    </div>
  <? 
  } 
}

?>
<div class="foot">
  <ol class="breadcrumb">
    <?= $LINKS?>
  </ol>
</div>
<?