]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.sugar_replace_vars.php
Release 6.5.0
[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 = empty($params['fields']) ? $smarty->get_template_vars('fields') : $params['fields'];
48         $lDelim = "[";
49     $rDelim = "]";
50     if ($params['use_curly'])
51     {
52         $lDelim = "{";
53         $rDelim = "}";
54     }
55     $subject = $params['subject'];
56         $matches = array();
57         $count = preg_match_all('/\\' . $lDelim . '([^\\' . $rDelim . ']*)\\' . $rDelim . '/', $subject, $matches);
58     for($i = 0; $i < $count; $i++) {
59                 $match = $matches[1][$i];
60         //List views will have fields be an array where all the keys are upper case and the values are jsut strings
61         if (!isset($fields[$match]) && isset($fields[strtoupper($match)]))
62             $match = strtoupper($match);
63
64         $value = isset($fields[$match]) ? $fields[$match] : null;
65         if (!is_null($value)) {
66                         if (is_array($value) && isset($value['value']))
67                 $value = $value['value'];
68
69             if (isset($fields[$match]['type']) && $fields[$match]['type']=='enum'
70                                 && isset($fields[$match]['options']) && isset($fields[$match]['options'][$value]))
71                         {
72                                 $subject = str_replace($matches[0][$i], $fields[$match]['options'][$value], $subject);
73                         } else 
74                         {
75                                 $subject = str_replace($matches[0][$i], $value, $subject);
76                         }
77                 }
78         }
79                 
80         if (!empty($params['assign']))
81         {
82                 $smarty->assign($params['assign'], $subject);
83                 return '';
84         }
85         
86         return $subject;
87 }