]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.sugar_replace_vars.php
Release 6.2.0beta4
[Github/sugarcrm.git] / include / Smarty / plugins / function.sugar_replace_vars.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r34090 - 2008-04-14 11:15:00 -0700 (Mon, 14 Apr 2008) - dwheeler - 20936: Link field now uses the vardef's default value when creating generated links instead of the database value.
22
23 r34040 - 2008-04-11 11:48:13 -0700 (Fri, 11 Apr 2008) - dwheeler - Fixed Typo that prevented enum fields from being replaced properly.
24
25 r33968 - 2008-04-10 11:30:04 -0700 (Thu, 10 Apr 2008) - dwheeler - bug 20936: Added smarty function to find and replace dynamic strings for custom dynamic fields, now handles translating strings for enum variables. 
26
27
28 */
29
30
31
32 /**
33  * This function will replace fields taken from the fields variable
34  * and insert them into the passed string replacing [variableName] 
35  * tokens where found.
36  *
37  * @param unknown_type $params
38  * @param unknown_type $smarty
39  * @return unknown
40  */
41 function smarty_function_sugar_replace_vars($params, &$smarty)
42 {
43         if(empty($params['subject']))  {
44             $smarty->trigger_error("sugarvar: missing 'subject' parameter");
45             return;
46         } 
47         $fields = $smarty->get_template_vars('fields');
48         $subject = $params['subject'];
49         $matches = array();
50         $count = preg_match_all('/\[([^\]]*)\]/', $subject, $matches);
51         for($i = 0; $i < $count; $i++) {
52                 $match = $matches[1][$i];
53                 if (!empty($fields[$match]) && isset($fields[$match]['value'])) {
54                         $value = $fields[$match]['value'];
55                         if (isset($fields[$match]['type']) && $fields[$match]['type']=='enum' 
56                                 && isset($fields[$match]['options']) && isset($fields[$match]['options'][$value]))
57                         {
58                                 $subject = str_replace($matches[0][$i], $fields[$match]['options'][$value], $subject);
59                         } else 
60                         {
61                                 $subject = str_replace($matches[0][$i], $value, $subject);
62                         }
63                 }
64         }
65                 
66         if (!empty($params['assign']))
67         {
68                 $smarty->assign($params['assign'], $subject);
69                 return '';
70         }
71         
72         return $subject;
73 }