<?php
session_start();

defined('PUBLIC_ACCESS') or die('Restricted Access');

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

class MainMenu {

private $path;
  
  public function __construct($path, $lo_id) {

    $this->path = (isset($path)) ? $path : '';
    $this->lo_id = (isset($lo_id)) ? $lo_id : 0;

  }

  public function before() {

    $dal = new DAL();

    $sql = "SELECT MM.path, MM.name " . 
           "FROM loginmenu LM, mainmenu MM " . 
           "WHERE LM.login_id = $this->lo_id " .
           "AND MM.id = LM.mainmenu_id " .
           "AND LM.rank > 0 AND LM.rank <= " .
           "(SELECT LM1.rank FROM mainmenu MM1, loginmenu LM1 " .
           "WHERE MM1.path = '$this->path' AND LM1.mainmenu_id = MM1.id AND LM1.login_id = $this->lo_id) " .
           "ORDER by LM.rank ";
    $items = $dal->query($sql);
  
    $menuBefore = "";
    foreach ($items as $item) {
      $menuBefore .= "
<a name='tag$item->path'></a>
<a id='tag$item->path' href=\"#tag$item->path\"></a>
<div id='hdr$item->path' onclick='location.href=\"/$item->path\";' 
  style='width:100%;height:65px;background-color:#f1f1f1;cursor:pointer;border: 1px solid black;
         border-radius:5px 5px 0px 0px;border-bottom:none;'>
  <h4>$item->name</h4>

  <table class='table table-condensed'>
    <tr>
      <td style='width:30%;'><div id='hdrmsg$item->path' style='color:red;'></div></td>
      <td style='width:30%;'></div></td>
      <td style='width:20%;'><div id='login$item->path'></div></td>
      <td style='width:20%;'><div id='event$item->path'></div></td>
    </tr>
  </table>
</div>
      ";
    }
    return $menuBefore;
  }
  
  public function after() {

    $dal = new DAL();

    $sql = "SELECT MM.path, MM.name " . 
           "FROM loginmenu LM, mainmenu MM " . 
           "WHERE LM.login_id = $this->lo_id " .
           "AND MM.id = LM.mainmenu_id " .
           "AND LM.rank > " .
           "(SELECT LM1.rank FROM mainmenu MM1, loginmenu LM1 " .
           "WHERE MM1.path = '$this->path' AND LM1.mainmenu_id = MM1.id AND LM1.login_id = $this->lo_id) " .
           "ORDER by LM.rank ";
    $items = $dal->query($sql);

    $menuAfter = "";
    foreach ($items as $item) {
      $menuAfter .= "
<div id='hdr$item->path' onclick='location.href=\"/$item->path\";' 
  style='width:100%;height:60px;background-color:#f1f1f1;cursor:pointer;border: 1px solid black;
         border-radius:5px 5px 0px 0px;border-bottom:none;'>
  <h4>$item->name</h4>
  <div id='hdrmsg$item->path' style='color:red;'></div>
</div>
      ";
    }
    return $menuAfter;
  }
}
?>  
  
  
