Projet

Général

Profil

Paste
Télécharger (3,73 ko) Statistiques
| Branche: | Révision:

root / addParams.php @ a1063f68

1
<?php
2
require_once('class/site_point.class.php');
3

    
4
   // tableau de vérification de conformité
5
$params = array('title' => array('name' => 'titre',
6
                                  'pattern' => '^.{1,50}$',
7
                                  'required' => true),
8
                 'latitude' => array('name' => 'latitude',
9
                                     'type' => 'numeric',
10
                                     'min' => -180,
11
                                     'max' => 180,
12
                                     'required' => true),
13
                 'longitude' => array('name' => 'longitude',
14
                                     'type' => 'numeric',
15
                                      'min' => -180,
16
                                      'max' => 180,
17
                                      'required' => true),
18
                 'altitude' => array('name' => 'altitude',
19
                                     'type' => 'numeric',
20
                                     'min' => -400,
21
                                     'required' => true),
22
                 'loop' => array('name' => 'image_loop',
23
                                 'type' => 'boolean',
24
                                 'required' => false),
25
                 'panorama' => array('required' => true));
26

    
27

    
28
class FormValidationError extends Exception {}
29

    
30
function ini_value($k, $v, $params_format) {
31
  /** According to the $params global reference table, format the value for
32
  storing in an ini file and returns it.
33
  */
34
  if (isset($params_format[$k]['type']) && $params_format[$k]['type'] == 'numeric') {
35
        $ini_val = $v;
36
  } else if (isset($params_format[$k]['type']) && $params_format[$k]['type'] == 'boolean') {
37
        $ini_val = $v ? "true" : "false";
38
  } else { //string
39
        $ini_val = "\"$v\"";
40
  }
41
  return $ini_val;
42
}
43

    
44
function is_ini_key($k, $params_format) {
45
  /** Do we need to store that information in the params ini file ?
46
  */
47
  return isset($params_format[$k]['name']);
48
}
49

    
50
function ini_key($k, $params_format) {
51
  /** For a given form key, returns the key for ini file
52
  */
53
  if (isset($params_format[$k]['name'])) {
54
    return $params_format[$k]['name'];
55
  } else {
56
    throw (new FormValidationError('"'.$k.'" is an unknown key.'));
57
  }
58
}
59

    
60
$wrong = array();
61
$values = array();
62
// vérification de la conformité
63
foreach($params as $param => $check) {
64
  if (isset($_REQUEST['param_'.$param])) {
65
    $tst = $_REQUEST['param_'.$param];
66
    if ((isset($check['min']) || isset($check['max'])) && ! is_numeric($tst)) $wrong[$param] = "<em>$tst</em> ne correspond pas à une valeur numérique";
67
    else if (isset($check['min']) && $tst < $check['min']) $wrong[$param] = "<em>$tst</em> trop bas";
68
    else if (isset($check['max']) && $tst > $check['max']) $wrong[$param] = "<em>$tst</em> trop haut";
69
    else if (isset($check['pattern']) && preg_match('/'.preg_quote($check['pattern']).'/', $tst)) $wrong[$param] = "<em>$tst</em> non conforme";
70
    else $values[$param] = $tst;
71
  } else if (isset($check['required']) && $check['required']) {
72
    $wrong[$param] = '<em>$tst</em> est un paramètre manquant';
73
  }
74
}
75
?>
76
<!DOCTYPE html>
77
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
78
<head>
79
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
80
   <link rel="stylesheet" media="screen" href="css/base.css" />
81
   <title>Positionnerment dun panoramique</title>
82

    
83
<?php
84
if (count($wrong) == 0) {
85
  $pano = site_point::get($values['panorama']);
86

    
87
  // On vérifie qu'on a bien créée un nouveau fichier .params et on écrit dedans.
88
  echo '<p>Les valeurs suivantes sont utilisées.</p>'."\n";
89
  echo "<dl>\n";
90

    
91
  foreach ($values as $k => $v) {
92
    if (is_ini_key($k, $params)) {
93
      $storable_key = ini_key($k, $params);
94
      $storable_val = ini_value($k, $v, $params);
95

    
96
          $pano->set_param($storable_key, $storable_val);
97
          printf("<dt>%s</dt>\n<dd>%s</dd>\n", $storable_key, $storable_val);
98
    }
99
  }
100
  $pano->save_params();
101

    
102
  echo "</dl>\n";
103
  echo '<p class="succes">Paramétrage terminé.</p>'."\n";
104
  printf('<a href="%s">Retour au panorama</a></p>'."\n", $pano->get_url());
105

    
106

    
107
 } else {
108
        echo '<p class="error">Les valeurs suivantes sont incorrectes.</p>'."\n";
109
        echo "<dl>\n";
110
        foreach ($wrong as $k => $v) {
111
                printf("<dt>%s</dt>\n<dd>%s</dd>\n", $k, $v);
112
        }
113
        echo "</dl>\n";
114
}
115
?>
116
</html>