Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocAttribute.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  * Document Attributes
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.DocAttribute.php,v 1.47 2008/12/11 10:06:51 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 /**
18  *
19  * Generic attribute class
20  *
21  * @author anakeen
22  */
24 {
25  public $id;
26  public $docid;
27  public $labelText;
28  public $visibility; // W, R, H, O, M, I
29  public $options;
30  public $docname;
31  public $type; // text, longtext, date, file, ...
32 
33 
34  /**
35  * Construct a basic attribute
36  *
37  * @param string $id logical name of the attr
38  * @param string $docid
39  * @param string $label
40  */
42  {
43  $this->id = $id;
44  $this->docid = $docid;
45  $this->labelText = $label;
46  }
47  /**
48  * Return attribute label
49  *
50  * @return string
51  */
52  function getLabel()
53  {
54  $r = $this->docname . '#' . $this->id;
55  $i = _($r);
56  if ($i != $r) return $i;
57  return $this->labelText;
58  }
59  /**
60  * Return value of option $x
61  *
62  * @param $x option name
63  * @param $def default value
64  *
65  * @return string
66  */
67  function getOption($x, $def = "")
68  {
69  if (!isset($this->_topt)) {
70  $topt = explode("|", $this->options);
71  $this->_topt = array();
72  foreach ($topt as $k => $v) {
73  list($vn, $vv) = explode("=", $v, 2);
74  $this->_topt[$vn] = $vv;
75  }
76  }
77  $r = $this->docname . '#' . $this->id . '#' . $x;
78  $i = _($r);
79  if ($i != $r) return $i;
80  $v = $this->_topt[$x];
81  return ($v ? $v : $def);
82  }
83  /**
84  * Return all value of options
85  *
86  * @return array
87  */
88  function getOptions()
89  {
90  if (!isset($this->_topt)) {
91  $this->getOption('a');
92  }
93  return $this->_topt;
94  }
95  /**
96  * Temporary change option
97  *
98  * @param $x name
99  * @param $v value
100  *
101  * @return void
102  */
103  function setOption($x, $v)
104  {
105  if (!isset($this->_topt)) {
106  $this->getOption($x);
107  }
108  $this->_topt[$x] = $v;
109  }
110  /**
111  * temporary change visibility
112  * @param char $vis new visibility : R|H|W|O|I
113  * @return void
114  */
115  function setVisibility($vis)
116  {
117  $this->mvisibility = $vis;
118  }
119  /**
120  * temporary change need
121  * @param bool $need true means needed, false not needed
122  * @return void
123  */
124  function setNeeded($need)
125  {
126  $this->needed = $need;
127  }
128  /**
129  * test if attribute is not a auto created attribute
130  *
131  * @return void
132  */
133  function isReal()
134  {
135  return $this->getOption("autocreated") != "yes";
136  }
137  /**
138  * Escape value with xml entities
139  *
140  * @param string $s value
141  *
142  * @return string
143  */
144  static function encodeXml($s, $quot = false)
145  {
146  if ($quot) {
147  return str_replace(array(
148  '&',
149  '<',
150  '>',
151  '"'
152  ) , array(
153  '&amp;',
154  '&lt;',
155  '&gt;',
156  '&quot;'
157  ) , $s);
158  } else {
159  return str_replace(array(
160  '&',
161  '<',
162  '>'
163  ) , array(
164  '&amp;',
165  '&lt;',
166  '&gt;'
167  ) , $s);
168  }
169  }
170  /**
171  * to see if an attribute is n item of an array
172  *
173  * @return boolean
174  */
175  function inArray()
176  {
177  if (get_class($this) == "NormalAttribute") {
178  if ($this->fieldSet->type == "array") return true;
179  }
180  return false;
181  }
182  /**
183  * verify if accept multiple value
184  *
185  * @return boolean
186  */
187  function isMultiple()
188  {
189  return ($this->inArray() || ($this->getOption('multiple') == 'yes'));
190  }
191  /**
192  * Get tab ancestor
193  *
194  * @return FieldSetAttribute
195  */
196  function getTab()
197  {
198  if ($this->type == 'tab') return $this;
199  if ($this->fieldSet && ($this->fieldSet->id != 'FIELD_HIDDENS')) return $this->fieldSet->getTab();
200  return false;
201  }
202  /**
203  * Export values as xml fragment
204  *
205  * @return string
206  */
207  function getXmlSchema()
208  {
209  return sprintf("<!-- no Schema %s (%s)-->", $this->id, $this->type);
210  }
211  /**
212  * Export values as xml fragment
213  *
214  * @return string
215  */
216  function getXmlValue()
217  {
218  return sprintf("<!-- no value %s (%s)-->", $this->id, $this->type);
219  }
220  /**
221  * Get human readable textual value
222  * Fallback method
223  *
224  * @return NULL
225  */
226  function getTextualValue()
227  {
228  return null;
229  }
230  /**
231  * Generate XML schema layout
232  *
233  * @param unknown_type $play
234  */
235  function common_getXmlSchema(&$play)
236  {
237 
238  $lay = new Layout(getLayoutFile("FDL", "infoattribute_schema.xml"));
239  $lay->set("aname", $this->id);
240  $lay->set("label", $this->encodeXml($this->labelText));
241  $lay->set("type", $this->type);
242  $lay->set("visibility", $this->visibility);
243  $lay->set("isTitle", $this->isInTitle);
244  $lay->set("phpfile", $this->phpfile);
245  $lay->set("phpfunc", $this->phpfunc);
246 
247  if (($this->ype == "enum") && (!$this->phpfile) || ($this->phpfile == "-")) {
248  $lay->set("phpfile", false);
249  $lay->set("phpfunc", false);
250  }
251  $lay->set("computed", ((!$this->phpfile) && (substr($this->phpfunc, 0, 2) == "::")));
252  $lay->set("link", $this->encodeXml($this->link));
253  $lay->set("elink", $this->encodeXml($this->elink));
254  $lay->set("default", false); // TODO : need detect default value
255  $lay->set("constraint", $this->phpconstraint);
256  $tops = $this->getOptions();
257  $t = array();
258  foreach ($tops as $k => $v) {
259  if ($k) $t[] = array(
260  "key" => $k,
261  "val" => $this->encodeXml($v)
262  );
263  }
264  $lay->setBlockData("options", $t);
265 
266  $play->set("minOccurs", $this->needed ? "1" : "0");
267  $play->set("isnillable", $this->needed ? "false" : "true");
268  $play->set("maxOccurs", (($this->getOption('multiple') == 'yes') ? "unbounded" : "1"));
269  $play->set("aname", $this->id);
270  $play->set("appinfos", $lay->gen());
271  }
272 }
273 /**
274  * NormalAttribute Class
275  * Non structural attribute (all attribute except frame and tab)
276  *
277  * @author anakeen
278  *
279  */
281 {
282  public $needed; // Y / N
283  public $format; // C format
284  public $eformat; // format for edition : list,vcheck,hcheck
285  public $repeat; // true if is a repeatable attribute
286  public $isInTitle;
288  public $fieldSet; // field set object
289  public $link; // hypertext link
290  public $phpfile;
291  public $phpfunc;
292  public $elink; // extra link
293  public $ordered;
294  public $phpconstraint; // special constraint set
295  public $usefor; // = Q if parameters
296 
297  /**
298  * Array of separator by level of multiplicity for textual export
299  * @var unknown_type
300  */
302  0 => "\n",
303  1 => ", "
304  );
305  /**
306  * Normal Attribute constructor : non structural attribute
307  *
308  * @param int $id id of the attribute
309  * @param int $docid id of the family
310  * @param string $label default translate key
311  * @param string $type kind of attribute
312  * @param string $format format option
313  * @param string $repeat is repeteable attr
314  * @param int $order display order
315  * @param string $link link option
316  * @param string $visibility visibility option
317  * @param char $needed is mandotary attribute
318  * @param char $isInTitle is used to compute title
319  * @param char $isInAbstract is used in abstract view
320  * @param string $fieldSet parent attribute
321  * @param string $phpfile php file used with the phpfunc
322  * @param string $phpfunc helpers function
323  * @param string $elink eling option
324  * @param string $phpconstraint class php function
325  * @param string $usefor Attribute or Parameter
326  * @param string $eformat eformat option
327  * @param string $options option string
328  * @param unknown_type $docname
329  */
331  {
332  $this->id = $id;
333  $this->docid = $docid;
334  $this->labelText = $label;
335  $this->type = $type;
336  $this->format = $format;
337  $this->eformat = $eformat;
338  $this->ordered = $order;
339  $this->link = $link;
340  $this->visibility = $visibility;
341  $this->needed = $needed;
342  $this->isInTitle = $isInTitle;
343  $this->isInAbstract = $isInAbstract;
344  $this->fieldSet = & $fieldSet;
345  $this->phpfile = $phpfile;
346  $this->phpfunc = $phpfunc;
347  $this->elink = $elink;
348  $this->phpconstraint = $phpconstraint;
349  $this->usefor = $usefor;
350  $this->repeat = $repeat;
351  $this->options = $options;
352  $this->docname = $docname;
353  }
354  /**
355  * Generate the xml schema fragment
356  *
357  * @param array $la array of DocAttribute
358  *
359  * @return string
360  */
361  function getXmlSchema($la)
362  {
363  switch ($this->type) {
364  case 'text':
365  return $this->text_getXmlSchema($la);
366  case 'longtext':
367  case 'htmltext':
368  return $this->longtext_getXmlSchema($la);
369  case 'int':
370  case 'integer':
371  return $this->int_getXmlSchema($la);
372  case 'float':
373  case 'money':
374  return $this->float_getXmlSchema($la);
375  case 'image':
376  case 'file':
377  return $this->file_getXmlSchema($la);
378  case 'enum':
379  return $this->enum_getXmlSchema($la);
380  case 'thesaurus':
381  case 'docid':
382  return $this->docid_getXmlSchema($la);
383  case 'date':
384  return $this->date_getXmlSchema($la);
385  case 'timestamp':
386  return $this->timestamp_getXmlSchema($la);
387  case 'time':
388  return $this->time_getXmlSchema($la);
389  case 'array':
390  return $this->array_getXmlSchema($la);
391  case 'color':
392  return $this->color_getXmlSchema($la);
393  default:
394  return sprintf("<!-- no Schema %s (type %s)-->", $this->id, $this->type);;
395  }
396  }
397  /**
398  * export values as xml fragment
399  *
400  * @param Doc $doc working doc
401  * @param StdObject $opt
402  *
403  * @return string
404  */
405  function getXmlValue(Doc & $doc, $opt = false)
406  {
407  if ($opt->index > - 1) $v = $doc->getTvalue($this->id, '', $opt->index);
408  else $v = $doc->getValue($this->id);
409  //if (! $v) return sprintf("<!-- no value %s -->",$this->id);
410  if ($this->getOption("autotitle") == "yes") return sprintf("<!--autotitle %s %s -->", $this->id, $v);
411  if ((!$v) && ($this->type != 'array')) {
412  if (($this->type == 'file') || ($this->type == 'image')) return sprintf('<%s mime="" title="" xsi:nil="true"/>', $this->id);
413  else return sprintf('<%s xsi:nil="true"/>', $this->id);
414  }
415  switch ($this->type) {
416  case 'timestamp':
417  case 'date':
418  $v = stringDateToIso($v);
419  return sprintf("<%s>%s</%s>", $this->id, $v, $this->id);
420  case 'array':
421  $la = $doc->getAttributes();
422  $xmlvalues = array();
423  $av = $doc->getAvalues($this->id);
424  $axml = array();
425  foreach ($av as $k => $col) {
426  $xmlvalues = array();
427  foreach ($col as $aid => $aval) {
428  $oa = $doc->getAttribute($aid);
429  if (empty($opt->exportAttributes[$doc->fromid]) || in_array($aid, $opt->exportAttributes[$doc->fromid])) {
430  $opt->index = $k;
431  $xmlvalues[] = $oa->getXmlValue($doc, $opt);
432  }
433  }
434  $axml[] = sprintf("<%s>%s</%s>", $this->id, implode("\n", $xmlvalues) , $this->id);
435  }
436  $opt->index = - 1; // restore initial index
437  return implode("\n", $axml);
438  case 'image':
439  case 'file':
440  if (preg_match(PREGEXPFILE, $v, $reg)) {
441  if ($opt->withIdentificator) $vid = $reg[2];
442  else $vid = '';
443  $mime = $reg[1];
444  $name = $reg[3];
445  $base = getParam("CORE_EXTERNURL");
446  $href = $base . str_replace('&', '&amp;', $doc->getFileLink($this->id));
447  if ($opt->withFile) {
448  $path = $doc->vault_filename_fromvalue($v, true);
449 
450  if (is_file($path)) {
451  if ($opt->outFile) {
452  return sprintf('<%s vid="%d" mime="%s" title="%s">[FILE64:%s]</%s>', $this->id, $vid, $mime, $name, $path, $this->id);
453  } else {
454  return sprintf('<%s vid="%d" mime="%s" title="%s">%s</%s>', $this->id, $vid, $mime, $name, base64_encode(file_get_contents($path)) , $this->id);
455  }
456  } else {
457  return sprintf('<!-- file not found --><%s vid="%d" mime="%s" title="%s"/>', $this->id, $vid, $mime, $name, $this->id);
458  }
459  } else {
460  return sprintf('<%s vid="%d" mime="%s" href="%s" title="%s"/>', $this->id, $vid, $mime, $href, $this->encodeXml($name));
461  }
462  } else {
463  return sprintf("<%s>%s</%s>", $this->id, $v, $this->id);
464  }
465  case 'docid':
466  $info = getTDoc($doc->dbaccess, $v, array() , array(
467  "title",
468  "name",
469  "id",
470  "initid",
471  "locked"
472  ));
473 
474  if ($info) {
475  $docid = $info["id"];
476  $latestTitle = ($this->getOption("docrev", "latest") == "latest");
477  if ($latestTitle) {
478  $docid = $info["initid"];
479  if ($info["locked"] == - 1) {
480  $info["title"] = $doc->getLastTitle($docid);
481  }
482  }
483  if ($info["name"]) {
484  if ($opt->withIdentificator) {
485  return sprintf('<%s id="%s" name="%s">%s</%s>', $this->id, $docid, $info["name"], $this->encodeXml($info["title"]) , $this->id);
486  } else {
487  return sprintf('<%s name="%s">%s</%s>', $this->id, $info["name"], $this->encodeXml($info["title"]) , $this->id);
488  }
489  } else {
490  if ($opt->withIdentificator) {
491  return sprintf('<%s id="%s">%s</%s>', $this->id, $docid, $this->encodeXml($info["title"]) , $this->id);
492  } else {
493 
494  return sprintf('<%s>%s</%s>', $this->id, $this->encodeXml($info["title"]) , $this->id);
495  }
496  }
497  } else {
498  if ((strpos($v, '<BR>') === false) && (strpos($v, "\n") === false)) {
499  return sprintf('<%s id="%s">%s</%s>', $this->id, $v, _("unreferenced document") , $this->id);
500  } else {
501  return sprintf('<%s id="%s">%s</%s>', $this->id, str_replace(array(
502  "\n",
503  '<BR>'
504  ) , ',', $v) , _("multiple document") , $this->id);
505  }
506  }
507  default:
508  return sprintf("<%s>%s</%s>", $this->id, $this->encodeXml($v) , $this->id);
509  }
510  }
511  /**
512  * custom textual XML schema
513  *
514  * @return string
515  */
516  function text_getXmlSchema()
517  {
518  $lay = new Layout(getLayoutFile("FDL", "textattribute_schema.xml"));
519  $this->common_getXmlSchema($lay);
520 
521  $lay->set("maxlength", false);
522  $lay->set("pattern", false);
523  return $lay->gen();
524  }
525  /**
526  * enum XML schema
527  *
528  * @return string
529  */
530  function enum_getXmlSchema()
531  {
532  $lay = new Layout(getLayoutFile("FDL", "enumattribute_schema.xml"));
533  $this->common_getXmlSchema($lay);
534 
535  $la = $this->getEnum();
536  $te = array();
537  foreach ($la as $k => $v) {
538  $te[] = array(
539  "key" => $k,
540  "val" => $this->encodeXml($v)
541  );
542  }
543  $lay->setBlockData("enums", $te);
544  return $lay->gen();
545  }
546  /**
547  * docid XML schema
548  *
549  * @return string
550  */
552  {
553  $lay = new Layout(getLayoutFile("FDL", "docidattribute_schema.xml"));
554  $this->common_getXmlSchema($lay);
555 
556  $lay->set("famid", $this->format);
557  return $lay->gen();
558  }
559  /**
560  * date XML schema
561  *
562  * @return string
563  */
564  function date_getXmlSchema()
565  {
566  $lay = new Layout(getLayoutFile("FDL", "dateattribute_schema.xml"));
567  $this->common_getXmlSchema($lay);
568  return $lay->gen();
569  }
570  /**
571  * timeStamp XML schema
572  *
573  * @return string
574  */
576  {
577  $lay = new Layout(getLayoutFile("FDL", "timestampattribute_schema.xml"));
578  $this->common_getXmlSchema($lay);
579  return $lay->gen();
580  }
581  /**
582  * Color XML schema
583  *
584  * @return string
585  */
587  {
588  $lay = new Layout(getLayoutFile("FDL", "colorattribute_schema.xml"));
589  $this->common_getXmlSchema($lay);
590  return $lay->gen();
591  }
592  /**
593  * int XML schema
594  *
595  * @return string
596  */
597  function int_getXmlSchema()
598  {
599  $lay = new Layout(getLayoutFile("FDL", "intattribute_schema.xml"));
600  $this->common_getXmlSchema($lay);
601  return $lay->gen();
602  }
603  /**
604  * longText XML schema
605  *
606  * @return string
607  */
609  {
610  $lay = new Layout(getLayoutFile("FDL", "longtextattribute_schema.xml"));
611  $this->common_getXmlSchema($lay);
612  return $lay->gen();
613  }
614  /**
615  * Float XML schema
616  *
617  * @return string
618  */
620  {
621  $lay = new Layout(getLayoutFile("FDL", "floatattribute_schema.xml"));
622  $this->common_getXmlSchema($lay);
623  return $lay->gen();
624  }
625  /**
626  * Time XML schema
627  *
628  * @return string
629  */
630  function time_getXmlSchema()
631  {
632  $lay = new Layout(getLayoutFile("FDL", "timeattribute_schema.xml"));
633  $this->common_getXmlSchema($lay);
634  return $lay->gen();
635  }
636  /**
637  * File XML schema
638  *
639  * @return string
640  */
641  function file_getXmlSchema()
642  {
643  $lay = new Layout(getLayoutFile("FDL", "fileattribute_schema.xml"));
644  $this->common_getXmlSchema($lay);
645  return $lay->gen();
646  }
647  /**
648  * Array XML schema
649  *
650  * @return string
651  */
653  {
654  $lay = new Layout(getLayoutFile("FDL", "arrayattribute_schema.xml"));
655  $this->common_getXmlSchema($lay);
656  $lay->set("minOccurs", "0");
657  $lay->set("maxOccurs", "unbounded");
658  $tax = array();
659  foreach ($la as $k => $v) {
660  if ($v->fieldSet && $v->fieldSet->id == $this->id) {
661  $tax[] = array(
662  "axs" => $v->getXmlSchema($la)
663  );
664  }
665  }
666  $lay->setBlockData("ATTR", $tax);
667  return $lay->gen();
668  }
669  /**
670  * Get the textual value of an attribute
671  *
672  * @param Doc $doc current Doc
673  * @param int $index index if multiple
674  * @param array $configuration value config array : dateFormat => 'US' 'ISO', decimalSeparator => '.',
675  * multipleSeparator => array(0 => 'arrayLine', 1 => 'multiple') (defaultValue : dateFormat : 'US', decimalSeparator : '.', multiple => array(0 => "\n", 1 => ", "))
676  *
677  * @return string
678  */
679  public function getTextualValue(Doc $doc, $index = - 1, Array $configuration = array())
680  {
681  $oldMultipleSep = $this->textualValueMultipleSeparator;
682  $this->textualValueMultipleSeparator = (isset($configuration['multipleSeparator']) && is_array($configuration['multipleSeparator'])) ? $configuration['multipleSeparator'] : $this->textualValueMultipleSeparator;
683  $return = "";
684  switch ($this->type) {
685  case 'text':
686  case 'longtext':
687  case 'time':
688  case 'htmltext':
689  $return = $this->getTextualValueText($doc, $index);
690  break;
691 
692  case 'image':
693  case 'file':
694  $return = $this->getTextualValueFile($doc, $index);
695  break;
696 
697  case 'enum':
698  $return = $this->getTextualValueEnum($doc, $index);
699  break;
700 
701  case 'thesaurus':
702  case 'docid':
703  $return = $this->getTextualValueDocId($doc, $index);
704  break;
705 
706  case 'timestamp':
707  case 'date':
708  $date = isset($configuration['dateFormat']) ? $configuration['dateFormat'] : 'US';
709  $return = $this->getTextualValueDate($doc, $index, $date);
710  break;
711 
712  case 'array':
713  break;
714 
715  case 'float':
716  case 'money':
717  $decimalSeparator = isset($configuration['decimalSeparator']) ? $configuration['decimalSeparator'] : '.';
718  $return = $this->getTextualFloat($doc, $index, $decimalSeparator);
719  break;
720 
721  case 'int':
722  case 'integer':
723  case 'color':
724  default:
725  $return = $this->getTextualValueRaw($doc, $index);
726  }
727  $this->textualValueMultipleSeparator = $oldMultipleSep;
728  return $return;
729  }
730  /**
731  * Get textual value for a float attribute
732  *
733  * @param Doc $doc class doc
734  * @param int $index current index of the element if multiple
735  * @param string $decimalSeparator current decimal separator
736  *
737  * @return string
738  */
739  private function getTextualFloat(Doc $doc, $index, $decimalSeparator)
740  {
741  $values = $this->getTextualValueRaw($doc, $index);
742  if ($decimalSeparator != ".") {
743  return str_replace(".", $decimalSeparator, $values);
744  } else {
745  return $values;
746  }
747  }
748  /**
749  * Get textual value for a text attribute
750  *
751  * @param Doc $doc class doc
752  * @param int $index current index of the element if multiple
753  *
754  * @return string
755  */
756  private function getTextualValueText(Doc $doc, $index = - 1)
757  {
758  if ($this->inArray()) {
759  if ($index >= 0) {
760  return strip_tags($doc->getTValue($this->id, "", $index));
761  } else {
762  $nbValue = count($doc->getTValue($this->id));
763  $returnValues = array();
764  for ($i = 0; $i < $nbValue; $i++) {
765  $returnValues[] = $this->getTextualValueText($doc, $i);
766  }
767  return implode($this->textualValueMultipleSeparator[0], $returnValues);
768  }
769  } else {
770  return strip_tags($doc->getValue($this->id));
771  }
772  }
773  /**
774  * Get textual value for a attribute
775  *
776  * @param Doc $doc class doc
777  * @param int $index current index of the element if multiple
778  *
779  * @return string
780  */
781  private function getTextualValueRaw(Doc $doc, $index = - 1)
782  {
783  if ($this->inArray()) {
784  if ($index >= 0) {
785  return $doc->getTValue($this->id, "", $index);
786  } else {
787  $returnValues = $doc->getTValue($this->id);
788  return implode($this->textualValueMultipleSeparator[0], $returnValues);
789  }
790  } else {
791  return $doc->getValue($this->id);
792  }
793  }
794  /**
795  * Get textual value for a date attribute
796  *
797  * @param Doc $doc class doc
798  * @param int $index current index of the element if multiple
799  * @param string $dateFormat US/ISO/FR
800  *
801  * @return string
802  */
803  private function getTextualValueDate(Doc $doc, $index = - 1, $dateFormat)
804  {
805  $convertDate = function ($date) use ($dateFormat)
806  {
807  if (strtoupper($dateFormat) == "US") {
808  // it is not US is ISO without T
809  $date = stringDateToIso($date, false);
810  } elseif (strtoupper($dateFormat) == "ISO") {
811  $date = stringDateToIso($date);
812  } elseif (getLcdate() == "iso") { // FR
813  $ldate = stringDateToLocaleDate($date, '%d/%m/%Y %H:%M');
814  if (strlen($date) < 11) $date = substr($ldate, 0, strlen($date));
815  else $date = $ldate;
816  }
817  return $date;
818  };
819  if ($this->inArray()) {
820  if ($index >= 0) {
821  return $convertDate($doc->getTValue($this->id, "", $index));
822  } else {
823  $nbValue = count($doc->getTValue($this->id));
824  $returnValues = array();
825  for ($i = 0; $i < $nbValue; $i++) {
826  $returnValues[] = $this->getTextualValueDate($doc, $i, $dateFormat);
827  }
828  return implode($this->textualValueMultipleSeparator[0], $returnValues);
829  }
830  } else {
831  return $convertDate($doc->getValue($this->id));
832  }
833  }
834  /**
835  * Get textual value for a file attribute
836  *
837  * @param Doc $doc class doc
838  * @param int $index current index of the element if multiple
839  *
840  * @return string
841  */
842  private function getTextualValueFile(Doc $doc, $index = - 1)
843  {
844  if ($this->inArray()) {
845  if ($index >= 0) {
846  return $doc->vault_filename($this->id, false, $index);
847  } else {
848  $nbValue = count($doc->getTValue($this->id));
849  $returnValues = array();
850  for ($i = 0; $i < $nbValue; $i++) {
851  $returnValues[] = $this->getTextualValueFile($doc, $i);
852  }
853  return implode($this->textualValueMultipleSeparator[0], $returnValues);
854  }
855  } else {
856  return $doc->vault_filename($this->id);
857  }
858  }
859  /**
860  * Get textual value for an enum attribute
861  *
862  * @param Doc $doc class doc
863  * @param int $index current index of the element if multiple
864  *
865  * @return string
866  */
867  private function getTextualValueEnum(Doc $doc, $index = - 1)
868  {
869  if ($this->inArray()) {
870  if ($index >= 0) {
871  return $this->getEnumLabel($doc->getTValue($this->id, "", $index));
872  } else {
873  $nbValue = count($doc->getTValue($this->id));
874  $returnValues = array();
875  for ($i = 0; $i < $nbValue; $i++) {
876  $returnValues[] = $this->getTextualValueEnum($doc, $i);
877  }
878  return implode($this->textualValueMultipleSeparator[0], $returnValues);
879  }
880  } else {
881  if ($this->getOption('multiple') == 'yes') {
882  $value = $doc->getValue($this->id);
883  $values = $doc->_val2array($value);
884  $returnValues = array();
885  foreach ($values as $currentKey) {
886  $returnValues[] = $this->getEnumLabel($currentKey);
887  }
888  return implode($this->textualValueMultipleSeparator[1], $returnValues);
889  }
890  return $this->getEnumLabel($doc->getValue($this->id));
891  }
892  }
893  /**
894  * Get textual value for a docid attribute
895  *
896  * @param Doc $doc class doc
897  * @param int $index current index of the element if multiple
898  *
899  * @return string
900  */
901  private function getTextualValueDocId(Doc $doc, $index = - 1)
902  {
903  $optionDoc = $this->getOption('docrev', "");
904  $displayTitle = function ($id) use ($optionDoc, $doc)
905  {
906  if ($optionDoc == "fixed") {
907  return $doc->getTitle($id);
908  } else {
909  return $doc->getTitle($id, "", true);
910  }
911  };
912  if ($this->inArray()) {
913  if ($index >= 0) {
914  if ($this->getOption('multiple') == 'yes') {
915  $values = explode("<BR>", $doc->getTValue($this->id, "", $index));
916  if (is_array($values)) {
917  $returnValues = array();
918  foreach ($values as $currentId) {
919  $returnValues[] = $displayTitle($currentId);
920  }
921  return implode($this->textualValueMultipleSeparator[1], $returnValues);
922  }
923  return $displayTitle($values);
924  } else {
925  return $displayTitle($doc->getTValue($this->id, "", $index));
926  }
927  } else {
928  $nbValue = count($doc->getTValue($this->id));
929  $returnValues = array();
930  for ($i = 0; $i < $nbValue; $i++) {
931  $returnValues[] = $this->getTextualValueDocId($doc, $i);
932  }
933  return implode($this->textualValueMultipleSeparator[0], $returnValues);
934  }
935  } else {
936  if ($this->getOption('multiple') == 'yes') {
937  $value = $doc->getValue($this->id);
938  $values = $doc->_val2array($value);
939  if ($index >= 0) {
940  return $displayTitle($values[$index]);
941  } else {
942  $returnValues = array();
943  foreach ($values as $currentId) {
944  $returnValues[] = $displayTitle($currentId);
945  }
946  return implode($this->textualValueMultipleSeparator[1], $returnValues);
947  }
948  }
949  return $displayTitle($doc->getValue($this->id));
950  }
951  }
952  /**
953  * Return array of enumeration definition
954  * the array's keys are the enum key and the values are the labels
955  *
956  * @return array
957  */
958  function getEnum()
959  {
960  global $__tenum; // for speed optimization
961  global $__tlenum;
962 
963  if (isset($__tenum[$this->id])) return $__tenum[$this->id]; // not twice
964  if (($this->type == "enum") || ($this->type == "enumlist")) {
965  // set the enum array
966  $this->enum = array();
967  $this->enumlabel = array();
968  $br = $this->docname . '#' . $this->id . '#'; // id i18n prefix
969  if (($this->phpfile != "") && ($this->phpfile != "-")) {
970  // for dynamic specification of kind attributes
971  if (!include_once ("EXTERNALS/$this->phpfile")) {
972  global $action;
973  $action->exitError(sprintf(_("the external pluggin file %s cannot be read") , $this->phpfile));
974  }
975  if (preg_match("/(.*)\((.*)\)/", $this->phpfunc, $reg)) {
976  $args = explode(",", $reg[2]);
977  if (preg_match("/linkenum\((.*),(.*)\)/", $this->phpfunc, $dreg)) {
978  $br = $dreg[1] . '#' . strtolower($dreg[2]) . '#';
979  }
980  if (function_exists($reg[1])) {
981  $this->phpfunc = call_user_func_array($reg[1], $args);
982  } else {
983  AddWarningMsg(sprintf(_("function [%s] not exists") , $this->phpfunc));
984  $this->phpfunc = "";
985  }
986  } else {
987  AddWarningMsg(sprintf(_("invalid syntax for [%s] for enum attribute") , $this->phpfunc));
988  }
989  }
990 
991  $sphpfunc = str_replace("\\.", "-dot-", $this->phpfunc); // to replace dot & comma separators
992  $sphpfunc = str_replace("\\,", "-comma-", $sphpfunc);
993  if ($sphpfunc != "") {
994  $tenum = explode(",", $sphpfunc);
995  foreach ($tenum as $k => $v) {
996  list($enumKey, $enumValue) = explode("|", $v);
997  $treeKeys = explode(".", $enumKey);
998  $enumKey = trim($enumKey);
999  if (strlen($enumKey) == 0) $enumKey = " ";
1000  $enumValue = trim($enumValue);
1001  $translatedEnumValue = _($br . $enumKey);
1002  if ($translatedEnumValue != $br . $enumKey) {
1003  $enumValue = $translatedEnumValue;
1004  }
1005 
1006  $n = count($treeKeys);
1007  if ($n <= 1) {
1008  $enumValue = str_replace(array(
1009  '-dot-',
1010  '-comma-'
1011  ) , array(
1012  '\.',
1013  ','
1014  ) , $enumValue);
1015  $this->enum[str_replace(array(
1016  '-dot-',
1017  '-comma-'
1018  ) , array(
1019  '\.',
1020  ','
1021  ) , $enumKey) ] = $enumValue;
1022  $this->enumlabel[str_replace(array(
1023  '-dot-',
1024  '-comma-'
1025  ) , array(
1026  '.',
1027  ','
1028  ) , $enumKey) ] = $enumValue;
1029  } else {
1030  $enumlabelKey = '';
1031  $tmpKey = '';
1032  $previousKey = '';
1033  foreach ($treeKeys as $i => $treeKey) {
1034  $enumlabelKey = $treeKey;
1035 
1036  if ($i < $n - 1) {
1037  if ($i > 0) {
1038  $tmpKey.= '.';
1039  }
1040  $tmpKey.= $treeKey;
1041  }
1042  }
1043  $tmpKey = str_replace(array(
1044  '-dot-',
1045  '-comma-'
1046  ) , array(
1047  '\.',
1048  ','
1049  ) , $tmpKey);
1050  $enumlabelValue = $this->enum[$tmpKey] . '/' . $enumValue;
1051  $enumlabelValue = str_replace(array(
1052  '-dot-',
1053  '-comma-'
1054  ) , array(
1055  '\.',
1056  ','
1057  ) , $enumlabelValue);
1058  $this->enum[str_replace(array(
1059  '-dot-',
1060  '-comma-'
1061  ) , array(
1062  '\.',
1063  ','
1064  ) , $enumKey) ] = $enumValue;
1065  $this->enumlabel[str_replace(array(
1066  '-dot-',
1067  '-comma-'
1068  ) , array(
1069  '.',
1070  ','
1071  ) , $enumlabelKey) ] = $enumlabelValue;
1072  }
1073  }
1074  }
1075  }
1076  $__tenum[$this->id] = $this->enum;
1077  $__tlenum[$this->id] = $this->enumlabel;
1078  return $this->enum;
1079  }
1080  /**
1081  * return array of enumeration definition
1082  * the array'skeys are the enum single key and the values are the complete labels
1083  *
1084  * @param string $enumid the key of enumerate (if no parameter all labels are returned
1085  *
1086  * @return array
1087  */
1088  function getEnumLabel($enumid = null)
1089  {
1090  global $__tlenum;
1091 
1092  $this->getEnum();
1093 
1094  $implode = false;
1095  if (isset($__tlenum[$this->id])) { // is set
1096  if ($enumid === null) return $__tlenum[$this->id];
1097  if (strstr($enumid, "\n")) {
1098  $enumid = explode("\n", $enumid);
1099  $implode = true;
1100  }
1101  if (is_array($enumid)) {
1102  $tv = array();
1103  foreach ($enumid as $v) {
1104  if (isset($__tlenum[$this->id][$v])) $tv[] = $__tlenum[$this->id][$v];
1105  else $tv[] = $enumid;
1106  }
1107  if ($implode) return implode("\n", $tv);
1108  return $tv;
1109  } else {
1110  if (isset($__tlenum[$this->id][$enumid])) return $__tlenum[$this->id][$enumid];
1111  else return $enumid;
1112  }
1113  }
1114  }
1115  /**
1116  * add new item in enum list items
1117  *
1118  * @param string $dbaccess dbaccess string
1119  * @param string $key database key
1120  * @param string $label human label
1121  *
1122  * @return string error message (empty means ok)
1123  */
1124  function addEnum($dbaccess, $key, $label)
1125  {
1126  if ($key == "") return "";
1127 
1128  $a = new DocAttr($dbaccess, array(
1129  $this->docid,
1130  $this->id
1131  ));
1132  if ($a->isAffected()) {
1133  $tenum = $this->getEnum();
1134 
1135  $key = str_replace(array(
1136  '|'
1137  ) , array(
1138  '_'
1139  ) , $key);
1140  $label = str_replace(array(
1141  '|'
1142  ) , array(
1143  '_'
1144  ) , $label);
1145  if (!array_key_exists($key, $tenum)) {
1146  $tenum[$key] = $label;
1147  global $__tenum; // modify cache
1148  global $__tlenum;
1149  $__tenum[$this->id][$key] = $label;
1150  $__tlenum[$this->id][$key] = $label;
1151  // convert array to string
1152  $tsenum = array();
1153  foreach ($tenum as $k => $v) {
1154  $v = str_replace(array(
1155  ',',
1156  '|'
1157  ) , array(
1158  '\,',
1159  '_'
1160  ) , $v);
1161  $k = str_replace(array(
1162  ',',
1163  '|'
1164  ) , array(
1165  '\,',
1166  '_'
1167  ) , $k);
1168  $tsenum[] = "$k|$v";
1169  }
1170  $senum = implode($tsenum, ',');
1171  $a->phpfunc = $senum;
1172  $err = $a->modify();
1173  if ($err == "") {
1174  include_once ("FDL/Lib.Attr.php");
1175  refreshPhpPgDoc($dbaccess, $this->docid);
1176  }
1177  }
1178  } else {
1179  $err = sprintf(_("unknow attribute %s (family %s)") , $this->id, $this->docid);
1180  }
1181 
1182  return $err;
1183  }
1184  /**
1185  * Test if an enum key existe
1186  *
1187  * @param string $key enumKey
1188  *
1189  * @return boolean
1190  */
1191  function existEnum($key)
1192  {
1193  if ($key == "") return false;
1194  $this->getEnum();
1195  if (isset($this->enum[$key])) return true;
1196  return false;
1197  }
1198  }
1199  /**
1200  * Structural attribute (attribute that contain other attribute : tab, frame)
1201  *
1202  * @author anakeen
1203  *
1204  */
1206  {
1207 
1208  public $fieldSet; // field set object
1209 
1210  /**
1211  * Constructor
1212  *
1213  * @param string $id logical name
1214  * @param $id $docid famid
1215  * @param string $label default translation key
1216  * @param string $visibility visibility option
1217  * @param string $usefor Attr or Param usage
1218  * @param string $type kind of
1219  * @param string $fieldSet parent attribute
1220  * @param string $options option string
1221  * @param string $docname
1222  */
1223  function __construct($id, $docid, $label, $visibility = "", $usefor = "", $type = "frame", &$fieldSet = null, $options = "", $docname = "")
1224  {
1225  $this->id = $id;
1226  $this->docid = $docid;
1227  $this->labelText = $label;
1228  $this->visibility = $visibility;
1229  $this->usefor = $usefor;
1230  $this->type = $type;
1231  $this->fieldSet = & $fieldSet;
1232  $this->options = $options;
1233  $this->docname = $docname;
1234  }
1235  /**
1236  * Generate the xml schema fragment
1237  *
1238  * @param array $la array of DocAttribute
1239  *
1240  * @return string
1241  */
1242  function getXmlSchema(&$la)
1243  {
1244  $lay = new Layout(getLayoutFile("FDL", "fieldattribute_schema.xml"));
1245  $lay->set("aname", $this->id);
1246  $this->common_getXmlSchema($lay);
1247 
1248  $lay->set("minOccurs", "0");
1249  $lay->set("maxOccurs", "1");
1250  $lay->set("notop", ($this->fieldSet->id != '' && $this->fieldSet->id != 'FIELD_HIDDENS'));
1251  $tax = array();
1252  foreach ($la as $k => $v) {
1253  if ($v->fieldSet && $v->fieldSet->id == $this->id) {
1254  $tax[] = array(
1255  "axs" => $v->getXmlSchema($la)
1256  );
1257  }
1258  }
1259 
1260  $lay->setBlockData("ATTR", $tax);
1261  return $lay->gen();
1262  }
1263  /**
1264  * export values as xml fragment
1265  *
1266  * @param Doc $doc working doc
1267  * @param StdObject $opt
1268  *
1269  * @return string
1270  */
1271  function getXmlValue(Doc & $doc, $opt)
1272  {
1273  $la = $doc->getAttributes();
1274  $xmlvalues = array();
1275  foreach ($la as $k => $v) {
1276  if ($v->fieldSet && $v->fieldSet->id == $this->id && (empty($opt->exportAttributes[$doc->fromid]) || in_array($v->id, $opt->exportAttributes[$doc->fromid]))) {
1277  $xmlvalues[] = $v->getXmlValue($doc, $opt);
1278  }
1279  }
1280  if ($opt->flat) return implode("\n", $xmlvalues);
1281  else return sprintf("<%s>%s</%s>", $this->id, implode("\n", $xmlvalues) , $this->id);
1282  }
1283  }
1284 
1286  {
1287  public $link; // hypertext link
1288  public $ordered;
1289  public $precond; // pre-condition to activate menu
1290  function __construct($id, $docid, $label, $order, $link, $visibility = "", $precond = "", $options = "", $docname = "")
1291  {
1292  $this->id = $id;
1293  $this->docid = $docid;
1294  $this->labelText = $label;
1295  $this->ordered = $order;
1296  $this->link = $link;
1297  $this->visibility = $visibility;
1298  $this->options = $options;
1299  $this->precond = $precond;
1300  $this->type = "menu";
1301  $this->docname = $docname;
1302  }
1303  }
1304 
1306  {
1307 
1308  public $wapplication; // the what application name
1309  public $waction; // the what action name
1310  public $ordered;
1311  public $precond; // pre-condition to activate action
1312  function __construct($id, $docid, $label, $order, $visibility = "", $wapplication = "", $waction = "", $precond = "", $options = "", $docname = "")
1313  {
1314  $this->id = $id;
1315  $this->docid = $docid;
1316  $this->labelText = $label;
1317  $this->visibility = $visibility;
1318  $this->ordered = $order;
1319  $this->waction = $waction;
1320  $this->wapplication = $wapplication;
1321  $this->options = $options;
1322  $this->precond = $precond;
1323  $this->type = "action";
1324  $this->docname = $docname;
1325  }
1326  function getLink($docid)
1327  {
1328  $l = getParam("CORE_STANDURL");
1329  $batch = ($this->getOption("batchfolder") == "yes");
1330  if ($batch) {
1331  $l.= "&app=FREEDOM&action=BATCHEXEC&sapp=" . $this->wapplication;
1332  $l.= "&saction=" . $this->waction;
1333  $l.= "&id=" . $docid;
1334  } else {
1335  $l.= "&app=" . $this->wapplication;
1336  $l.= "&action=" . $this->waction;
1337  if (!stristr($this->waction, "&id=")) $l.= "&id=" . $docid;
1338  }
1339  return $l;
1340  }
1341  }
1342 ?>
← centre documentaire © anakeen - published under CC License - Dynacase