1 <?php
2 /**
3 * Smarty plugin to format text blocks
4 *
5 * @package Smarty
6 * @subpackage PluginsBlock
7 */
9 /**
10 * Smarty {textformat}{/textformat} block plugin
11 *
12 * Type: block function<br>
13 * Name: textformat<br>
14 * Purpose: format text a certain way with preset styles
15 * or custom wrap/indent settings<br>
16 *
17 * @link http://smarty.php.net/manual/en/language.function.textformat.php {textformat}
18 (Smarty online manual)
19 * @param array $params parameters
20 * <pre>
21 * Params: style: string (email)
22 * indent: integer (0)
23 * wrap: integer (80)
24 * wrap_char string ("\n")
25 * indent_char: string (" ")
26 * wrap_boundary: boolean (true)
27 * </pre>
28 * @author Monte Ohrt <monte at ohrt dot com>
29 * @param string $content contents of the block
30 * @param object $smarty Smarty object
31 * @param boolean &$repeat repeat flag
32 * @param object $template template object
33 * @return string content re-formatted
34 */
35 function smarty_block_textformat($params, $content, $smarty, &$repeat, $template)
36 {
37 if (is_null($content)) {
38 return;
39 }
41 $style = null;
42 $indent = 0;
43 $indent_first = 0;
44 $indent_char = ' ';
45 $wrap = 80;
46 $wrap_char = "\n";
47 $wrap_cut = false;
48 $assign = null;
50 foreach ($params as $_key => $_val) {
51 switch ($_key) {
52 case 'style':
53 case 'indent_char':
54 case 'wrap_char':
55 case 'assign':
56 $$_key = (string)$_val;
57 break;
59 case 'indent':
60 case 'indent_first':
61 case 'wrap':
62 $$_key = (int)$_val;
63 break;
65 case 'wrap_cut':
66 $$_key = (bool)$_val;
67 break;
69 default:
70 $smarty->trigger_error("textformat: unknown attribute '$_key'");
71 }
72 }
74 if ($style == 'email') {
75 $wrap = 72;
76 }
77 // split into paragraphs
78 $_paragraphs = preg_split('![\r\n][\r\n]!', $content);
79 $_output = '';
81 for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
82 if ($_paragraphs[$_x] == '') {
83 continue;
84 }
85 // convert mult. spaces & special chars to single space
86 $_paragraphs[$_x] = preg_replace(array('!\s+!', '!(^\s+)|(\s+$)!'), array(' ', ''), $_paragraphs[$_x]);
87 // indent first line
88 if ($indent_first > 0) {
89 $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
90 }
91 // wordwrap sentences
92 $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
93 // indent lines
94 if ($indent > 0) {
95 $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
96 }
97 }
98 $_output = implode($wrap_char . $wrap_char, $_paragraphs);
100 return $assign ? $template->assign($assign, $_output) : $_output;
101 }
103 ?>