<?php  ### calculate.php ###
//
//  Die PEAR-Basisinstallation muss eingerichtet sein!
//
//  ToDo:  pi^e lässt sich ausrechnen
//         e^pi verursacht einen Fehler bei der Erkennung de Tokens von pi
//         
//         Der Tokenizer muss also überarbeitet werden
//           
//  heute erledigt (2009-07-14) Thomas Schmieder, bitworks.de 
//
//
//

#-----------------------------------------------------------------------------------
include('CLASS_RPN.php');

$rpn = new Math_Rpn();

#-----------------------------------------------------------------------------------
function make_table($_records)
{
    $out = '<p>keine Einträge vorhanden</p>';

    if ($_records)
    {
        $out = "\r\n    <table class=\"record\">\r\n";

        $_heads = array_keys($_records[0]);

        $out .= "        <tr>\r\n";        
        
        foreach($_heads as $name)
        {
            $out .= "            <td class=\"caption\">"
                 . htmlspecialchars(ucfirst(strtolower($name))) ."</td>\r\n";                
        }

        $out .= "        </tr>\r\n";        

        foreach($_records as $key => $_fields)
        {
            $out .= "        <tr>\r\n";        

            foreach($_fields as $data)
            {
                $out .= "            <td class=\"data\">"
                     . htmlspecialchars($data) ."</td>\r\n";                
            }

            $out .= "        </tr>\r\n";        
        }

        $out .= "    </table>\r\n";
    }

    return $out;
}

#===================================================================================
# php main
#===================================================================================
$_ctrl['angletype']['deg'] = 'checked="checked"';
$_ctrl['angletype']['rad'] = false;
$angletype = 'deg';


if (isset($_POST['ctrl']['angletype']))
{

    if ($_POST['ctrl']['angletype'] == 'deg')
    {
        $_ctrl['angletype']['deg'] = 'checked="checked"';
        $_ctrl['angletype']['rad'] = false;
        $angletype = 'deg';
    }
    elseif($_POST['ctrl']['angletype'] == 'rad')
    {
        $_ctrl['angletype']['rad'] = 'checked="checked"';
        $_ctrl['angletype']['deg'] = false;
        $angletype = 'rad';
    }
}    

#-----------------------------------------------------------------------------------

if (isset($_POST['btn']['calculate']))
{
    if (isset($_POST['data']['string']))
    {
        $result = $rpn->calculate($_POST['data']['string'], $angletype, false);
        file_put_contents('aufgaben.html', "<p>".date('Y-m-d, H:i:s, ')
            .$_SERVER['REMOTE_ADDR'].'('.$rpn->get_version().'): <b>'
            .htmlspecialchars($_POST['data']['string'])." = $result</b>, "
            .htmlspecialchars(str_replace("\r\n", "\t", $_POST['data']['comment']))
            ."<p>\r\n", FILE_APPEND);
        
        $_file = file ('aufgaben.html');
        $_file = array_reverse($_file);
        $result = htmlspecialchars($result)."\r\n<hr>\r\n" . implode($_file);
    }    
}
elseif (isset($_POST['btn']['showops']))
{
    $_ops = $rpn->getOperators();
}
elseif (isset($_GET['query']) and is_string($_GET['query']) )
{
    $result = $rpn->calculate($_GET['query'], $angletype, false);
    
    if (isset($_GET['plaintext'])) 
    {
        header('Content-Type: text/plain');
        die("$result");
    }    
}


 
####################################################################################
# HTML output
####################################################################################
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>PHP-Calculator für Infix-Notation</title>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <meta name="robots" content="index,follow">

    <style type="text/css">

        * html div, * html ul 
        {
           height:1%;  /* notwendiges hasLayout für IE 6 */
        }

        body
        {
          font-family:century gothic, tahoma, arial,sans-serif;
          font-size: 0.8em;
          height: 100%;
          margin:10px;
        }

        table.record
        {
            border-collapse: collapse;
            empty-cells: show;
            border: 1px solid #000080;
            margin-bottom: 10px;
        }

        table.record td
        {
            border: 1px solid #000080;
            padding:4px;
        }

        td.caption
        {
            width: 20px;
            font-weight: bold;
            background-color: #C0C0FF;
        }

        td.data
        {
            white-space:nowrap;
        }

    </style>

</HEAD>

 
<BODY style="font-family: monospace; font-size:14px;">

    <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" enctype="multipart/form-data">
        
        <p>Aufgabe: <input style="font-family: monospace; font-size:14px;" type="text" name="data[string]" size="50"
        <?php if(isset($_POST['data']['string']))
              {
                  echo " value=\"" . htmlspecialchars($_POST['data']['string']) . "\"";
              }
              elseif(isset($_GET['query']) and is_string($_GET['query']) )
              {
                  echo " value=\"" . htmlspecialchars($_GET['query']) . "\"";
              }
              
        ;?> >
        
        <input type="submit" name="btn[calculate]" value="ausrechnen"></p>

        <p>Kommentar: <textarea rows="5" cols="50" style="font-family: monospace; font-size:14px;" 
                                name="data[comment]"><?php
              if(isset($_POST['data']['string']))
              {
                  echo htmlspecialchars($_POST['data']['comment']);
              }

        ?></textarea></p      

        <p style="margin-left: 100px;"><input type="submit" name="btn[showops]" value="mögliche Rechenarten anzeigen">
           &nbsp;<b>Winkel:</b> 
           Grad<input type="radio" name="ctrl[angletype]" value="deg" <?php echo $_ctrl['angletype']['deg']; ?> >
           Bogenmaß<input type="radio" name="ctrl[angletype]" value="rad" <?php echo $_ctrl['angletype']['rad']; ?> >
        </p>
        
        <?php 
        
            if(isset($result)) echo "<p><strong>Ergebnis: </strong>$result</p>\r\n"; 
            if(isset($_ops))
            {
                echo make_table ($_ops);
            }
        
        ?>
    
    </form>
 
</BODY>
</HTML>