INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Core / Smarty.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
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
39 */
40if (!class_exists('Smarty')) {
41 require_once 'Smarty/Smarty.class.php';
42}
43
44/**
45 *
46 */
47class CRM_Core_Smarty extends Smarty {
7da04cde 48 const
6a488035
TO
49 // use print.tpl and bypass the CMS. Civi prints a valid html file
50 PRINT_PAGE = 1,
da3c7979 51 // this and all the below bypasses the CMS html surrounding it and assumes we will embed this within other pages
6a488035
TO
52 PRINT_SNIPPET = 2,
53 // sends the generated html to the chosen pdf engine
54 PRINT_PDF = 3,
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
58 PRINT_NOFORM = 4,
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?
61 PRINT_QFKEY = 5,
fc05b8da
CW
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
65 PRINT_JSON = 'json';
6a488035
TO
66
67 /**
68 * We only need one instance of this object. So we use the singleton
69 * pattern and cache the instance in this variable
70 *
71 * @var object
72 * @static
73 */
74 static private $_singleton = NULL;
75
17f267d6
TO
76 /**
77 * @var array (string $name => mixed $value) a list of variables ot save temporarily
78 */
79 private $backupFrames = array();
80
6a488035 81 /**
100fef9d 82 * Class constructor
6a488035
TO
83 *
84 * @return CRM_Core_Smarty
6a488035
TO
85 */
86 private function __construct() {
87 parent::__construct();
88 }
89
2aa397bc 90 private function initialize() {
6a488035
TO
91 $config = CRM_Core_Config::singleton();
92
93 if (isset($config->customTemplateDir) && $config->customTemplateDir) {
94 $this->template_dir = array_merge(array($config->customTemplateDir),
95 $config->templateDir
96 );
97 }
98 else {
99 $this->template_dir = $config->templateDir;
100 }
101 $this->compile_dir = $config->templateCompileDir;
102
103 // check and ensure it is writable
104 // else we sometime suppress errors quietly and this results
105 // in blank emails etc
106 if (!is_writable($this->compile_dir)) {
107 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
108 exit();
109 }
110
111 //Check for safe mode CRM-2207
112 if (ini_get('safe_mode')) {
113 $this->use_sub_dirs = FALSE;
114 }
115 else {
116 $this->use_sub_dirs = TRUE;
117 }
118
119 $customPluginsDir = NULL;
120 if (isset($config->customPHPPathDir)) {
121 $customPluginsDir =
122 $config->customPHPPathDir . DIRECTORY_SEPARATOR .
123 'CRM' . DIRECTORY_SEPARATOR .
124 'Core' . DIRECTORY_SEPARATOR .
125 'Smarty' . DIRECTORY_SEPARATOR .
126 'plugins' . DIRECTORY_SEPARATOR;
127 if (!file_exists($customPluginsDir)) {
128 $customPluginsDir = NULL;
129 }
130 }
131
132 if ($customPluginsDir) {
133 $this->plugins_dir = array($customPluginsDir, $config->smartyDir . 'plugins', $config->pluginsDir);
134 }
135 else {
136 $this->plugins_dir = array($config->smartyDir . 'plugins', $config->pluginsDir);
137 }
138
139 // add the session and the config here
140 $session = CRM_Core_Session::singleton();
141
142 $this->assign_by_ref('config', $config);
143 $this->assign_by_ref('session', $session);
144
145 // check default editor and assign to template
146 $defaultWysiwygEditor = $session->get('defaultWysiwygEditor');
147 if (!$defaultWysiwygEditor && !CRM_Core_Config::isUpgradeMode()) {
148 $defaultWysiwygEditor = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
149 'editor_id'
150 );
151 // For logged-in users, store it in session to reduce db calls
152 if ($session->get('userID')) {
153 $session->set('defaultWysiwygEditor', $defaultWysiwygEditor);
154 }
155 }
156
157 $this->assign('defaultWysiwygEditor', $defaultWysiwygEditor);
158
159 global $tsLocale;
160 $this->assign('tsLocale', $tsLocale);
161
162 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
163 if (!CRM_Core_Config::isUpgradeMode()) {
164 $this->assign('langSwitch', CRM_Core_I18n::languages(TRUE));
165 }
166
167 $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL'));
d9aa1c6b 168 $this->load_filter('pre', 'resetExtScope');
abbf7b48
TO
169
170 $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions());
6a488035
TO
171 }
172
173 /**
174 * Static instance provider.
175 *
176 * Method providing static instance of SmartTemplate, as
177 * in Singleton pattern.
178 */
00be9182 179 public static function &singleton() {
6a488035
TO
180 if (!isset(self::$_singleton)) {
181 self::$_singleton = new CRM_Core_Smarty( );
182 self::$_singleton->initialize( );
183
184 self::registerStringResource();
185 }
186 return self::$_singleton;
187 }
188
189 /**
100fef9d 190 * Executes & returns or displays the template results
6a488035
TO
191 *
192 * @param string $resource_name
193 * @param string $cache_id
194 * @param string $compile_id
6a0b768e 195 * @param bool $display
77b97be7
EM
196 *
197 * @return bool|mixed|string
6a488035 198 */
00be9182 199 public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
7155133f
ND
200 if (preg_match( '/^(\s+)?string:/', $resource_name)) {
201 $old_security = $this->security;
202 $this->security = TRUE;
203 }
204 $output = parent::fetch($resource_name, $cache_id, $compile_id, $display);
205 if (isset($old_security)) {
206 $this->security = $old_security;
207 }
208 return $output;
6a488035
TO
209 }
210
9b7526a8
TO
211 /**
212 * Fetch a template (while using certain variables)
213 *
214 * @param string $resource_name
6a0b768e
TO
215 * @param array $vars
216 * (string $name => mixed $value) variables to export to Smarty.
9b7526a8
TO
217 * @throws Exception
218 * @return bool|mixed|string
219 */
00be9182 220 public function fetchWith($resource_name, $vars) {
9b7526a8
TO
221 $this->pushScope($vars);
222 try {
223 $result = $this->fetch($resource_name);
0db6c3e1
TO
224 }
225 catch (Exception $e) {
9b7526a8
TO
226 // simulate try { ... } finally { ... }
227 $this->popScope();
228 throw $e;
229 }
230 $this->popScope();
231 return $result;
232 }
233
a0ee3941 234 /**
100fef9d 235 * @param string $name
a0ee3941
EM
236 * @param $value
237 */
00be9182 238 public function appendValue($name, $value) {
6a488035
TO
239 $currentValue = $this->get_template_vars($name);
240 if (!$currentValue) {
241 $this->assign($name, $value);
242 }
243 else {
244 if (strpos($currentValue, $value) === FALSE) {
245 $this->assign($name, $currentValue . $value);
246 }
247 }
248 }
249
00be9182 250 public function clearTemplateVars() {
6a488035
TO
251 foreach (array_keys($this->_tpl_vars) as $key) {
252 if ($key == 'config' || $key == 'session') {
253 continue;
254 }
255 unset($this->_tpl_vars[$key]);
256 }
257 }
258
00be9182 259 public static function registerStringResource() {
6a488035
TO
260 require_once 'CRM/Core/Smarty/resources/String.php';
261 civicrm_smarty_register_string_resource();
262 }
263
a0ee3941
EM
264 /**
265 * @param $path
266 */
00be9182 267 public function addTemplateDir($path) {
6a488035
TO
268 if ( is_array( $this->template_dir ) ) {
269 array_unshift( $this->template_dir, $path );
0db6c3e1
TO
270 }
271 else {
6a488035
TO
272 $this->template_dir = array( $path, $this->template_dir );
273 }
274
275 }
17f267d6
TO
276
277 /**
278 * Temporarily assign a list of variables.
279 *
280 * @code
281 * $smarty->pushScope(array(
282 * 'first_name' => 'Alice',
283 * 'last_name' => 'roberts',
284 * ));
285 * $html = $smarty->fetch('view-contact.tpl');
286 * $smarty->popScope();
287 * @endcode
288 *
6a0b768e
TO
289 * @param array $vars
290 * (string $name => mixed $value).
17f267d6
TO
291 * @return CRM_Core_Smarty
292 * @see popScope
293 */
294 public function pushScope($vars) {
295 $oldVars = $this->get_template_vars();
296 $backupFrame = array();
297 foreach ($vars as $key => $value) {
298 $backupFrame[$key] = isset($oldVars[$key]) ? $oldVars[$key] : NULL;
299 }
300 $this->backupFrames[] = $backupFrame;
301
302 $this->assignAll($vars);
303
304 return $this;
305 }
306
307 /**
308 * Remove any values that were previously pushed.
309 *
310 * @return CRM_Core_Smarty
311 * @see pushScope
312 */
313 public function popScope() {
314 $this->assignAll(array_pop($this->backupFrames));
315 return $this;
316 }
317
318 /**
6a0b768e
TO
319 * @param array $vars
320 * (string $name => mixed $value).
17f267d6
TO
321 * @return CRM_Core_Smarty
322 */
323 public function assignAll($vars) {
324 foreach ($vars as $key => $value) {
325 $this->assign($key, $value);
326 }
327 return $this;
328 }
6a488035 329}