1 | <?php |
---|
2 | /** |
---|
3 | * Gestion du formulaire de d'édition de produit |
---|
4 | * |
---|
5 | * @plugin Produits |
---|
6 | * @copyright 2015 |
---|
7 | * @author Les Développements Durables |
---|
8 | * @licence GNU/GPL |
---|
9 | * @package SPIP\Produits\Formulaires |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('_ECRIRE_INC_VERSION')) { |
---|
13 | return; |
---|
14 | } |
---|
15 | |
---|
16 | include_spip('inc/config'); |
---|
17 | include_spip('inc/actions'); |
---|
18 | include_spip('inc/editer'); |
---|
19 | |
---|
20 | function formulaires_editer_produit_saisies_dist($id_produit = 'new', $id_rubrique = 0, $retour = '') { |
---|
21 | include_spip('inc/config'); |
---|
22 | $editer_ttc = lire_config('produits/editer_ttc'); |
---|
23 | $taxe_defaut = 100 * lire_config('produits/taxe', 0); |
---|
24 | |
---|
25 | $saisies = array( |
---|
26 | array( |
---|
27 | 'saisie' => 'input', |
---|
28 | 'options' => array( |
---|
29 | 'nom' => 'titre', |
---|
30 | 'label' => _T('produits:produit_champ_titre_label'), |
---|
31 | 'obligatoire' => 'oui', |
---|
32 | 'placeholder' => _T('info_sans_titre') |
---|
33 | ) |
---|
34 | ), |
---|
35 | array( |
---|
36 | 'saisie' => 'input', |
---|
37 | 'options' => array( |
---|
38 | 'nom' => 'reference', |
---|
39 | 'label' => _T('produits:produit_champ_reference_label'), |
---|
40 | ) |
---|
41 | ), |
---|
42 | array( |
---|
43 | 'saisie' => 'selecteur_rubrique', |
---|
44 | 'options' => array( |
---|
45 | 'nom' => 'parent', |
---|
46 | 'label' => _T('produits:produit_champ_rubrique_label'), |
---|
47 | 'multiple' => '0' |
---|
48 | ) |
---|
49 | ), |
---|
50 | array( |
---|
51 | 'saisie' => 'input', |
---|
52 | 'options' => array( |
---|
53 | 'nom' => ($editer_ttc) ? 'prix_ttc' : 'prix_ht', |
---|
54 | 'label' => ($editer_ttc) ? |
---|
55 | _T('produits:produit_champ_prix_ttc_label') : |
---|
56 | _T('produits:produit_champ_prix_ht_label'), |
---|
57 | 'obligatoire' => 'oui' |
---|
58 | ), |
---|
59 | 'verifier' => array( |
---|
60 | 'type' => 'decimal' |
---|
61 | ) |
---|
62 | ), |
---|
63 | array( |
---|
64 | 'saisie' => 'input', |
---|
65 | 'options' => array( |
---|
66 | 'nom' => 'taxe', |
---|
67 | 'label' => _T('produits:produit_champ_taxe_label'), |
---|
68 | 'explication' => _T( |
---|
69 | 'produits:produit_champ_taxe_explication', |
---|
70 | array('taxe' => $taxe_defaut.' %') |
---|
71 | ), |
---|
72 | 'inserer_fin' => '<span class="pourcent"> %</span>' |
---|
73 | ), |
---|
74 | 'verifier' => array( |
---|
75 | 'type' => 'decimal' |
---|
76 | ) |
---|
77 | ), |
---|
78 | array( |
---|
79 | 'saisie' => 'textarea', |
---|
80 | 'options' => array( |
---|
81 | 'nom' => 'descriptif', |
---|
82 | 'label' => _T('produits:produit_champ_descriptif_label'), |
---|
83 | 'rows' => 5 |
---|
84 | ) |
---|
85 | ), |
---|
86 | array( |
---|
87 | 'saisie' => 'textarea', |
---|
88 | 'options' => array( |
---|
89 | 'nom' => 'texte', |
---|
90 | 'label' => _T('produits:produit_champ_texte_label'), |
---|
91 | 'rows' => 7 |
---|
92 | ) |
---|
93 | ), |
---|
94 | array( |
---|
95 | 'saisie' => 'fieldset', |
---|
96 | 'options' => array( |
---|
97 | 'nom' => 'conditionnement', |
---|
98 | 'label' => _T('produits:legend_dimensions') |
---|
99 | ), |
---|
100 | 'saisies' => array( |
---|
101 | array( |
---|
102 | 'saisie' => 'case', |
---|
103 | 'options' => array( |
---|
104 | 'nom' => 'immateriel', |
---|
105 | 'label_case' => _T('produits:produit_champ_immateriel_label'), |
---|
106 | 'li_class' => 'pleine_largeur', |
---|
107 | 'valeur_oui' => '1' |
---|
108 | ) |
---|
109 | ), |
---|
110 | array( |
---|
111 | 'saisie' => 'input', |
---|
112 | 'options' => array( |
---|
113 | 'nom' => 'poids', |
---|
114 | 'label' => _T('produits:produit_champ_poids_label'), |
---|
115 | 'li_class' => 'unit size1of4', |
---|
116 | 'afficher_si' => '@immateriel@==""' |
---|
117 | ) |
---|
118 | ), |
---|
119 | array( |
---|
120 | 'saisie' => 'input', |
---|
121 | 'options' => array( |
---|
122 | 'nom' => 'largeur', |
---|
123 | 'label' => _T('produits:produit_champ_largeur_label'), |
---|
124 | 'li_class' => 'unit size1of4', |
---|
125 | 'afficher_si' => '@immateriel@==""' |
---|
126 | ) |
---|
127 | ), |
---|
128 | array( |
---|
129 | 'saisie' => 'input', |
---|
130 | 'options' => array( |
---|
131 | 'nom' => 'longueur', |
---|
132 | 'label' => _T('produits:produit_champ_longueur_label'), |
---|
133 | 'li_class' => 'unit size1of4', |
---|
134 | 'afficher_si' => '@immateriel@==""' |
---|
135 | ) |
---|
136 | ), |
---|
137 | array( |
---|
138 | 'saisie' => 'input', |
---|
139 | 'options' => array( |
---|
140 | 'nom' => 'hauteur', |
---|
141 | 'label' => _T('produits:produit_champ_hauteur_label'), |
---|
142 | 'li_class' => 'unit size1of4', |
---|
143 | 'afficher_si' => '@immateriel@==""' |
---|
144 | ) |
---|
145 | ) |
---|
146 | ) |
---|
147 | ) |
---|
148 | ); |
---|
149 | |
---|
150 | return $saisies; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Identifier le formulaire en faisant abstraction des paramètres qui ne représentent pas l'objet edité |
---|
155 | * |
---|
156 | * @param int|string $id_produit |
---|
157 | * Identifiant du produit. 'new' pour un nouveau produit. |
---|
158 | * @param int $id_rubrique |
---|
159 | * Identifiant de la rubrique parente (si connue) |
---|
160 | * @param string $retour |
---|
161 | * URL de redirection après le traitement |
---|
162 | * @return string |
---|
163 | * Hash du formulaire |
---|
164 | */ |
---|
165 | function formulaires_editer_produit_identifier_dist($id_produit = 'new', $id_rubrique = 0, $retour = '') { |
---|
166 | return serialize(array(intval($id_produit))); |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Chargement du formulaire d'édition de produit |
---|
171 | * |
---|
172 | * Déclarer les champs postés et y intégrer les valeurs par défaut |
---|
173 | * |
---|
174 | * @uses formulaires_editer_objet_charger() |
---|
175 | * |
---|
176 | * @param int|string $id_produit |
---|
177 | * Identifiant du produit. 'new' pour un nouveau produit. |
---|
178 | * @param int $id_rubrique |
---|
179 | * Identifiant de la rubrique parente (si connue) |
---|
180 | * @param string $retour |
---|
181 | * URL de redirection après le traitement |
---|
182 | * @return array |
---|
183 | * Environnement du formulaire |
---|
184 | */ |
---|
185 | function formulaires_editer_produit_charger($id_produit = 'new', $id_rubrique = 0, $retour = '', $lier_trad = 0) { |
---|
186 | $config = lire_config('produits') ; |
---|
187 | |
---|
188 | // Si pas de rubrique passée et que l'insertion est limitée à une seule rubrique, on peut déjà la passer par defaut |
---|
189 | if (!$id_rubrique && $config['limiter_ajout'] && (count($config['limiter_ident_secteur']) == 1)) { |
---|
190 | $id_rubrique = $config['limiter_ident_secteur'][0]; |
---|
191 | } |
---|
192 | |
---|
193 | $contexte = formulaires_editer_objet_charger('produit', $id_produit, $id_rubrique, $lier_trad, $retour, ''); |
---|
194 | |
---|
195 | //Si on a déjà le $id_produit il faut afficher sa rubrique! |
---|
196 | if ($id_produit > 0) { |
---|
197 | $id_rubrique = sql_getfetsel('id_rubrique', 'spip_produits', 'id_produit='.sql_quote($id_produit)); |
---|
198 | } |
---|
199 | if ($id_rubrique != 0) { |
---|
200 | $contexte['parent'] = 'rubrique|'.($contexte['id_rubrique'] ? $contexte['id_rubrique'] : $id_rubrique); |
---|
201 | } |
---|
202 | |
---|
203 | //Calculer le prix TTC selon le contexte |
---|
204 | $taxe = floatval($contexte['taxe'] ? $contexte['taxe'] : lire_config('produits/taxe', 0)); |
---|
205 | if (strlen($contexte['taxe'])) { |
---|
206 | $contexte['taxe'] = 100 * $contexte['taxe']; |
---|
207 | } |
---|
208 | $contexte['_taxe_defaut'] = 100 * lire_config('produits/taxe', 0); |
---|
209 | |
---|
210 | $precision_ttc = intval(lire_config('produits/precision_ttc', 2)); |
---|
211 | $contexte['prix_ht'] = floatval($contexte['prix_ht']); |
---|
212 | $contexte['prix_ttc'] = round($contexte['prix_ht'] * (1+$taxe), $precision_ttc); |
---|
213 | |
---|
214 | unset($contexte['id_produit']); |
---|
215 | unset($contexte['id_rubrique']); |
---|
216 | |
---|
217 | return $contexte; |
---|
218 | } |
---|
219 | |
---|
220 | /** |
---|
221 | * Vérifications du formulaire d'édition de produit |
---|
222 | * |
---|
223 | * Vérifier les champs postés et signaler d'éventuelles erreurs |
---|
224 | * |
---|
225 | * @uses formulaires_editer_objet_verifier() |
---|
226 | * |
---|
227 | * @param int|string $id_produit |
---|
228 | * Identifiant du produit. 'new' pour un nouveau produit. |
---|
229 | * @param int $id_rubrique |
---|
230 | * Identifiant de la rubrique parente (si connue) |
---|
231 | * @param string $retour |
---|
232 | * URL de redirection après le traitement |
---|
233 | * @return array |
---|
234 | * Tableau des erreurs |
---|
235 | */ |
---|
236 | function formulaires_editer_produit_verifier($id_produit = 'new', $id_rubrique = 0, $retour = '', $lier_trad = 0) { |
---|
237 | $erreurs = array(); |
---|
238 | $config = lire_config('produits'); |
---|
239 | |
---|
240 | // Vérifier que la rubrique choisie se trouve dans les secteurs autorisés |
---|
241 | if ( |
---|
242 | !empty($config['limiter_ajout']) |
---|
243 | and is_array($config['limiter_ident_secteur']) |
---|
244 | and $id_rubrique = produits_id_parent() |
---|
245 | ) { |
---|
246 | $id_secteur = sql_getfetsel('id_secteur', 'spip_rubriques', 'id_rubrique=' . intval(produits_id_parent())); |
---|
247 | if (!in_array($id_secteur, $config['limiter_ident_secteur'])) { |
---|
248 | $titres = ''; |
---|
249 | foreach ($config['limiter_ident_secteur'] as $id_secteur) { |
---|
250 | $titres .= sql_getfetsel('titre', 'spip_rubriques', 'id_rubrique=' . intval($id_secteur)).' / '; |
---|
251 | } |
---|
252 | $erreurs['parent'] .= _T('produits:secteurs_autorises', array('secteurs' => $titres)); |
---|
253 | } |
---|
254 | } |
---|
255 | |
---|
256 | return $erreurs ; |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * Traitement du formulaire d'édition de produit |
---|
261 | * |
---|
262 | * Traiter les champs postés |
---|
263 | * |
---|
264 | * @uses formulaires_editer_objet_traiter() |
---|
265 | * |
---|
266 | * @param int|string $id_produit |
---|
267 | * Identifiant du produit. 'new' pour un nouveau produit. |
---|
268 | * @param int $id_rubrique |
---|
269 | * Identifiant de la rubrique parente (si connue) |
---|
270 | * @param string $retour |
---|
271 | * URL de redirection après le traitement |
---|
272 | * @return array |
---|
273 | * Retours des traitements |
---|
274 | */ |
---|
275 | function formulaires_editer_produit_traiter($id_produit = 'new', $id_rubrique = 0, $retour = '', $lier_trad = 0) { |
---|
276 | set_request('id_parent', produits_id_parent()); |
---|
277 | if ($taxe = _request('taxe')) { |
---|
278 | set_request('taxe', $taxe/100); |
---|
279 | } |
---|
280 | |
---|
281 | if (lire_config('produits/editer_ttc')) { |
---|
282 | $prix_ht = _request('prix_ttc') / (1 + _request('taxe', lire_config('produits/taxe', 0))); |
---|
283 | set_request('prix_ht', $prix_ht); |
---|
284 | } |
---|
285 | |
---|
286 | $retours = formulaires_editer_objet_traiter('produit', $id_produit, $id_rubrique, $lier_trad, $retour); |
---|
287 | |
---|
288 | // cas d’erreur conserver la valeur de taxe saisie. |
---|
289 | if (!empty($retours['message_erreur'])) { |
---|
290 | if ($taxe = _request('taxe')) { |
---|
291 | set_request('taxe', $taxe*100); |
---|
292 | } |
---|
293 | } |
---|
294 | return $retours; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * Fonction qui retourne l'identifiant de la rubrique choisie via le sélecteur de rubrique |
---|
299 | * |
---|
300 | * Le sélecteur retourne un tableau : `array('rubrique|10')` |
---|
301 | * |
---|
302 | * @return int |
---|
303 | * Identifiant de la rubrique |
---|
304 | */ |
---|
305 | function produits_id_parent() { |
---|
306 | // On reformule l'id_parent |
---|
307 | $id_parent = _request('parent'); |
---|
308 | // La saisie retourne normalement un tableau, dans ce cas on considére |
---|
309 | // la premiere valeur comme vrai parent (logique issue de polyhierarchie) |
---|
310 | if (is_array($id_parent)) { |
---|
311 | $id_parent = array_shift($id_parent); |
---|
312 | } |
---|
313 | return( str_replace('rubrique|', '', $id_parent)); |
---|
314 | } |
---|