Code

Completed migration
[gosa.git] / include / smarty / plugins / block.textformat.php
1 <?php
2 /**
3  * Smarty plugin
4  * @package Smarty
5  * @subpackage plugins
6  */
8 /**
9  * Smarty {textformat}{/textformat} block plugin
10  *
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
29  */
30 function smarty_block_textformat($params, $content, &$smarty)
31 {
32     if (is_null($content)) {
33         return;
34     }
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;
44     
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'");
66         }
67     }
69     if ($style == 'email') {
70         $wrap = 72;
71     }
73     // split into paragraphs
74     $_paragraphs = preg_split('![\r\n][\r\n]!',$content);
75     $_output = '';
77     for($_x = 0, $_y = count($_paragraphs); $_x < $_y; $_x++) {
78         if ($_paragraphs[$_x] == '') {
79             continue;
80         }
81         // convert mult. spaces & special chars to single space
82         $_paragraphs[$_x] = preg_replace(array('!\s+!','!(^\s+)|(\s+$)!'), array(' ',''), $_paragraphs[$_x]);
83         // indent first line
84         if($indent_first > 0) {
85             $_paragraphs[$_x] = str_repeat($indent_char, $indent_first) . $_paragraphs[$_x];
86         }
87         // wordwrap sentences
88         $_paragraphs[$_x] = wordwrap($_paragraphs[$_x], $wrap - $indent, $wrap_char, $wrap_cut);
89         // indent lines
90         if($indent > 0) {
91             $_paragraphs[$_x] = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraphs[$_x]);
92         }
93     }
94     $_output = implode($wrap_char . $wrap_char, $_paragraphs);
96     return $assign ? $smarty->assign($assign, $_output) : $_output;
98 }
100 /* vim: set expandtab: */
102 ?>