Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

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