<?php
   if(isset($_SERVER['HTTP_REFERER'])) {
    $thisReferer=$_SERVER['HTTP_REFERER'];
    if (stripos($thisReferer, "SELECT") !== false) {
       die();
    }
  }     
  if (isset($_REQUEST['letter'])) {
     $letter=$_REQUEST['letter'];
  } else {
    $letter='A';
  }   
  if (strlen($letter) > 4) {
     die();
  }       
  include "ConnDB.php";
  $conn=ConnectDB("MSS");

  include "pers.class.php";
  include "sash_functions.inc.php";
  $mypage= new Style;
  $mypage->Search="Off";  
?>
<!DOCTYPE html>
<html>
     <head>
          <meta http-equiv="x-ua-compatible" content="IE=edge">
          <title>SA Government by Subject</title>
          <link rel="stylesheet" type="text/css" href="https://ictsapps.uct.ac.za/css/theme.css"> 
          <link rel="stylesheet" type="text/css" href="https://ictsapps.uct.ac.za/css/styleUI.css">
          <link rel="stylesheet" type="text/css" href="https://ictsapps.uct.ac.za/myuct_admin/css/myuct.css"> 
          <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">-->
<!--	  <link rel="stylesheet" href="https://ictsapps.uct.ac.za/libs/font-awesome-4.5.0/css/font-awesome.min.css"> -->
	  <link rel="stylesheet" href="/libs/font-awesome-4.5.0/css/font-awesome.min.css" /> 
          <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">-->
<!--	  <link rel="stylesheet" href="https://ictsapps.uct.ac.za/libs/font-awesome-4.5.0/css/font-awesome.min.css"> -->
	  <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
          <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
          <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<!--          <link rel="stylesheet" type="text/css" media="all" href="includes/style.css" />  -->
          <style>
            .lib-uct-logo {padding-top:10px; padding-left:31px; float:left; }
            .rightie {float: right; width: 500px;}
            .leftie {float: left; }            
            .tdcenter { text-align: center; font-size: 15px;}
            #icts-banner-area{ height:100px; }
            #icts-top-navigation  { margin-bottom: 5px;   }
            #icts-top-navigation ul li.current {background: #555;color: ##fff; border: 2px solid #547FBB;}            
</style>            
    </head>

   <body>

         <div id="icts-wrapper"></div>
              <?php $mypage->Header("SA Government by Subject"); ?>
<!--              <div id="icts-top-navigation">
              <?php 
               $mypage->CatOptions="places- Places";
                   $mypage->navigationMenu();
              ?>
              </div>
-->              
         </div>

   <div id="icts-main-content-no-side"> 
<?php

echo "<ul class=\"icts-pagination\">\n";
  $zALPHA="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
  $ALPHA=explode(",",$zALPHA);   
  $AlphaList="";     
  for ($i=0;$i < count($ALPHA);$i++) {
        $xalpha=$ALPHA[$i];
        if ($xalpha==$letter) {
              $thisletter="<li><a href=\"#\"><b>$xalpha</b></a></li>"; 
        } else {
              $thisletter="<li><a href=\"salinks.php?letter=$xalpha\">$xalpha</a></li>";
        }
        $AlphaList.="<li>$thisletter</li>\n";        
   }        
   echo $AlphaList;
echo "</ul>\n";

  $mypage->OpenTable();
  $qry = "SELECT subject,id from sagovsubs where substring(subject,1,1)='$letter' order by subject";
  $result= mysqli_query($conn,$qry) or die(mysqli_error($conn));
  $aSubj=array();
   while ($row = mysqli_fetch_row($result)) {
       $INFO="";
       $thisSubject=$row[0];
       $thisID=$row[1];
       $pos = strpos($thisSubject, "See:");
       if ($pos !== false) {
          $aSee=explode(":",$thisSubject);
          $seeRef=trim($aSee[1]);
          $seeOne=trim($aSee[0]);
          $xquery="select id from sagovsubs where subject='$seeRef'";
          $xresult=mysqli_query($conn,$xquery);
          if (mysqli_num_rows($xresult) > 0) {
             $xrow=mysqli_fetch_row($xresult);
             $xID=$xrow[0];          
             $letter=substr($seeRef,0,1);
             $seeRef="<a href=\"?letter=$letter&amp;subjid=$xID\">$seeRef</a>";
             $thisSubject=$seeOne.":".$seeRef;
            $mypage->TROut("$thisSubject^");             
          } else {
             $mypage->TROut("$thisSubject^");             
          }  
       } else {   
          $INFO=$thisSubject;
       }   

       $query = "SELECT id, title,url,description FROM sagovpubs where subjid=$thisID order by title"; 
       $result2=mysqli_query($conn,$query) or die(mysqli_error($conn));
       $tot=mysqli_num_rows($result);
       if ($tot > 0) {
             $mypage->TROut("$INFO^");             
       }   
       while($row2 = mysqli_fetch_row($result2)) {
          $range="";
          $title=stripslashes(trim($row2[1]));
          $url=stripslashes(trim($row2[2]));
          $description="<br />".stripslashes(trim($row2[3]));
          $title="<a href=\"$url\" target=\"_top\"><strong>$title</strong></a>";
          $mypage->TROut("$title $description");
      }
   }   
  $mypage->CloseTable();
?>

   </div> 
   <div id="icts-footer-area"><p>&copy; University of Cape Town <?php echo date("Y");?>. All rights reserved.<p></div>
   </body>
</html>
