PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/include/Smarty/plugins/function.sugar_replace_vars.php

https://github.com/mikmagic/sugarcrm_dev
PHP | 73 lines | 32 code | 6 blank | 35 comment | 10 complexity | a72d58f8842fcd9b39126d6324f1157a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  5. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  6. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  7. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  8. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  9. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  10. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  11. 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.
  12. r34040 - 2008-04-11 11:48:13 -0700 (Fri, 11 Apr 2008) - dwheeler - Fixed Typo that prevented enum fields from being replaced properly.
  13. 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.
  14. */
  15. /**
  16. * This function will replace fields taken from the fields variable
  17. * and insert them into the passed string replacing [variableName]
  18. * tokens where found.
  19. *
  20. * @param unknown_type $params
  21. * @param unknown_type $smarty
  22. * @return unknown
  23. */
  24. function smarty_function_sugar_replace_vars($params, &$smarty)
  25. {
  26. if(empty($params['subject'])) {
  27. $smarty->trigger_error("sugarvar: missing 'subject' parameter");
  28. return;
  29. }
  30. $fields = $smarty->get_template_vars('fields');
  31. $subject = $params['subject'];
  32. $matches = array();
  33. $count = preg_match_all('/\[([^\]]*)\]/', $subject, $matches);
  34. for($i = 0; $i < $count; $i++) {
  35. $match = $matches[1][$i];
  36. if (!empty($fields[$match]) && isset($fields[$match]['value'])) {
  37. $value = $fields[$match]['value'];
  38. if (isset($fields[$match]['type']) && $fields[$match]['type']=='enum'
  39. && isset($fields[$match]['options']) && isset($fields[$match]['options'][$value]))
  40. {
  41. $subject = str_replace($matches[0][$i], $fields[$match]['options'][$value], $subject);
  42. } else
  43. {
  44. $subject = str_replace($matches[0][$i], $value, $subject);
  45. }
  46. }
  47. }
  48. if (!empty($params['assign']))
  49. {
  50. $smarty->assign($params['assign'], $subject);
  51. return '';
  52. }
  53. return $subject;
  54. }