<?
if ($level < 2) {
  header('Location: /index.php');
  exit;
}

if (isset($_GET['truncate']) && $level == 4) {
  $sql -> delete("DELETE FROM `admin_log` WHERE `id_user` = '$user[id]'");
  $_SESSION['message'] = 'Логи успешно очищены';
  header('Location: ?func=' . $func);
  exit;
}

$system['title'] = 'Действия администрации';
require SYS . 'header.php';

if (isset($_GET['razd']) && isset($_GET['cat'])) {
  $razd = (int) $_GET['razd'];
  $cat = (int) $_GET['cat'];
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `admin_log` WHERE `razd` = '$razd' ORDER BY `time`");

  if ($k_post == 0) {
    ?>
    <div class="alert" style="margin: 2px;">Список действий пуст</div>
    <?
  } else {
    $k_page = k_page($k_post, $system['page_str']);
    $page = page($k_page);
    $start =  $system['page_str'] * $page -  $system['page_str'];

    $array = $sql -> select("SELECT * FROM `admin_log` WHERE `razd` = '$razd' ORDER BY `time` DESC LIMIT $start, $system[page_str]");
    
    foreach ($array AS $post) {
      $num++;
      ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
        <?= login($post['id_user'], 1)?> <?= vremja($post['time'])?> -> 
        <?= text_out($post['msg'])?><br />
      </div>
      <?
    }

    if ($k_page > 1) { 
      str('/?func=' . $func . '&amp;cat=' . $cat . '&amp;razd=' . $razd . '&amp;', $k_page, $page);
    }
  }
  
?>
  <div class="foot">
  &laquo; <a href="?func=<?= $func?>&amp;cat=<?= $cat?>">К категории</a>
  </div>
<?
} elseif (isset($_GET['cat'])) {
  $cat = (int) $_GET['cat'];


  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `admin_log_razd` WHERE `cat` = '$cat' ORDER BY `cat` ");

  if ($k_post == 0) {
    ?>
    <div class="alert" style="margin: 2px;">Раздел пуст</div>
    <?
  } else {
    $k_page = k_page($k_post, $system['page_str']);
    $page = page($k_page);
    $start =  $system['page_str'] * $page -  $system['page_str'];
    $array = $sql -> select("SELECT * FROM `admin_log_razd` WHERE `cat` = '$cat' ORDER BY `cat` DESC LIMIT $start, $system[page_str]");

    foreach ($array AS $post) {
    ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <a href="?func=<?= $func?>&amp;cat=<?= $cat?>&amp;razd=<?= $post['id']?>"><i class="icon-folder-close"></i> <?= $post['name']?> (<?= $sql -> selectCount("SELECT COUNT(*) FROM `admin_log` WHERE `razd` = '$post[id]'")?>)</a><br />
      </div>
    <?
    }

    if ($k_page > 1) { 
      str('/?func=' . $func . '&amp;cat=' . $cat . '&amp;', $k_page, $page);
    }
  }
?>
  <div class="foot">
  <a href="?func=<?= $func?>"><i class="icon-folder-open"></i> К разделам</a>
  </div>
<?
} else {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `admin_log_cat`");
  
  if ($k_post == 0) {
    ?>
    <div class="alert" style="margin: 2px;">Каталог пуст</div>
    <?
  } else {
    $array = $sql -> select("SELECT * FROM `admin_log_cat`");

    foreach ($array AS $post) {
    ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <a href="?func=<?= $func?>&amp;cat=<?= $post['id']?>"><i class="icon-folder-close"></i> <?= $post['name']?> (<?= $sql -> selectCount("SELECT COUNT(*) FROM `admin_log` WHERE `cat` = '$post[id]'")?>)</a><br />
      </div>
    <?
    }
  }
}
?>
<div class="foot">
&laquo; <a href="?func=admin">В админку</a><?= ($level == 4 ? ' | <a href="?func=admin.log&amp;truncate">Очистить логи</a>' : '')?>
</div>