<?php
include_once("_nemo.basic.cls.php");
@include_once("_nemo.menu.cls.php");

class Nemo extends NemoBasic
{
   public $Pages; // $Pages[URL]->Security->blnX = X;
                  // $Pages[URL]->Filter[X] = X;
                  // $Pages[URL]->Sort[]
                  // $Pages[URL]->PageNumber = p

   public $Menu;
   public $Entity;

   public $LastVisited = array();

   public $ToolBar;
   protected $Buttons = array();

   public function __construct()
   {
      parent::__construct();

      if(!isset($_SESSION[USER]))
      {
         windowLocation($this->SystemSettings[SessionExRedirect]);
         exit();
      }

      $this->iniSecurity(); //must be after $_SESSION load
      $this->iniSort();
      $this->iniFilters();
      $this->iniLastVisited();
      $this->iniToolbar();
      $this->iniPaging();

   }

//*************************
//***PUBLIC FUNCTIONS******
//*************************

   public function Display($index="")
   {
      //print_rr($this->Layouts);
      if(empty($index))
         $index = "layout.back.incl.php";

      include_once($this->Layouts[$index]);
   }




//*************************
//***PROTECTED FUNCTIONS***
//*************************
   protected function Header()
   {//override basic.header()
      $db = "";
      if($this->SystemSettings[SERVER_NAME] == "lamp"){
         global $DATABASE_SETTINGS;
         $db = ": ". $DATABASE_SETTINGS[lamp]->database;
      }
      return "
      <div id='divHeader'>
         ". $this->SystemSettings["Brand"]. $db ."
         <div id=divLogout>
            <a href='". $this->SystemSettings[LogoutRedirect] ."'>Logout: ". $this->SystemSettings[USER]->USERNAME ."</a>
         </div class=Logout>
      </div id='divHeader'>";//SystemSettings["Title"] ."
   }

   protected function Logout()
   {//gets called from layout.basic
      return "";
   }

   protected function Menu()
   {//gets called from layout.basic

      $this->Menu = new NemoMenu($this);
      $this->Menu->BuildMenu();

      //print_rr($this->Menu);

      return "
      <div id=divMenu class='menu'>
        ". $this->Menu->getControl() ."
      </div id=divMenu>";
   }

   protected function LastVisited()
   {//gets called from layout
      $pipe = "";
      if(count($this->LastVisited) > 0){
      foreach($this->LastVisited as $idx => $Entity)
      {
         $strLastVisited .= "$pipe<a href='$Entity->URL'>$Entity->Name</a>";
         $pipe = " | ";
      }
      }

      return "
      <div id=divLastVisited>
         Last Visited: ". $strLastVisited ."
      </div class=Logout>";
   }

   protected function ToolBar()
   {
      //$this->renderToolbar();

      return "
      <div id='divToolBar'>
         <div style='float:left;'>
            <table class='tblBlank'>
               <tr>
                  <td><img src='images/extraimages/header/icon-48-module.png' height='44'></img></td>
                  <td style='padding: 16px;'><span class='textHeading'>". $this->ToolBar->Label ."</span></td>
                  <td style='position: relative; border: 0px dashed orange; padding: 0px;'>". $this->ToolBar->Block . $this->renderFilters() ."</td>
               </tr>
            </table>
         </div>
         <div class='divButtons'>
            ". $this->renderToolbarButtons() ."
         </div>
      </div>
      ";

   }



//*************************
//***PRIVATE FUNCTIONS*****
//*************************

   private function iniSecurity()
   {

      $rst = $this->db->doQuery("SELECT sysMenuLevel2.strEntity AS Entity, sysMenuLevel2.strUrl AS Url, sysSecurity.blnView, sysSecurity.blnDelete, sysSecurity.blnSave, sysSecurity.blnNew, sysSecurity.blnSpecial
                                  FROM sysSecurity INNER JOIN sysMenuLevel2 ON sysSecurity.refMenulevel2ID = sysMenuLevel2.MenuLevel2ID
                                  WHERE sysSecurity.refSecurityGroupID=". $this->SystemSettings[USER]->SECURITYGROUPID ."",0);
      while($row = $this->db->fetch())
      {
         $this->Pages[$row->Url]->Security = $row;
         $this->Pages[$row->Url]->Entity = $row->Entity;
      }
      $this->Security = $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Security;

      //print_rr($this->SystemSettings);
      if($this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Security->blnView != 1){
         if($_SESSION[intAttempt] > 5){
            windowLocation("message.php?MID=e10&p=". $this->SystemSettings[SCRIPT_NAME] ."");
         }
         else{
            windowLocation("message.php?MID=r09&p=". $this->SystemSettings[SCRIPT_NAME] ."");
         }

         $_SESSION[intAttempt] += 1;
         //print_rr($this->Pages);
      }else{
         $_SESSION[intAttempt] = 0;
      }

      //set Entity
      $this->Entity->Name = $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Entity;
      $this->Entity->URL = $this->SystemSettings[FULL_URL];
   }

   private function iniSort()
   {
      //set sort
      if($_GET[exeClearSort] || $_REQUEST[Action] == "Clear")
      {
         unset($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort);
      }
      if($_GET[srtNew] != "")
      {
         $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort[srtNew] = $_GET[srtNew];
         $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort[srtCurrent] = $_GET[srtCurrent];
         $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort[srtDir] = $_GET[srtDir];
      }

      //load sort
      if(count($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort) > 0){
      foreach($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Sort as $key => $value)
      {//? Do we care about other pages' filters at this point? no
         $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Sort[$key] = $value;
      }}
   }

   protected function iniFilters()
   {  /*notes: filters have 2 stages. stage 1, iniFilters, executes onConstruct of the Nemo. stage 2, renderFilters(), executes onDisplay() of Nemo
      *     : $this->Filters referst to the public Filters collection in the Child-Class, eg. Client->Filters
      *
      *
      */

      //save || delete session filters
      if(($_REQUEST[Action] == "Filter" || $_POST[Action] == "Run Report") && count($this->Filters) >0 )
      {//&& because $this->Filters doesn't have an ini value when $page = new Nemo(), but has in $page = new Client() [extends Nemo]
         foreach($this->Filters as $key => &$filter)
         {
            $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Filters[$key] = $_REQUEST[$key];
         }//20110916 - changed post to request - pj
      }
      //print_rr($_SESSION[PAGES]);
      //print_rr($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Filters);
      if($_REQUEST[Action] == "Clear")
      {
         unset($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Filters);
      }

      //load filter values from session
      if(count($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Filters) > 0){
      foreach($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Filters as $key => $value)
      {//? Do we care about other pages' filters at this point? no
         $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Filters[$key] = $value;
      }}

      //build filter controls
      if(count($this->Filters)>0){
      foreach($this->Filters as $key => &$filter)
      {
         $filter->html->name = $filter->html->id = $key;
         if($this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Filters[$key] != ""){//load filter value from session
            $filter->html->value = $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Filters[$key];
            if($filter->html->type == "checkbox")
               $filter->html->checked = "checked";
         }

         if($filter->tag == "select"){//load options from sql
            $filter->html->innerHTML = $this->db->ListOptions($filter->sql, $filter->html->value);
         }

         //print_rr($this->Filters[$key]);
      }}
   }

   private function iniPaging()
   {//new 20111010 - pj
      if($_REQUEST[Action] == "Filter" || $_GET[exeClearSort] || $_REQUEST[Action] == "Clear" || $_GET[srtNew] != "")
      {//if new sorting, restart paging
         unset($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Paging);
      }

      if($_GET[pagPageNumber] != "")
      {
         $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Paging[PageNumber] = $_GET[pagPageNumber];
      }

      if($_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Paging[PageNumber] < 1)
         $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Paging[PageNumber] = 1;

      $this->Pages[$this->SystemSettings[SCRIPT_NAME]]->PageNumber = $_SESSION[PAGES]->Entity[$this->SystemSettings[SCRIPT_NAME]]->Paging[PageNumber];

   }

   protected function renderFilters()
   {//note $this->Filters referst to the public Filters collection in the Child-Class, eg. Client->Filters
      //print_rr($this->Pages[$this->SystemSettings[SCRIPT_NAME]]->Filters);
      global $SP, $BR;
//INI
      $strFilterControls = "";

//go:
      if(count($this->Filters)>0){//
         $i = 0;
         foreach($this->Filters as $key => &$filter){
            $filter->ControlHTML = NemoControl::renderControl($filter->tag, $filter->html);

            if($i % 3 == 0 && $i != 0)
            {
               $strFilterControls .= "</tr><tr>";
            }
            $strFilterControls .= "<td align='right' nowrap><label for=\"$key\">". NemoDetails::cleanColumnName($key) .":</label></td><td align='left'>". $filter->ControlHTML ."</td>";
            $i++;
         }

         $strFilterControls .= "
            <td rowspan=2 valign=middle align=center ></td>";

      //print_rr($this->Filters);
         return "<table class='tblBlank' style='margin-bottom: 0px;'><tr>$strFilterControls</tr></table>
            </td>
            <td nowrap><input type=submit name='Action' id='btnFilter' class='btn' value='Filter'>
             $SP<input type=submit name='Action' id='btnClear' class='btn' value='Clear'>"; //</td> ends outside in the calling function
      }
   }

   private function iniLastVisited()
   {

      if(count($_SESSION[LASTVISITED]) > 0){
      foreach($_SESSION[LASTVISITED] as $idx => $value){
         array_push($this->LastVisited, $value);
      }
      }

      if($this->Entity->Name != "")
      {
         $this->pushLastVisited($this->Entity);
      }

      if(count($this->LastVisited) > $this->SystemSettings[LastVisited])
      {
         while(count($this->LastVisited) > $this->SystemSettings[LastVisited])
         {
            array_pop($this->LastVisited);
         }
      }



      $_SESSION[LASTVISITED] = $this->LastVisited;

      //print_rr($_SESSION[LASTVISITED]);
      //print_rr($this->LastVisited);

   }

   protected function pushLastVisited($Entity)
   {
      $this->LastVisited = array_reverse($this->LastVisited);
      array_push($this->LastVisited, $Entity);
      $this->LastVisited = array_reverse($this->LastVisited);

      $_SESSION[LASTVISITED] = $this->LastVisited;
   }

   public function overrideLastVisited($Entity)
   {
      $this->LastVisited = array_reverse($this->LastVisited);
      array_pop($this->LastVisited);
      array_push($this->LastVisited, $Entity);
      $this->LastVisited = array_reverse($this->LastVisited);

      $_SESSION[LASTVISITED] = $this->LastVisited;

   }

   protected function renderToolbarButtons()
   {
//print_rr($this->ToolBar->Buttons);
      foreach($this->ToolBar->Buttons as $id => $control)
      {
         if($control->blnShow == 1)
         {
            $button = NemoControl::renderControl($control->Control->tag, $control->Control);
            $span = NemoControl::renderControl($control->Span->tag, $control->Span);

            $toolbar .= "
            <td >
               $span
               $button
            </td >
            ";
         }
      }

      return "
      <table border='0' id='toolbar' class='toolbar' style='border:0px purple dashed;'>
         <tr>
            $toolbar
         </tr>
      </table>
      ";
      //print_rr($this->ToolBar->Buttons);

   }

   protected function iniToolbar()
   {
      //print_rr($this);

      $this->Buttons[btnPreview]->Control->class = "linkbutton";
      $this->Buttons[btnPreview]->Control->value = "Preview";
      $this->Buttons[btnPreview]->Span->class= "icon-32-search";

      //default 4
      $this->Buttons[btnReload]->Control->class = "linkbutton";
      $this->Buttons[btnReload]->Control->value = "Reload";
      $this->Buttons[btnReload]->Span->class= "icon-32-refresh";

      $this->Buttons[btnNew2]->Control->class = "linkbutton";
      $this->Buttons[btnNew2]->Control->value = "New";
      $this->Buttons[btnNew2]->Span->class= "icon-32-edit";

      $this->Buttons[btnNew]->Control->class = "linkbutton";
      $this->Buttons[btnNew]->Control->value = "New";
      $this->Buttons[btnNew]->Span->class= "icon-32-new";

      $this->Buttons[btnDelete]->Control->class = "linkbutton";
      $this->Buttons[btnDelete]->Control->value = "Delete";
      $this->Buttons[btnDelete]->Control->onclick = "return confirm(\"Are you sure you want to delete ". $this->Entity->Name ."(s) and related entities?\");";
      $this->Buttons[btnDelete]->Span->class = "icon-32-trash";

      $this->Buttons[btnRevert]->Control->class = "linkbutton";
      $this->Buttons[btnRevert]->Control->value = "Revert";
      $this->Buttons[btnRevert]->Control->onclick = "";
      $this->Buttons[btnRevert]->Span->class = "icon-32-revert";

      $this->Buttons[btnRevert2]->Control->class = "linkbutton";
      $this->Buttons[btnRevert2]->Control->value = "Revert2";
      $this->Buttons[btnRevert2]->Control->onclick = "";
      $this->Buttons[btnRevert2]->Span->class = "icon-32-revert";

      $this->Buttons[btnSend]->Control->class = "linkbutton";
      $this->Buttons[btnSend]->Control->value = "Send";
      $this->Buttons[btnSend]->Span->class = "icon-32-send";

      $this->Buttons[btnSave]->Control->class = "linkbutton";
      $this->Buttons[btnSave]->Control->value = "Save";
      $this->Buttons[btnSave]->Control->onclick = "return jsNemoValidateSave();";
      $this->Buttons[btnSave]->Span->class = "icon-32-save";


      $this->Buttons[btnClose]->Control->class = "linkbutton";
      $this->Buttons[btnClose]->Control->value = "Close";
      $this->Buttons[btnClose]->Span->class = "icon-32-cancel";

      $this->Buttons[btnExport]->Control->class = "linkbutton";
      $this->Buttons[btnExport]->Control->value = "Export";
      $this->Buttons[btnExport]->Span->class = "icon-32-export";

      //extra
      $this->Buttons[btnNext]->Control->class = "linkbutton";
      $this->Buttons[btnNext]->Control->value = "Next";
      $this->Buttons[btnNext]->Span->class = "icon-32-forward";

      $this->Buttons[btnBack]->Control->class = "linkbutton";
      $this->Buttons[btnBack]->Control->value = "Back";
      $this->Buttons[btnBack]->Span->class = "icon-32-back";

      $this->Buttons[btnHelp]->Control->class = "linkbutton";
      $this->Buttons[btnHelp]->Control->value = "Help";
      $this->Buttons[btnHelp]->Span->class = "icon-32-help";

      $this->Buttons[btnFinalize]->Control->class = "linkbutton";
      $this->Buttons[btnFinalize]->Control->value = "Finalize";
      $this->Buttons[btnFinalize]->Span->class = "icon-32-finalize";

      foreach($this->Buttons as $id => $control)
      {

         $this->ToolBar->Buttons[$id]->Control = nCopy($control->Control);
         $this->ToolBar->Buttons[$id]->Control->id = $id;
         $this->ToolBar->Buttons[$id]->Control->name = "Action";
         $this->ToolBar->Buttons[$id]->Control->type = "submit";
         $this->ToolBar->Buttons[$id]->Control->tag = "input";

         $this->ToolBar->Buttons[$id]->Span = nCopy($control->Span);
         $this->ToolBar->Buttons[$id]->Span->tag = "span";
         $this->ToolBar->Buttons[$id]->Span->title = $control->Control->value;

         $this->ToolBar->Buttons[$id]->blnShow = $control->blnShow;

      }
      //print_rr($this->ToolBar->Buttons);
      $this->ToolBar->Label = $this->Entity->Name;
   }

}




?>
