| 1 | joko | 1.1 | <?php | 
| 2 | joko | 1.2 | /** | 
| 3 |  |  | * Smarty plugin | 
| 4 |  |  | * @package Smarty | 
| 5 |  |  | * @subpackage plugins | 
| 6 |  |  | */ | 
| 7 | joko | 1.1 |  | 
| 8 | joko | 1.2 | /** | 
| 9 |  |  | * Smarty {textformat}{/textformat} block plugin | 
| 10 |  |  | * | 
| 11 |  |  | * Type:     block function<br> | 
| 12 |  |  | * Name:     textformat<br> | 
| 13 | joko | 1.1 | * Purpose:  format text a certain way with preset styles | 
| 14 | joko | 1.2 | *           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 | joko | 1.1 | * 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 | joko | 1.2 | * </pre> | 
| 26 |  |  | * @param string contents of the block | 
| 27 |  |  | * @param Smarty clever simulation of a method | 
| 28 |  |  | * @return string string $content re-formatted | 
| 29 | joko | 1.1 | */ | 
| 30 | joko | 1.2 | function smarty_block_textformat($params, $content, &$smarty) | 
| 31 | joko | 1.1 | { | 
| 32 | joko | 1.2 | if (is_null($content)) { | 
| 33 |  |  | return; | 
| 34 |  |  | } | 
| 35 |  |  |  | 
| 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; | 
| 53 |  |  |  | 
| 54 |  |  | case 'indent': | 
| 55 |  |  | case 'indent_first': | 
| 56 |  |  | case 'wrap': | 
| 57 |  |  | $$_key = (int)$_val; | 
| 58 |  |  | break; | 
| 59 |  |  |  | 
| 60 |  |  | case 'wrap_cut': | 
| 61 |  |  | $$_key = (bool)$_val; | 
| 62 |  |  | break; | 
| 63 |  |  |  | 
| 64 |  |  | default: | 
| 65 |  |  | $smarty->trigger_error("textformat: unknown attribute '$_key'"); | 
| 66 |  |  | } | 
| 67 |  |  | } | 
| 68 |  |  |  | 
| 69 |  |  | if ($style == 'email') { | 
| 70 |  |  | $wrap = 72; | 
| 71 |  |  | } | 
| 72 |  |  |  | 
| 73 |  |  | // split into paragraphs | 
| 74 |  |  | $_paragraphs = preg_split('![\r\n][\r\n]!',$content); | 
| 75 |  |  | $_output = ''; | 
| 76 |  |  |  | 
| 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); | 
| 95 |  |  |  | 
| 96 |  |  | return $assign ? $smarty->assign($assign, $_output) : $_output; | 
| 97 |  |  |  | 
| 98 | joko | 1.1 | } | 
| 99 |  |  |  | 
| 100 |  |  | /* vim: set expandtab: */ | 
| 101 |  |  |  | 
| 102 |  |  | ?> |