Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocOooFormat.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Get OpenText Value for document
8  * @class DocOooFormat
9  *
10  */
12 {
13  /**
14  * @var Doc
15  */
16  public $doc = null;
17  private $index = - 1;
18  /**
19  * @var NormalAttribute
20  */
21  private $oattr = null;
22  private $attrid = '';
23  /**
24  * format set in type
25  * @var string
26  */
27  private $cFormat = '';
28 
29  public function __construct(Doc & $doc)
30  {
31  $this->setDoc($doc);
32  }
33 
34  public function setDoc(Doc & $doc)
35  {
36  $this->doc = $doc;
37  }
38  /**
39  * @param NormalAttribute $oattr
40  * @param string $value
41  * @param int $index
42  * @return string the formated value
43  */
44  public function getOooValue($oattr, $value, $index = - 1)
45  {
46 
47  $this->oattr = $oattr;
48  $this->index = $index;
49  $this->cFormat = $this->oattr->format;
50  $atype = $this->oattr->type;
51 
52  if (($this->oattr->repeat) && ($this->index <= 0)) {
53  $tvalues = explode("\n", $value);
54  } else {
55  $tvalues[$this->index] = $value;
56  }
57  $this->attrid = $this->oattr->id;
58 
59  $thtmlval = array();
60  foreach ($tvalues as $kvalue => $avalue) {
61  $oooval = "";
62  switch ($atype) {
63  case "idoc":
64  // nothing
65  break;
66 
67  case "image":
68 
69  $oooval = $this->formatImage($avalue);
70  break;
71 
72  case "file":
73  // file name
74  $oooval = $this->formatFile($avalue);
75  break;
76 
77  case "longtext":
78  case "xml":
79 
80  $oooval = $this->formatLongtext($avalue);
81  break;
82 
83  case "password":
84 
85  break;
86 
87  case "enum":
88 
89  $oooval = $this->formatEnum($avalue);
90  break;
91 
92  case "thesaurus":
93 
94  $oooval = $this->formatThesaurus($avalue);
95  break;
96 
97  case "array":
98  break;
99 
100  case "doc":
101  break;
102 
103  case "account":
104  $oooval = $this->formatAccount($avalue);
105  break;
106 
107  case "docid":
108  $oooval = $this->formatDocid($avalue);
109  break;
110 
111  case "option":
112  break;
113 
114  case "money":
115  //$oooval=str_replace(" ","&nbsp;",$oooval); // need to replace space by non breaking spaces
116  $oooval = $this->formatMoney($avalue);
117  break;
118 
119  case "htmltext":
120 
121  $oooval = $this->formatHtmltext($avalue);
122  break;
123 
124  case 'date':
125 
126  $oooval = $this->formatDate($avalue);
127  break;
128 
129  case 'time':
130 
131  $oooval = $this->formatTime($avalue);
132  break;
133 
134  case 'timestamp':
135 
136  $oooval = $this->formatTimestamp($avalue);
137  break;
138 
139  case 'ifile':
140 
141  $oooval = $this->formatIfile($avalue);
142  break;
143 
144  case 'color':
145 
146  $oooval = $this->formatColor($avalue);
147  break;
148 
149  default:
150 
151  $oooval = $this->formatDefault($avalue);
152  break;
153  }
154 
155  if (($this->cFormat != "") && ($atype != "doc") && ($atype != "array") && ($atype != "option")) {
156  //printf($oooval);
157  $oooval = sprintf($this->xmlEncode($this->cFormat) , $oooval);
158  }
159 
160  $thtmlval[$kvalue] = $oooval;
161  }
162  return implode("<text:tab/>", $thtmlval);
163  }
164  /**
165  * format Default attribute
166  *
167  * @param string $avalue raw value of attribute
168  * @return string openText value
169  */
170  public function formatDefault($avalue)
171  {
172  $oooval = $this->xmlEncode($avalue);
173  //$oooval=str_replace('"','&quot;',$oooval);
174  return $oooval;
175  }
176 
177  public static function xmlEncode($s)
178  {
179  return str_replace(array(
180  '&',
181  '"',
182  "<",
183  ">"
184  ) , array(
185  "&amp;",
186  '&quot;',
187  "&lt;",
188  "&gt;"
189  ) , $s);
190  }
191  /**
192  * format Idoc attribute
193  *
194  * @param string $avalue raw value of attribute
195  * @return string openText value
196  */
197  public function formatIDoc(
198  /** @noinspection PhpUnusedParameterInspection */
199 
200  $avalue)
201  {
202 
203  $oooval = "";
204 
205  return $oooval;
206  }
207  /**
208  * format Image attribute
209  *
210  * @param string $avalue raw value of attribute
211  * @return string openText value
212  */
213  public function formatImage($avalue)
214  {
215 
216  $oooval = $this->doc->vault_filename_fromvalue($avalue, true);
217 
218  return $oooval;
219  }
220  /**
221  * format File attribute
222  *
223  * @param string $avalue raw value of attribute
224  * @return string openText value
225  */
226  public function formatFile($avalue)
227  {
228 
229  $oooval = $this->doc->vault_filename_fromvalue($avalue, false);
230  return $oooval;
231  }
232  /**
233  * format Longtext attribute
234  *
235  * @param string $avalue raw value of attribute
236  * @return string openText value
237  */
238  public function formatLongtext($avalue)
239  {
240 
241  $oooval = str_replace("&", "&amp;", $avalue);
242  $oooval = str_replace(array(
243  "<",
244  ">"
245  ) , array(
246  "&lt;",
247  "&gt;"
248  ) , $oooval);
249  $oooval = str_replace("\n", "<text:line-break/>", $oooval);
250  $oooval = str_replace("&lt;BR&gt;", "<text:line-break/>", $oooval);
251  $oooval = str_replace("\r", "", $oooval);
252  return $oooval;
253  }
254  /**
255  * format Image attribute
256  *
257  * @param string $avalue raw value of attribute
258  * @return string
259  */
260  public function formatPassword(
261  /** @noinspection PhpUnusedParameterInspection */
262  $avalue)
263  {
264 
265  $oooval = "*****";
266  return $oooval;
267  }
268  /**
269  * format Image attribute
270  *
271  * @param string $avalue raw value of attribute
272  * @return string openText value
273  */
274  public function formatEnum($avalue)
275  {
276 
277  $enumlabel = $this->oattr->getEnumlabel();
278  $colors = $this->oattr->getOption("boolcolor");
279  if ($colors != "") {
280  if (isset($enumlabel[$avalue])) {
281  reset($enumlabel);
282  $tcolor = explode(",", $colors);
283  if (current($enumlabel) == $enumlabel[$avalue]) {
284  $color = $tcolor[0];
285  $oooval = sprintf('<pre style="background-color:%s;display:inline">&nbsp;-&nbsp;</pre>', $color);
286  } else {
287  $color = $tcolor[1];
288  $oooval = sprintf('<pre style="background-color:%s;display:inline">&nbsp;&bull;&nbsp;</pre>', $color);
289  }
290  } else $oooval = $avalue;
291  } else {
292  if (isset($enumlabel[$avalue])) $oooval = $enumlabel[$avalue];
293  else $oooval = $avalue;
294  }
295  return $oooval;
296  }
297  /**
298  * format Image attribute
299  *
300  * @param string $avalue raw value of attribute
301  * @return string openText value
302  */
303  public function formatArray(
304  /** @noinspection PhpUnusedParameterInspection */
305  $avalue)
306  {
307 
308  $oooval = "";
309  return $oooval;
310  }
311  /**
312  * format Doc attribute
313  *
314  * @param string $avalue raw value of attribute
315  * @return string openText value
316  */
317  public function formatDoc(
318  /** @noinspection PhpUnusedParameterInspection */
319  $avalue)
320  {
321  $oooval = "";
322 
323  return $oooval;
324  }
325  /**
326  * format Account attribute
327  * @param $avalue
328  * @return string HTML value
329  */
330  public function formatAccount($avalue)
331  {
332  if (!$this->oattr->format) $this->oattr->format = "x";
333  return $this->formatDocid($avalue);
334  }
335  /**
336  * format Docid attribute
337  *
338  * @param string $avalue raw value of attribute
339  * @return string openText value
340  */
341  public function formatDocid($avalue)
342  {
343 
344  if ($this->oattr->format != "") {
345 
346  $this->cFormat = "";
347  $isLatest = $this->oattr->getOption("docrev", "latest") == "latest";
348 
349  $multiple = ($this->oattr->getOption("multiple") == "yes");
350 
351  if ($multiple) {
352  $avalue = str_replace("\n", "<BR>", $avalue);
353  $tval = explode("<BR>", $avalue);
354  $thval = array();
355  foreach ($tval as $kv => $vv) {
356  if (trim($vv) == "") $thval[] = $vv;
357  else $thval[] = $this->doc->getDocAnchor(trim($vv) , '', false);
358  }
359  $oooval = implode("<text:tab/>", $thval);
360  } else {
361  if ($avalue == "") $oooval = $avalue;
362  else {
363  $title = DocTitle::getRelationTitle(trim($avalue) , $isLatest, $this->doc);
364  if ($title === false) $title = $this->doc->htmlEncode($this->oattr->getOption("noaccesstext", _("information access deny")));
365  $oooval = $this->doc->htmlEncode($title);
366  }
367  }
368  } else $oooval = $avalue;
369  return $oooval;
370  }
371  /**
372  * format Thesaurus attribute
373  *
374  * @param string $avalue raw value of attribute
375  * @return string openText value
376  */
377  public function formatThesaurus($avalue)
378  {
379 
380  $this->cFormat = "";
381  $multiple = ($this->oattr->getOption("multiple") == "yes");
382  if ($multiple) {
383  $avalue = str_replace("\n", "<BR>", $avalue);
384  $tval = explode("<BR>", $avalue);
385  $thval = array();
386  foreach ($tval as $kv => $vv) {
387  if (trim($vv) == "") $thval[] = $vv;
388  else {
389  $thc = new_doc($this->doc->dbaccess, trim($vv));
390  if ($thc->isAlive()) $thval[] = $thc->getCustomTitle();
391  else $thval[] = "th error $vv";
392  }
393  }
394  $oooval = implode("<text:tab/>", $thval);
395  } else {
396  if ($avalue == "") $oooval = $avalue;
397  else {
398  $thc = new_doc($this->doc->dbaccess, $avalue);
399  if ($thc->isAlive()) $oooval = $thc->getCustomTitle();
400  else $oooval = "th error $avalue";
401  }
402  }
403  return $oooval;
404  }
405  /**
406  * format Option attribute
407  *
408  * @param string $avalue raw value of attribute
409  * @return string openText value
410  */
411  public function formatOption(
412  /** @noinspection PhpUnusedParameterInspection */
413  $avalue)
414  {
415 
416  $oooval = "";
417  return $oooval;
418  }
419  /**
420  * format Money attribute
421  *
422  * @param string $avalue raw value of attribute
423  * @return string openText value
424  */
425  public function formatMoney($avalue)
426  {
427 
428  $oooval = money_format('%!.2n', doubleval($avalue));
429  return $oooval;
430  }
431  /**
432  * format Htmltext attribute
433  *
434  * @param string $avalue raw value of attribute
435  * @return string openText value
436  */
437  public function formatHtmltext($avalue)
438  {
439  $oooval = '';
440  $attrid = $this->oattr->id;
441  $html_body = trim($avalue);
442  if (!$html_body) return '';
443  $html_body = \Dcp\Utils\htmlclean::normalizeHTMLFragment($html_body, $error);
444  if ($error != '') {
445  addWarningMsg(sprintf(_("Malformed HTML in attribute '%s' from document '%s': %s") , $this->oattr->id, $this->doc->title, $error));
446  }
447  if ($html_body === false) {
448  return '';
449  }
450  $xhtml_body = \Dcp\Utils\htmlclean::convertHTMLFragmentToXHTMLDocument($html_body, $error);
451  if ($error != '') {
452  addWarningMsg(sprintf(_("Error converting HTML from attribute '%s' from document '%s': %s") , $this->oattr->id, $this->doc->title, $error));
453  }
454  if ($xhtml_body === false) {
455  return '';
456  }
457 
458  $xhtml_body = preg_replace("/<!--.*?-->/ums", "", $xhtml_body); //delete comments
459  $xhtml_body = preg_replace_callback('/(<\/?)([^\s>]+)([^>]*)(>)/u', function ($matches)
460  {
461  return $this->_fixupStyle($matches[1], $matches[2], $matches[3], $matches[4]);
462  }
463  , $xhtml_body);
464  $xhtml_body = $this->cleanhtml($xhtml_body);
465 
466  $domHtml = new \Dcp\Utils\XDOMDocument();
467  $domHtml->load(DEFAULT_PUBDIR . "/CORE/Layout/html2odt.xsl");
468  $xslt = new xsltProcessor;
469  $xslt->importStyleSheet($domHtml);
470  $dom = null;
471  // set_error_handler('HandleXmlError');
472  try {
473  $dom = new \Dcp\Utils\XDOMDocument();
474  $dom->loadXML($xhtml_body);
475  }
476  catch(Exception $e) {
477  addWarningMsg(sprintf(_("possible incorrect conversion HTML to ODT %s: %s") , $this->doc->title, $e->getMessage()));
478  /*
479  print "Exception catched:\n";
480  print "Code: ".$e->getCode()."\n";
481  print "Message: ".$e->getMessage()."\n";
482  print "Line: ".$e->getLine();
483  // error in XML
484  print "\n<br>ERRORXSLT:".$this->doc->id.$this->doc->title."\n";
485  print "\n=========RAWDATA=================\n";
486  print $avalue;
487  print "\n=========XMLDATA=================\n";
488  print_r2($xmldata);
489  exit;*/
490 
491  $dom = null;
492  }
493  //restore_error_handler();
494  if ($dom) {
495  $xmlout = $xslt->transformToXML($dom);
496 
497  $dxml = new \Dcp\Utils\XDOMDocument();
498  $dxml->loadXML($xmlout);
499 
500  $ot = $dxml->getElementsByTagNameNS("urn:oasis:names:tc:opendocument:xmlns:office:1.0", "text");
501  $ot1 = $ot->item(0);
502  $officetext = $ot1->ownerDocument->saveXML($ot1);
503  $oooval = str_replace(array(
504  '<office:text>',
505  '</office:text>',
506  '<office:text/>'
507  ) , "", $officetext);
508  // work around : tables are not in paragraph
509  $oooval = preg_replace('!(<text:p>\s*<table:table )!u', "<table:table ", $oooval);
510  $oooval = preg_replace('!(</table:table>\s*</text:p>)!u', "</table:table> ", $oooval);
511 
512  $oooval = sprintf('<text:section text:style-name="Sect%s" text:name="Section%s" aid="%s">%s</text:section>', $attrid, $attrid, $attrid, $oooval);
513  } else {
514  addWarningMsg(sprintf(_("incorrect conversion HTML to ODT %s") , $this->doc->title));
515  }
516  return $oooval;
517  }
518  /**
519  * function use by Doc::getOOoValue()
520  * use to convert html to xhtml
521  * @param string $lt the < character
522  * @param string $tag the tag name
523  * @param string $attr all attributes of tag
524  * @param string $gt the > tag
525  * @return string the new tag
526  */
527  protected function _fixupStyle($lt, $tag, $attr, $gt)
528  {
529  $px2mm = 3 / 16;
530  $attr = str_replace(':=', '=', $attr);
531  $inlineStyle = array();
532  if (preg_match('/\s+style\s*=\s*"(?P<style>[^"]*)"/u', $attr, $m)) {
533  $inlineStyle = $this->parseInlineStyle($m['style']);
534  };
535  if ($tag === "img") {
536  if (isset($inlineStyle['width']) && preg_match('/^(?P<width>[0-9\.]+)(?P<unit>[a-z]*)$/u', $inlineStyle['width'], $m)) {
537  // width in mm :
538  switch ($m['unit']) {
539  case "px":
540  $width = intval($m['width']) * $px2mm;
541  break;
542 
543  case "cm":
544  $width = floatval($m['width']) * 10;
545  break;
546 
547  case "mm":
548  $width = intval($m['width']);
549  break;
550 
551  default:
552  $width = intval($m['width']);
553  }
554  $attr = sprintf(' width="%d" ', $width) . $attr;
555  }
556  if (isset($inlineStyle['height']) && preg_match('/^(?P<height>[0-9\.]+)(?P<unit>[a-z]*)$/u', $inlineStyle['height'], $m)) {
557  // height in mm
558  switch ($m['unit']) {
559  case "px":
560  $height = intval($m['height']) * $px2mm;
561  break;
562 
563  case "cm":
564  $height = floatval($m['height']) * 10;
565  break;
566 
567  case "mm":
568  $height = intval($m['height']);
569  break;
570 
571  default:
572  $height = intval($m['height']);
573  }
574  $attr = sprintf(' height="%d" ', $height) . $attr;
575  }
576  }
577  return ($lt . $tag . $attr . $gt);
578  }
579 
580  protected function cleanhtml($html)
581  {
582  $html = preg_replace(':</?span[^>]*>:us', "", $html);
583  $html = preg_replace(':</?font[^>]*>:us', "", $html);
584  $html = preg_replace(':</?meta[^>]*>:us', "", $html);
585  $html = preg_replace(':<style[^>]*>.*?</style>:us', "", $html);
586  $html = preg_replace(':<([^>]*) style=\"[^\"]*\":us', "<\\1", $html);
587  $html = preg_replace(':<([^>]*) class=\"[^\"]*\":us', "<\\1", $html);
588  return $html;
589  }
590  /**
591  * format Date attribute
592  *
593  * @param string $avalue raw value of attribute
594  * @return string openText value
595  */
596  public function formatDate($avalue)
597  {
598 
599  if (($this->cFormat != "") && (trim($avalue) != "")) {
600  if ($avalue) $oooval = strftime($this->cFormat, stringDateToUnixTs($avalue));
601  else $oooval = $avalue;
602  } elseif (trim($avalue) == "") {
603  $oooval = "";
604  } else {
605  $oooval = stringDateToLocaleDate($avalue);
606  }
607  $this->cFormat = "";
608  return $oooval;
609  }
610  /**
611  * format Time attribute
612  *
613  * @param string $avalue raw value of attribute
614  * @return string openText value
615  */
616  public function formatTime($avalue)
617  {
618 
619  if ($this->cFormat != "") {
620  if ($avalue) $oooval = strftime($this->cFormat, strtotime($avalue));
621  else $oooval = $avalue;
622  $this->cFormat = "";
623  } else {
624  $oooval = substr($avalue, 0, 5); // do not display second
625 
626  }
627  return $oooval;
628  }
629  /**
630  * format TimeStamp attribute
631  *
632  * @param string $avalue raw value of attribute
633  * @return string openText value
634  */
635  public function formatTimestamp($avalue)
636  {
637 
638  if (($this->cFormat != "") && (trim($avalue) != "")) {
639  if ($avalue) $oooval = strftime($this->cFormat, stringDateToUnixTs($avalue));
640  else $oooval = $avalue;
641  } elseif (trim($avalue) == "") {
642  $oooval = "";
643  } else {
644  $oooval = stringDateToLocaleDate($avalue);
645  }
646  $this->cFormat = "";
647  return $oooval;
648  }
649  /**
650  * format iFile attribute
651  *
652  * @param string $avalue raw value of attribute
653  * @return string openText value
654  */
655  public function formatIfile(
656  /** @noinspection PhpUnusedParameterInspection */
657  $avalue)
658  {
659  global $action;
660  $lay = new Layout("FDL/Layout/viewifile.xml", $action);
661  $lay->set("aid", $this->oattr->id);
662  $lay->set("id", $this->doc->id);
663  $lay->set("iheight", $this->oattr->getOption("height", "200px"));
664  $oooval = $lay->gen();
665  return $oooval;
666  }
667  /**
668  * format Color attribute
669  *
670  * @param string $avalue raw value of attribute
671  * @return string openText value
672  */
673  public function formatColor($avalue)
674  {
675 
676  $oooval = sprintf("<span style=\"background-color:%s\">%s</span>", $avalue, $avalue);
677  return $oooval;
678  }
679  /**
680  * Basic inline style CSS parser
681  *
682  * Parse the value of a style="..." attribute and return a hash containing
683  * the property-names => property-values.
684  *
685  * @param $str
686  * @return array hash of property-name => property-value
687  */
688  protected function parseInlineStyle($str)
689  {
690  $rules = array();
691  preg_match_all('/(?P<propName>[\w-]+)\s*:\s*(?P<propValue>[^;]*)(;|$)/u', $str, $matches, PREG_SET_ORDER);
692  foreach ($matches as $m) {
693  $rules[strtolower($m['propName']) ] = $m['propValue'];
694  }
695  return $rules;
696  }
697 }
Layout is a template generator.
__construct(Doc &$doc)
global $action
stringDateToLocaleDate($fdate, $format= '')
Definition: Lib.Util.php:98
addWarningMsg($msg)
Definition: Lib.Common.php:95
if($famId) $s
static getRelationTitle($docid, $latest=true, Doc $doc, $docrevOption="", array &$info=array())
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
formatThesaurus($avalue)
formatTimestamp($avalue)
getOooValue($oattr, $value, $index=-1)
stringDateToUnixTs($isodate, $utc=false)
Definition: Lib.Util.php:217
static xmlEncode($s)
$value
_fixupStyle($lt, $tag, $attr, $gt)
← centre documentaire © anakeen