97650d44014067674375a0222064496a32906f21
[lhc/web/www.git] / www / plugins / formidable / echanger / formulaire / forms.php
1 <?php
2
3 // Sécurité
4 if (!defined("_ECRIRE_INC_VERSION")) return;
5
6 // Importation d'un formulaire forms&table
7
8 function echanger_formulaire_forms_importer_dist($fichier){
9 include_spip('inc/xml');
10 $arbre = spip_xml_load($fichier, false);
11
12 if ($arbre and is_array($arbre) and isset($arbre['forms'])){
13 foreach($arbre['forms'] as $forms){
14 foreach ($forms['form'] as $form){
15 $formulaire = array();
16
17 $form_source = array(
18 'id_form' => intval(trim(spip_xml_aplatit($form['id_form']))),
19 'titre' => trim(spip_xml_aplatit($form['titre'])),
20 'descriptif' => trim(spip_xml_aplatit($form['descriptif'])),
21 'texte' => trim(spip_xml_aplatit($form['texte'])),
22 'email' => unserialize(trim(spip_xml_aplatit($form['email']))),
23 'moderation' => trim(spip_xml_aplatit($form['moderation'])),
24 'modifiable' => trim(spip_xml_aplatit($form['modifiable'])),
25 'multiple' => trim(spip_xml_aplatit($form['multiple'])),
26 'champconfirm' => trim(spip_xml_aplatit($form['champconfirm'])),
27 );
28
29 // configurer le formulaire (titre etc)
30 forms_configure_formulaire($form_source,$formulaire);
31
32 // ajouter les champs de saisies
33 foreach($form['fields'] as $fields){
34 foreach($fields['field'] as $field){
35 $champ = array(
36 'champ'=>trim(spip_xml_aplatit($field['champ'])),
37 'titre'=>trim(spip_xml_aplatit($field['titre'])),
38 'type'=>trim(spip_xml_aplatit($field['type'])),
39 'obligatoire'=>trim(spip_xml_aplatit($field['obligatoire'])),
40 'taille'=>trim(spip_xml_aplatit($field['taille'])),
41 'extra_info'=>trim(spip_xml_aplatit($field['extra_info'])),
42 'aide'=>trim(spip_xml_aplatit($field['aide'])),
43 'saisie'=>trim(spip_xml_aplatit($field['saisie'])),
44 );
45
46 // Les choix pour les types select et multiple
47 if(isset($field['les_choix']) and is_array($field['les_choix'])){
48 $champ['choix'] = array();
49 foreach($field['les_choix'] as $les_choix){
50 foreach($les_choix['un_choix'] as $un_choix){
51 $champ['choix'][] = array(
52 'choix'=>trim(spip_xml_aplatit($un_choix['choix'])),
53 'titre'=>trim(spip_xml_aplatit($un_choix['titre'])),
54 );
55 }
56 }
57 }
58
59 if ($saisie = forms_champ_vers_saisie($champ))
60 $formulaire['saisies'][] = $saisie;
61 }
62 }
63
64 // les traitements
65 forms_configure_traitement_formulaire($form_source,$formulaire);
66 $id_formulaire = forms_importe_en_base($formulaire);
67 }
68 }
69 }
70
71 if ($id_formulaire){
72 return $id_formulaire;
73 }
74 else{
75 return _T('formidable:erreur_importer_forms');
76 }
77 }
78
79 /**
80 * Importer le tableau $formulaire en base
81 * @param array $formulaire
82 * @return bool|int
83 */
84 function forms_importe_en_base($formulaire){
85 include_spip('action/editer_formulaire');
86 // On insère un nouveau formulaire
87 // cas utilise par l'installation/import f&t
88 if (isset($formulaire['id_formulaire']) AND !sql_countsel("spip_formulaires","id_formulaire=".intval($formulaire['id_formulaire']))){
89 $champs = array(
90 'id_formulaire' => $formulaire['id_formulaire'],
91 'statut' => 'prop',
92 'date_creation' => date('Y-m-d H:i:s'),
93 );
94 // Envoyer aux plugins
95 $champs = pipeline('pre_insertion',
96 array(
97 'args' => array(
98 'table' => 'spip_formulaires',
99 ),
100 'data' => $champs
101 )
102 );
103 $id_formulaire = sql_insertq("spip_formulaires", $champs);
104
105 pipeline('post_insertion',
106 array(
107 'args' => array(
108 'table' => 'spip_formulaires',
109 'id_objet' => $id_formulaire
110 ),
111 'data' => $champs
112 )
113 );
114 }
115 else
116 $id_formulaire = formulaire_inserer();
117
118 $formulaire['saisies'] = forms_regroupe_saisies_fieldset($formulaire['saisies']);
119
120 if (is_array($formulaire['saisies']))
121 $formulaire['saisies'] = serialize($formulaire['saisies']);
122 if (is_array($formulaire['traitements']))
123 $formulaire['traitements'] = serialize($formulaire['traitements']);
124
125 // si l'identifiant existe deja (multiples imports du meme form)
126 // le dater
127 if (sql_countsel("spip_formulaires","identifiant=".sql_quote($formulaire['identifiant']))){
128 $formulaire['identifiant'] .= "_".date('Ymd_His');
129 }
130
131 // Si ok on modifie les champs de base
132 if ($id_formulaire>0
133 AND !($erreur = formulaire_modifier($id_formulaire, $formulaire))){
134
135 return $id_formulaire;
136 }
137
138 return false;
139 }
140
141 /**
142 * Configuration de l'objet formulaire formidable a partir du form f&t
143 * @param array $form
144 * @param array $formulaire
145 */
146 function forms_configure_formulaire($form,&$formulaire){
147
148 // Le titre
149 $formulaire['titre'] = ($form['titre'] ? $form['titre'] : _T('info_sans_titre'));
150
151 // Generer un identifiant
152 // si id_form fourni, on s'en sert
153 if (isset($form['id_form'])){
154 $formulaire['identifiant'] = "form_import_".$form['id_form'];
155 }
156 else {
157 $formulaire['identifiant'] = "form_import_".preg_replace(",\W,","_",strtolower($formulaire['titre']));
158 }
159
160 // Le descriptif
161 $formulaire['descriptif'] = (isset($form['descriptif']) ? $form['descriptif'] : '');
162
163 // Le message de retour si ok
164 $formulaire['message_retour'] = (isset($form['texte']) ? $form['texte'] : '');
165
166 if (!isset($formulaire['traitements']))
167 $formulaire['traitements'] = array();
168
169 if (!isset($formulaire['saisies']))
170 $formulaire['saisies'] = array();
171 }
172
173 /**
174 * Configurer les traitements
175 *
176 * @param array $form
177 * @param array $formulaire
178 */
179 function forms_configure_traitement_formulaire($form,&$formulaire){
180 // Le traitement email
181 if ($form['champconfirm']){
182 if (!isset($formulaire['traitements']['email']))
183 $formulaire['traitements']['email'] = array();
184 $formulaire['traitements']['email']['champ_courriel_destinataire_form'] = $form['champconfirm'];
185 }
186
187 // $form['email'] est possiblement serialize
188 if (is_string($form['email']) AND $a=unserialize($form['email']))
189 $form['email'] = $a;
190 if (is_array($form['email'])){
191
192 if ($email_defaut = $form['email']['defaut']){
193 if (!isset($formulaire['traitements']['email']))
194 $formulaire['traitements']['email'] = array();
195 $formulaire['traitements']['email']['destinataires_plus'] = $email_defaut;
196 }
197
198 // TODO email route : feature qui n'existe pas dans formidable
199 if ($route = $form['email']['route']){
200
201 }
202 }
203
204 // Le traitement enregistrement : toujours
205 $formulaire['traitements']['enregistrement'] = array(
206 'moderation' => ($form['moderation'] == 'priori') ? 'priori' : 'posteriori',
207 'modifiable' => ($form['modifiable'] == 'oui') ? 'on' : '',
208 'multiple' => ($form['multiple'] == 'non') ? '' : 'on'
209 );
210
211 }
212
213
214 /**
215 * On a genere un fieldset pour chaque separateur de f&t
216 * il faut le peupler avec les saisies qui le suivent
217 *
218 * @param array $saisies
219 * @return array
220 */
221 function forms_regroupe_saisies_fieldset($saisies){
222 $s = array();
223 $ins = &$s;
224
225 foreach($saisies as $k=>$saisie){
226 if ($saisie['saisie']=='fieldset'){
227 if (!isset($saisies[$k]['saisies']))
228 $saisies[$k]['saisies'] = array();
229 $ins = &$saisies[$k]['saisies'];
230 $s[] = &$saisies[$k];
231 }
232 else
233 $ins[] = &$saisies[$k];
234 }
235
236 return $s;
237 }
238
239 /**
240 * Transforme un champ f&t en Saisie
241 * @param array $champ
242 * string champ
243 * string titre
244 * string type
245 * string obligatoire
246 * string taille
247 * string aide
248 * string extra_info
249 * string saisie oui/non
250 * array choix
251 * string choix
252 * string titre
253 * @return array|bool
254 */
255 function forms_champ_vers_saisie($champ){
256
257 // Le truc par défaut
258 $saisie = array(
259 'saisie' => 'input',
260 'options' => array('size'=>40)
261 );
262
263 // On essaye de traduire tous les types de champs
264 $type = $champ['type'];
265 switch ($type){
266 case 'texte':
267 $saisie['saisie'] = 'textarea';
268 unset($saisie['options']['size']);
269 $saisie['options']['rows'] = 5;
270 $saisie['options']['cols'] = 40;
271 break;
272 case 'password':
273 $saisie['options']['type'] = 'password';
274 break;
275 case 'date':
276 $saisie['saisie'] = 'date';
277 $saisie['verifier'] = array(
278 'type' => 'date'
279 );
280 break;
281 case 'num':
282 case 'monnaie':
283 if (!isset($champ['taille']) OR !intval($taille = $champ['taille'])){
284 $saisie['verifier'] = array(
285 'type' => 'entier'
286 );
287 }
288 else {
289 $saisie['verifier'] = array(
290 'type' => 'decimal'
291 );
292 $saisie['verifier']['options'] = array('nb_decimales' => $taille);
293 }
294 break;
295 case 'email':
296 $saisie['verifier'] = array(
297 'type' => 'email'
298 );
299 break;
300 case 'telephone':
301 $saisie['verifier'] = array(
302 'type' => 'telephone'
303 );
304 break;
305 case 'select':
306 unset($saisie['options']['size']);
307 $liste = $champ['extra_info'];
308 if ($liste == 'radio')
309 $saisie['saisie'] = 'radio';
310 else
311 $saisie['saisie'] = 'selection';
312 break;
313 case 'multiple':
314 $saisie['saisie'] = 'checkbox';
315 unset($saisie['options']['size']);
316 break;
317 case 'mot':
318 $saisie['saisie'] = 'mot';
319 $saisie['options']['id_groupe'] = $champ['extra_info'];
320 unset($saisie['options']['size']);
321 break;
322 case 'textestatique':
323 $saisie['saisie'] = 'explication';
324 unset($saisie['options']['size']);
325 $saisie['options']['texte'] = $champ['titre'];
326 unset($champ['titre']);
327 unset($champ['aide']);
328 break;
329 case 'separateur':
330 $saisie['saisie'] = 'fieldset';
331 $saisie['saisies'] = array();
332 unset($saisie['options']['size']);
333 break;
334 case 'fichier':
335 // TODO saisie file NIY
336 $saisie = null;
337 break;
338 }
339
340 // On continue seulement si on a toujours une saisie
341 if (!$saisie)
342 return false;
343
344 // Les choix pour les types select et multiple
345 if(isset($champ['choix']) and is_array($champ['choix'])){
346 $saisie['options']['datas'] = array();
347 foreach($champ['choix'] as $un_choix){
348 $choix = $un_choix['choix'];
349 $titre = $un_choix['titre'];
350 $saisie['options']['datas'][$choix] = $titre;
351 }
352 }
353
354 // Le nom
355 $saisie['options']['nom'] = $champ['champ'];
356
357 // Le label
358 if (isset($champ['titre']) AND $champ['titre'])
359 $saisie['options']['label'] = $champ['titre'];
360
361 // Obligatoire
362 if (isset($champ['obligatoire']) AND $champ['obligatoire'] == 'oui')
363 $saisie['options']['obligatoire'] = 'on';
364
365 // Explication éventuelle
366 if (isset($champ['aide']) AND $explication = $champ['aide'])
367 $saisie['options']['explication'] = $explication;
368
369 if (isset($champ['saisie']) AND $champ['saisie']=='non'){
370 $saisie['options']['disable'] = 'on';
371 // masquer en JS, fallback
372 $saisie['options']['afficher_si'] = 'false';
373 }
374
375 return $saisie;
376 }