1 | <?php |
---|
2 | |
---|
3 | // Sécurité |
---|
4 | if (!defined('_ECRIRE_INC_VERSION')) { |
---|
5 | return; |
---|
6 | } |
---|
7 | |
---|
8 | include_spip('inc/formidable'); |
---|
9 | include_spip('inc/config'); |
---|
10 | |
---|
11 | function formulaires_importer_formulaire_reponses_charger($id_formulaire = 0) { |
---|
12 | $contexte = array(); |
---|
13 | $contexte['id_formulaire'] = intval($id_formulaire); |
---|
14 | |
---|
15 | return $contexte; |
---|
16 | } |
---|
17 | |
---|
18 | function formulaires_importer_formulaire_reponses_verifier($id_formulaire = 0) { |
---|
19 | $erreurs = array(); |
---|
20 | return $erreurs; |
---|
21 | } |
---|
22 | |
---|
23 | function formulaires_importer_formulaire_reponses_traiter($id_formulaire = 0) { |
---|
24 | $retours = array(); |
---|
25 | $statut_reponses = _request('statut_reponses'); |
---|
26 | $ok = false; |
---|
27 | if (isset($_FILES) && isset($_FILES['fichier']) && !$_FILES['fichier']['error']) { |
---|
28 | $type_import = _request('type_import'); |
---|
29 | $fichier = $_FILES['fichier']['tmp_name']; |
---|
30 | if (_request('type_import') == 'csv') { |
---|
31 | $res = importer_formulaires_reponses($id_formulaire, $fichier, ',', $statut_reponses); |
---|
32 | $retours = $res; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | if (!$res) { |
---|
37 | $retours['editable'] = 1; |
---|
38 | $retours['message_erreur'] = _T('formidable:info_aucune_reponse'); |
---|
39 | } |
---|
40 | |
---|
41 | return $retours; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Importer toutes les réponses du fichier |
---|
46 | * @param integer $id_formulaire |
---|
47 | * @return unknown_type |
---|
48 | */ |
---|
49 | function importer_formulaires_reponses($id_formulaire, $fichier, $delim = ',', $statut_reponses = 'publie') { |
---|
50 | // on ne fait des choses seulements si le formulaire existe et qu'il a des enregistrements |
---|
51 | if ($id_formulaire > 0 |
---|
52 | and $formulaire = sql_fetsel('*', 'spip_formulaires', 'id_formulaire = ' . $id_formulaire)) { |
---|
53 | include_spip('inc/saisies'); |
---|
54 | include_spip('inc/filtres'); |
---|
55 | $reponses_completes = array(); |
---|
56 | // Charger la fonction inc_importer_csv de bonux |
---|
57 | $importer_csv = charger_fonction('importer_csv', 'inc'); |
---|
58 | $reponses = $importer_csv($fichier, true); |
---|
59 | |
---|
60 | if (is_array($reponses) and count($reponses) >= 1) { |
---|
61 | $saisies = saisies_lister_par_nom(unserialize($formulaire['saisies']), false); |
---|
62 | $saisies_nom = array(); |
---|
63 | $saisies_obligatoires = array(); |
---|
64 | foreach ($saisies as $nom => $saisie) { |
---|
65 | if ($saisie['saisie'] != 'explication') { // on exporte tous les champs sauf explications |
---|
66 | $saisies_nom[] = $saisie['options']['nom']; |
---|
67 | if ($saisie['options']['obligatoire'] == 'on') { |
---|
68 | $saisies_obligatoires[] = $saisie['options']['nom']; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | spip_log($saisies_nom, 'test.'._LOG_ERREUR); |
---|
73 | foreach ($saisies_obligatoires as $champ) { |
---|
74 | if (!isset($reponses[0][$champ])) { |
---|
75 | $retours = array( |
---|
76 | 'message_erreur' => _T('formidable_importerreponse:message_champ_obligatoire_non_dispo', array('champ' => $champ)), |
---|
77 | 'editable' => ' ' |
---|
78 | ); |
---|
79 | return $retours; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | include_spip('inc/acces'); |
---|
84 | // On parcourt chaque réponse |
---|
85 | foreach ($reponses as $reponse) { |
---|
86 | spip_log($reponse, 'test.'._LOG_ERREUR); |
---|
87 | $info_reponse = array( |
---|
88 | 'id_formulaire' => $id_formulaire, |
---|
89 | 'cookie' => creer_uniqid() |
---|
90 | ); |
---|
91 | // Est-ce qu'il y a un auteur avec un nom |
---|
92 | if (isset($reponse['id_auteur'])) { |
---|
93 | $info_reponse['id_auteur'] = intval($reponse['id_auteur']); |
---|
94 | unset($reponse['id_auteur']); |
---|
95 | } else { |
---|
96 | $info_reponse['id_auteur'] = $GLOBALS['visiteur_session']['id_auteur']; |
---|
97 | } |
---|
98 | if (isset($reponse['date'])) { |
---|
99 | $info_reponse['date'] = $reponse['date']; |
---|
100 | unset($reponse['date']); |
---|
101 | } else { |
---|
102 | $info_reponse['date'] = date('Y-m-d H:i:s'); |
---|
103 | } |
---|
104 | if (isset($reponse['ip'])) { |
---|
105 | $info_reponse['ip'] = $reponse['ip']; |
---|
106 | unset($reponse['ip']); |
---|
107 | } |
---|
108 | $info_reponse['statut'] = $statut_reponses; |
---|
109 | $id_formulaires_reponse = sql_insertq( |
---|
110 | 'spip_formulaires_reponses', |
---|
111 | $info_reponse |
---|
112 | ); |
---|
113 | |
---|
114 | $insertions = array(); |
---|
115 | foreach ($saisies_nom as $saisie) { |
---|
116 | spip_log($saisie.' => '.$reponse[$saisie], 'test.'._LOG_ERREUR); |
---|
117 | if (isset($reponse[$saisie])) { |
---|
118 | $insertions[] = array( |
---|
119 | 'id_formulaires_reponse' => $id_formulaires_reponse, |
---|
120 | 'nom' => $saisie, |
---|
121 | 'valeur' => is_array($reponse[$saisie]) ? serialize($reponse[$saisie]) : $reponse[$saisie] |
---|
122 | ); |
---|
123 | |
---|
124 | } |
---|
125 | } |
---|
126 | if (count($insertions) > 0) { |
---|
127 | sql_insertq_multi( |
---|
128 | 'spip_formulaires_reponses_champs', |
---|
129 | $insertions |
---|
130 | ); |
---|
131 | } |
---|
132 | } |
---|
133 | $retours = array( |
---|
134 | 'message_ok' => _T('formidable_importerreponse:message_insertions_nb', array('nb' => count($reponses))), |
---|
135 | 'editable' => ' ' |
---|
136 | ); |
---|
137 | return $retours; |
---|
138 | } else { |
---|
139 | $retours = array( |
---|
140 | 'message_erreur' => _T('formidable_importerreponse:message_fichier_sans_reponse'), |
---|
141 | 'editable' => ' ' |
---|
142 | ); |
---|
143 | return $retours; |
---|
144 | } |
---|
145 | } else { |
---|
146 | $retours = array( |
---|
147 | 'message_erreur' => _T('formidable_importerreponse:message_formulaire_inexistant'), |
---|
148 | 'editable' => ' ' |
---|
149 | ); |
---|
150 | return $retours; |
---|
151 | } |
---|
152 | } |
---|