3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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
74 static private $_singleton = NULL;
77 * @var array (string $name => mixed $value) a list of variables ot save temporarily
79 private $backupFrames = array();
84 * @return CRM_Core_Smarty
87 private function __construct() {
88 parent
::__construct();
91 private function initialize( ) {
92 $config = CRM_Core_Config
::singleton();
94 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
95 $this->template_dir
= array_merge(array($config->customTemplateDir
),
100 $this->template_dir
= $config->templateDir
;
102 $this->compile_dir
= $config->templateCompileDir
;
104 // check and ensure it is writable
105 // else we sometime suppress errors quietly and this results
106 // in blank emails etc
107 if (!is_writable($this->compile_dir
)) {
108 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
112 //Check for safe mode CRM-2207
113 if (ini_get('safe_mode')) {
114 $this->use_sub_dirs
= FALSE;
117 $this->use_sub_dirs
= TRUE;
120 $customPluginsDir = NULL;
121 if (isset($config->customPHPPathDir
)) {
123 $config->customPHPPathDir
. DIRECTORY_SEPARATOR
.
124 'CRM' . DIRECTORY_SEPARATOR
.
125 'Core' . DIRECTORY_SEPARATOR
.
126 'Smarty' . DIRECTORY_SEPARATOR
.
127 'plugins' . DIRECTORY_SEPARATOR
;
128 if (!file_exists($customPluginsDir)) {
129 $customPluginsDir = NULL;
133 if ($customPluginsDir) {
134 $this->plugins_dir
= array($customPluginsDir, $config->smartyDir
. 'plugins', $config->pluginsDir
);
137 $this->plugins_dir
= array($config->smartyDir
. 'plugins', $config->pluginsDir
);
140 // add the session and the config here
141 $session = CRM_Core_Session
::singleton();
143 $this->assign_by_ref('config', $config);
144 $this->assign_by_ref('session', $session);
146 // check default editor and assign to template
147 $defaultWysiwygEditor = $session->get('defaultWysiwygEditor');
148 if (!$defaultWysiwygEditor && !CRM_Core_Config
::isUpgradeMode()) {
149 $defaultWysiwygEditor = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
152 // For logged-in users, store it in session to reduce db calls
153 if ($session->get('userID')) {
154 $session->set('defaultWysiwygEditor', $defaultWysiwygEditor);
158 $this->assign('defaultWysiwygEditor', $defaultWysiwygEditor);
161 $this->assign('tsLocale', $tsLocale);
163 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
164 if (!CRM_Core_Config
::isUpgradeMode()) {
165 $this->assign('langSwitch', CRM_Core_I18n
::languages(TRUE));
168 $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL'));
169 $this->load_filter('pre', 'resetExtScope');
171 $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions());
175 * Static instance provider.
177 * Method providing static instance of SmartTemplate, as
178 * in Singleton pattern.
180 static function &singleton() {
181 if (!isset(self
::$_singleton)) {
182 self
::$_singleton = new CRM_Core_Smarty( );
183 self
::$_singleton->initialize( );
185 self
::registerStringResource();
187 return self
::$_singleton;
191 * executes & returns or displays the template results
193 * @param string $resource_name
194 * @param string $cache_id
195 * @param string $compile_id
196 * @param boolean $display
198 * @return bool|mixed|string
200 function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
201 if (preg_match( '/^(\s+)?string:/', $resource_name)) {
202 $old_security = $this->security
;
203 $this->security
= TRUE;
205 $output = parent
::fetch($resource_name, $cache_id, $compile_id, $display);
206 if (isset($old_security)) {
207 $this->security
= $old_security;
213 * Fetch a template (while using certain variables)
215 * @param string $resource_name
216 * @param array $vars (string $name => mixed $value) variables to export to Smarty
218 * @return bool|mixed|string
220 function fetchWith($resource_name, $vars) {
221 $this->pushScope($vars);
223 $result = $this->fetch($resource_name);
224 } catch (Exception
$e) {
225 // simulate try { ... } finally { ... }
237 function appendValue($name, $value) {
238 $currentValue = $this->get_template_vars($name);
239 if (!$currentValue) {
240 $this->assign($name, $value);
243 if (strpos($currentValue, $value) === FALSE) {
244 $this->assign($name, $currentValue . $value);
249 function clearTemplateVars() {
250 foreach (array_keys($this->_tpl_vars
) as $key) {
251 if ($key == 'config' ||
$key == 'session') {
254 unset($this->_tpl_vars
[$key]);
258 static function registerStringResource() {
259 require_once 'CRM/Core/Smarty/resources/String.php';
260 civicrm_smarty_register_string_resource();
266 function addTemplateDir($path) {
267 if ( is_array( $this->template_dir
) ) {
268 array_unshift( $this->template_dir
, $path );
270 $this->template_dir
= array( $path, $this->template_dir
);
276 * Temporarily assign a list of variables.
279 * $smarty->pushScope(array(
280 * 'first_name' => 'Alice',
281 * 'last_name' => 'roberts',
283 * $html = $smarty->fetch('view-contact.tpl');
284 * $smarty->popScope();
287 * @param array $vars (string $name => mixed $value)
288 * @return CRM_Core_Smarty
291 public function pushScope($vars) {
292 $oldVars = $this->get_template_vars();
293 $backupFrame = array();
294 foreach ($vars as $key => $value) {
295 $backupFrame[$key] = isset($oldVars[$key]) ?
$oldVars[$key] : NULL;
297 $this->backupFrames
[] = $backupFrame;
299 $this->assignAll($vars);
305 * Remove any values that were previously pushed.
307 * @return CRM_Core_Smarty
310 public function popScope() {
311 $this->assignAll(array_pop($this->backupFrames
));
316 * @param array $vars (string $name => mixed $value)
317 * @return CRM_Core_Smarty
319 public function assignAll($vars) {
320 foreach ($vars as $key => $value) {
321 $this->assign($key, $value);