Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.Attr.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  * Generation of PHP Document classes
9  *
10  * @author Anakeen 2000
11  * @version $Id: Lib.Attr.php,v 1.85 2009/01/14 09:18:05 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 include_once ('FDL/Class.Doc.php');
20 include_once ('FDL/Class.DocFam.php');
21 /**
22  * Generate Class.Docxxx.php files
23  *
24  * @param string $dbaccess database specification
25  * @param array $tdoc array of family definition
26  */
28 {
29  global $action;
30 
31  $GEN = getGen($dbaccess);
32  $phpAdoc = new Layout("FDL/Layout/Class.Doc.xml", $action);
33 
34  if ($tdoc["classname"] == "") { // default classname
35  if ($tdoc["fromid"] == 0) $tdoc["classname"] = "DocFile";
36  else $tdoc["classname"] = "Doc" . $tdoc["fromid"];
37  }
38 
39  $phpAdoc->Set("docid", $tdoc["id"]);
40  $phpAdoc->Set("include", "");
41  $phpAdoc->Set("GEN", "");
42  if ($tdoc["fromid"] == 0) {
43  $phpAdoc->Set("DocParent", $tdoc["classname"]);
44  $phpAdoc->Set("AParent", "ADoc");
45  $phpAdoc->Set("fromid", "");
46  $phpAdoc->Set("pinit", "DocCtrl");
47  } else {
48  $phpAdoc->Set("fromid", $tdoc["fromid"]);
49  if ($tdoc["classname"] != "Doc" . $tdoc["fromid"]) {
50  $phpAdoc->Set("DocParent", $tdoc["classname"]);
51  $phpAdoc->Set("pinit", $tdoc["classname"]);
52  $phpAdoc->Set("include", "include_once(\"FDL$GEN/Class.Doc" . $tdoc["fromid"] . ".php\");");
53  } else {
54  $phpAdoc->Set("GEN", $GEN);
55  $phpAdoc->Set("DocParent", "Doc" . $tdoc["fromid"]);
56  if ($tdoc["usefor"] == "W") $phpAdoc->Set("pinit", "WDoc"); // special init for workflow
57  else $phpAdoc->Set("pinit", "DocCtrl");
58  }
59  $phpAdoc->Set("AParent", "ADoc" . $tdoc["fromid"]);
60  }
61  $phpAdoc->Set("title", $tdoc["title"]);
62  $query = new QueryDb($dbaccess, "DocAttr");
63  $query->AddQuery("docid=" . $tdoc["id"]);
64  $query->order_by = "ordered";
65 
66  $table1 = $query->Query();
67 
68  $phpAdoc->Set("sattr", "");
69 
70  $phpAdoc->set("hasattr", false);
71  $pa = getParentAttributes($dbaccess, $tdoc["fromid"]);
72 
73  if ($query->nb > 0) {
74 
75  $tmenu = array();
76  $tfield = array();
77  $tnormal = array();
78  $tattr = array();
79  $attrids = array();
80  $tcattr = array();
81 
82  foreach ($table1 as $k => $v) {
83  $type = trim(strtok($v->type, "("));
84  if ($type == "docid") {
85  // add title auto
86  if (preg_match("/doctitle=([A-Za-z0-9_-]+)/", $v->options, $reg)) {
87  $doctitle = $reg[1];
88  if ($doctitle == "auto") $doctitle = $v->id . "_title";
89  $doctitle = strtolower($doctitle);
90  $table1[$doctitle] = clone ($v);
91  $table1[$doctitle]->id = $doctitle;
92  $table1[$doctitle]->type = "text";
93  $table1[$doctitle]->visibility = "H";
94  $table1[$doctitle]->phpfile = "";
95  if (!preg_match("/docrev=(fixed|state)/", $v->options)) {
96  $table1[$doctitle]->phpfunc = "::getLastTitle(" . $v->id . ",' )";
97  } else {
98  $table1[$doctitle]->phpfunc = "::getTitle(" . $v->id . ",' )";
99  }
100  $table1[$doctitle]->options = "autotitle=yes";
101  $table1[$doctitle]->title = "N";
102  $table1[$doctitle]->abstract = "N";
103  $table1[$doctitle]->needed = "N";
104  $table1[$doctitle]->usefor = "A";
105  $table1[$doctitle]->link = "";
106  $table1[$doctitle]->phpconstraint = "";
107  $table1[$doctitle]->labeltext = $v->labeltext . ' ' . _("(title)");
108  $table1[$doctitle]->ordered = $v->ordered + 1;
109  }
110  }
111  }
112 
113  foreach ($table1 as $k => $v) {
114  if ($v->id[0] == ':') $v = completeAttribute($dbaccess, $v);
115 
116  if ($v->visibility == "F") $v->type = "frame"; // old notation compliant
117  if ($v->visibility == "M") $v->type = "menu"; // old notation compliant
118  if ($v->type == "integer") $v->type = "int"; // old notation compliant
119  $v->phpfunc = str_replace("\"", "\\\"", $v->phpfunc);
120  switch (strtolower($v->type)) {
121  case "menu": // menu
122  if (substr($v->link, 0, 2) == "::") {
123  if (preg_match('/::([^\(]+)\(([^\)]*)\)/', $v->link, $reg)) {
124  /*
125  $iattr = explode(",",$reg[2]);
126  $iattr2 = $iattr;
127  $tiattr=array();
128  while(list($ka,$va) = each($iattr)) {
129  $tiattr[]= array("niarg"=>trim($va));
130  if ($va[0] == "'") unset($iattr2[$ka]); // not really attribute
131 
132  }*/
133  $method = $reg[1];
134  $v->link = "%S%app=FDL&action=FDL_METHOD&id=%I%&method=$method";
135  }
136  }
137  $tmenu[strtolower($v->id) ] = array(
138  "attrid" => strtolower($v->id) ,
139  "label" => str_replace("\"", "\\\"", $v->labeltext) ,
140  "order" => intval($v->ordered) ,
141  "link" => str_replace("\"", "\\\"", $v->link) ,
142  "visibility" => $v->visibility,
143  "options" => str_replace("\"", "\\\"", $v->options) ,
144  "precond" => $v->phpfunc
145  );
146  break;
147 
148  case "tab":
149  case "frame": // frame
150  $tfield[strtolower($v->id) ] = array(
151  "attrid" => strtolower($v->id) ,
152  "visibility" => $v->visibility,
153  "label" => str_replace("\"", "\\\"", $v->labeltext) ,
154  "usefor" => $v->usefor,
155  "type" => $v->type,
156  "options" => str_replace("\"", "\\\"", $v->options) ,
157  "frame" => ($v->frameid == "") ? "FIELD_HIDDENS" : strtolower($v->frameid)
158  );
159  break;
160 
161  case "action": // action
162  $taction[strtolower($v->id) ] = array(
163  "attrid" => strtolower($v->id) ,
164  "visibility" => $v->visibility,
165  "label" => str_replace("\"", "\\\"", $v->labeltext) ,
166  "order" => intval($v->ordered) ,
167  "options" => str_replace("\"", "\\\"", $v->options) ,
168  "wapplication" => $v->phpfile,
169  "waction" => $v->phpfunc,
170  "precond" => str_replace("\"", "\\\"", $v->phpconstraint)
171  );
172  break;
173 
174  default: // normal
175  if (preg_match('/\[([a-z=0-9]+)\](.*)/', $v->phpfunc, $reg)) {
176  $v->phpfunc = $reg[2];
177  $funcformat = $reg[1];
178  } else {
179  $funcformat = "";
180  }
181 
182  if (preg_match("/([a-z]+)\([\"'](.*)[\"']\)/i", $v->type, $reg)) {
183  $atype = $reg[1];
184  $aformat = $reg[2];
185  if ($atype == "idoc") {
186  if (!is_numeric($aformat)) $aformat = getFamIdFromName($dbaccess, $aformat);
187  }
188  } else {
189  $atype = $v->type;
190  $aformat = "";
191  }
192  if (preg_match("/([a-z]+)list/i", $atype, $reg)) {
193  $atype = $reg[1];
194  $repeat = "true";
195  } else {
196  if ($tnormal[strtolower($v->frameid) ]["type"] == "array") $repeat = "true";
197  else if (strpos($v->options, "multiple=yes") !== false) $repeat = "true";
198  else if (getTypeMain($pa[strtolower($v->frameid) ]["type"]) == "array") $repeat = "true";
199  else $repeat = "false";
200  }
201  $atype = strtolower(trim($atype));
202  // create code for calculated attributes
203  if (substr($v->phpfunc, 0, 2) == "::") {
204  if (preg_match('/::([^\(]+)\(([^\)]*)\)[:]{0,1}(.*)/', $v->phpfunc, $reg)) {
205  $iattr = explode(",", $reg[2]);
206  $iattr2 = $iattr;
207  $tiattr = array();
208  foreach ($iattr as $ka => $va) {
209  $tiattr[] = array(
210  "niarg" => trim($va)
211  );
212  if (($va[0] == "'") || ($va[0] == '"')) unset($iattr2[$ka]); // not really attribute
213 
214 
215  }
216 
217  $phpAdoc->SetBlockData("biattr" . $v->id, $tiattr);
218  $tcattr[] = array(
219  "method" => $reg[1],
220  "callmethod" => $v->phpfunc,
221  "callattr" => $v->id,
222  "biattr" => "biattr" . $v->id,
223  "rarg" => ($reg[3] == "") ? $v->id : trim($reg[3]) ,
224  "niargs" => implode(",", $iattr2)
225  );
226  }
227  }
228  // complete attributes characteristics
229  $v->id = chop(strtolower($v->id));
230 
231  if (!$v->phpconstraint) {
232  if (($atype == "integer") || ($atype == "int")) {
233  $v->phpconstraint = sprintf("::isInteger(%s)", $v->id);
234  } elseif (($atype == "money") || ($atype == "double")) {
235  $v->phpconstraint = sprintf("::isFloat(%s)", $v->id);
236  }
237  }
238  $tnormal[($v->id) ] = array(
239  "attrid" => ($v->id) ,
240  "label" => str_replace("\"", "\\\"", $v->labeltext) ,
241  "type" => $atype,
242  "format" => str_replace("\"", "\\\"", $aformat) ,
243  "eformat" => str_replace("\"", "\\\"", $funcformat) ,
244  "options" => str_replace("\"", "\\\"", $v->options) ,
245  "order" => intval($v->ordered) ,
246  "link" => str_replace("\"", "\\\"", $v->link) ,
247  "visibility" => $v->visibility,
248  "needed" => ($v->needed == "Y") ? "true" : "false",
249  "title" => ($v->title == "Y") ? "true" : "false",
250  "repeat" => $repeat,
251  "abstract" => ($v->abstract == "Y") ? "true" : "false",
252  "frame" => ($v->frameid == "") ? "FIELD_HIDDENS" : strtolower($v->frameid) ,
253  "elink" => $v->elink,
254  "phpfile" => $v->phpfile,
255  "phpfunc" => str_replace(", |", ", |", $v->phpfunc) ,
256  "phpconstraint" => str_replace("\"", "\\\"", $v->phpconstraint) ,
257  "usefor" => $v->usefor
258  );
259 
260  if (($atype != "array") && ($v->usefor != "Q")) {
261  if ($atype != "array") $tattr[$v->id] = array(
262  "attrid" => ($v->id)
263  );
264  if (($repeat == "true") || ($tnormal[$v->frameid]["type"] == "array")) {
265  $attrids[$v->id] = ($v->id) . " text"; // for the moment all repeat are text
266 
267  } else {
268  switch ($atype) {
269  case double:
270  case float:
271  case money:
272  $attrids[$v->id] = ($v->id) . " float8";
273  break;
274 
275  case int:
276  case integer:
277  $attrids[$v->id] = ($v->id) . " int4";
278  break;
279 
280  case date:
281  $attrids[$v->id] = ($v->id) . " date";
282  break;
283 
284  case timestamp:
285  $attrids[$v->id] = ($v->id) . " timestamp without time zone";
286  break;
287 
288  case time:
289  $attrids[$v->id] = ($v->id) . " time";
290  break;
291 
292  default:
293  $attrids[$v->id] = ($v->id) . " text";
294  }
295  }
296  }
297  }
298  }
299 
300  $phpAdoc->Set("sattr", implode(",", $attrids));
301  $phpAdoc->SetBlockData("MATTR", $tmenu);
302  $phpAdoc->SetBlockData("FATTR", $tfield);
303  $phpAdoc->SetBlockData("AATTR", $taction);
304  $phpAdoc->SetBlockData("NATTR", $tnormal);
305  $phpAdoc->SetBlockData("ATTRFIELD", $tattr);
306 
307  $phpAdoc->set("hasattr", (count($tattr) > 0));
308  $phpAdoc->SetBlockData("ACALC", $tcattr);
309  }
310 
311  if ($tdoc["name"] == '') $tdoc["name"] = 'F__' . $tdoc["id"];
312  if ($tdoc["name"] != "") { // create name alias classes
313  $phpAdoc->SetBlockData("CLASSALIAS", array(
314  array(
315  "zou"
316  )
317  ));
318  $phpAdoc->Set("docName", $tdoc["name"]);
319  $phpAdoc->Set("PHPclassName", str_replace(array(
320  ":",
321  "-"
322  ) , "_", $tdoc["name"]));
323  }
324  $phpAdoc->Set("docTitle", str_replace('"', '\\"', $tdoc["title"]));
325  //----------------------------------
326  // Add specials methods
327  $cmethod = ""; // method file which is use as inherit virtual class
328  if (isset($tdoc["methods"]) && ($tdoc["methods"] != "")) {
329  $tfmethods = explode("\n", $tdoc["methods"]);
330  $contents = "";
331  foreach ($tfmethods as $fmethods) {
332  if ($fmethods[0] == "*") {
333  $cmethod = substr($fmethods, 1);
334  $filename = GetParam("CORE_PUBDIR") . "/FDL/" . $cmethod;
335  $fd = fopen($filename, "rb");
336  $contents2 = fread($fd, filesize($filename)); // only one
337  $contents2 = preg_replace('%(?: //[^\n]*\@begin-method-ignore| /\*+[^/]*?\@begin-method-ignore)(.*?)(?: //[^\n]*\@end-method-ignore[^\n]*| /\*+[^/]*?\@end-method-ignore[^/]*?\*/)%xms', '', $contents2);
338  fclose($fd);
339  } else {
340  $filename = GetParam("CORE_PUBDIR") . "/FDL/" . $fmethods;
341  $fd = fopen($filename, "rb");
342  $contents.= fread($fd, filesize($filename));
343  fclose($fd);
344  }
345  }
346  $contents = preg_replace('%(?: //[^\n]*\@begin-method-ignore| /\*+[^/]*?\@begin-method-ignore)(.*?)(?: //[^\n]*\@end-method-ignore[^\n]*| /\*+[^/]*?\@end-method-ignore[^/]*?\*/)%xms', '', $contents);
347  $phpAdoc->Set("METHODS", str_replace(array(
348  "<?php\n",
349  "<?php\r\n",
350  "\n?>"
351  ) , "", $contents));
352  } else $phpAdoc->Set("METHODS", "");
353 
354  $phpAdoc->Set("DocParent1", $phpAdoc->Get("DocParent"));
355  if ($cmethod != "") {
356  $phpAdoc->Set("METHODS2", str_replace(array(
357  "<?php\n",
358  "<?php\r\n",
359  "\n?>"
360  ) , "", $contents2));
361  $phpAdoc->SetBlockData("INDIRECT", array(
362  array(
363  "zou"
364  )
365  ));
366  $phpAdoc->Set("docNameIndirect", "Doc" . $tdoc["id"] . "__");
367  $phpAdoc->Set("RedirectDocParent", $phpAdoc->Get("DocParent"));
368  $phpAdoc->Set("DocParent", $phpAdoc->Get("docNameIndirect"));
369  }
370  return $phpAdoc->gen();
371  }
372 
373  function PgUpdateFamilly($dbaccess, $docid, $docname = "")
374  {
375  $msg = "";
376  $GEN = getGen($dbaccess);
378  $err = $doc->exec_query("SELECT oid FROM pg_class where relname='doc" . $docid . "';");
379  if ($doc->numrows() == 0) {
380  $msg.= "Create table doc" . $docid . "\n";
381  // create postgres table if new familly
382  $cdoc = createDoc($dbaccess, $docid);
383  $triggers = $cdoc->sqltrigger(false, true);
384  $cdoc->exec_query($triggers, 1);
385  // step by step
386  $cdoc->Create();
388 
389  $err = $doc->exec_query("SELECT oid FROM pg_class where relname='doc" . $docid . "';");
390  if ($doc->numrows() == 0) {
391  $msg.= "Cannot create Table : $err\n";
392  }
393  }
394  $row = $doc->fetch_array(0, PGSQL_ASSOC);
395  $relid = $row["oid"]; // pg id of the table
396  // create view
397  if ($docname != "") {
398  $docname = strtolower($docname);
399  $err = $doc->exec_query(sprintf("SELECT oid from pg_class where relname='%s' and relnamespace=(select oid from pg_namespace where nspname='family');", $docname));
400  $updateview = false;
401  if ($doc->numrows() == 1) {
402  // update view
403  $sql = sprintf("drop view family.\"%s\"", $docname);
404  $doc->exec_query($sql, 1);
405  $updateview = true;
406  }
407  $err = $doc->exec_query(sprintf("SELECT oid from pg_class where relname='%s' and relnamespace=(select oid from pg_namespace where nspname='family');", $docname));
408  if ($doc->numrows() == 0) {
409  if (!$updateview) $msg.= "Create view family." . $docname . "\n";
410  // create postgres table if new familly
411  $sql = sprintf("create view family.\"%s\" as select * from doc%d", ($docname) , $docid);
412  $doc->exec_query($sql, 1);
413 
414  $err = $doc->exec_query(sprintf("SELECT oid from pg_class where relname='%s' and relnamespace=(select oid from pg_namespace where nspname='family');", $docname));
415  if ($doc->numrows() == 0) {
416  $msg.= "Cannot create view : $err\n";
417  }
418  }
419  }
420 
421  $sqlquery = "select attname FROM pg_attribute where attrelid=$relid;";
422  $doc->exec_query($sqlquery, 1); // search existed attribute of the table
423  $nbidx = $doc->numrows();
424  $pgatt = array();
425  for ($c = 0; $c < $nbidx; $c++) {
426  $row = $doc->fetch_array($c, PGSQL_ASSOC);
427  $pgatt[$row["attname"]] = $row["attname"];
428  }
429  // -----------------------------
430  // add column attribute
431  $classname = "Doc" . $docid;
432  include_once ("FDL$GEN/Class.$classname.php");
433  $cdoc = new $classname($dbaccess);
434 
435  $qattr = new QueryDb($dbaccess, "DocAttr");
436  $qattr->AddQuery("docid=" . $docid);
437  $qattr->AddQuery("type != 'menu'");
438  $qattr->AddQuery("type != 'frame'");
439  $qattr->AddQuery("type != 'tab'");
440  $qattr->AddQuery("type != 'action'");
441  $qattr->AddQuery("id !~ '^:'");
442  // $qattr->AddQuery("type !~ '^array'"); // must be visible to know for child attributes
443  $qattr->AddQuery("visibility != 'M'");
444  $qattr->AddQuery("visibility != 'F'");
445  $qattr->AddQuery("usefor != 'Q' or usefor is null");
446 
447  $oattr = $qattr->Query();
448  if ($qattr->nb > 0) {
449  foreach ($oattr as $ka => $attr) {
450  $tattr[strtolower($attr->id) ] = $attr;
451  if ($attr->type == 'file') {
452  $tattr[strtolower($attr->id) . '_txt'] = $attr;
453  $tattr[strtolower($attr->id) . '_vec'] = clone ($attr);
454  $tattr[strtolower($attr->id) . '_vec']->type = 'tsvector';
455  } else if (substr($attr->type, 0, 5) == "docid") {
456  if (preg_match("/doctitle=([A-Za-z0-9_-]+)/", $attr->options, $reg)) {
457  $doctitle = $reg[1];
458  if ($doctitle == "auto") $doctitle = $attr->id . "_title";
459  $doctitle = strtolower($doctitle);
460  $tattr[$doctitle] = $attr;
461  $tattr[$doctitle]->id = $doctitle;
462  $tattr[$doctitle]->type = "text";
463  }
464  }
465  }
466 
467  foreach ($tattr as $ka => $attr) {
468  $attr->id = chop($attr->id);
469  if (substr($attr->type, 0, 5) == "array") continue; // skip array but must be in table to search element in arrays
470  if ($attr->docid == $docid) { // modify my field not inherited fields
471  if (!in_array($ka, $pgatt)) {
472  $msg.= "add field $ka in table doc" . $docid . "\n";
473 
474  if (($attr->repeat) || (($tattr[$attr->frameid]->type == "array") && ($attr->type != 'tsvector'))) {
475 
476  $sqltype = " text"; // for the moment all repeat are text
477 
478  } else {
479  $rtype = strtok($attr->type, "(");
480  switch ($rtype) {
481  case double:
482  case float:
483  case money:
484  $sqltype = " float8";
485  break;
486 
487  case int:
488  case integer:
489  $sqltype = " int4";
490  break;
491 
492  case date:
493  $sqltype = " date";
494  break;
495 
496  case timestamp:
497  $sqltype = " timestamp without time zone";
498  break;
499 
500  case time:
501  $sqltype = " time";
502  break;
503 
504  case tsvector:
505  $sqltype = " tsvector";
506  break;
507 
508  default:
509  $sqltype = " text";
510  }
511  }
512  $sqlquery = "ALTER TABLE doc" . $docid . " ADD COLUMN $ka $sqltype;";
513  $doc->exec_query($sqlquery, 1); // add new field
514 
515  }
516  }
517  }
518  }
519 
520  $ncdoc = new_Doc($dbaccess, $docid);
521  if (isset($ncdoc->attributes->fromids) && (in_array(2, $ncdoc->attributes->fromids)) && ($ncdoc->usefor == "N")) {
522  // its a folder
523  $ncdoc->usefor = "F";
524  print "\nchange usefor to F\n";
525  $ncdoc->modify();
526  }
527 
528  return $msg;
529  }
530 
532  {
533 
534  $GEN = getGen($dbaccess);
535  $pubdir = GetParam("CORE_PUBDIR");
536  $dfile = "$pubdir/FDL$GEN/Class.Doc" . $tdoc["id"] . ".php";
537 
538  $fphp = fopen($dfile, "w");
539  if ($fphp) {
540  $err = fwrite($fphp, AttrtoPhp($dbaccess, $tdoc));
541  if ($err === false) print_r2("cannot access $dfile");
542  fclose($fphp);
543  @chmod($dfile, 0666); // write for nobody
544 
545  } else {
546  print_r2("cannot access $dfile");
547  }
548 
549  return $dfile;
550  }
551 
553  {
554  $cdoc = createTmpDoc($dbaccess, $docid);
555  $msg = $cdoc->exec_query($cdoc->sqltrigger(false, true) , 1);
556  $sqlcmds = explode(";", $cdoc->SqlTrigger());
557  //$cdoc = new_Doc($dbacceanss, $docid);
558  // print $cdoc->SqlTrigger();
559  foreach ($sqlcmds as $k => $sqlquery) {
560  if ($sqlquery != "") $msg = $cdoc->exec_query($sqlquery, 1);
561  }
562  }
564  {
565  $cdoc = createDoc($dbaccess, $docid);
566  $indexes = $cdoc->GetSqlIndex();
567  if ($indexes) $msg = $cdoc->exec_query($indexes);
568  }
569  // refresh PHP Class & Postgres Table Definition
571  {
572 
573  $query = new QueryDb($dbaccess, "DocFam");
574  $query->AddQuery("doctype='C'");
575  $query->AddQuery("id=$docid");
576  $table1 = $query->Query(0, 0, "TABLE");
577 
578  if ($query->nb > 0) {
579  $v = $table1[0];
580  $df = createDocFile($dbaccess, $v);
581 
582  $msg = PgUpdateFamilly($dbaccess, $v["id"]);
583  //------------------------------
584  // see if workflow
585  AddLogMsg($msg);
586  // -----------------------------
587  // activate trigger by trigger
589  }
590  }
591  /**
592  * complete attribute properties from parent attribute
593  */
595  {
596  $ta->id = substr($ta->id, 1);
597  $fromid = getFamFromId($dbaccess, $ta->docid);
598  $tfromid[] = $fromid;
599  while ($fromid = getFamFromId($dbaccess, $fromid)) {
600  $tfromid[] = $fromid;
601  }
602  $tfromid[] = $ta->docid; // itself
603  $query = new QueryDb($dbaccess, "DocAttr");
604  $query->AddQuery(GetSqlCond($tfromid, 'docid'));
605  $query->AddQuery("id='" . pg_escape_string($ta->id) . "'");
606  $query->order_by = "docid";
607  $tas = $query->Query(0, 0, "TABLE");
608 
609  if ($query->nb == 0) {
610  error_log("MODATTR error for " . $ta->id);
611  return $ta;
612  } else {
613  $tw = $ta;
614 
615  foreach ($tas as $ta1) {
616  foreach ($ta1 as $k => $v) {
617  if ($v && (!$ta->$k)) $tw->$k = $v;
618  if ($ta->$k == "-") $tw->$k = ""; // suppress value
619 
620  }
621  }
622 
623  return $tw;
624  }
625  }
626  /**
627  * get parent attributes
628  */
629  function getParentAttributes($dbaccess, $fromid)
630  {
631  if ($fromid > 0) {
632  $query = new QueryDb($dbaccess, "DocAttr");
633  $query->AddQuery("docid=" . $fromid);
634 
635  $pa = $query->Query(0, 0, "TABLE");
636  if (is_array($pa)) {
637 
638  $nextfromid = getFamFromId($dbaccess, $fromid);
639  if ($nextfromid > 0) $pa = array_merge($pa, getParentAttributes($dbaccess, $nextfromid));
640  $paf = array();
641  foreach ($pa as $v) $paf[$v["id"]] = $v;
642  return $paf;
643  }
644  }
645  return array();
646  }
647  /**
648  * Extract the main type and the format from a type string
649  *
650  * @param string $type e.g. 'array("empty")'
651  *
652  * @return array() struct e.g. array('type' => 'array', 'format' => '"empty"')
653  */
654  function parseType($type)
655  {
656  if (preg_match('/^\s*(?P<type>[a-z]+)(?P<format>\(.+\))?\s*$/i', $type, $m)) {
657  /* Remove leading and trailing parenthesis from format */
658  $m['format'] = substr($m['format'], 1, -1);
659  return array(
660  'type' => $m['type'],
661  'format' => $m['format']
662  );
663  }
664  return array(
665  'type' => $type,
666  'format' => ''
667  );
668  }
669 
670  function getTypeMain($type)
671  {
672  $p = parseType($type);
673  return $p['type'];
674  }
675 
677  {
678  $p = parseType($type);
679  return $p['format'];
680  }
681 ?>
← centre documentaire © anakeen - published under CC License - Dynacase