Highway to PSR2
[openemr.git] / portal / patient / fwk / libs / verysimple / String / SimpleTemplate.php
blob8406d78fc32f96d7fb09ab9401c43d781e4335ed
1 <?php
2 /** @package verysimple::String */
3 require_once("util/html2text.php");
5 /**
6 * A set of utility functions for working with simple template files
8 * @package verysimple::String
9 * @author Jason Hinkle
10 * @copyright 1997-2011 VerySimple, Inc.
11 * @license http://www.gnu.org/licenses/lgpl.html LGPL
12 * @version 1.0
14 class SimpleTemplate
17 /** @var used internally for merging. */
18 static $_MERGE_TEMPLATE_VALUES = null;
20 /**
21 * Transforms HTML into formatted plain text.
23 * @param
24 * string HTML
25 * @return string plain text
27 static function HtmlToText($html)
29 return convert_html_to_text($html);
32 /**
33 * Transforms plain text into formatted HTML.
35 * @param
36 * string plain text
37 * @return string HTML
39 static function TextToHtml($txt)
41 // Kills double spaces and spaces inside tags.
42 while (! (strpos($txt, ' ') === false)) {
43 $txt = str_replace(' ', ' ', $txt);
46 $txt = str_replace(' >', '>', $txt);
47 $txt = str_replace('< ', '<', $txt);
49 // Transforms accents in html entities.
50 $txt = htmlentities($txt);
52 // We need some HTML entities back!
53 $txt = str_replace('&quot;', '"', $txt);
54 $txt = str_replace('&lt;', '<', $txt);
55 $txt = str_replace('&gt;', '>', $txt);
56 $txt = str_replace('&amp;', '&', $txt);
58 // Ajdusts links - anything starting with HTTP opens in a new window
59 // $txt = str_ireplace("<a href=\"http://","<a target=\"_blank\" href=\"http://",$txt);
60 // $txt = str_ireplace("<a href=http://","<a target=\"_blank\" href=http://",$txt);
62 // Basic formatting
63 $eol = (strpos($txt, "\r") === false) ? "\n" : "\r\n";
64 $html = '<p>' . str_replace("$eol$eol", "</p><p>", $txt) . '</p>';
65 $html = str_replace("$eol", "<br />\n", $html);
66 $html = str_replace("</p>", "</p>\n\n", $html);
67 $html = str_replace("<p></p>", "<p>&nbsp;</p>", $html);
69 // Wipes <br> after block tags (for when the user includes some html in the text).
70 $wipebr = array (
71 "table",
72 "tr",
73 "td",
74 "blockquote",
75 "ul",
76 "ol",
77 "li"
80 for ($x = 0; $x < count($wipebr); $x ++) {
81 $tag = $wipebr [$x];
82 $html = str_ireplace("<$tag><br />", "<$tag>", $html);
83 $html = str_ireplace("</$tag><br />", "</$tag>", $html);
86 return $html;
89 /**
90 * Merges data into a template with placeholder variables
91 * (for example "Hello {{NAME}}").
92 * Useful for simple templating
93 * needs such as email, form letters, etc.
95 * If a placeholder is in the template but there is no matching value,
96 * then the placeholder will be left alone and will appear in the output.
98 * Note that there is no escape character so ensure the right and
99 * left delimiters do not appear as normal text within the template
101 * @param string $template
102 * string with placeholder variables
103 * @param
104 * mixed (array or object) $values an associative array or object with key/value pairs
105 * @param
106 * bool true to strip out placeholders with missing value, false to leave them as-is in the output (default true)
107 * @param
108 * string the left (opening) delimiter for placeholders. default = {{
109 * @param
110 * string the right (closing) delimiter for placeholders. default = }}
111 * @return string merged template
113 static function Merge($template, $values, $stripMissingValues = true, $ldelim = "{{", $rdelim = "}}")
115 return $stripMissingValues ? self::MergeRegEx($template, $values, $ldelim, $rdelim) : self::MergeSimple($template, $values, $ldelim, $rdelim);
119 * Used internally by Merge, or may be called directly.
120 * If a placeholder is in the template but there is no matching value,
121 * it will be left alone and appear in the template, for example: {{PLACEHOLDER}}.
123 * @param string $template
124 * string with placeholder variables
125 * @param
126 * mixed (array or object) $values an associative array or object with key/value pairs
127 * @param
128 * string the left (opening) delimiter for placeholders. default = {{
129 * @param
130 * string the right (closing) delimiter for placeholders. default = }}
131 * @return string merged template
133 static function MergeSimple($template, $values, $ldelim = "{{", $rdelim = "}}")
135 $replacements = array ();
137 foreach ($values as $key => $val) {
138 $replacements [$ldelim . $key . $rdelim] = $val;
141 return strtr($template, $replacements);
145 * Used internally by Merge, or may be called directly.
146 * If a placeholder is in the template but there is no matching value,
147 * it will be replaced with empty string and will NOT appear in the output.
149 * @param string $template
150 * string with placeholder variables
151 * @param
152 * mixed (array or object) $values an associative array or object with key/value pairs
153 * @param
154 * string the left (opening) delimiter for placeholders. default = {{
155 * @param
156 * string the right (closing) delimiter for placeholders. default = }}
157 * @return string merged template
159 static function MergeRegEx($template, $values, $ldelim = "{{", $rdelim = "}}")
161 self::$_MERGE_TEMPLATE_VALUES = $values;
163 if ($ldelim != "{{" || $rdelim != "}}") {
164 throw new Exception("Custom delimiters are not yet implemented. Sorry!");
167 $results = preg_replace_callback('!\{\{(\w+)\}\}!', 'SimpleTemplate::_MergeRegExCallback', $template);
169 self::$_MERGE_TEMPLATE_VALUES = null;
171 return $results;
175 * called internally by preg_replace_callback
177 * @param array $matches
179 static function _MergeRegExCallback($matches)
181 if (isset(self::$_MERGE_TEMPLATE_VALUES [$matches [1]])) {
182 return self::$_MERGE_TEMPLATE_VALUES [$matches [1]];
183 } else {
184 return "";