Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-10-28-14-52-15
[civicrm-core.git] / CRM / Core / Smarty.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 {
48 CONST
49 // use print.tpl and bypass the CMS. Civi prints a valid html file
50 PRINT_PAGE = 1,
51 // this and all the below bypasses the CMS html surronding it and assumes we will embed this within other pages
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,
62 // this sends the output back in json
63 PRINT_JSON = 6;
64
65 /**
66 * We only need one instance of this object. So we use the singleton
67 * pattern and cache the instance in this variable
68 *
69 * @var object
70 * @static
71 */
72 static private $_singleton = NULL;
73
74 /**
75 * class constructor
76 *
77 * @return CRM_Core_Smarty
78 * @access private
79 */
80 private function __construct() {
81 parent::__construct();
82 }
83
84 private function initialize( ) {
85 $config = CRM_Core_Config::singleton();
86
87 if (isset($config->customTemplateDir) && $config->customTemplateDir) {
88 $this->template_dir = array_merge(array($config->customTemplateDir),
89 $config->templateDir
90 );
91 }
92 else {
93 $this->template_dir = $config->templateDir;
94 }
95 $this->compile_dir = $config->templateCompileDir;
96
97 // check and ensure it is writable
98 // else we sometime suppress errors quietly and this results
99 // in blank emails etc
100 if (!is_writable($this->compile_dir)) {
101 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
102 exit();
103 }
104
105 //Check for safe mode CRM-2207
106 if (ini_get('safe_mode')) {
107 $this->use_sub_dirs = FALSE;
108 }
109 else {
110 $this->use_sub_dirs = TRUE;
111 }
112
113 $customPluginsDir = NULL;
114 if (isset($config->customPHPPathDir)) {
115 $customPluginsDir =
116 $config->customPHPPathDir . DIRECTORY_SEPARATOR .
117 'CRM' . DIRECTORY_SEPARATOR .
118 'Core' . DIRECTORY_SEPARATOR .
119 'Smarty' . DIRECTORY_SEPARATOR .
120 'plugins' . DIRECTORY_SEPARATOR;
121 if (!file_exists($customPluginsDir)) {
122 $customPluginsDir = NULL;
123 }
124 }
125
126 if ($customPluginsDir) {
127 $this->plugins_dir = array($customPluginsDir, $config->smartyDir . 'plugins', $config->pluginsDir);
128 }
129 else {
130 $this->plugins_dir = array($config->smartyDir . 'plugins', $config->pluginsDir);
131 }
132
133 // add the session and the config here
134 $session = CRM_Core_Session::singleton();
135
136 $this->assign_by_ref('config', $config);
137 $this->assign_by_ref('session', $session);
138
139 // check default editor and assign to template
140 $defaultWysiwygEditor = $session->get('defaultWysiwygEditor');
141 if (!$defaultWysiwygEditor && !CRM_Core_Config::isUpgradeMode()) {
142 $defaultWysiwygEditor = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
143 'editor_id'
144 );
145 // For logged-in users, store it in session to reduce db calls
146 if ($session->get('userID')) {
147 $session->set('defaultWysiwygEditor', $defaultWysiwygEditor);
148 }
149 }
150
151 $this->assign('defaultWysiwygEditor', $defaultWysiwygEditor);
152
153 global $tsLocale;
154 $this->assign('tsLocale', $tsLocale);
155
156 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
157 if (!CRM_Core_Config::isUpgradeMode()) {
158 $this->assign('langSwitch', CRM_Core_I18n::languages(TRUE));
159 }
160
161 $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL'));
d9aa1c6b 162 $this->load_filter('pre', 'resetExtScope');
6a488035
TO
163 }
164
165 /**
166 * Static instance provider.
167 *
168 * Method providing static instance of SmartTemplate, as
169 * in Singleton pattern.
170 */
171 static function &singleton() {
172 if (!isset(self::$_singleton)) {
173 self::$_singleton = new CRM_Core_Smarty( );
174 self::$_singleton->initialize( );
175
176 self::registerStringResource();
177 }
178 return self::$_singleton;
179 }
180
181 /**
182 * executes & returns or displays the template results
183 *
184 * @param string $resource_name
185 * @param string $cache_id
186 * @param string $compile_id
187 * @param boolean $display
188 */
189 function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
7155133f
ND
190 if (preg_match( '/^(\s+)?string:/', $resource_name)) {
191 $old_security = $this->security;
192 $this->security = TRUE;
193 }
194 $output = parent::fetch($resource_name, $cache_id, $compile_id, $display);
195 if (isset($old_security)) {
196 $this->security = $old_security;
197 }
198 return $output;
6a488035
TO
199 }
200
201 function appendValue($name, $value) {
202 $currentValue = $this->get_template_vars($name);
203 if (!$currentValue) {
204 $this->assign($name, $value);
205 }
206 else {
207 if (strpos($currentValue, $value) === FALSE) {
208 $this->assign($name, $currentValue . $value);
209 }
210 }
211 }
212
213 function clearTemplateVars() {
214 foreach (array_keys($this->_tpl_vars) as $key) {
215 if ($key == 'config' || $key == 'session') {
216 continue;
217 }
218 unset($this->_tpl_vars[$key]);
219 }
220 }
221
222 static function registerStringResource() {
223 require_once 'CRM/Core/Smarty/resources/String.php';
224 civicrm_smarty_register_string_resource();
225 }
226
227 function addTemplateDir($path) {
228 if ( is_array( $this->template_dir ) ) {
229 array_unshift( $this->template_dir, $path );
230 } else {
231 $this->template_dir = array( $path, $this->template_dir );
232 }
233
234 }
235}
236