7 protected $fields_types = null;
8 protected $config = null;
9 protected $modified = [];
11 static protected $_instance = null;
13 static public function getInstance()
15 return self
::$_instance ?
: self
::$_instance = new Config
;
18 private function __clone()
22 protected function __construct()
24 // Définition des types de données stockées
30 $object = new \stdClass
;
32 $this->fields_types
= [
33 'nom_asso' => $string,
34 'adresse_asso' => $string,
35 'email_asso' => $string,
36 'site_asso' => $string,
41 'champs_membres' => $object,
43 'email_envoi_automatique'=> $string,
45 'categorie_membres' => $int,
47 'categorie_dons' => $int,
48 'categorie_cotisations' => $int,
50 'accueil_wiki' => $string,
51 'accueil_connexion' => $string,
53 'frequence_sauvegardes' => $int,
54 'nombre_sauvegardes' => $int,
56 'champ_identifiant' => $string,
57 'champ_identite' => $string,
62 $db = DB
::getInstance();
64 $this->config
= $db->simpleStatementFetchAssoc('SELECT cle, valeur FROM config ORDER BY cle;');
66 foreach ($this->config
as $key=>&$value)
68 if (!array_key_exists($key, $this->fields_types
))
70 // Ancienne clé de config qui n'est plus utilisée
74 if (is_array($this->fields_types
[$key]))
76 $value = explode(',', $value);
78 elseif ($key == 'champs_membres')
80 $value = new Champs_Membres((string)$value);
84 settype($value, gettype($this->fields_types
[$key]));
89 public function __destruct()
91 if (!empty($this->modified
))
93 //echo '<div style="color: red; background: #fff;">Il y a des champs modifiés non sauvés dans '.__CLASS__.' !</div>';
97 public function save()
99 if (empty($this->modified
))
104 $db = DB
::getInstance();
107 foreach ($this->modified
as $key=>$modified)
109 $value = $this->config
[$key];
111 if (is_array($value))
113 $value = implode(',', $value);
115 elseif (is_object($value))
117 $value = (string) $value;
120 $db->simpleExec('INSERT OR REPLACE INTO config (cle, valeur) VALUES (?, ?);',
124 if (!empty($this->modified
['champ_identifiant']))
126 // Mettre les champs identifiant vides à NULL pour pouvoir créer un index unique
127 $db->exec('UPDATE membres SET '.$this->get('champ_identifiant').' = NULL
128 WHERE '.$this->get('champ_identifiant').' = "";');
130 // Création de l'index unique
131 $db->exec('DROP INDEX IF EXISTS membres_identifiant;');
132 $db->exec('CREATE UNIQUE INDEX membres_identifiant ON membres ('.$this->get('champ_identifiant').');');
137 $this->modified
= [];
142 public function get($key)
144 if (!array_key_exists($key, $this->fields_types
))
146 throw new \
OutOfBoundsException('Ce champ est inconnu.');
149 if (!array_key_exists($key, $this->config
))
154 return $this->config
[$key];
157 public function getVersion()
159 if (!array_key_exists('version', $this->config
))
164 return $this->config
['version'];
167 public function setVersion($version)
169 $this->config
['version'] = $version;
171 $db = DB
::getInstance();
172 $db->simpleExec('INSERT OR REPLACE INTO config (cle, valeur) VALUES (?, ?);',
173 'version', $version);
178 public function set($key, $value)
180 if (!array_key_exists($key, $this->fields_types
))
182 throw new \
OutOfBoundsException('Ce champ est inconnu.');
185 if (is_array($this->fields_types
[$key]))
187 $value = !empty($value) ?
(array) $value : [];
189 elseif (is_int($this->fields_types
[$key]))
191 $value = (int) $value;
193 elseif (is_float($this->fields_types
[$key]))
195 $value = (float) $value;
197 elseif (is_bool($this->fields_types
[$key]))
199 $value = (bool) $value;
201 elseif (is_string($this->fields_types
[$key]))
203 $value = (string) $value;
212 throw new UserException('Le nom de l\'association ne peut rester vide.');
217 case 'accueil_connexion':
221 $key = str_replace('accueil_', '', $key);
222 throw new UserException('Le nom de la page d\'accueil ' . $key . ' ne peut rester vide.');
227 case 'email_envoi_automatique':
229 if (!filter_var($value, FILTER_VALIDATE_EMAIL
))
231 throw new UserException('Adresse e-mail invalide.');
235 case 'champs_membres':
237 if (!($value instanceOf Champs_Membres
))
239 throw new \
UnexpectedValueException('$value doit être de type Champs_Membres');
243 case 'champ_identite':
244 case 'champ_identifiant':
246 $champs = $this->get('champs_membres');
247 $db = DB
::getInstance();
249 // Vérification que le champ existe bien
250 if (!$champs->get($value))
252 throw new UserException('Le champ '.$value.' n\'existe pas pour la configuration de '.$key);
255 // Vérification que le champ est unique pour l'identifiant
256 if ($key == 'champ_identifiant'
257 && !$db->simpleQuerySingle('SELECT (COUNT(DISTINCT '.$value.') = COUNT(*))
258 FROM membres WHERE '.$value.' IS NOT NULL AND '.$value.' != \'\';'))
260 throw new UserException('Le champ '.$value.' comporte des doublons et ne peut donc pas servir comme identifiant pour la connexion.');
264 case 'categorie_cotisations':
265 case 'categorie_dons':
268 $db = DB
::getInstance();
269 if (!$db->simpleQuerySingle('SELECT 1 FROM compta_categories WHERE id = ?;', false, $value))
271 throw new UserException('Champ '.$key.' : La catégorie comptable numéro \''.$value.'\' ne semble pas exister.');
275 case 'categorie_membres':
277 $db = DB
::getInstance();
278 if (!$db->simpleQuerySingle('SELECT 1 FROM membres_categories WHERE id = ?;', false, $value))
280 throw new UserException('La catégorie de membres par défaut numéro \''.$value.'\' ne semble pas exister.');
288 throw new UserException('La monnaie doit être renseignée.');
295 if (!trim($value) ||
!utils
::getCountryName($value))
297 throw new UserException('Le pays renseigné est invalide.');
306 if (!isset($this->config
[$key]) ||
$value !== $this->config
[$key])
308 $this->config
[$key] = $value;
309 $this->modified
[$key] = true;
315 public function getFieldsTypes()
317 return $this->fields_types
;
320 public function getConfig()
322 return $this->config
;