<?php

  /*

      footer.php

      Common footer file. [Commented]

      ----

      Copyright (C) 2003  Central Manchester CLC
      Copyright (C) 2003  David Thorne (davethorne@gmail.com)

      This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License
      as published by the Free Software Foundation version 2.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   */

  require_once 'system/global_preferences.php';
  
  // Get all navigation category name
  function &navigation_categories()
  {
    static $navigation_tree;
    if (!isset($navigation_tree)) { $navigation_tree = Array(); }
    return $navigation_tree;
  }

  // Get all navigation subtrees
  function &navigation_subtrees()
  {
    static $navigation_subtrees;
    if (!isset($navigation_subtrees)) { $navigation_subtrees = Array(); }
    return $navigation_subtrees;
  }

  // Get a navigation category
  function &navigation_category($category)
  {
    // Create if not already present
    $navigation_categories = &navigation_categories();
    $navigation_subtrees = &navigation_subtrees();
    if (!in_array($category, $navigation_categories))
    {
      $navigation_categories[] = $category;
      $navigation_subtrees[$category] = Array();
    }
    
    return $navigation_subtrees[$category];
  }

  // Get a navigation item
  function add_navigation_item($category, $title, $link, $access_key = '')
  {
    $navigation_categories = &navigation_categories();
    $navigation_subtrees = &navigation_subtrees();
    if (!in_array($category, $navigation_categories))
    {
      $navigation_categories[] = $category;
      $navigation_subtrees[$category] = Array();
    }
    $category = &$navigation_subtrees[$category];
    $category[] = Array('title' => $title, 'link' => $link, 'access_key' => $access_key);
  }

  // Build navigation tree
  if ($_SESSION['_username'] == '') {
    // Set up sidebar links from DB
    $linkcatRS = db_recordset("SELECT * FROM tbl_Link_Categories where lang='".$_lang."' ORDER BY sortorder");
    foreach ($linkcatRS as $category) {
      $sql = "SELECT * FROM tbl_Links WHERE category={$category['id']} ORDER BY sortorder";
      $linkRS = db_recordset($sql);
      foreach ($linkRS as $link) {
        add_navigation_item($category['name'], $link['text'], $link['link']);
      }
    }
  } else {
  if ($_SESSION['_usertype'] != 'Root') {
      add_navigation_item('Issues', 'New Issue', 'newissue.php', 'n');
      add_navigation_item('Issues', 'My Issues', 'myissues.php', 'm');
      if ($_SESSION['_usertype'] != 'Client') {
        add_navigation_item('Issues', 'Unassigned Issue', 'unassignedissues.php', 'u');
        add_navigation_item('Issues', "My Sites' Issues", 'mysitesissues.php', 's');
      }
      add_navigation_item('Issues', 'Find Issue', 'find.php', 'f');
      add_navigation_item('Knowledge Base', 'Search', 'search.php');
      add_navigation_item('Knowledge Base', 'Recent', 'recent.php');
    }
    if ($_SESSION['_usertype'] != 'Client') {
      if ($_SESSION['_usertype'] != 'Root') {
        add_navigation_item('Statistics', 'Summary', 'summary.php', 'y');
        add_navigation_item('Statistics', 'Reports', 'reports.php');
      }
      // Don't display admin section if it would be empty otherwise
      if (!(($_SESSION['_usertype'] == 'Support Agent' ||
             $_SESSION['_usertype'] == 'Site Contact') ||
             $_SESSION['_restricted'] == '1')) {
        if ($_SESSION['_usertype'] == 'Root') {
          add_navigation_item('Admin', 'Domains', 'domains.php');
        }
        if ($_SESSION['_usertype'] != 'Client' && $_SESSION['_restricted']!='1') {
          add_navigation_item('Admin', 'Sites and Users', 'sitesandusers.php');
//           add_navigation_item('Admin', 'Assets', 'assets.php');
        }
        if ($_SESSION['_usertype'] == 'Domain Administrator' || $_SESSION['_usertype'] == 'Root') {
          add_navigation_item('Admin', 'Problem Categories', 'problemcategories.php');
          add_navigation_item('Admin', 'Levels', 'levels.php');
          add_navigation_item('Admin', 'Priorities', 'priorities.php');
          add_navigation_item('Admin', 'Statuses', 'statuses.php');
          add_navigation_item('Admin', 'Public Notice', 'publicnotice.php');
        }
        if ($_SESSION['_usertype'] == 'Root') {
          add_navigation_item('Admin', 'Problem Categories', 'publicnotice.php?global=true');
          add_navigation_item('Admin', 'Front Page News', 'managenews.php');
          add_navigation_item('Admin', 'Front Page Sidebar', 'links.php');
          add_navigation_item('Admin', 'Front Page Text', 'frontpagepara.php');
          add_navigation_item('Admin', 'Global Preferences', 'globalpreferences.php');
        }
      }
    }
    if ($_SESSION['_restricted'] != '1') {
      add_navigation_item('My Account', 'My Details', 'details.php', 'd');
      if ($_SESSION['_usertype'] != 'Root') {
        add_navigation_item('My Account', 'My Preferences', 'preferences.php', 'x');
      }
      // Domain wide preferences
      if ($_SESSION['_usertype'] != 'Client' &&
          $_SESSION['_usertype'] != 'Site Contact' &&
          $_SESSION['_usertype'] != 'Support Agent' &&
          $_SESSION['_usertype'] != 'Administrator') {
        add_navigation_item('My Account', 'Domain Preferences', 'preferences.php?type=domain');
      }
      if ($_SESSION['_usertype'] == 'Root') {
        add_navigation_item('My Account', 'Preference Defaults', 'preferences.php?type=default');
      }
    }
    add_navigation_item('My Account', 'Logout', 'logout.php', 'x');
  }
?>

        </div>
      </div>

      <div class="navigation_wrapper">
        <div class="navigation">
          <ul>
<?php
  // Build navigation
  foreach (navigation_categories() as $category)
  {
?>
            <li><span><?php echo $category; ?></span>
              <ul>
<?php
    $items = navigation_category($category);
    foreach ($items as $item)
    {
      echo '                <li><a href="'.$item['link'].'"'.($item['access_key']?' accesskey="'.$link['access_key'].'"':'').'>'.gettext($item['title']).'</a></li>'."\n";
    }
?>
              </ul>
            </li>
<?php
  }
?>
          </ul>
<?php
    // Conditionally show the other domains this user has access to and invite them to
    // jump straight to them.
    if ($_SESSION[_id]) {
    if ($_SESSION['_usertype'] == 'Root') {
      // Show all domains except the current one for the root user
      $sql = "SELECT * FROM tbl_Domains WHERE id<>$_SESSION[_domain] AND id>0 AND active=1";
      $accept = 'root';
    } else {
      // Show all of this user's domains except the current one
    ## $sql = "SELECT DISTINCT vw_Users.domain AS id,tbl_Domains.domain FROM vw_Users,tbl_Domains,vw_Sites WHERE vw_Users.domain=tbl_Domains.id AND tbl_Domains.active=1 AND vw_Users.id=$_SESSION[_id] AND tbl_Domains.id<>$_SESSION[_domain] AND vw_Sites.domain=vw_Users.domain AND vw_Sites.\"user\"=vw_Users.id";
      $sql = "SELECT DISTINCT tbl_Sites.domain as id, tbl_Domains.domain
              FROM tbl_Sites,tbl_Domains,tbl_UserSites
              WHERE tbl_UserSites.userid=$_SESSION[_id]
                AND tbl_Sites.id=tbl_UserSites.site
                AND tbl_Sites.domain != $_SESSION[_domain]
                AND tbl_Domains.id=tbl_Sites.domain";
    }
    $domainsRS = db_recordset($sql);
    if (count($domainsRS) > 0) {
      // If there are indeed other domains available then show them, otherwise don't
      // even show this menu item. Submitting this form passes execution back to the
      // login.php page and tells it to change the domain.
?>
    <div class="menutitle"><?php echo gettext("My Domains")?></div>
    <div class="menulogingroup">
      <form name="login" id="login" method="post" class="login" action="login.php">
        <input type="hidden" name="act" value="accept<?php echo $accept?>" />
        <select name="domain" style="width:13em;">
<?php
      foreach ($domainsRS as $record) {
        print "          <option value=\"$record[id]\">$record[domain]</option>\n";
      }
?>
        </select>
        <div class="loginbutton"><input type="submit"
            value="<?php echo gettext("Jump To");?>" /></div>
      </form>
    </div>
<?php
    }
    }
?>
        </div>
      </div>

      <!-- Here starts the footnote, with license and standards information -->
      <div class="footnote">
        Helpdesk Issue Manager v<?php echo $global_prefs['version']; ?>
        <br />
        <a href="http://validator.w3.org/check/referer" target="_blank" title="XHTML Transitional">XHTML</a>
        |
        <a href="http://jigsaw.w3.org/css-validator/" target="_blank" title="CSS2 Compliance">CSS</a>
        |
        <a href="http://www.w3.org/TR/WAI-WEBCONTENT/" title="Web Content Accessibility Guidelines">WCAG</a>
        |
        Licensed under the <a href="http://www.gnu.org/licenses/licenses.html#GPL" target="_blank" title="GNU Public License">GPL</a> (2003-2007)
        <br />
        This is a <a href="http://www.sourceforge.net/projects/helpmeict">Sourceforge.net project</a>. Visit the <a href="http://lists.sourceforge.net/lists/listinfo/helpmeict-discuss">discussion list</a> for further help.
      </div>

    </div>

    <script language="javascript" type="text/javascript">
    //<![CDATA[
    <!--
    stripe();
    //-->
    //]]>
    </script>
    
<?php
  // Include template
  require_once 'system/themes.php';
  include get_theme_path("footer");
?>
