[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /*+*********************************************************************************** 3 * The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 *************************************************************************************/ 10 11 /** 12 * Template class will enable you to replace a merge fields defined in the String 13 * with values set dynamically. 14 * 15 * @author Prasad 16 * @package vtlib 17 */ 18 class Vtiger_StringTemplate { 19 // Template variables set dynamically 20 var $tplvars = Array(); 21 22 /** 23 * Identify variable with the following pattern 24 * $VARIABLE_KEY$ 25 */ 26 var $_lookfor = '/\$([^\$]+)\$/'; 27 28 /** 29 * Constructor 30 */ 31 function __construct() { 32 } 33 34 /** 35 * Assign replacement value for the variable. 36 */ 37 function assign($key, $value) { 38 $this->tplvars[$key] = $value; 39 } 40 41 /** 42 * Get replacement value for the variable. 43 */ 44 function get($key) { 45 $value = false; 46 if(isset($this->tplvars[$key])) { 47 $value = $this->tplvars[$key]; 48 } 49 return $value; 50 } 51 52 /** 53 * Clear all the assigned variable values. 54 * (except the once in the given list) 55 */ 56 function clear($exceptvars=false) { 57 $restorevars = Array(); 58 if($exceptvars) { 59 foreach($exceptvars as $varkey) { 60 $restorevars[$varkey] = $this->get($varkey); 61 } 62 } 63 unset($this->tplvars); 64 65 $this->tplvars = Array(); 66 foreach($restorevars as $key=>$val) $this->assign($key, $val); 67 } 68 69 /** 70 * Merge the given file with variable values assigned. 71 * @param $instring input string template 72 * @param $avoidLookup should be true if only verbatim file copy needs to be done 73 * @returns merged contents 74 */ 75 function merge($instring, $avoidLookup=false) { 76 if(empty($instring)) return $instring; 77 78 if(!$avoidLookup) { 79 80 /** Look for variables */ 81 $matches = Array(); 82 preg_match_all($this->_lookfor, $instring, $matches); 83 84 /** Replace variables found with value assigned. */ 85 $matchcount = count($matches[1]); 86 for($index = 0; $index < $matchcount; ++$index) { 87 $matchstr = $matches[0][$index]; 88 $matchkey = $matches[1][$index]; 89 90 $matchstr_regex = $this->__formatAsRegex($matchstr); 91 92 $replacewith = $this->get($matchkey); 93 if($replacewith) { 94 $instring = preg_replace( 95 "/$matchstr_regex/", $replacewith, $instring); 96 } 97 } 98 } 99 return $instring; 100 } 101 102 /** 103 * Clean up the input to be used as a regex 104 * @access private 105 */ 106 function __formatAsRegex($value) { 107 // If / is not already escaped as \/ do it now 108 $value = preg_replace('/\//', '\\/', $value); 109 // If $ is not already escaped as \$ do it now 110 $value = preg_replace('/(?<!\\\)\$/', '\\\\$', $value); 111 return $value; 112 } 113 114 } 115 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |