commit ffa856dcf969c4c8d7722007fe0031e5131c28de
Author: Jocelyn Delande <jocelyn at delalande dot fr>
Date:   Mon Dec 23 23:09:49 2013 +0100

    added params generation to upload

diff --git a/class/FormValidator.class.php b/class/FormValidator.class.php
index 38a05b5..5be9de8 100644
--- a/class/FormValidator.class.php
+++ b/class/FormValidator.class.php
@@ -112,5 +112,16 @@ FormValidator::register(
 	  }
   }
 );
+// Intended to validate checkbox which takes NULL for unchecked
+FormValidator::register(
+  'boolean',
+  function ($v) {
+	  if ($v == NULL) {
+		  return false;
+	  } else {
+		  return true;
+	  }
+  }
+);
 
 ?>
diff --git a/class/site_point.class.php b/class/site_point.class.php
index 1993f28..9098806 100644
--- a/class/site_point.class.php
+++ b/class/site_point.class.php
@@ -1,7 +1,7 @@
 <?php
 require_once(dirname(__FILE__).'/../constants.inc.php');
 require_once(dirname(__FILE__).'/utils.class.php');
-
+utils::init();
 //
 class PanoramaFormatException extends Exception {
 	/** If the files organization is not correct for a panorama, we can't let it go...
@@ -206,6 +206,18 @@ class site_point {
 	  return new site_point($pano_dir);
   }
 
+  public static function create($filepath) {
+	  /** creates a new panorama, given its name, from an uploaded file.
+	   */
+	  $name = utils::strip_extension(basename($filepath));
+	  $pano_dir = PANORAMA_PATH.'/'.$name;
+	  $pano = new site_point($pano_dir);
+	  if (!mkdir($pano->tiles_path())) {
+		  return false;
+	  } else {
+		  return $pano;
+	  }
+  }
 
   public function to_geoJSON() {
 	  $prm = $this->get_params();
@@ -229,7 +241,8 @@ class site_point {
 
   public static function get_all($only_with_params=false) {
 	  /**
-	   * @param $only_with_params : filters out the panoramas which are not parametrized
+	   * @param $only_with_params : filters out the panoramas which
+	   *        are not parametrized
 	   */
 	  $panos = array_diff(scandir(PANORAMA_PATH), array('..', '.'));
 	  $pano_instances = array();
diff --git a/uploadReceive.php b/uploadReceive.php
index ab26a89..b73370e 100644
--- a/uploadReceive.php
+++ b/uploadReceive.php
@@ -1,6 +1,7 @@
 <?php
  
 require_once('class/FormValidator.class.php');
+require_once('class/site_point.class.php');
 require_once('constants.inc.php');
 
 class UploadReceiveError extends Exception {}
@@ -23,7 +24,7 @@ function handle_upload() {
     if(!empty($file)) {
       if(isset($file) && UPLOAD_ERR_OK === $file_err) {
 	      move_uploaded_file($file_tmp, $file_finalpath);
-        return sprintf("transfert de %s réalisé",  $file);
+        return $file_finalpath;
       } else {
         throw new UploadReceiveError(
           'Une erreur interne a empêché l\'envoi de l\'image :'. $file_err);
@@ -35,22 +36,38 @@ function handle_upload() {
   }
 }
 
+function existant_and_set($list, $keys) {
+  /** For HTTP data : keys of $keys are set within $list and they are not empty
+  * or false nor empty
+  */
+  foreach($keys as $key) {
+    if (!isset($list[$key]) || !$list[$key]) {
+      return false;
+    }
+  }
+  return true;
+}
+
 ////////////////////// main //////////////////////////////////////////
 
-$fields_spec = array('lat'         => array('required', 'numeric', 'positive'),
+$fields_spec = array('lat'         => array('numeric', 'positive'),
                      'lon'         => array('numeric', 'positive'),
-                     'alt'  => array('numeric'),
+                     'alt'  => array('numeric', 'positive'),
+                     'loop'  => array('boolean'),
 );
 
 $validator = new FormValidator($fields_spec);
-$upload_success = false;
 
 ////// STEP 1 : UPLOAD ////////
 
+$upload_success = false;
+$uploaded_filepath = '';
+
 if ($validator->validate($_REQUEST)) {
   try {
-    $message = handle_upload();
+    $uploaded_filepath = handle_upload();
     $upload_success = true;
+    $message = sprintf("transfert de %s réalisé", basename($uploaded_filepath));
   } catch (UploadReceiveError $e) {
     $message = $e->getMessage();
   }
@@ -63,7 +80,23 @@ if ($validator->validate($_REQUEST)) {
 $params_success = false;
 
 if ($upload_success) {
-  //pass
+  $vals = $validator->sane_values();
+  // There is no point setting a part of the parameters only ; check that all
+  // are present.  
+  if (existant_and_set($vals, array('lat', 'alt', 'lon'))) {
+    try {
+      $panorama = site_point::create($uploaded_filepath);
+      $panorama->set_param('titre', 'Sans nom 1');//FIXME
+      $panorama->set_param('latitude',  $vals['lat']);
+      $panorama->set_param('longitude', $vals['lon']);
+      $panorama->set_param('altitude',  $vals['alt']);
+      $panorama->set_param('image_loop', $vals['loop']);
+      $panorama->save_params();
+      $params_success = true;
+    } catch (Exception $e) {
+      $message = 'erreur à la création du panorama : '.$e->getMessage();
+    }
+  }
 }
 
 
