3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * Fix for bug CRM-392. Not sure if this is the best fix or it will impact
38 * other similar PEAR packages. doubt it
40 if (!class_exists('Smarty')) {
41 require_once 'Smarty/Smarty.class.php';
47 class CRM_Core_Smarty
extends Smarty
{
49 // use print.tpl and bypass the CMS. Civi prints a valid html file
51 // this and all the below bypasses the CMS html surrounding it and assumes we will embed this within other pages
53 // sends the generated html to the chosen pdf engine
55 // this options also skips the enclosing form html and does not
56 // generate any of the hidden fields, most notably qfKey
57 // this is typically used in ajax scripts to embed form snippets based on user choices
59 // this prints a complete form and also generates a qfKey, can we replace this with
60 // snippet = 2?? Does the constant _NOFFORM do anything?
62 // Note: added in v 4.3 with the value '6'
63 // Value changed in 4.5 to 'json' for better readability
64 // @see CRM_Core_Page_AJAX::returnJsonResponse
68 * We only need one instance of this object. So we use the singleton
69 * pattern and cache the instance in this variable
73 static private $_singleton = NULL;
76 * @var array (string $name => mixed $value) a list of variables ot save temporarily
78 private $backupFrames = array();
83 * @return CRM_Core_Smarty
85 private function __construct() {
86 parent
::__construct();
89 private function initialize() {
90 $config = CRM_Core_Config
::singleton();
92 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
93 $this->template_dir
= array_merge(array($config->customTemplateDir
),
98 $this->template_dir
= $config->templateDir
;
100 $this->compile_dir
= $config->templateCompileDir
;
102 // check and ensure it is writable
103 // else we sometime suppress errors quietly and this results
104 // in blank emails etc
105 if (!is_writable($this->compile_dir
)) {
106 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
110 //Check for safe mode CRM-2207
111 if (ini_get('safe_mode')) {
112 $this->use_sub_dirs
= FALSE;
115 $this->use_sub_dirs
= TRUE;
118 $customPluginsDir = NULL;
119 if (isset($config->customPHPPathDir
)) {
121 = $config->customPHPPathDir
. DIRECTORY_SEPARATOR
.
122 'CRM' . DIRECTORY_SEPARATOR
.
123 'Core' . DIRECTORY_SEPARATOR
.
124 'Smarty' . DIRECTORY_SEPARATOR
.
125 'plugins' . DIRECTORY_SEPARATOR
;
126 if (!file_exists($customPluginsDir)) {
127 $customPluginsDir = NULL;
131 if ($customPluginsDir) {
132 $this->plugins_dir
= array($customPluginsDir, $config->smartyDir
. 'plugins', $config->pluginsDir
);
135 $this->plugins_dir
= array($config->smartyDir
. 'plugins', $config->pluginsDir
);
138 // add the session and the config here
139 $session = CRM_Core_Session
::singleton();
141 $this->assign_by_ref('config', $config);
142 $this->assign_by_ref('session', $session);
145 $this->assign('tsLocale', $tsLocale);
147 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
148 if (!CRM_Core_Config
::isUpgradeMode()) {
149 $this->assign('langSwitch', CRM_Core_I18n
::languages(TRUE));
152 $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL'));
153 $this->load_filter('pre', 'resetExtScope');
155 $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions());
159 * Static instance provider.
161 * Method providing static instance of SmartTemplate, as
162 * in Singleton pattern.
164 public static function &singleton() {
165 if (!isset(self
::$_singleton)) {
166 self
::$_singleton = new CRM_Core_Smarty();
167 self
::$_singleton->initialize();
169 self
::registerStringResource();
171 return self
::$_singleton;
175 * Executes & returns or displays the template results
177 * @param string $resource_name
178 * @param string $cache_id
179 * @param string $compile_id
180 * @param bool $display
182 * @return bool|mixed|string
184 public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
185 if (preg_match('/^(\s+)?string:/', $resource_name)) {
186 $old_security = $this->security
;
187 $this->security
= TRUE;
189 $output = parent
::fetch($resource_name, $cache_id, $compile_id, $display);
190 if (isset($old_security)) {
191 $this->security
= $old_security;
197 * Fetch a template (while using certain variables)
199 * @param string $resource_name
201 * (string $name => mixed $value) variables to export to Smarty.
203 * @return bool|mixed|string
205 public function fetchWith($resource_name, $vars) {
206 $this->pushScope($vars);
208 $result = $this->fetch($resource_name);
210 catch (Exception
$e) {
211 // simulate try { ... } finally { ... }
220 * @param string $name
223 public function appendValue($name, $value) {
224 $currentValue = $this->get_template_vars($name);
225 if (!$currentValue) {
226 $this->assign($name, $value);
229 if (strpos($currentValue, $value) === FALSE) {
230 $this->assign($name, $currentValue . $value);
235 public function clearTemplateVars() {
236 foreach (array_keys($this->_tpl_vars
) as $key) {
237 if ($key == 'config' ||
$key == 'session') {
240 unset($this->_tpl_vars
[$key]);
244 public static function registerStringResource() {
245 require_once 'CRM/Core/Smarty/resources/String.php';
246 civicrm_smarty_register_string_resource();
252 public function addTemplateDir($path) {
253 if (is_array($this->template_dir
)) {
254 array_unshift($this->template_dir
, $path);
257 $this->template_dir
= array($path, $this->template_dir
);
263 * Temporarily assign a list of variables.
266 * $smarty->pushScope(array(
267 * 'first_name' => 'Alice',
268 * 'last_name' => 'roberts',
270 * $html = $smarty->fetch('view-contact.tpl');
271 * $smarty->popScope();
275 * (string $name => mixed $value).
276 * @return CRM_Core_Smarty
279 public function pushScope($vars) {
280 $oldVars = $this->get_template_vars();
281 $backupFrame = array();
282 foreach ($vars as $key => $value) {
283 $backupFrame[$key] = isset($oldVars[$key]) ?
$oldVars[$key] : NULL;
285 $this->backupFrames
[] = $backupFrame;
287 $this->assignAll($vars);
293 * Remove any values that were previously pushed.
295 * @return CRM_Core_Smarty
298 public function popScope() {
299 $this->assignAll(array_pop($this->backupFrames
));
305 * (string $name => mixed $value).
306 * @return CRM_Core_Smarty
308 public function assignAll($vars) {
309 foreach ($vars as $key => $value) {
310 $this->assign($key, $value);