1 |
<?php |
2 |
/** |
3 |
* |
4 |
* |
5 |
* @package openmairie_exemple |
6 |
* @version SVN : $Id: requeteur.php 315 2010-12-06 12:13:51Z fmichon $ |
7 |
*/ |
8 |
|
9 |
require_once "../obj/utils.class.php"; |
10 |
$f = new utils("nohtml", "reqmo"); |
11 |
|
12 |
/** |
13 |
* |
14 |
*/ |
15 |
// Nom de l'objet metier |
16 |
(isset($_GET['obj']) ? $obj = $_GET['obj'] : $obj = ""); |
17 |
|
18 |
/** |
19 |
* |
20 |
* Requêteur |
21 |
* principe de REQMO (requete memorisée): |
22 |
* permet de faire des requetes memorisees |
23 |
* la requete est parametree en sql/typedebase/langue/obj.reqmo.inc |
24 |
* $reqmo['sql'] = requete parametrable |
25 |
* les parametres sont entre crochets |
26 |
* type de parametre = $reqmo['parametre'] |
27 |
* checked : case à cocher pour que la zone soit prise en compte |
28 |
* liste : liste de valeur proposé pour parametrer une selection ou un tri |
29 |
* select : liste de valeur proposé pour parametrer une selection ou un tri d apres une requete dans une table |
30 |
* $reqmo['libelle'] = libelle de la requete |
31 |
* $reqmo['separateur'] = separateur pour fichier csv |
32 |
*/ |
33 |
|
34 |
/** |
35 |
* Fichiers requis |
36 |
*/ |
37 |
if (file_exists ("../dyn/var.inc")) |
38 |
include ("../dyn/var.inc"); |
39 |
|
40 |
/** |
41 |
* Paramètres |
42 |
*/ |
43 |
set_time_limit(180); |
44 |
$DEBUG=0; |
45 |
$aff = "requeteur"; |
46 |
$validation = 0; |
47 |
if (isset ($_GET['validation'])) |
48 |
$validation = $_GET['validation']; |
49 |
$idx = ""; |
50 |
if (isset($_GET['idx'])) |
51 |
$idx = $_GET['idx']; |
52 |
$ent = "reqmo"."->".$obj; |
53 |
$ico = "ico_aide.png"; |
54 |
// |
55 |
$f->setTitle(_("Requetes memorisees")." -> ".$obj); |
56 |
$f->setFlag(NULL); |
57 |
$f->display(); |
58 |
|
59 |
if (file_exists ("../sql/".$f -> phptype."/".$obj.".reqmo.inc")) |
60 |
include ("../sql/".$f -> phptype."/".$obj.".reqmo.inc"); |
61 |
|
62 |
if (isset ($_GET ['step'])) |
63 |
$step = $_GET ['step']; |
64 |
else |
65 |
$step = 0; |
66 |
|
67 |
|
68 |
/** |
69 |
* Ouverture du conteneur de la page |
70 |
*/ |
71 |
// |
72 |
echo "\n<div id=\"generator-generate\">\n"; |
73 |
// |
74 |
echo "<div id=\"formulaire\">\n\n"; |
75 |
// |
76 |
echo "<ul>\n"; |
77 |
if (isset($reqmo["reqmo_libelle"])) { |
78 |
echo "\t<li><a href=\"#tabs-1\">"._("Recherche")." "._($reqmo["reqmo_libelle"])."</a></li>\n"; |
79 |
} elseif (isset($reqmo["libelle"])) { |
80 |
echo "\t<li><a href=\"#tabs-1\">"._("Recherche")." "._($reqmo["libelle"])."</a></li>\n"; |
81 |
} else { |
82 |
echo "\t<li><a href=\"#tabs-1\">"._("Recherche")." "._($obj)."</a></li>\n"; |
83 |
} |
84 |
echo "</ul>\n"; |
85 |
// |
86 |
echo "\n<div id=\"tabs-1\">\n"; |
87 |
|
88 |
/** |
89 |
* |
90 |
*/ |
91 |
if ($step == 0) { |
92 |
|
93 |
// |
94 |
$validation = 1; |
95 |
$cptemp = 0; |
96 |
$cpts=0; |
97 |
$cptsel=0; |
98 |
|
99 |
/** |
100 |
* Ouverture du formulaire |
101 |
*/ |
102 |
// Ouverture de la balise formulaire |
103 |
echo "<form method=\"post\" action=\"requeteur.php?obj=".$obj."&step=1\" name=\"f1\">\n"; |
104 |
// Ouverture de la balise table |
105 |
echo "<table cellpadding=\"0\" class=\"formEntete ui-corner-all\">\n"; |
106 |
// |
107 |
echo "<tr><td colspan=\"2\">"; |
108 |
// |
109 |
echo "<fieldset class=\"cadre ui-corner-all ui-widget-content\">\n"; |
110 |
// |
111 |
echo "\t<legend class=\"ui-corner-all ui-widget-content ui-state-active\">"; |
112 |
echo _("Criteres de la requete"); |
113 |
echo "</legend>\n"; |
114 |
// |
115 |
echo "<table>"; |
116 |
// |
117 |
echo "<tr>"; |
118 |
// On separe tous les champs entre crochets dans la requête |
119 |
$temp = explode ("[", $reqmo ["sql"]); |
120 |
// |
121 |
for ($i = 1; $i < sizeof($temp); $i++) { |
122 |
// On vire le crochet de la fin |
123 |
$temp1 = explode("]", $temp[$i]); |
124 |
// On check si alias |
125 |
$temp4 = explode (" as ", $temp1[0]); |
126 |
if (isset($temp4[1])) { |
127 |
$temp1[0] = $temp4[1]; |
128 |
} |
129 |
// |
130 |
$temp6 = $temp1[0]; |
131 |
if (!isset($reqmo[$temp1[0]])) { |
132 |
// saisie criteres where |
133 |
// |
134 |
if ($cpts == 0) { |
135 |
echo "<tr>\n"; |
136 |
} elseif ($cpts == 4) { |
137 |
echo "</tr>\n<tr>\n"; |
138 |
$cpts = 0; |
139 |
} |
140 |
// |
141 |
echo "\t<td class=\"tri\">"; |
142 |
echo " "._($temp6)." <input type=\"text\" name=\"".$temp1[0]."\" value=\"\" size=\"30\" class=\"champFormulaire\" />"; |
143 |
echo "</td>\n"; |
144 |
// |
145 |
$cpts++; |
146 |
} else { |
147 |
// |
148 |
if ($reqmo[$temp1[0]] == "checked") { |
149 |
// |
150 |
if ($cptemp == 0) { |
151 |
echo "<tr>\n"; |
152 |
echo "\t<td colspan=\"4\"><b>"; |
153 |
echo _("Choix des champs a afficher"); |
154 |
echo "</b></td>\n"; |
155 |
echo "</tr>\n"; |
156 |
} elseif ($cptemp == 4) { |
157 |
echo "</tr>\n<tr>\n"; |
158 |
$cptemp = 0; |
159 |
} |
160 |
// |
161 |
echo "\t<td class='champs'>"; |
162 |
echo "<input type=\"checkbox\" value=\"Oui\" name=\"".$temp1[0]."\" size=\"40\" class=\"champFormulaire\" checked=\"checked\" />"; |
163 |
echo " "._($temp6)." "; |
164 |
echo "</td>\n"; |
165 |
// |
166 |
$cptemp++; |
167 |
} else { |
168 |
// |
169 |
$temp3 = ""; |
170 |
$temp3 = $reqmo[$temp1[0]]; |
171 |
if(!is_array($temp3)) { |
172 |
$temp3 = substr($temp3, 0, 6); |
173 |
} |
174 |
// |
175 |
if ($temp3 == "select") { |
176 |
// |
177 |
if ($cptsel == 0) { |
178 |
echo "<tr>\n"; |
179 |
echo "\t<td colspan=\"4\"><b>"; |
180 |
echo _("Choix des criteres de tri"); |
181 |
echo "</b></td>\n"; |
182 |
echo "</tr>\n"; |
183 |
} elseif ($cptsel == 4) { |
184 |
echo "</tr>\n<tr>\n"; |
185 |
$cptsel = 0; |
186 |
} |
187 |
// |
188 |
echo "\t<td class=\"tri\">"; |
189 |
echo _($temp6)." "; |
190 |
echo "<select name=\"".$temp1[0]."\" class=\"champFormulaire\">"; |
191 |
$res1 = $f->db->query($reqmo[$temp1[0]]); |
192 |
$f->isDatabaseError($res); |
193 |
while ($row1 =& $res1->fetchRow()) { |
194 |
echo "<option value=\"".$row1[0]."\">".$row1[1]."</option>"; |
195 |
} |
196 |
echo "</select>"; |
197 |
echo "</td>\n"; |
198 |
// |
199 |
$cptsel++; |
200 |
} else { |
201 |
// |
202 |
if ($cptsel == 0) { |
203 |
echo "<tr>\n"; |
204 |
echo "\t<td colspan=\"4\"><b>"; |
205 |
echo _("Choix des criteres de tri"); |
206 |
echo "</b></td>\n"; |
207 |
echo "</tr>\n"; |
208 |
} elseif ($cptsel == 4) { |
209 |
echo "</tr>\n<tr>\n"; |
210 |
$cptsel = 0; |
211 |
} |
212 |
// |
213 |
echo "\t<td class=\"tri\">"; |
214 |
echo _($temp6)." "; |
215 |
echo "<select name=\"".$temp1[0]."\" class=\"champFormulaire\">"; |
216 |
foreach ($reqmo [$temp1 [0]] as $elem) { |
217 |
echo "<option>".$elem."</option>"; |
218 |
} |
219 |
echo "</select>"; |
220 |
echo "</td>\n"; |
221 |
// |
222 |
$cptsel++; |
223 |
} |
224 |
} |
225 |
} |
226 |
// re initialisation |
227 |
$temp1[0] = ""; |
228 |
} |
229 |
echo "</tr>"; |
230 |
echo "</table>"; |
231 |
// |
232 |
echo "</fieldset>\n"; |
233 |
// |
234 |
echo "</td></tr>\n"; |
235 |
// |
236 |
echo "<tr><td colspan=\"2\">"; |
237 |
// |
238 |
echo "<fieldset class=\"cadre ui-corner-all ui-widget-content\">\n"; |
239 |
// |
240 |
echo "\t<legend class=\"ui-corner-all ui-widget-content ui-state-active\">"; |
241 |
echo _("Parametres de sortie"); |
242 |
echo "</legend>\n"; |
243 |
// |
244 |
echo "<table>"; |
245 |
// |
246 |
echo "<tr>"; |
247 |
// |
248 |
echo "<td class=\"params\">"._("Choix du format de sortie")." "; |
249 |
echo "<select name=\"sortie\" class=\"champFormulaire\">"; |
250 |
echo "<option value=\"tableau\">"._("Tableau - Affichage a l'ecran")."</option>"; |
251 |
echo "<option value=\"csv\">"._("CSV - Export vers logiciel tableur")."</option>"; |
252 |
echo "</select>"; |
253 |
echo "</td>"; |
254 |
// |
255 |
echo "</tr>"; |
256 |
echo "<tr>"; |
257 |
// |
258 |
echo "<td class=\"params\">"._("Separateur de champs (pour le format CSV)")." "; |
259 |
echo "<select name=\"separateur\" class=\"champFormulaire\">"; |
260 |
echo "<option>;</option>"; |
261 |
echo "<option>|</option>"; |
262 |
echo "<option>,</option>"; |
263 |
echo "</select>"; |
264 |
echo "</td>"; |
265 |
// |
266 |
echo "</tr>"; |
267 |
echo "<tr>"; |
268 |
// |
269 |
echo "<td class=\"params\" >"._("Nombre limite d'enregistrements a afficher (pour le format Tableau)")." "; |
270 |
echo "<input type=\"text\" name=\"limite\" value=\"100\" size=\"5\" class=\"champFormulaire\" />"; |
271 |
echo "</td>"; |
272 |
echo "</tr>"; |
273 |
echo "</table>"; |
274 |
// |
275 |
echo "</fieldset>\n"; |
276 |
// |
277 |
echo "</td></tr>\n"; |
278 |
// Fermeture de la balise table |
279 |
echo "</table>\n"; |
280 |
// Affichage des actions de controles du formulaire |
281 |
echo "<div class=\"formControls\">"; |
282 |
// Bouton de validation du formulaire |
283 |
echo "<input type=\"submit\" name=\"valid.reqmo\" value=\""._("Executer la requete memorisee sur :")." '".$obj."'\" />"; |
284 |
// Lien retour |
285 |
echo "<a href=\"reqmo.php\" class=\"retour\">"; |
286 |
echo _("Retour"); |
287 |
echo "</a>"; |
288 |
// Fermeture du conteneur des actions de controles du formulaire |
289 |
echo "</div>"; |
290 |
// Fermeture de la balise formulaire |
291 |
echo "\n</form>\n"; |
292 |
|
293 |
} else { // On affiche le csv ou le tableau |
294 |
$temp = explode ("[",$reqmo["sql"]); |
295 |
for($i = 1; $i < sizeof ($temp); $i++) |
296 |
{ |
297 |
$temp1 = explode ("]", $temp [$i]); |
298 |
$temp4 = explode (" as ", $temp1 [0]); |
299 |
if (isset ($temp4 [1])) |
300 |
$temp5 = $temp4 [1]; // uniquement as |
301 |
else |
302 |
$temp5 = $temp1 [0]; // en entier |
303 |
if (isset ($_POST [$temp5])) |
304 |
$temp2 = $_POST [$temp5]; |
305 |
else |
306 |
$temp2 = ""; |
307 |
// **** |
308 |
if(isset($reqmo[$temp5])){ |
309 |
if($reqmo[$temp5]=="checked") |
310 |
{ |
311 |
if ($temp2 == 'Oui') |
312 |
{ |
313 |
$reqmo ['sql'] = str_replace ("[".$temp1[0]."]",$temp1[0],$reqmo['sql']); |
314 |
} |
315 |
else |
316 |
{ |
317 |
$reqmo['sql']=str_replace("[".$temp1[0]."],",'',$reqmo['sql']); |
318 |
$reqmo['sql']=str_replace(",[".$temp1[0]."]",'',$reqmo['sql']); |
319 |
$reqmo['sql']=str_replace(", [".$temp1[0]."]",'',$reqmo['sql']); |
320 |
$reqmo['sql']=str_replace("[".$temp1[0]."]",'',$reqmo['sql']); |
321 |
} |
322 |
} |
323 |
else |
324 |
{ |
325 |
$reqmo['sql']=str_replace("[".$temp1[0]."]",$temp2,$reqmo['sql']); |
326 |
} |
327 |
//**** |
328 |
}else |
329 |
{ |
330 |
$reqmo['sql']=str_replace("[".$temp1[0]."]",$temp2,$reqmo['sql']); |
331 |
}//**** |
332 |
$temp1[0]=""; |
333 |
} |
334 |
|
335 |
$blanc = 0; |
336 |
$temp = ""; |
337 |
for($i=0;$i<strlen($reqmo['sql']);$i++) |
338 |
{ |
339 |
if (substr($reqmo['sql'],$i,1)==chr(13) or substr($reqmo['sql'],$i,1)==chr(10) or substr($reqmo['sql'],$i,1)==chr(32)) |
340 |
{ |
341 |
if ($blanc==0) |
342 |
$temp=$temp.chr(32); |
343 |
$blanc=1; |
344 |
} |
345 |
else |
346 |
{ |
347 |
$temp=$temp.substr($reqmo['sql'],$i,1); |
348 |
$blanc=0; |
349 |
} |
350 |
} |
351 |
$reqmo['sql']=$temp ; |
352 |
$reqmo['sql']=str_replace(',,',',',$reqmo['sql']); |
353 |
$reqmo['sql']=str_replace(', ,',',',$reqmo['sql']); |
354 |
$reqmo['sql']=str_replace(', from',' from',$reqmo['sql']); |
355 |
$reqmo['sql']=str_replace('select ,','select ',$reqmo['sql']); |
356 |
// post limite |
357 |
if (isset($_POST['limite'])) |
358 |
$limite = $_POST['limite']; |
359 |
else |
360 |
$limite = 100; |
361 |
// post sortie |
362 |
if (isset ($_POST['sortie'])) |
363 |
$sortie= $_POST['sortie']; |
364 |
else |
365 |
$sortie ='tableau'; |
366 |
// post separateur de champ (csv) |
367 |
if (isset ($_POST['separateur'])) |
368 |
$separateur= $_POST['separateur']; |
369 |
else |
370 |
$separateur =';'; |
371 |
// limite uniquement pour tableau |
372 |
if ($sortie =='tableau') |
373 |
$reqmo['sql']= $reqmo['sql']." limit ".$limite; |
374 |
// execution de la requete |
375 |
$res = $f -> db -> query ($reqmo['sql']); |
376 |
$f->isDatabaseError($res); |
377 |
|
378 |
$info = $res -> tableInfo (); |
379 |
if ($sortie =='tableau') { |
380 |
// |
381 |
echo "<table class=\"tab-tab\">\n"; |
382 |
// |
383 |
echo "<tr class=\"ui-tabs-nav ui-accordion ui-state-default tab-title\">"; |
384 |
foreach($info as $elem) { |
385 |
echo "\t<th class=\"title\"><center>"._($elem['name'])."</center></th>\n"; |
386 |
} |
387 |
echo "</tr>\n"; |
388 |
// |
389 |
$cptenr = 0; |
390 |
while ($row=& $res->fetchRow()) { |
391 |
// |
392 |
echo "<tr class=\"tab-data ".($cptenr % 2 == 0 ? "odd" : "even")."\">\n"; |
393 |
// |
394 |
$cptenr = $cptenr + 1; |
395 |
$i = 0; |
396 |
foreach ($row as $elem) { |
397 |
if (is_numeric($elem)) |
398 |
echo "<td class='resultrequete' align='right'>"; |
399 |
else |
400 |
echo "<td class='resultrequete'>"; |
401 |
$tmp=""; |
402 |
$tmp=str_replace(chr(13).chr(10),'<br>', $elem); |
403 |
echo $tmp."</td>"; |
404 |
$i++; |
405 |
} |
406 |
echo "</tr>\n"; |
407 |
} |
408 |
// |
409 |
echo "</table>\n"; |
410 |
if ($cptenr==0){ |
411 |
echo "<br>"._('aucun')." "._('enregistrement')."<br>"; |
412 |
} |
413 |
} |
414 |
else |
415 |
{ |
416 |
$inf=""; |
417 |
foreach ($info as $elem) |
418 |
{ |
419 |
//echo $elem['name'].$separateur; |
420 |
$inf=$inf.$elem['name'].$separateur; |
421 |
} |
422 |
//echo "<br />"; |
423 |
$inf .= "\n"; |
424 |
$cptenr=0; |
425 |
while ($row=& $res->fetchRow()) |
426 |
{ |
427 |
$cptenr=$cptenr+1; |
428 |
$i=0; |
429 |
foreach($row as $elem) |
430 |
{ |
431 |
//echo $elem.$separateur; |
432 |
//**** |
433 |
$tmp=""; |
434 |
$tmp=str_replace(chr(13).chr(10),' / ', $elem); |
435 |
$tmp=str_replace(';',' ', $tmp); |
436 |
//***** |
437 |
$inf .= $tmp.$separateur; |
438 |
$i++; |
439 |
} |
440 |
//echo "<br />"; |
441 |
$inf .= "\n"; |
442 |
} |
443 |
if ($cptenr==0){ |
444 |
$inf .="\n"._('aucun')." "._('enregistrement')."\n"; |
445 |
} |
446 |
$nom_fichier="export_".$obj.".csv"; |
447 |
$fic = fopen ("../tmp/".$nom_fichier,"w"); |
448 |
fwrite ($fic, $inf); |
449 |
fclose ($fic); |
450 |
// $msg = _("voir")." "._("resultat")." "._("recherche")." : "; |
451 |
// $msg .= "<a href=\"javascript:traces('".$nom_fichier."');\"><img src=\"../img/voir.png\" style='vertical-align:middle' alt=\""._("voir")." ".$nom_fichier."\" title=\""._("voir")." ".$nom_fichier."\" /></a><br />"; |
452 |
// $msg .= _("resultat")." /tmp/".$nom_fichier.".<br />"; |
453 |
// echo $msg; |
454 |
|
455 |
// modification du 25 aout pour acces enregistrement sur clic |
456 |
$msg = _("msg1"); |
457 |
$msg .= "<a href=\"javascript:traces('".$nom_fichier."');\"><img src=\"../img/voir.png\" hspace='5' alt=\"Fichier export\" title=\"Fichier export\" /></a><br />"; |
458 |
$msg .= _("msg2")."\"".$nom_fichier."\".<br />"; |
459 |
echo $msg; |
460 |
} |
461 |
|
462 |
// Affichage des actions de controles du formulaire |
463 |
echo "<div class=\"formControls\">"; |
464 |
// Lien retour |
465 |
echo "<a href=\"../scr/requeteur.php?obj=".$obj."&step=0\" class=\"retour\">"; |
466 |
echo _("Retour"); |
467 |
echo "</a>"; |
468 |
// Fermeture du conteneur des actions de controles du formulaire |
469 |
echo "</div>"; |
470 |
} |
471 |
|
472 |
/** |
473 |
* Fermeture du conteneur de la page |
474 |
*/ |
475 |
// |
476 |
echo "</div>\n"; |
477 |
// |
478 |
echo "</div>\n"; |
479 |
// |
480 |
echo "</div>\n"; |
481 |
?> |