Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.OldQueryObj.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Generated Header (not documented yet)
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.OldQueryObj.php,v 1.2 2003/08/18 15:46:42 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage CORE
15  */
16 /**
17  */
18 // ---------------------------------------------------------------------------
19 // anakeen 2000 - Yannick Le Briquer
20 // ---------------------------------------------------------------------------
21 // This program is free software; you can redistribute it and/or modify
22 // it under the terms of the GNU General Public License as published by
23 // the Free Software Foundation; either version 2 of the License, or (at
24 // your option) any later version.
25 //
26 // This program is distributed in the hope that it will be useful, but
27 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 // for more details.
30 //
31 // You should have received a copy of the GNU General Public License along
32 // with this program; if not, write to the Free Software Foundation, Inc.,
33 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 // ---------------------------------------------------------------------------
35 // $Id: Class.OldQueryObj.php,v 1.2 2003/08/18 15:46:42 eric Exp $
36 // $Log: Class.OldQueryObj.php,v $
37 // Revision 1.2 2003/08/18 15:46:42 eric
38 // phpdoc
39 //
40 // Revision 1.1 2002/01/08 12:41:34 eric
41 // first
42 //
43 // Revision 1.1 2001/01/25 03:21:38 marianne
44 // initialisation
45 //
46 // ---------------------------------------------------------------------------
47 // This class is designed to perform query constructs on objects
48 // It returns a table of objects that match the query
49 //
50 // It gives the HTML/JScript element for the gui
51 // and it gives the result of the query
52 $CLASS_OLDQUERYOBJ_PHP = '$Id: Class.OldQueryObj.php,v 1.2 2003/08/18 15:46:42 eric Exp $';
53 
54 include_once ('Class.Out.php');
55 include_once ('Class.Table.php');
56 include_once ('Class.Log.php');
57 
59 {
60 
61  var $nb = 0;
62  var $LastQuery = "";
63 
64  var $table;
65 
66  var $operators = array(
67  "none" => array(
68  "lib" => " --",
69  "oper" => "",
70  "param" => "NONE"
71  ) ,
72  "begin" => array(
73  "lib" => "Commence par",
74  "oper" => "like",
75  "param" => "SEMIPERCENT"
76  ) ,
77  "like" => array(
78  "lib" => "Contient",
79  "oper" => "like",
80  "param" => "PERCENT"
81  ) ,
82  "nlike" => array(
83  "lib" => "Ne Contient Pas",
84  "oper" => "not like",
85  "param" => "PERCENT"
86  ) ,
87  "=" => array(
88  "lib" => "Est égal à",
89  "oper" => "=",
90  "param" => "NORMAL"
91  ) ,
92  "!=" => array(
93  "lib" => "Est différent de",
94  "oper" => "!=",
95  "param" => "NORMAL"
96  ) ,
97  ">" => array(
98  "lib" => "Est Supérieur à",
99  "oper" => ">",
100  "param" => "NORMAL"
101  ) ,
102  "<" => array(
103  "lib" => "Est Inférieur à",
104  "oper" => "<",
105  "param" => "NORMAL"
106  ) ,
107  "notn" => array(
108  "lib" => "N'est pas Vide",
109  "oper" => "is not null",
110  "param" => "NONE"
111  ) ,
112  "null" => array(
113  "lib" => "Est Vide",
114  "oper" => "is null",
115  "param" => "NONE"
116  )
117  );
118 
119  var $down = "&nbsp;v";
120  var $up = "&nbsp;^";
121 
122  var $display_alpha = "FALSE";
123  var $alpha_default = "NONE";
124  var $distinct = "distinct";
125 
127  {
128  //
129  $this->log = new Log("", "Query", "$class");
130  $this->log->debug("Nouveau QueryObj $dbaccess/$class");
131  $this->basic_elem = new $class($dbaccess);
132 
133  $this->dbaccess = $dbaccess;
134  $this->class = $class;
135  $this->init("string", "");
136  $this->init("operator", "none");
137  $this->init("criteria", "");
138  $this->init("case", "");
139  $this->init("order_by", "");
140  $this->init("casse", "NON");
141  $this->init("alpha_criteria", "");
142  $this->init("desc", "down");
143  $this->init("start", 0);
144  $this->init("slice", 20);
145 
146  $this->table = new Table();
147  $this->table->start = $this->start;
148  $this->table->slice = $this->slice;
149  }
150  // Perform the query : the result can be a table or a list of objects
151  // depending on the third arg.
152  // the third ARG should be :
153  // LIST : means a table of objects
154  // LISTC : means a table of completed objects
155  // TABLE : means a table of table fields
156  function Query($start = 0, $slice = 0, $res_type = "LIST", $p_query = "")
157  {
158  if ($p_query == '') {
159  // select construct
160  $select = "";
161  reset($this->basic_elem->fields);
162  while (list($k, $v) = each($this->basic_elem->fields)) {
163  $select = $select . " " . $this->basic_elem->dbtable . "." . $v . ",";
164  }
165 
166  reset($this->basic_elem->sup_fields);
167  while (list($k, $v) = each($this->basic_elem->sup_fields)) {
168  $select = $select . " " . $v . ",";
169  }
170  $select = substr($select, 0, strlen($select) - 1);
171  // from
172  $from = $this->basic_elem->dbtable;
173  reset($this->basic_elem->sup_tables);
174  while (list($k, $v) = each($this->basic_elem->sup_tables)) {
175  $from = $from . "," . $v;
176  }
177 
178  $query = "select " . $this->distinct . " {$select}
179  from {$from} ";
180 
181  $nb_where = 0;
182  $where[$nb_where] = $this->CriteriaClause();
183  if ($where[$nb_where] != "") {
184  $nb_where++;
185  }
186  $where[$nb_where] = $this->AlphaClause();
187  if ($where[$nb_where] != "") {
188  $nb_where++;
189  }
190  $where[$nb_where] = $this->SupClause();
191  if ($where[$nb_where] != "") {
192  $nb_where++;
193  }
194 
195  if ($nb_where > 0) {
196  $i = 0;
197  $query = $query . ' where ';
198  reset($where);
199  while (list($k, $v) = each($where)) {
200  if ($v != "") {
201  if ($i == 0) {
202  $query = $query . $v;
203  } else {
204  $query = $query . ' AND ' . $v;
205  }
206  $i++;
207  }
208  }
209  }
210  // Order by
211  if ($this->order_by != "") {
212  $query = $query . " order by " . $this->order_by;
213  if ($this->desc == "up") {
214  $query = $query . " desc";
215  }
216  }
217  $query = $query . ';';
218  $query = $query . ';';
219  } else {
220  $query = $p_query;
221  }
222  $this->dbid = pg_connect("{$this->dbaccess}");
223  ### echo $this->dbaccess;
224  $chrono = microtime();
225  ###$this->log->debug("Requète : $query / $chrono");
226  $this->LastQuery = $query;
227  ### echo "<br>$query";
228  $res = pg_exec($this->dbid, $query);
229 
230  if (!$res) return FALSE;
231 
232  $this->nb = pg_numrows($res);
233 
234  if ($this->nb == 0) return FALSE;
235  $chrono = microtime();
236  $this->log->debug("Résultats : {$this->nb} / $chrono");
237 
238  if ($start >= $this->nb) {
239  $start = 0;
240  }
241  if ($slice == 0) {
242  $slice = $this->nb;
243  }
244 
245  if (($start + $slice) >= $this->nb) {
246  $end = $this->nb;
247  } else {
248  $end = $start + $slice;
249  }
250  $this->log->debug("Extraction de " . $start . " à " . $end);
251  for ($c = 0; $c < $this->nb; $c++) {
252  if (($c >= $start) && ($c < $end)) {
253  $result = pg_fetch_array($res, $c);
254  $this->log->debug("Affectation de " . $c);
255  if (($res_type == "LIST") || ($res_type == "LISTC")) {
256  $this->list[$c] = new $this->class($this->dbaccess, "", $result, $this->dbid);
257  } else {
258  while (list($k, $v) = each($result)) {
259  $this->list[$c][$k] = $v;
260  }
261  }
262  } else {
263  $this->list[$c] = "";
264  }
265  }
266  $chrono = microtime();
267  $this->log->debug("Affectation du tableau : $chrono");
268  return ($this->list);
269  }
270 
271  function CriteriaClause()
272  {
273  $out = "";
274  if (($this->criteria != "") && ($this->operator != "none")) {
275  if ($this->casse == "NON") {
276  $out = $out . " upper(" . $this->criteria . ") " . $this->operators[$this->operator]["oper"];
277  } else {
278  $out = $out . $this->criteria . " " . $this->operators[$this->operator]["oper"];
279  }
280  $string = "";
281  switch ($this->operators[$this->operator]["param"]) {
282  case "NORMAL":
283  $string = " '{$this->string}'";
284  break;
285 
286  case "PERCENT":
287  $string = " '%{$this->string}%'";
288  break;
289 
290  case "SEMIPERCENT":
291  $string = " '{$this->string}%'";
292  }
293  if (($this->operator != 'null') && ($this->operator != 'notn')) {
294  if ($this->casse == "NON") {
295  $out.= " upper({$string})";
296  } else {
297  $out.= $string;
298  }
299  }
300  }
301  return ($out);
302  }
303 
304  function AlphaClause()
305  {
306  $out = "";
307  if (($this->display_alpha == "TRUE") && ($this->alpha != "ALL") && ($this->alpha != "NONE")) {
308  $out = " upper({$this->alpha_criteria}) like upper('{$this->alpha}%') ";
309  }
310  return ($out);
311  }
312 
313  function SupClause()
314  {
315  $out = "";
316  $this->log->debug("Supplementaires");
317  if (sizeof($this->basic_elem->sup_where) > 0) {
318  reset($this->basic_elem->sup_where);
319  $count = 0;
320  while (list($k, $v) = each($this->basic_elem->sup_where)) {
321  if ($count > 0) {
322  $out = $out . " AND " . $v;
323  } else {
324  $out = $out . " " . $v;
325  }
326  $count++;
327  }
328  }
329  return ($out);
330  }
331  // Create a complete page with the form, the table, the links...
332  // url = url called back on next/prev/start/end button (the calling page)
333  // nores = HTML message displayed when the query result is empty
334  // complete = boolean : say TRUE if you need that the object list is completely
335  // initiated (means that some attributes of the object are checked out from
336  // different tables). say FALSE if all the fields you need are checked out in
337  // the query. If you use complete on large results, it may take a while and
338  // a lot of memory.
339  function GenPage($url, $nores, $complete = "FALSE", $display_form = "YES")
340  {
341 
342  $this->init("alpha", $this->alpha_default);
343 
344  $this->table->start = $this->start;
345  $this->table->slice = $this->slice;
346 
347  $o = new Out();
348 
349  $o->cat($this->GenForm($url, $display_form));
350 
351  if (($this->display_alpha == "TRUE") && ($this->alpha == "NONE")) {
352  return ($o->flush());
353  }
354 
355  if ($complete == "FALSE") {
356  $this->table->array = $this->Query($this->start, $this->slice, "TABLE");
357  } else {
358  $this->table->array = $this->Query($this->start, $this->slice, "LISTC");
359  $this->table->arrayobj = "TRUE";
360  }
361 
362  $this->table->sort_link = "javascript:void(0)\\\" onClick=\\\"SortPage(
363  document.QueryObj,'%s');return false";
364  $this->table->page_link = "javascript:void(0)\\\" onclick=\\\"GoPage(
365  document.QueryObj,%s,%s);return false";
366 
367  if (!isset($this->table->headcontent["$this->order_by"])) {
368  $this->table->headcontent["$this->order_by"] = $this->order_by;
369  }
370  $desc = $this->desc;
371  $this->table->headcontent["$this->order_by"].= $this->$desc;
372 
373  if ($this->nb == 0) {
374  $o->cat($nores);
375  } else {
376  $o->cat($this->table->get());
377  }
378 
379  $o->cat('<table border=0 cellspacing=0 cellpadding=0 width="100%">
380  <tr><td class="textinv2">Total : ' . $this->nb . '</td></tr>
381  </table>');
382 
383  return ($o->flush());
384  }
385  // Generate the Query Form
386  function GenForm($url, $display_form = "YES")
387  {
388 
389  if (sizeof($this->basic_elem->criterias) == 0) return ("");
390  $this->log->debug("Nouvelle forme");
391 
392  $o = new Out();
393  // JavaScript Construct
394  $o->cat('
395  <script language="javascript">
396  function selFiltre(form_cour, champ) {
397  var oper_sel=form_cour.p_operator[form_cour.p_operator.selectedIndex].value;
398  var crit_sel=form_cour.p_criteria[form_cour.p_criteria.selectedIndex].value;
399  var crit_idx=form_cour.p_criteria.selectedIndex;
400  tab_types = new Array();');
401 
402  $ind = 0;
403  // table of types
404  reset($this->basic_elem->criterias);
405  while (list($k, $v) = each($this->basic_elem->criterias)) {
406  $o->cat("tab_types[{$ind}]=\"" . $v["type"] . "\";");
407  $ind++;
408  }
409 
410  $o->cat('
411  if ((tab_types[crit_idx]=="NUM") && (oper_sel=="nlike")) {
412  alert ("Un champ numérique ne peut-être recherché avec \'Ne Contient Pas\'!!");
413  form_cour.p_operator.options[0].selected=true;
414  }
415  if ((tab_types[crit_idx]=="NUM") && (oper_sel=="like")) {
416  alert ("Un champ numérique ne peut-être recherché avec \'Contient\'!!");
417  form_cour.p_operator.options[0].selected=true;
418  }
419  if ((crit_sel=="") && (champ.name=="p_criteria")) {
420  form_cour.p_string.value = "";
421  form_cour.p_operator.options[0].selected=true;
422  }
423  }');
424 
425  $o->cat('
426  function checkFiltre(form_cour) {
427  ');
428  if ($display_form == 'YES') {
429  $o->cat('
430  var ok=true;
431  var oper_sel=form_cour.p_operator[form_cour.p_operator.selectedIndex].value;
432  var crit_sel=form_cour.p_criteria[form_cour.p_criteria.selectedIndex].value;
433  var casse=form_cour.chk_casse.checked;
434  if (!casse) {
435  form_cour.p_casse.value="NON";
436  } else {
437  form_cour.p_casse.value="OUI";
438  }
439  if ((crit_sel=="") && (oper_sel!="")) {
440  ok=false;
441  alert ("Vous devez choisir un critere!!"); }
442  if ((crit_sel!="") && (oper_sel=="")) {
443  ok=false;
444  alert ("Vous devez choisir un operator!!"); }
445  return(ok);
446  }
447  ');
448  } else {
449  $o->cat(' var ok=true; return(ok); }');
450  }
451  $o->cat('
452 
453  function queryFiltre(form_cour) {
454 
455  ok = checkFiltre(form_cour);
456  if (ok) {
457  form_cour.p_start.value=0;
458  form_cour.submit();
459  }
460  }
461 
462  function resetFiltre(form_cour) {
463  form_cour.p_criteria.selectedIndex=0;
464  form_cour.p_operator.selectedIndex=0;
465  form_cour.p_start.value=0;
466  form_cour.p_string.value="";
467  form_cour.submit();
468  }
469 
470  function GoPage(form_cour,start,slice) {
471  form_cour.p_start.value = start;
472  form_cour.p_slice.value = slice;
473  ok=checkFiltre(form_cour);
474  if (ok) {form_cour.submit();}
475  }
476 
477  function SortPage(form_cour,order) {
478  if (form_cour.p_order_by.value == order) {
479  if (form_cour.p_desc.value == "down") {
480  form_cour.p_desc.value = "up";
481  } else {
482  form_cour.p_desc.value = "down";
483  }
484  } else {
485  form_cour.p_order_by.value = order;
486  form_cour.p_desc.value = "down";
487  }
488  ok=checkFiltre(form_cour);
489  if (ok) {form_cour.submit();}
490  }
491 
492  function setAlpha(form_cour,car) {
493  form_cour.p_alpha.value = car;
494  form_cour.p_start.value = 0;
495  ok=checkFiltre(form_cour);
496  if (ok) {form_cour.submit();}
497  }
498 
499  </script>');
500  // Form Construct
501  $o->cat('
502  <form name="QueryObj" method="POST" action="' . $url . '">');
503  if ($display_form == "YES") {
504  $o->cat('<table bgcolor="#F2F2F2" border="0" cellspacing="0" cellpadding="2" >
505  <tr>');
506  // Alpha Index
507  if ($this->display_alpha == "TRUE") {
508  $o->cat('<td><small><select name="p_alpha_criteria">\n');
509  reset($this->basic_elem->criterias);
510  while (list($k, $v) = each($this->basic_elem->criterias)) {
511  if ($v["type"] == "TXT") {
512  if ($this->alpha_criteria == $k) {
513  $o->cat("<OPTION SELECTED VALUE=\"{$k}\">" . $v["libelle"] . "\n");
514  } else {
515  $o->cat("<OPTION VALUE=\"{$k}\">" . $v["libelle"] . "\n");
516  }
517  }
518  }
519  $o->cat('</select></small></td>');
520  $o->cat('<td nowrap colspan="3">');
521  for ($i = ord("A"); $i <= ord("Z"); $i++) {
522  $c = chr($i);
523  $o->cat(' ');
524  if ($this->alpha == $c) {
525  $o->cat('<b>[');
526  }
527  $o->cat("<a href=\"javascript:void(0)\"
528  onClick=\"setAlpha(document.QueryObj,'" . $c . "');return false\">");
529  $o->cat('<font size="-1">' . $c . '</font></a>');
530  if ($this->alpha == $c) {
531  $o->cat(']</b>');
532  }
533  }
534  $o->cat("</td><td align=\"center\">");
535  if ($this->alpha == "ALL") $o->cat('<b>[');
536  $o->cat("<a href=\"javascript:void(0)\"
537  onClick=\"setAlpha(document.QueryObj,'ALL');return false\">");
538  $o->cat('<font size="-1">Tout</font></a>');
539  if ($this->alpha == "ALL") $o->cat(']</b>');
540  $o->cat("<br>");
541  if ($this->alpha == "NONE") {
542  $o->cat('<b>[');
543  }
544  $o->cat("<a href=\"javascript:void(0)\"
545  onClick=\"setAlpha(document.QueryObj,'NONE');return false\">");
546  $o->cat("<font size=\"-1\">Rien</font></a>");
547  if ($this->alpha == "NONE") {
548  $o->cat(']</b>');
549  }
550  $o->cat("</tr>");
551  }
552  // Criteria
553  $o->cat("<tr>
554  <TD><SMALL><SELECT NAME=\"p_criteria\"
555  onChange=\"selFiltre(document.QueryObj,this);\">\n");
556  reset($this->basic_elem->criterias);
557  while (list($k, $v) = each($this->basic_elem->criterias)) {
558  if ($this->criteria == $k) {
559  $o->cat("<OPTION SELECTED VALUE=\"{$k}\">" . $v["libelle"] . "\n");
560  } else {
561  $o->cat("<OPTION VALUE=\"{$k}\">" . $v["libelle"] . "\n");
562  }
563  }
564  $o->cat("</SELECT></SMALL></TD>\n");
565  // Operator
566  $o->cat("
567  <TD><SMALL><SELECT NAME=\"p_operator\"
568  onChange=\"selFiltre(document.QueryObj,this);\">\n");
569  while (list($k, $v) = each($this->operators)) {
570  if ($this->operator == $k) {
571  $o->cat("<OPTION SELECTED VALUE=\"{$k}\">" . $v["lib"] . "\n");
572  } else {
573  $o->cat("<OPTION VALUE=\"{$k}\">" . $v["lib"] . "\n");
574  }
575  }
576  $o->cat("</SELECT></SMALL></TD>\n");
577  // String (value)
578  $o->cat('
579  <td><small><input name="p_string" maxlength=15 size=10 class="finput"
580  value="' . $this->string . '">
581  </td>');
582  // Query buttons
583  $o->cat('
584  <td><a href="javascript:void(0)"
585  onClick="queryFiltre(document.QueryObj);return false">
586  <font size="-1">Recherche</font></a>
587  <a href="javascript:void(0)"
588  onClick="resetFiltre(document.QueryObj);return false">
589  <font size="-1">Réinitialiser</font></a>
590  </td>');
591  // Case sensitivity
592  $o->cat('
593  <td nowrap><input type="checkbox"');
594  if ($this->casse == "OUI") {
595  $o->cat(' checked ');
596  }
597  $o->cat('name="chk_casse"><font size="-1">Respecter la casse</font></td>');
598  // TODO
599  // Form end
600  $o->cat('
601  </tr></table>');
602  }
603  $o->cat('<input type="hidden" name="p_slice" value="' . $this->slice . '">
604  <input type="hidden" name="p_start" value="' . $this->start . '">
605  <input type="hidden" name="p_order_by" value="' . $this->order_by . '">
606  <input type="hidden" name="p_casse" value="' . $this->casse . '">
607  <input type="hidden" name="p_desc" value="' . $this->desc . '">
608  <input type="hidden" name="p_alpha" value="' . $this->alpha . '">
609  </form>');
610 
611  return ($o->flush());
612  }
613 
614  function init($attr, $default)
615  {
616  global $HTTP_POST_VARS;
617  if (is_array($HTTP_POST_VARS) && isset($HTTP_POST_VARS["p_$attr"])) {
618  $this->$attr = $HTTP_POST_VARS["p_$attr"];
619  } else {
620  if (isset($this->basic_elem->def[$attr])) {
621  $this->$attr = $this->basic_elem->def[$attr];
622  } else {
623  $this->$attr = $default;
624  }
625  }
626  }
627 }
628 ?>
← centre documentaire © anakeen - published under CC License - Dynacase