<?php

include ('Faisceau.php');
include ('Cavité.php');
include ('Cible.php');

class Paramètres
{
  var $script;
  var $http;
  var $name;
  var $uri;

  var $twiss = 1;
  var $énergie = 2;

  var $champ = 3;
  var $longueur = 4;

  var $largeur = 5;

  var $épaisseur = 6;

  function Paramètres ()
    {
      $this->http = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . ":";
      $this->name = "//" . $_SERVER['SERVER_NAME'];
      $this->uri = $_SERVER['REQUEST_URI'];

      $this->script = $this->http . $this->name . $this->uri;

      $pos = strpos ($this->uri, "/pspa.php");
      $this->base = $this->http . $this->name . substr ($this->uri, 0, $pos);
     }
};

$Paramètres = new Paramètres ();

class TRANSPORT
{
  function TRANSPORT ()
    {
    }

  function run ($faisceau, $cavité)
    {
      global $Paramètres;

      $string = '<pspa></pspa>';

      $xml = new SimpleXMLElement($string);

      $f = $xml->addChild ("faisceau");
      $f->addAttribute ("twiss", $faisceau->twiss);
      $f->addAttribute ("energie", $faisceau->énergie);
      $c = $xml->addChild ("cavite");
      $c->addAttribute ("longueur", $cavité->longueur);
      $c->addAttribute ("champ", $cavité->champ);

      $xml->asXML('transport.xml');

      $sortie = new Faisceau ();
 
      //$h = popen ("Transport.exe transport.data", "r");

      $input = $Paramètres->base . "/Transport.php?data=transport.xml";
      echo "Transport: input={$input}<br>";
      $h = fopen ($input, "r");

      if ($h)
        {
          echo "Transport ok<br>";
          $s = '';
          while (($buffer = fgets ($h)) !== false)
            {
              $buffer = trim ($buffer);
              $s .= $buffer;
            }
          //echo htmlentities($s);
          //echo "<br>";

          $xml = new SimpleXMLElement($s);
          //print_r ($xml);
          //echo "<br>";

          $f = $xml->faisceau;
          $sortie->twiss = (double) $f['twiss'];
          $sortie->énergie = (double) $f['energie'];

          fclose ($h);
        }
      else
        {
          echo "impossible de lancer Transport<br>";
        }

      return ($sortie);
    }
};

class EGS
{
  function EGS ()
    {
    }

  function run ($cible, $faisceau)
    {
      global $Paramètres;

      $string = '<pspa></pspa>';

      $xml = new SimpleXMLElement($string);

      $f = $xml->addChild ("faisceau");
      $f->addAttribute ("twiss", $faisceau->twiss);
      $f->addAttribute ("energie", $faisceau->énergie);
      $c = $xml->addChild ("cible");
      $c->addAttribute ("epaisseur", $cible->épaisseur);

      $xml->asXML('egs.xml');

      $sortie = array();
      $f = new Faisceau ();

      $first = true;

      //$h = popen ("EGS.exe egs.data", "r");

      $input = $Paramètres->base . "/EGS.php?data=egs.xml";
      echo "EGS: input={$input}<br>";
      $h = fopen ($input, "r");
      if ($h)
        {
          echo "EGS ok<br>";
          $s = '';
          while (($buffer = fgets ($h)) !== false)
            {
              $buffer = trim ($buffer);
              $s .= $buffer;
            }
          //echo htmlentities($s);
          //echo "<br>";

          $xml = new SimpleXMLElement($s);

          foreach ($xml as $k => $v)
            {
              $nom = (string) $v;
              $f = new Faisceau ($nom, (double) $v['twiss'], (double) $v['energie']);
              $sortie[$f->nom] = $f;
            }

          fclose ($h);
        }

      return ($sortie);
    }
};



class Control
{
  function header ()
    {
      echo "
<html>
<head>
<style type='text/css'>
</style>
</head>
";
    }


  function get_post ()
    {
      global $_POST;
      global $Paramètres;

      foreach ($_POST as $cle => $value)
	{
          $words = explode ('_', $cle);

          $n = count ($words);

          $classe = $words[0];
          $champ = '';
          if ($n > 1) $champ = str_replace ($classe . '_', '', $cle);

          if (is_array ($value))
            {
              $str = "cle=($cle) _POST[{$classe}.{$champ}]=";
            }
          else
            {
              $Paramètres->$cle = $value;
            }
	}
    }


  function faisceau_entrée ()
    {
      global $Paramètres;

      echo "
  <tr>
    <td>
      <b><i>Définition du faisceau d'entrée</i></b>
    </td>
    <td>
      Twiss
    </td>
    <td>
      <input type='text' name='twiss' value='{$Paramètres->twiss}' size='6'>
    </td>
    <td>
      Energie
    </td>
    <td>
      <input type='text' name='énergie' value='{$Paramètres->énergie}' size='6'>
MeV
    </td>
  </tr>
";

    }

  function cavité ()
    {
      global $Paramètres;

      echo "
  <tr>
    <td>
      <b><i>Définition de la cavité</i></b>
    </td>
    <td>
      Champ
    </td>
    <td>
      <input type='text' name='champ' value='{$Paramètres->champ}' size='6'>
MV/m
    </td>
    <td>
      Longueur
    </td>
    <td>
      <input type='text' name='longueur' value='{$Paramètres->longueur}' size='6'>
m
    </td>
  </tr>
";

    }

  function distribution ()
    {
      global $Paramètres;

      echo "
  <tr>
    <td>
      <b><i>Distribution des particules</i></b>
    </td>
    <td>
      Largeur
    </td>
    <td>
      <input type='text' name='largeur' value='{$Paramètres->largeur}' size='6'>
m
    </td>
  </tr>
";

    }


  function cible ()
    {
      global $Paramètres;

      echo "
  <tr>
    <td>
      <b><i>Définition de la cible</i></b>
    </td>
    <td>
      Epaisseur
    </td>
    <td>
      <input type='text' name='épaisseur' value='{$Paramètres->épaisseur}' size='6'>
cm
    </td>
  </tr>
";

    }

  function faisceaux_sortie ($électrons, $photons, $positrons)
    {
      global $Paramètres;

      echo "
<table>
  <tr>
    <td colspan='2'>
      <center><hr></center>
    </td>
    <td>
      <center><i>Sortie</i></center>
    </td>
    <td colspan='2'>
      <center><hr></center>
    </td>
  </tr>
  <tr>
    <td>
      <b><i>Electrons</i></b>
    </td>
    <td>
      Twiss
    </td>
    <td>
      {$électrons->twiss}
    </td>
    <td>
      Energie
    </td>
    <td>
      {$électrons->énergie} MeV
    </td>
  </tr>
  <tr>
    <td>
      <b><i>Photons</i></b>
    </td>
    <td>
      Twiss
    </td>
    <td>
      {$photons->twiss}
    </td>
    <td>
      Energie
    </td>
    <td>
      {$photons->énergie} MeV
    </td>
  </tr>
  <tr>
    <td>
      <b><i>Positrons</i></b>
    </td>
    <td>
      Twiss
    </td>
    <td>
      {$positrons->twiss}
    </td>
    <td>
      Energie
    </td>
    <td>
      {$positrons->énergie} MeV
    </td>
  </tr>
</table>
";

    }

  function step1 ($faisceaux = null)
    {
      global $Paramètres;

      echo "
<table>
  <form enctype='multipart/form-data' action='{$Paramètres->script}' method='post'>
  <tr>
    <td colspan='5'>
      <i>Step 1</i>
    </td>
  </tr>
";

      $this->faisceau_entrée ();
      $this->cavité ();

      echo "
<tr>
  <td>
    <input name='largeur' value='{$Paramètres->largeur}' type='hidden' />
    <input name='épaisseur' value='{$Paramètres->épaisseur}' type='hidden' />
    <input type='submit' name='submit' value='Run'>
  </td>
</tr>
";

      echo "
  </form>
</table>
";

      echo "
<tr>
  <td colspan='5'>
    <center><hr></center>
  </td>
</tr>
";

      echo "
<tr>
  <td colspan='5'>
    <center>TRANSPORT</center>
  </td>
</tr>
";

      $faisceau = new Faisceau ("", $Paramètres->twiss, $Paramètres->énergie);
      $cavité = new Cavité ($Paramètres->champ, $Paramètres->longueur);

      $transport = new TRANSPORT ();
      $faisceau_intermédiaire = $transport->run ($faisceau, $cavité);

      //echo "twiss={$faisceau_intermédiaire->twiss}<br>";
      //echo "énergie={$faisceau_intermédiaire->énergie}<br>";

    echo "
<tr>
  <td colspan='5'>
    <center><hr></center>
  </td>
</tr>
";

      $sortie = array ("électrons" => $faisceau_intermédiaire);

      return ($sortie);
    }

  function step2 ($faisceaux)
    {
      global $Paramètres;

      $faisceau = NULL;
      foreach ($faisceaux as $k => $f)
        {
          $faisceau = $f;
        }

      echo "
<table>
  <form enctype='multipart/form-data' action='{$Paramètres->script}' method='post'>
  <tr>
    <td colspan='5'>
      <i>Step 2</i>
    </td>
  </tr>
";

      $this->distribution ();
      $this->cible ();

      echo "
<tr>
  <td>
    <input name='twiss' value='{$Paramètres->twiss}' type='hidden' />
    <input name='énergie' value='{$Paramètres->énergie}' type='hidden' />
    <input name='champ' value='{$Paramètres->champ}' type='hidden' />
    <input name='longueur' value='{$Paramètres->longueur}' type='hidden' />
    <input type='submit' name='submit' value='Run'>
  </td>
</tr>
";

      echo "
  </form>
</table>
";

      echo "
<tr>
  <td colspan='5'>
    <center><hr></center>
  </td>
</tr>

<tr>
  <td colspan='5'>
    <center>EGS</center>
  </td>
</tr>
";

      $cible = new Cible ($Paramètres->épaisseur);

      $egs = new EGS ();
      $sortie = $egs->run ($cible, $faisceau);

    echo "
<tr>
  <td colspan='5'>
    <center><hr></center>
  </td>
</tr>
";

      return ($sortie);
    }

  function init ()
    {
      global $Paramètres;

      $this->get_post ();
      $this->header ();
    }

  function run ()
    {
      global $Paramètres;

/*
      echo "
<table>
  <tr>
    <td><center>
      <b><i>Valeurs</i></b>
    </center></td>
    <td><center>
      Twiss = {$Paramètres->twiss} Energie = {$Paramètres->énergie} Champ={$Paramètres->champ} Largeur = {$Paramètres->largeur}
Epaisseur={$Paramètres->épaisseur}
    </center></td>
  </tr>
</table>
<hr>
";
*/

      $steps = array ('Transport' => 'step1', 'EGS' => 'step2');

      $this->init ();

      $faisceaux = null;

      foreach ($steps as $step => $action)
        {
          $faisceaux = $this->$action ($faisceaux);
        }

/*
      foreach ($faisceaux as $n => $f)
       {
          echo "s[$n]={$f->nom}<br>";
        }
*/
      $électrons = $faisceaux["electrons"];
      $photons = $faisceaux["photons"];
      $positrons = $faisceaux["positrons"];

      $this->faisceaux_sortie ($électrons, $photons, $positrons);
    }
};

$C = new Control ();

$C->run ();


?>
