[PLUGINS] +set de base
[lhc/web/www.git] / www / plugins / formidable_1_0 / 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 // Le titre
18 $titre = trim(spip_xml_aplatit($form['titre']));
19 $formulaire['titre'] = $titre ? $titre : _T('info_sans_titre');
20
21 // L'identifiant il faut le générer
22 $formulaire['identifiant'] = 'form_'.time();
23
24 // Le descriptif
25 $descriptif = trim(spip_xml_aplatit($form['descriptif']));
26 $formulaire['descriptif'] = $descriptif ? $descriptif : '';
27
28 // Le message de retour si ok
29 $message_retour = trim(spip_xml_aplatit($form['texte']));
30 $formulaire['message_retour'] = $message_retour ? $message_retour : '';
31
32 // Les champs
33 $formulaire['saisies'] = array();
34 foreach($form['fields'] as $fields){
35 foreach($fields['field'] as $field){
36 // Le truc par défaut
37 $saisie = array(
38 'saisie' => 'input',
39 'options' => array('size'=>40)
40 );
41
42 // On essaye de traduire tous les types de champs
43 $type = trim(spip_xml_aplatit($field['type']));
44 switch ($type){
45 case 'texte':
46 $saisie['saisie'] = 'textarea';
47 unset($saisie['options']['size']);
48 $saisie['options']['rows'] = 5;
49 $saisie['options']['cols'] = 40;
50 break;
51 case 'password':
52 $saisie['options']['type'] = 'password';
53 break;
54 case 'date':
55 $saisie['saisie'] = 'date';
56 $saisie['verifier'] = array(
57 'type' => 'date'
58 );
59 break;
60 case 'num':
61 case 'monnaie':
62 $saisie['verifier'] = array(
63 'type' => 'entier'
64 );
65 if ($taille = trim(spip_xml_aplatit($field['taille'])))
66 $saisie['verifier']['options'] = array('max' => (pow(10, $taille)-1));
67 break;
68 case 'email':
69 $saisie['verifier'] = array(
70 'type' => 'email'
71 );
72 break;
73 case 'telephone':
74 $saisie['verifier'] = array(
75 'type' => 'telephone'
76 );
77 break;
78 case 'select':
79 unset($saisie['options']['size']);
80 $liste = trim(spip_xml_aplatit($field['extra_info']));
81 if ($liste == 'radio')
82 $saisie['saisie'] = 'radio';
83 else
84 $saisie['saisie'] = 'selection';
85 break;
86 case 'multiple':
87 $saisie['saisie'] = 'checkbox';
88 unset($saisie['options']['size']);
89 break;
90 case 'fichier':
91 case 'separateur':
92 $saisie = null;
93 }
94
95 // On continue seulement si on a toujours une saisie
96 if ($saisie){
97 // Les choix pour les types select et multiple
98 if(isset($field['les_choix']) and is_array($field['les_choix'])){
99 $saisie['options']['datas'] = array();
100 foreach($field['les_choix'] as $les_choix){
101 foreach($les_choix['un_choix'] as $un_choix){
102 $choix = trim(spip_xml_aplatit($un_choix['choix']));
103 $titre = trim(spip_xml_aplatit($un_choix['titre']));
104 $saisie['options']['datas'][$choix] = $titre;
105 }
106 }
107 }
108
109 // Le nom
110 $saisie['options']['nom'] = trim(spip_xml_aplatit($field['champ']));
111
112 // Le label
113 $saisie['options']['label'] = trim(spip_xml_aplatit($field['titre']));
114
115 // Obligatoire
116 if (trim(spip_xml_aplatit($field['obligatoire'])) == 'oui')
117 $saisie['options']['obligatoire'] = 'on';
118
119 // Explication éventuelle
120 if ($explication = trim(spip_xml_aplatit($field['aide'])))
121 $saisie['options']['explication'] = $explication;
122
123 // On ajoute enfin la saisie
124 $formulaire['saisies'][] = $saisie;
125 }
126 }
127 }
128
129 // Les traitements
130 $formulaire['traitements'] = array();
131
132 // Le traitement email
133 $config_email = unserialize(trim(spip_xml_aplatit($form['email'])));
134 if (is_array($config_email)){
135 if ($email_defaut = $config_email['defaut'])
136 $formulaire['traitements']['email'] = array(
137 'destinataires_plus' => $email_defaut
138 );
139 }
140
141 // Le traitement enregistrement
142 $formulaire['traitements']['enregistrement'] = array(
143 'moderation' => (trim(spip_xml_aplatit($form['moderation'])) == 'priori') ? 'priori' : 'posteriori',
144 'modifiable' => (trim(spip_xml_aplatit($form['modifiable'])) == 'oui') ? 'on' : '',
145 'multiple' => (trim(spip_xml_aplatit($form['multiple'])) == 'non') ? '' : 'on'
146 );
147
148 include_spip('action/editer_formulaire');
149 // On insère un nouveau formulaire
150 $id_formulaire = insert_formulaire();
151 // Si ça a marché on modifie les champs de base
152 if ($id_formulaire > 0 and !($erreur = formulaire_set($id_formulaire, $formulaire))){
153 // Et ensuite les saisies et les traitements
154 $ok = sql_updateq(
155 'spip_formulaires',
156 array(
157 'saisies' => serialize($formulaire['saisies']),
158 'traitements' => serialize($formulaire['traitements'])
159 ),
160 'id_formulaire = '.$id_formulaire
161 );
162 }
163 }
164 }
165 }
166
167 if ($id_formulaire and $ok){
168 return $id_formulaire;
169 }
170 else{
171 return _T('formidable:erreur_importer_forms');
172 }
173 }
174
175 ?>