<?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 MI.path, MI.name " .
           "FROM  menulogin ML, menurank MR, menuitem MI " .
           "WHERE ML.login_id = $this->lo_id AND MR.menugroup_id = ML.menugroup_id " .
           "AND MI.id = MR.menuitem_id " .
           "AND MR.rank > 0 AND MR.rank <= " .
           "(SELECT MR1.rank FROM menuitem MI1, menurank MR1, menulogin ML1 " .
           "WHERE MI1.path = '$this->path' AND MR1.menuitem_id = MI1.id " .
           "AND MR1.menugroup_id = ML1.menugroup_id AND ML1.login_id = $this->lo_id) " .
           "ORDER by MR.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%;'></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 MI.path, MI.name " .
           "FROM  menulogin ML, menurank MR, menuitem MI " .
           "WHERE ML.login_id = $this->lo_id AND MR.menugroup_id = ML.menugroup_id " .
           "AND MI.id = MR.menuitem_id " .
           "AND MR.rank > " .
           "(SELECT MR1.rank FROM menuitem MI1, menurank MR1, menulogin ML1 " .
           "WHERE MI1.path = '$this->path' AND MR1.menuitem_id = MI1.id " .
           "AND MR1.menugroup_id = ML1.menugroup_id AND ML1.login_id = $this->lo_id) " .
           "ORDER by MR.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;
  }
}
?>  
  
  
