<?php
  if (isset($_GET['source'])) {
        highlight_file($_SERVER['SCRIPT_FILENAME']);
        exit;
  }
?>
<html>
<head>
<title>Generating Email Addresses</title>
    
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="theme-color" content="#B5C7DD" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../css/reset.css">
        <link rel="stylesheet" href="../css/mandy.css">
        <link rel="stylesheet" href="../css/form.css">

<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
</head>
<body>

<h2 class="pad-top">    
    
<?php   # need this to be able to view source in the below anchor tag
  $urlPath=dirname($_SERVER['SCRIPT_URL']).'/'; 
  $labsPath=$urlPath.'labs/'; 
?>     

    <a href="../index.php">CS 130A</a> → Lab 11
<a href="<?=$urlPath?>index.php?source" target="labs">Source code</a>

</h2>   


<h4>Regular Expressions - Generating Email Addresses</h4>
    <p class="nudge in">Making email links out of an instructor list text file called <a href="cis.txt" target="_blank">cis.txt</a></p>

    <!--blue hover-->
<!--
    <style>
    table {color: var(--midtone); font-size: smaller; position: absolute; z-index: 2; max-width: 700px; margin: 0 0 0 -64px; background: rgba(253, 252, 246, 0.88)}
        
    tbody tr:hover {background-color: rgba(181, 199, 221, 0.33); color: var(--dark)}
        
    tr:nth-child(odd) {background-color: rgba(153, 149, 144, 0.07)}
/*        body, .stretch {padding:  0;}*/
    
    table a {color: #9bb9de; text-transform: lowercase}
    @media only screen and (max-width: 700px) {html {background-color: var(--light); background-image:none}
    </style>
-->
    
    <!--brown border hover-->
        <style>
    table {color: var(--midtone); font-size: smaller; position: absolute; z-index: 2; max-width: 700px; margin: 0 0 0 -64px; background: rgba(253, 252, 246, 0.88)}
    tr {border-bottom: 1px dashed rgba(137, 112, 82, 0); border-top: 1px dashed rgba(137, 112, 82, 0);}    
    tbody tr:hover 
        {
/*            background-color: rgba(158, 187, 221, 0.15);*/
            color: var(--dark); border-bottom: 1px solid rgba(137, 112, 82, 0.6); border-top: 1px solid rgba(137, 112, 82, 0.6); position: relative; z-index: 3}
        
    tr:nth-child(odd), #selected {background-color: rgba(153, 149, 144, 0.07)}
/*        body, .stretch {padding:  0;}*/
    
    table a {color: #9bb9de; text-transform: lowercase}
    @media only screen and (max-width: 700px) {html {background-color: var(--light); background-image:none} table {margin: 0 0 0 -24px;}}
        
        pre {position: fixed; top:0; background: var(--light); z-index: 5}
    </style>
    
    <style>input, .space.button:hover, .space.button:active, #selected {width: 3.5em; opacity: 1; border-color: rgba(153, 149, 144, 0.8)} .space.button {border-color: rgba(153, 149, 144, 0.38); opacity: .8} #selected {color: var(--dark)}
        .stretch {position:fixed; bottom: 0; padding: 340px }
    </style>
    
<form action='<?=$_SERVER['SCRIPT_URL']?>' method='post'>
  <?php     // main source code from lecture example via http://hills.ccsf.edu/~kfreedma/cs130a/examples/classlist.php?source
    
  $inputfile="cis.txt";

  $courseList = array();

  $handle = fopen($inputfile, "r") or die("Can't open file");
  $line = fgets($handle);
  // read file data into array.
  while(!feof($handle)) {
//  if (ereg("^[[:digit:]]{5} +CIS +([[:digit:]]{3}[[:alpha:]]?)", $line, $result)) {
      
      # reg modified to include ALL class results in its search
    preg_match("/^([[:digit:]]{5})(.*)CIS  ([[:digit:]]{3}[[:alpha:]]?[[:space:]]?) ([[:alnum:]]*)([[:blank:]]*)([[:alnum:]\.]*) ([[:alpha:]*[[:space:]]{1,}) ([[:alnum:]]*)  ([[:alnum:]]*)  ([[:alnum:]]*[[:space:]]?) ([[:alnum:]]*) ((?:(?![[:digit:]]).).*)/i", $line, $result); 
      
//      echo "<pre>";
//    count($result);  
//    print_r($result);
//      echo "</pre>";
//      
      
       # reg modified to include ALL class results in its search
      if (count($result)==13) {
      $newline=array();
      $newline['CRN']=(int)$result[1];
      $newline['SUBJ']='CIS';
      $course=$newline['CRSE']=trim($result[3]);
      $newline['SEC']=$result[4];
      $newline['UNIT']=$result[6];
//      $newline['DAYS']='<pre>'.$result[7].'</pre>';
      $newline['DAYS']=$result[7];      # with or without whitespace
      $newline['START']=$result[8];
      $newline['END']=$result[9];
      $newline['BLDG']=trim($result[10]);
      $newline['ROOM']=(int)$result[11];
      $newline['INSTRUCTOR']=trim($result[12]);
  //    print_r($newline);
  //    ([[:alpha:][:space:]]{7}) ([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+) 
      if ($newline['CRN']>0) {
        $emailparts=explode(',',$newline['INSTRUCTOR']);
        if (count($emailparts)>1) $newline['INSTRUCTOR']= '<a href="mailto:'.substr(trim($emailparts[1]),0,1).substr($emailparts[0],0,6).'@ccsf.edu">'.substr(trim($emailparts[1]),0,1).substr($emailparts[0],0,6). '@ccsf.edu</a>';
  //      print_r($emailparts); # names separated to last first parts, turned newline[INSTRUCTOR] into a href mailto:
          
//      echo "<pre>"; 
//    print_r($result);                       # raw nameless array from .txt file
//    print_r($newline);                      # nameless(?) arrays of all section infos
//    print_r($courses);                    # numerical arrrays of organized by course
//    print_r($course);                     # list of every section by course code
//    print_r($courseList);                   # incr.incr counts unique course types
//    print $newline['CRN'];
          
//      echo "</pre>";
  //      
        if (!in_array($course, $courseList)) {
           array_push($courseList, $course);
           $courses[$course]=array();
        }
        array_push($courses[$course],$newline);
      }
    }
    $line = fgets($handle);
  }
  fclose($handle);

  print "Pick a class: CIS ";
  print " ";
  if (!(isset($_POST['course']) && strlen($choice=trim($_POST['course'])))) {
    $choice=FALSE;
  }
  foreach ($courseList as $course) {
    print "<input type='submit' class='space button' name='course' value='$course'";
    if ($course==$choice) print " id='selected'";
    print ">";
  }


  function DisplayHeader($target) {
    print "<tr>\n";
    foreach (array_keys($target) as $key) {
      print "<th>$key</th>\n";
    }
    print "</tr>\n";
  }

  function DisplayRow($target) {
    print "<tr>\n";
    foreach (array_keys($target) as $key) {
      print "<td>$target[$key]</td>\n";
    }
    print "</tr>\n";
  }
?>
<!--
  <p>
    <input class="button" type='submit' value='Show Courses'>
  </p>
-->
</form>
<?php
if ($choice) {
  $now = date("Y-m-d H:m:s");
  $handle = fopen("lookuplog.txt", "a");
  fwrite($handle, "$now\t$course\n");
  fclose($handle);

  if (isset($courses[$choice])) {
    print "<table border=1 width='95%'>\n";
    DisplayHeader($courses[$choice][0]);
    foreach ($courses[$choice] as $line) {
      DisplayRow($line);
    }
  
    print "</table>\n";}
  } else if (strlen($choice)) {
    print "<p>No data for $choice</p>";
  } else print "<p>No Selection</p>";
?>


    
    <p class="hide">
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
</p>

    <div class="stretch">&nbsp;</div>
</body>
</html>