Browse Source

MMIDocument - ODT extends rendering - Hypertext links, logo, images, bgcolor, children

Mathieu Moulin 2 years ago
parent
commit
0576552dd6

+ 28 - 1
htdocs/core/modules/commande/doc/doc_generic_order_odt.modules.php

@@ -402,6 +402,12 @@ class doc_generic_order_odt extends ModelePDFCommandes
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
+						// Added by MMI Mathieu Moulin iProspective
+						// Display hypertext links
+						} elseif (is_string($value) && (preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+$/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 						} else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -431,7 +437,28 @@ class doc_generic_order_odt extends ModelePDFCommandes
 							$reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
 							foreach ($tmparray as $key => $val) {
 								try {
-									$listlines->setVars($key, $val, true, 'UTF-8');
+									// Added by MMI Mathieu Moulin iProspective
+									// Display logos
+									if (preg_match('/logo$/', $key) || preg_match('/logo2$/', $key)) { // Image
+										//var_dump($key); var_dump($val); die();
+										if (empty($val)) {
+											$listlines->setVars($key, $val, true, 'UTF-8');
+										}
+										elseif (file_exists($val)) {
+											//var_dump($key); var_dump($val); die();
+											$listlines->setImage($key, $val);
+										} else {
+											$listlines->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
+										}
+									} elseif (preg_match('/_$/', $key)) // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, false, 'UTF-8');
+									} else // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, true, 'UTF-8');
+									}
 								} catch (OdfException $e) {
 									dol_syslog($e->getMessage(), LOG_INFO);
 								} catch (SegmentException $e) {

+ 48 - 1
htdocs/core/modules/expedition/doc/doc_generic_shipment_odt.modules.php

@@ -384,6 +384,12 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('/^http/', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 						} else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -404,6 +410,12 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('/^http/', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 						} else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -426,6 +438,12 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('/^http/', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 						} else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -445,6 +463,12 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 								} else {
 									$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 								}
+							// Added by MMI Mathieu Moulin iProspective
+							// Hypertext links
+							} elseif (is_string($value) && (preg_match('/^http/', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+/i', $value))) // link
+							{
+								//var_dump($key); var_dump($value); die();
+								$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 							} else // Text
 							{
 								$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -470,6 +494,12 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('/^http/', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
 						} else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
@@ -497,7 +527,24 @@ class doc_generic_shipment_odt extends ModelePdfExpedition
 							$reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
 							foreach ($tmparray as $key => $val) {
 								try {
-									$listlines->setVars($key, $val, true, 'UTF-8');
+									// Added by MMI Mathieu Moulin iProspective
+									// Logo
+									if (preg_match('/logo$/', $key) || preg_match('/logo2$/', $key)) { // Image
+										//var_dump($key); var_dump($val); die();
+										if (empty($val)) {
+											$listlines->setVars($key, $val, true, 'UTF-8');
+										}
+										elseif (file_exists($val)) {
+											//var_dump($key); var_dump($val); die();
+											$listlines->setImage($key, $val);
+										} else {
+											$listlines->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
+										}
+									} else // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, true, 'UTF-8');
+									}
 								} catch (OdfException $e) {
 									dol_syslog($e->getMessage(), LOG_INFO);
 								} catch (SegmentException $e) {

+ 31 - 2
htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php

@@ -411,7 +411,14 @@ class doc_generic_invoice_odt extends ModelePDFFactures
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
-						} else // Text
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+$/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
+						}
+						 else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
 						}
@@ -440,7 +447,29 @@ class doc_generic_invoice_odt extends ModelePDFFactures
 							$reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
 							foreach ($tmparray as $key => $val) {
 								try {
-									$listlines->setVars($key, $val, true, 'UTF-8');
+									// Added by MMI Mathieu Moulin iProspective
+									// Logo
+									if (preg_match('/logo$/', $key) || preg_match('/logo2$/', $key)) { // Image
+										//var_dump($key); var_dump($val); die();
+										if (empty($val)) {
+											$listlines->setVars($key, $val, true, 'UTF-8');
+										}
+										elseif (file_exists($val)) {
+											//var_dump($key); var_dump($val); die();
+											$listlines->setImage($key, $val);
+										} else {
+											$listlines->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
+										}
+									} elseif (preg_match('/_$/', $key)) // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, false, 'UTF-8');
+										//var_dump($key, $val);
+									} else // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, true, 'UTF-8');
+									}
 								} catch (OdfException $e) {
 									dol_syslog($e->getMessage(), LOG_INFO);
 								} catch (SegmentException $e) {

+ 30 - 2
htdocs/core/modules/propale/doc/doc_generic_proposal_odt.modules.php

@@ -429,7 +429,14 @@ class doc_generic_proposal_odt extends ModelePDFPropales
 							} else {
 								$odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
 							}
-						} else // Text
+						// Added by MMI Mathieu Moulin iProspective
+						// Hypertext links
+						} elseif (is_string($value) && (preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $value) || preg_match('/^[a-z0-9\._-]+@[a-z0-9\._-]+$/i', $value))) // link
+						{
+							//var_dump($key); var_dump($value); die();
+							$odfHandler->setVars($key, '<a href="'.$value.'">'.$value.'</a>', false, 'UTF-8');
+						}
+						 else // Text
 						{
 							$odfHandler->setVars($key, $value, true, 'UTF-8');
 						}
@@ -458,7 +465,28 @@ class doc_generic_proposal_odt extends ModelePDFPropales
 							$reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
 							foreach ($tmparray as $key => $val) {
 								try {
-									$listlines->setVars($key, $val, true, 'UTF-8');
+									// Added by MMI Mathieu Moulin iProspective
+									// Logo
+									if (preg_match('/logo$/', $key) || preg_match('/logo2$/', $key)) { // Image
+										//var_dump($key); var_dump($val); die();
+										if (empty($val)) {
+											$listlines->setVars($key, $val, true, 'UTF-8');
+										}
+										elseif (file_exists($val)) {
+											//var_dump($key); var_dump($val); die();
+											$listlines->setImage($key, $val);
+										} else {
+											$listlines->setVars($key, 'ErrorFileNotFound', true, 'UTF-8');
+										}
+									} elseif (preg_match('/_$/', $key)) // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, false, 'UTF-8');
+									} else // Text
+									{
+										//var_dump($key); var_dump($val); die();
+										$listlines->setVars($key, $val, true, 'UTF-8');
+									}
 								} catch (OdfException $e) {
 									dol_syslog($e->getMessage(), LOG_INFO);
 								} catch (SegmentException $e) {

+ 15 - 2
htdocs/includes/odtphp/Segment.php

@@ -255,8 +255,21 @@ class Segment implements IteratorAggregate, Countable
         }
         // Set the width and height of the page
         list ($width, $height) = $size;
-        $width *= Odf::PIXEL_TO_CM;
-        $height *= Odf::PIXEL_TO_CM;
+        // Added by MMI Mathieu Moulin iProspective
+        if (preg_match('/produit/', $value)) {
+            if (preg_match('/logo2$/', $key)) {
+                $width = 75*Odf::PIXEL_TO_CM;
+                $height = 75*Odf::PIXEL_TO_CM;
+            }
+            else {
+                $width = 150*Odf::PIXEL_TO_CM;
+                $height = 150*Odf::PIXEL_TO_CM;
+            }
+        }
+        else {
+            $width *= Odf::PIXEL_TO_CM;
+            $height *= Odf::PIXEL_TO_CM;
+        }
         // Fix local-aware issues (eg: 12,10 -> 12.10)
         $width = sprintf("%F", $width);
         $height = sprintf("%F", $height);

+ 14 - 1
htdocs/includes/odtphp/odf.php

@@ -228,6 +228,9 @@ class Odf
                     case 'b':
                         $odtResult .= '<text:span text:style-name="boldText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
                         break;
+                    case 'a':
+                        $odtResult = '<text:a xlink:type="simple" xlink:href="'.(is_numeric(strpos($tag['innerText'], '@')) ?'mailto:'.$tag['innerText'] :$tag['innerText']).'" office:name="">'.$tag['innerText'].'</text:a>';
+                        break;
                     case 'i':
                     case 'em':
                         $odtResult .= '<text:span text:style-name="italicText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
@@ -245,6 +248,7 @@ class Odf
                         $odtResult .= '<text:span text:style-name="supText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
                         break;
                     case 'span':
+                    case 'p':
                         if (isset($tag['attributes']['style'])) {
                             $odtStyles = '';
                             foreach ($tag['attributes']['style'] as $styleName => $styleValue) {
@@ -273,13 +277,22 @@ class Odf
                                             $odtStyles .= '<style:text-properties fo:color="' . $styleValue . '" />';
                                         }
                                         break;
+                                    case 'background-color':
+                                        if (preg_match('/#[0-9A-Fa-f]{3}(?:[0-9A-Fa-f]{3})?/', $styleValue)) {
+                                            $odtStyles .= '<style:text-properties fo:background-color="' . $styleValue . '" />';
+                                            //var_dump($odtStyles); die();
+                                        }
+                                        break;
                                 }
                             }
                             if (strlen($odtStyles) > 0) {
 								// Generate a unique id for the style (using microtime and random because some CPUs are really fast...)
                                 $key = floatval(str_replace('.', '', microtime(true)))+rand(0, 10);
                                 $customStyles[$key] = $odtStyles;
-                                $odtResult .= '<text:span text:style-name="customStyle' . $key . '">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
+                                $odtResult .= '<text:'.($tag['name']).' text:style-name="customStyle' . $key . '">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:'.($tag['name']).'>';
+                            }
+                            else {
+                            	$odtResult .= ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']);
                             }
                         }
                         break;