1: <?php
2: 3: 4: 5: 6:
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: function smarty_block_textformat($params, $content, &$smarty)
32: {
33: if (is_null($content)) {
34: return;
35: }
36:
37: $style = null;
38: $indent = 0;
39: $indent_first = 0;
40: $indent_char = ' ';
41: $wrap = 80;
42: $wrap_char = "\n";
43: $wrap_cut = false;
44: $assign = null;
45:
46: foreach ($params as $_key => $_val) {
47: switch ($_key) {
48: case 'style':
49: case 'indent_char':
50: case 'wrap_char':
51: case 'assign':
52: $$_key = (string)$_val;
53: break;
54:
55: case 'indent':
56: case 'indent_first':
57: case 'wrap':
58: $$_key = (int)$_val;
59: break;
60:
61: case 'wrap_cut':
62: $$_key = (bool)$_val;
63: break;
64:
65: default:
66: $smarty->trigger_error("textformat: unknown attribute '$_key'");
67: }
68: }
69:
70: if ($style == 'email') {
71: $wrap = 72;
72: }
73:
74:
75: $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
76: $_output = '';
77:
78: for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
79: if ($_paragraphs[$_x] == '') {
80: continue;
81: }
82:
83: $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
84:
85: if($indent_first > 0) {
86: $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
87: }
88:
89: $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
90:
91: if($indent > 0) {
92: $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
93: }
94: }
95: $_output = implode($wrap_char . $wrap_char, $_paragraphs);
96:
97: return $assign ? $smarty->assign($assign, $_output) : $_output;
98:
99: }
100:
101:
102:
103: ?>
104: