97650d44014067674375a0222064496a32906f21
4 if (!defined("_ECRIRE_INC_VERSION")) return;
6 // Importation d'un formulaire forms&table
8 function echanger_formulaire_forms_importer_dist($fichier){
9 include_spip('inc/xml');
10 $arbre = spip_xml_load($fichier, false);
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();
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'])),
29 // configurer le formulaire (titre etc)
30 forms_configure_formulaire($form_source,$formulaire);
32 // ajouter les champs de saisies
33 foreach($form['fields'] as $fields){
34 foreach($fields['field'] as $field){
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'])),
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'])),
59 if ($saisie = forms_champ_vers_saisie($champ))
60 $formulaire['saisies'][] = $saisie;
65 forms_configure_traitement_formulaire($form_source,$formulaire);
66 $id_formulaire = forms_importe_en_base($formulaire);
72 return $id_formulaire;
75 return _T('formidable:erreur_importer_forms');
80 * Importer le tableau $formulaire en base
81 * @param array $formulaire
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']))){
90 'id_formulaire' => $formulaire['id_formulaire'],
92 'date_creation' => date('Y-m-d H:i:s'),
94 // Envoyer aux plugins
95 $champs = pipeline('pre_insertion',
98 'table' => 'spip_formulaires',
103 $id_formulaire = sql_insertq("spip_formulaires", $champs);
105 pipeline('post_insertion',
108 'table' => 'spip_formulaires',
109 'id_objet' => $id_formulaire
116 $id_formulaire = formulaire_inserer();
118 $formulaire['saisies'] = forms_regroupe_saisies_fieldset($formulaire['saisies']);
120 if (is_array($formulaire['saisies']))
121 $formulaire['saisies'] = serialize($formulaire['saisies']);
122 if (is_array($formulaire['traitements']))
123 $formulaire['traitements'] = serialize($formulaire['traitements']);
125 // si l'identifiant existe deja (multiples imports du meme form)
127 if (sql_countsel("spip_formulaires","identifiant=".sql_quote($formulaire['identifiant']))){
128 $formulaire['identifiant'] .= "_".date('Ymd_His');
131 // Si ok on modifie les champs de base
133 AND !($erreur = formulaire_modifier($id_formulaire, $formulaire))){
135 return $id_formulaire;
142 * Configuration de l'objet formulaire formidable a partir du form f&t
144 * @param array $formulaire
146 function forms_configure_formulaire($form,&$formulaire){
149 $formulaire['titre'] = ($form['titre'] ?
$form['titre'] : _T('info_sans_titre'));
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'];
157 $formulaire['identifiant'] = "form_import_".preg_replace(",\W,","_",strtolower($formulaire['titre']));
161 $formulaire['descriptif'] = (isset($form['descriptif']) ?
$form['descriptif'] : '');
163 // Le message de retour si ok
164 $formulaire['message_retour'] = (isset($form['texte']) ?
$form['texte'] : '');
166 if (!isset($formulaire['traitements']))
167 $formulaire['traitements'] = array();
169 if (!isset($formulaire['saisies']))
170 $formulaire['saisies'] = array();
174 * Configurer les traitements
177 * @param array $formulaire
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'];
187 // $form['email'] est possiblement serialize
188 if (is_string($form['email']) AND $a=unserialize($form['email']))
190 if (is_array($form['email'])){
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;
198 // TODO email route : feature qui n'existe pas dans formidable
199 if ($route = $form['email']['route']){
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'
215 * On a genere un fieldset pour chaque separateur de f&t
216 * il faut le peupler avec les saisies qui le suivent
218 * @param array $saisies
221 function forms_regroupe_saisies_fieldset($saisies){
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];
233 $ins[] = &$saisies[$k];
240 * Transforme un champ f&t en Saisie
241 * @param array $champ
249 * string saisie oui/non
255 function forms_champ_vers_saisie($champ){
257 // Le truc par défaut
260 'options' => array('size'=>40)
263 // On essaye de traduire tous les types de champs
264 $type = $champ['type'];
267 $saisie['saisie'] = 'textarea';
268 unset($saisie['options']['size']);
269 $saisie['options']['rows'] = 5;
270 $saisie['options']['cols'] = 40;
273 $saisie['options']['type'] = 'password';
276 $saisie['saisie'] = 'date';
277 $saisie['verifier'] = array(
283 if (!isset($champ['taille']) OR !intval($taille = $champ['taille'])){
284 $saisie['verifier'] = array(
289 $saisie['verifier'] = array(
292 $saisie['verifier']['options'] = array('nb_decimales' => $taille);
296 $saisie['verifier'] = array(
301 $saisie['verifier'] = array(
302 'type' => 'telephone'
306 unset($saisie['options']['size']);
307 $liste = $champ['extra_info'];
308 if ($liste == 'radio')
309 $saisie['saisie'] = 'radio';
311 $saisie['saisie'] = 'selection';
314 $saisie['saisie'] = 'checkbox';
315 unset($saisie['options']['size']);
318 $saisie['saisie'] = 'mot';
319 $saisie['options']['id_groupe'] = $champ['extra_info'];
320 unset($saisie['options']['size']);
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']);
330 $saisie['saisie'] = 'fieldset';
331 $saisie['saisies'] = array();
332 unset($saisie['options']['size']);
335 // TODO saisie file NIY
340 // On continue seulement si on a toujours une saisie
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;
355 $saisie['options']['nom'] = $champ['champ'];
358 if (isset($champ['titre']) AND $champ['titre'])
359 $saisie['options']['label'] = $champ['titre'];
362 if (isset($champ['obligatoire']) AND $champ['obligatoire'] == 'oui')
363 $saisie['options']['obligatoire'] = 'on';
365 // Explication éventuelle
366 if (isset($champ['aide']) AND $explication = $champ['aide'])
367 $saisie['options']['explication'] = $explication;
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';