commit 812f173f672ed18ebe3659473ef7ba84838a1162
Author: Jocelyn Delande <jocelyn at delalande dot fr>
Date:   Sat Dec 7 15:25:58 2013 +0100

    make use of constants

diff --git a/addParams.php b/addParams.php
index 7bb2e85..b2faa80 100644
--- a/addParams.php
+++ b/addParams.php
@@ -1,3 +1,7 @@
+<?php
+require_once('class/site_point.class.php');
+?>
+
 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
 <head>
@@ -6,24 +10,24 @@
    <title>Positionnerment dun panoramique</title>
 <?php
    // tableau de vérification de conformité
- $params = array('title' => array('name' => 'titre', 
+ $params = array('title' => array('name' => 'titre',
 				  'pattern' => '^.{1,50}$',
 				  'required' => true),
-		 'latitude' => array('name' => 'latitude', 
+		 'latitude' => array('name' => 'latitude',
 				     'type' => 'numeric',
 				     'min' => -180,
 				     'max' => 180,
 				     'required' => true),
-		 'longitude' => array('name' => 'longitude', 
+		 'longitude' => array('name' => 'longitude',
 				     'type' => 'numeric',
 				      'min' => -180,
 				      'max' => 180,
 				      'required' => true),
-		 'altitude' => array('name' => 'altitude', 
+		 'altitude' => array('name' => 'altitude',
 				     'type' => 'numeric',
 				     'min' => -400,
 				     'required' => true),
-		 'loop' => array('name' => 'image_loop', 
+		 'loop' => array('name' => 'image_loop',
 				 'type' => 'boolean',
 				 'required' => false),
 		 'dir' => array('required' => true),
@@ -52,52 +56,36 @@ if (isset($values['panorama'])) {
 }
 
 if (count($wrong) == 0) {
-  // On recherche le dossier correspondant au panorama en question
-  $dir_file = "./".$values['dir']."/".$values['panorama'];
-  $dir_open = opendir($dir_file);
-  while (false !== ($file = readdir($dir_open))) {
-    // Si on trouve bien des tuiles
-    if (preg_match('/(.*)_[0-9]+_[0-9]+_[0-9]+\.jpg$/', $file, $reg)) {
-      $prefix = $reg[1];
-      $new_param_file = $prefix.".params";
-      break;   // On sort à la première tuile trouvée
-    }
-  }
-  closedir($dir_open);
-  
+	$pano = site_point::get($values['panorama']);
+
   // On vérifie qu'on a bien créée un nouveau fichier .params et on écrit dedans.
-  if(isset($new_param_file)){
-    $fid = fopen($dir_file."/".$new_param_file,'a+');
-    echo '<p>Les valeurs suivantes sont utilisées.</p>'."\n";
-    echo "<dl>\n";
-    foreach ($values as $k => $v) {
-      echo "$k -$v<br/>\n";
-      if (isset($params[$k]['name'])) {
-	$nm = $params[$k]['name'];
-	if (isset($params[$k]['type']) && $params[$k]['type'] == 'numeric') {
-	  $vf = $v;
-	} else if (isset($params[$k]['type']) && $params[$k]['type'] == 'boolean') {
-	  $vf = $v ? "true" : "false"; 
-	} else {
-	  $vf = "\"$v\"";
+	echo '<p>Les valeurs suivantes sont utilisées.</p>'."\n";
+	echo "<dl>\n";
+	foreach ($values as $k => $v) {
+		if (isset($params[$k]['name'])) {
+			$nm = $params[$k]['name'];
+			if (isset($params[$k]['type']) && $params[$k]['type'] == 'numeric') {
+				$vf = $v;
+			} else if (isset($params[$k]['type']) && $params[$k]['type'] == 'boolean') {
+				$vf = $v ? "true" : "false";
+			} else {
+				$vf = "\"$v\"";
+			}
+			$pano->set_param($nm, $vf);
+			printf("<dt>%s</dt>\n<dd>%s</dd>\n", $nm, $vf);
+		}
 	}
-	fputs($fid, "$nm = $vf\n");
-	printf("<dt>%s</dt>\n<dd>%s</dd>\n", $nm, $vf);
-      }
-    }
-    echo "</dl>\n";
-    fclose($fid);
-    echo '<p class="succes">Paramétrage terminé.</p>'."\n";
-  } else {
-    printf("<p class=\"error\">impossible d'écrire dans le fichier '%s'</p>\n", $dir_file);
-  }
+	$pano->save_params();
+
+	echo "</dl>\n";
+	echo '<p class="succes">Paramétrage terminé.</p>'."\n";
  } else {
-  echo '<p class="error">Les valeurs suivantes sont incorrectes.</p>'."\n";
-  echo "<dl>\n";
-  foreach ($wrong as $k => $v) {
-    printf("<dt>%s</dt>\n<dd>%s</dd>\n", $k, $v);
-  }
-  echo "</dl>\n";
+	echo '<p class="error">Les valeurs suivantes sont incorrectes.</p>'."\n";
+	echo "<dl>\n";
+	foreach ($wrong as $k => $v) {
+		printf("<dt>%s</dt>\n<dd>%s</dd>\n", $k, $v);
+	}
+	echo "</dl>\n";
 }
 printf('<a href="%s">Retour au panorama</a></p>'."\n", $back_url);
 
diff --git a/ajax/all_points.php b/ajax/all_points.php
index 02a2f62..67db96d 100644
--- a/ajax/all_points.php
+++ b/ajax/all_points.php
@@ -24,12 +24,6 @@ function ref_point_to_geoJSONFeature($name, $values) {
 }
 
 
-function get_site_points() {
-	$dir = "../tiles";//FIXME
-	return (new sites_dir($dir))->get_sites();
-}
-
-
 function site_point_to_geoJSONFeature($sp) {
 	$prm = $sp->get_params();
 	$name = $sp->get_name();
@@ -60,7 +54,7 @@ foreach (get_ref_points() as $name => $vals) {
 }
 
 
-foreach(get_site_points() as $site_point) {
+foreach(site_point::get_all() as $site_point) {
 	$json['features'][] = site_point_to_geoJSONFeature($site_point);
 }
 
diff --git a/class/site_point.class.php b/class/site_point.class.php
index df9c60c..4698ba3 100644
--- a/class/site_point.class.php
+++ b/class/site_point.class.php
@@ -25,14 +25,14 @@ class site_point {
     }
     closedir($dir_fd);
     if ($this->prefix === false) return false;
-    $this->parse_and_store_params();
+    $this->parse_and_cache_params();
   }
 
-  private function params_path() {
+  public function params_path() {
 	  return $this->base_dir.'/'.$this->prefix.'.params';
   }
 
-  private function parse_and_store_params() {
+  private function parse_and_cache_params() {
     if (is_file($this->params_path())) {
 	    $this->params = @parse_ini_file($this->params_path());
     }
@@ -43,17 +43,32 @@ class site_point {
 	  if (isset($this->params)) {
 		  return $this->params;
 	  } else {
-		  return parse_and_store_params();
+		  return parse_and_cache_params();
 	  }
   }
 
-  public function has_params(){
+  public function save_params() {
+	  $o = '';
+	  $p = $this->get_params();
+	  foreach ($this->get_params() as $k => $v) {
+		  $o.= "$k = $v\n";
+	  }
+	  file_put_contents($this->params_path(), $o);
+  }
+
+  public function set_param($key, $value) {
 	  $p = $this->get_params();
-	  return (isset($prm['latitude'], $prm['longitude'],
-	                $prm['altitude'], $prm['titre']))
+	  $this->params[$key] = $value;
+	  if ($key == 'titre') {
+		  $this->name = $value;
+	  }
   }
-   if ($oname != $name && ) {
 
+  public function has_params(){
+	  $p = $this->get_params();
+	  return (isset($p['latitude'], $p['longitude'],
+	                $p['altitude'], $p['titre']));
+  }
 
   public function get_name() {
     return basename($this->base_dir);
@@ -109,9 +124,13 @@ class site_point {
     return array($d, $cap*180/M_PI, $e*180/M_PI);   // les résultats sont en degrés
   }
 
-  public function get_url() {
-	  return sprintf('panorama.php?dir=%s&amp;panorama=%s',
-	                 'tiles', $this->get_name());
+  public function get_url($cap=false, $ele=false) {
+	  $o = sprintf('panorama.php?dir=%s&panorama=%s',
+	                  PANORAMA_FOLDER, $this->get_name());
+	  if ($cap && $ele) {
+		  $o .= sprintf("&to_cap=%.3f&to_ele=%.3f", $cap, $ele);
+	  }
+	  return $o;
   }
 
   public static function get($name) {
diff --git a/constants.inc.php b/constants.inc.php
index e12590f..f4ff02c 100644
--- a/constants.inc.php
+++ b/constants.inc.php
@@ -1,5 +1,6 @@
 <?php
 define('CELUTZ_PATH', realpath(dirname(__FILE__)));
-define('PANORAMA_PATH', CELUTZ_PATH.'/tiles' );
+define('PANORAMA_FOLDER', 'tiles' );
+define('PANORAMA_PATH', CELUTZ_PATH.'/'.PANORAMA_FOLDER );
 define('UPLOAD_PATH', CELUTZ_PATH.'/upload' );
 ?>
diff --git a/genererPano.php b/genererPano.php
index fd25ab0..0402c3d 100644
--- a/genererPano.php
+++ b/genererPano.php
@@ -7,18 +7,18 @@
 </head>
 
 <?php
-require 'class/utils.class.php';
+require_once 'class/utils.class.php';
+require_once 'constants.inc.php';
 utils::init();
 if (isset($_GET['name'])) {
   $pano_name = $_GET['name'];
   $pano_basename = preg_replace('/\.[^.]+$/', '', $pano_name);
   
   //Partie exécutante du script gen_tiles qui gènere les tuiles à partir d'une image.
-  $panos_dir = 'tiles';
-  $pano_dest = $panos_dir.'/'.$pano_basename;
-  if (! is_dir($panos_dir)) {
-    if (! mkdir($panos_dir)) {
-        echo "<p class=\"error\">le répertoire \"$panos_dir\" n'est pas accessible et ne peut être créé</p>\n";
+  $pano_dest = PANORAMA_PATH.'/'.$pano_basename;
+  if (! is_dir(PANORAMA_PATH)) {
+    if (! mkdir(PANORAMA_PATH)) {
+        echo "<p class=\"error\">le répertoire \"PANORAMA_PATH\" n'est pas accessible et ne peut être créé</p>\n";
     }
   } else if (file_exists($pano_dest)) {
     echo "<p class=\"error\">le nom de répertoire \"$pano_dest\" est déjà pris</p>\n";
@@ -47,7 +47,7 @@ if (isset($_GET['name'])) {
       if (pclose($fp) === 0) {
 	print("<h4><span class=\"success\">Opération réussie</span></h4>\n");
 	printf("<p>Pour acceder directement au panorama <a href=\"panorama.php?dir=%s&amp;panorama=%s\">cliquer ici</a></p>\n", 
-	       $panos_dir, $pano_basename);
+	       PANORAMA_PATH, $pano_basename);
       } else {
 	print("<h4><span class=\"error\">Opération en échec durant l'exécution du script !</span></h4>\n");
       }
diff --git a/index.php b/index.php
index 9d3a5d9..33c45b3 100644
--- a/index.php
+++ b/index.php
@@ -12,13 +12,14 @@
     <section id="main">
       <h2>Liste des panoramas</h2>
       <?php
-require 'class/utils.class.php';
+require_once 'class/utils.class.php';
+require_once 'constants.inc.php';
 utils::init();
 
 if(isset($_GET['dir']) && is_dir($_GET['dir'])) {
   $base_dir = $_GET['dir'];
 } else {
-  $base_dir='tiles';
+  $base_dir=PANORAMA_PATH;
 }
 
 $dir = new sites_dir($base_dir);
diff --git a/panorama.php b/panorama.php
index ae318b5..bd15699 100644
--- a/panorama.php
+++ b/panorama.php
@@ -3,6 +3,7 @@
 <head>
   <?php
    require 'class/utils.class.php';
+   require_once 'constants.inc.php';
    utils::init();
 
    $form_extpoint = file_get_contents('html/form_extpoint.html');
@@ -13,7 +14,7 @@
      $dir   = $_GET['dir'];
      $name  = $_GET['panorama'];
    } else {
-     $dir   = 'tiles';
+     $dir   = PANORAMA_PATH;
      $name  = 'ttn_mediatheque';
    }
    $opt_vals = array();
@@ -48,6 +49,7 @@
   <script>
   <?php
      $zoom_array = $pt->get_magnifications();
+
      foreach($zoom_array as $zoom => $val) {
        echo "zooms[$zoom] = new tzoom($zoom);\n";
        echo "zooms[$zoom].ntiles.x = ".$val['nx'].";\n";
@@ -63,16 +65,17 @@
    $dir_list = new sites_dir($dir);
 
    $ipt = 0;
-   $scrname = getenv('SCRIPT_NAME');
-   foreach($dir_list->get_sites() as $opt) {
+  foreach(site_point::get_all() as $opt) {
      $prm = $opt->get_params();
      $oname = $opt->get_name();
-     if ($oname != $name && $opt->has_params()) {
+     if (($oname != $name) && $opt->has_params()) {
        list($dist, $cap, $ele) = $pt->coordsToCap($prm['latitude'], $prm['longitude'], $prm['altitude']);
-       $lnk = sprintf("%s?dir=%s&panorama=%s&to_cap=%.3f&to_ele=%.3f", $scrname, $dir, $oname, $cap + 180, -$ele);
+       // Looks back at the point from which we come.
+       $lnk = $opt->get_url($cap + 180, -$ele);
        printf('point_list[%d] = new Array("%s", %03lf, %03lf, %03lf, "%s");'."\n", $ipt++, $prm['titre'], $dist, $cap, $ele, $lnk);
      }
    }
+
    $ref_points = array ();
    $ref_points_filename = 'ref_points.local.php';
    if (file_exists($ref_points_filename)) {
