<?php

  set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . "/../phplib");

  require_once("functions.php");

  if (!$_SESSION["user_name"]) {
    header("location: login.php");
    exit;
  }

  function get_original_html_files($root, $path) {
    $ret = [];

    $files = scandir($root . "/" . $path);

    foreach($files as $file) {
      if ($file[0] == '.') continue;

      if ($file == "lo") continue;

      if (is_dir($root . "/" . $path . "/" . $file)) {
        $ret = array_merge($ret, get_original_html_files($root, $path . "/" . $file));
      } else if (strpos($file, ".html") !== false
            && strpos($file, "lecke_") === false
            ) {
        $ret[] = array($path, $file);
      }
    }

    return $ret;
  }

  $shared_user_root = $config_shared_root . "/users/" . $_SESSION["user_name"];

?>
<html>
<head>
    <meta charset="UTF-8">

    <title>NKP HTML-szerkesztő - Tankönyv kiválasztása</title>

    <link type="text/css" rel="stylesheet" href="lib/foundation-6.4.2/foundation.min.css" />
    <link type="text/css" rel="stylesheet" href="lib/jquery-ui-1.12.1/jquery-ui.min.css" />

    <link type="text/css" rel="stylesheet" href="css/hp.css" />
</head>

<body>

    <div class="top-bar">
        <div class="top-bar-left">
            <ul class="menu">
                <li class="menu-text">NKP HTML-szerkesztő</li>
            </ul>
        </div>
        <div class="top-bar-right">
            <ul class="menu">
                <li class="menu-text"><?=$_SESSION["user_name"]?></li>
                <li><a href="login.php?action=logout">Kijelentkezés</a></li>
            </ul>
        </div>
    </div>

    <br>

    <div class="grid-container">
        <h3>Tankönyv kiválasztása: <?=$shared_user_root?></h3>

        <table class="small">
            <thead>
                <tr>
                    <th>Alkönyvtár</th>
                    <th>Eredeti html szerkesztése</th>
                    <!--<th>Eredmény html letöltése</th>-->
                </tr>
            </thead>
            <tbody>

                <?php

                    $entries = get_original_html_files($shared_user_root, "");

                    foreach($entries as $entry) {
                    list($path, $file) = $entry;
                    $path = substr($path, 1);

                    $original_html_path = $shared_user_root."/".$path."/".$file;

                ?>

                    <tr>
                        <td>
                            <?=$path?>
                        </td>

                        <td>
                            <a href="editor.php?path=<?=$original_html_path?>">
                                <?=$file?>
                            </a>
                            <small>(<?=date("Y-m-d H:i", filemtime($original_html_path))?>)</small>
                        </td>

                    </tr>
                <?php
                    }
                ?>

            </tbody>
        </table>

    </div>

</body>
</html>
