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 $template template object
31 * @param boolean &$repeat repeat flag
32 * @return string content re-formatted
33 */
34 function smarty_block_textformat($params, $content, $template, &$repeat)
35 {
36 if (is_null($content)) {
37 return;
38 }
40 $style = null;
41 $indent = 0;
42 $indent_first = 0;
43 $indent_char = ' ';
44 $wrap = 80;
45 $wrap_char = "\n";
46 $wrap_cut = false;
47 $assign = null;
49 foreach ($params as $_key => $_val) {
50 switch ($_key) {
51 case 'style':
52 case 'indent_char':
53 case 'wrap_char':
54 case 'assign':
55 $$_key = (string)$_val;
56 break;
58 case 'indent':
59 case 'indent_first':
60 case 'wrap':
61 $$_key = (int)$_val;
62 break;
64 case 'wrap_cut':
65 $$_key = (bool)$_val;
66 break;
68 default:
69 trigger_error("textformat: unknown attribute '$_key'");
70 }
71 }
73 if ($style == 'email') {
74 $wrap = 72;
75 }
76 // split into paragraphs
77 $_paragraphs = preg_split('![\r\n][\r\n]!', $content);
78 $_output = '';
80 for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
81 if ($_paragraphs[$_x] == '') {
82 continue;
83 }
84 // convert mult. spaces & special chars to single space
85 $_paragraphs[$_x] = preg_replace(array('!\s+!', '!(^\s+)|(\s+$)!'), array(' ', ''), $_paragraphs[$_x]);
86 // indent first line
87 if ($indent_first > 0) {
88 $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
89 }
90 // wordwrap sentences
91 $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
92 // indent lines
93 if ($indent > 0) {
94 $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
95 }
96 }
97 $_output = implode($wrap_char . $wrap_char, $_paragraphs);
99 return $assign ? $template->assign($assign, $_output) : $_output;
100 }
102 ?>