Code type module improvements:
[openemr.git] / library / plugins / block.textformat.php
blob760b313fd681d57027797e00e86786f5e752e130
1 <?php
2 /**
3 * Smarty plugin
4 * @package Smarty
5 * @subpackage plugins
6 */
8 /**
9 * Smarty {textformat}{/textformat} block plugin
11 * Type: block function<br>
12 * Name: textformat<br>
13 * Purpose: format text a certain way with preset styles
14 * or custom wrap/indent settings<br>
15 * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
16 * (Smarty online manual)
17 * @param array
18 * <pre>
19 * Params: style: string (email)
20 * indent: integer (0)
21 * wrap: integer (80)
22 * wrap_char string ("\n")
23 * indent_char: string (" ")
24 * wrap_boundary: boolean (true)
25 * </pre>
26 * @param string contents of the block
27 * @param Smarty clever simulation of a method
28 * @return string string $content re-formatted
30 function smarty_block_textformat($params, $content, &$smarty)
32 if (is_null($content)) {
33 return;
36 $style = null;
37 $indent = 0;
38 $indent_first = 0;
39 $indent_char = ' ';
40 $wrap = 80;
41 $wrap_char = "\n";
42 $wrap_cut = false;
43 $assign = null;
45 foreach ($params as $_key => $_val) {
46 switch ($_key) {
47 case 'style':
48 case 'indent_char':
49 case 'wrap_char':
50 case 'assign':
51 $$_key = (string)$_val;
52 break;
54 case 'indent':
55 case 'indent_first':
56 case 'wrap':
57 $$_key = (int)$_val;
58 break;
60 case 'wrap_cut':
61 $$_key = (bool)$_val;
62 break;
64 default:
65 $smarty->trigger_error("textformat: unknown attribute '$_key'");
69 if ($style == 'email') {
70 $wrap = 72;
73 // split into paragraphs
74 $paragraphs = preg_split('![\r\n][\r\n]!',$content);
75 $output = '';
77 foreach ($paragraphs as $paragraph) {
78 if ($paragraph == '') {
79 continue;
81 // convert mult. spaces & special chars to single space
82 $paragraph = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'),array(' ',''),$paragraph);
83 // indent first line
84 if($indent_first > 0) {
85 $paragraph = str_repeat($indent_char,$indent_first) . $paragraph;
87 // wordwrap sentences
88 $paragraph = wordwrap($paragraph, $wrap - $indent, $wrap_char, $wrap_cut);
89 // indent lines
90 if($indent > 0) {
91 $paragraph = preg_replace('!^!m',str_repeat($indent_char,$indent),$paragraph);
93 $output .= $paragraph . $wrap_char . $wrap_char;
96 if ($assign) {
97 $smarty->assign($assign,$output);
98 } else {
99 return $output;
103 /* vim: set expandtab: */