INFRA-132 - Remove white space after an opening "(" or before a closing ")"
[civicrm-core.git] / CRM / Core / Region.php
CommitLineData
6a488035
TO
1<?php
2
3/**
4 * Maintain a set of markup/templates to inject inside various regions
5 */
6class CRM_Core_Region {
7 static private $_instances = NULL;
8
9 /**
10 * Obtain the content for a given region
11 *
12 * @param string $name
6a0b768e
TO
13 * @param bool $autocreate
14 * Whether to automatically create an empty region.
6a488035
TO
15 * @return CRM_Core_Region
16 */
00be9182 17 public static function &instance($name, $autocreate = TRUE) {
481a74f4 18 if ($autocreate && ! isset(self::$_instances[$name])) {
6a488035
TO
19 self::$_instances[$name] = new CRM_Core_Region($name);
20 }
21 return self::$_instances[$name];
22 }
23
24 /**
25 * Symbolic name of this region
26 *
27 * @var string
28 */
29 var $_name;
30
31 /**
32 * List of snippets to inject within region
33 *
34 * @var array; e.g. $this->_snippets[3]['type'] = 'template';
35 */
36 var $_snippets;
37
38 /**
39 * Whether the snippets array has been sorted
40 *
41 * @var boolean
42 */
43 var $_isSorted;
44
a0ee3941 45 /**
100fef9d 46 * @param string $name
a0ee3941 47 */
6a488035
TO
48 public function __construct($name) {
49 // Templates injected into regions should normally be file names, but sometimes inline notation is handy.
50 require_once 'CRM/Core/Smarty/resources/String.php';
481a74f4 51 civicrm_smarty_register_string_resource();
6a488035
TO
52
53 $this->_name = $name;
54 $this->_snippets = array();
55
56 // Placeholder which represents any of the default content generated by the main Smarty template
57 $this->add(array(
58 'name' => 'default',
59 'type' => 'markup',
60 'markup' => '',
61 'weight' => 0,
62 ));
63 $this->_isSorted = TRUE;
64 }
65
66 /**
67 * Add a snippet of content to a region
68 *
69 * @code
70 * CRM_Core_Region::instance('page-header')->add(array(
71 * 'markup' => '<div style="color:red">Hello!</div>',
72 * ));
73 * CRM_Core_Region::instance('page-header')->add(array(
74 * 'script' => 'alert("Hello");',
75 * ));
76 * CRM_Core_Region::instance('page-header')->add(array(
77 * 'template' => 'CRM/Myextension/Extra.tpl',
78 * ));
79 * CRM_Core_Region::instance('page-header')->add(array(
80 * 'callback' => 'myextension_callback_function',
81 * ));
82 * @endcode
83 *
84 * Note: This function does not perform any extra encoding of markup, script code, or etc. If
85 * you're passing in user-data, you must clean it yourself.
86 *
6a0b768e
TO
87 * @param $snippet
88 * Array; keys:.
6a488035
TO
89 * - type: string (auto-detected for markup, template, callback, script, scriptUrl, jquery, style, styleUrl)
90 * - name: string, optional
91 * - weight: int, optional; default=1
92 * - disabled: int, optional; default=0
93 * - markup: string, HTML; required (for type==markup)
94 * - template: string, path; required (for type==template)
95 * - callback: mixed; required (for type==callback)
96 * - arguments: array, optional (for type==callback)
97 * - script: string, Javascript code
98 * - scriptUrl: string, URL of a Javascript file
99 * - jquery: string, Javascript code which runs inside a jQuery(function($){...}); block
100 * - style: string, CSS code
101 * - styleUrl: string, URL of a CSS file
77b97be7
EM
102 *
103 * @return array
6a488035
TO
104 */
105 public function add($snippet) {
106 static $types = array('markup', 'template', 'callback', 'scriptUrl', 'script', 'jquery', 'style', 'styleUrl');
107 $defaults = array(
108 'region' => $this->_name,
109 'weight' => 1,
110 'disabled' => FALSE,
111 );
112 $snippet += $defaults;
113 if (!isset($snippet['type'])) {
114 foreach ($types as $type) {
115 // auto-detect
116 if (isset($snippet[$type])) {
117 $snippet['type'] = $type;
118 break;
119 }
120 }
121 }
122 if (!isset($snippet['name'])) {
123 $snippet['name'] = count($this->_snippets);
124 }
2aa397bc 125 $this->_snippets[$snippet['name']] = $snippet;
6a488035
TO
126 $this->_isSorted = FALSE;
127 return $snippet;
128 }
129
a0ee3941 130 /**
100fef9d 131 * @param string $name
a0ee3941
EM
132 * @param $snippet
133 */
6a488035
TO
134 public function update($name, $snippet) {
135 $this->_snippets[$name] = array_merge($this->_snippets[$name], $snippet);
136 $this->_isSorted = FALSE;
137 }
138
a0ee3941 139 /**
100fef9d 140 * @param string $name
a0ee3941
EM
141 *
142 * @return mixed
143 */
6a488035
TO
144 public function &get($name) {
145 return @$this->_snippets[$name];
146 }
147
148 /**
149 * Render all the snippets in a region
150 *
6a0b768e
TO
151 * @param string $default
152 * HTML, the initial content of the region.
153 * @param bool $allowCmsOverride
154 * Allow CMS to override rendering of region.
6a488035
TO
155 * @return string, HTML
156 */
157 public function render($default, $allowCmsOverride = TRUE) {
158 // $default is just another part of the region
159 if (is_array($this->_snippets['default'])) {
160 $this->_snippets['default']['markup'] = $default;
161 }
162 // We hand as much of the work off to the CMS as possible
163 $cms = CRM_Core_Config::singleton()->userSystem;
164
165 if (!$this->_isSorted) {
166 uasort($this->_snippets, array('CRM_Core_Region', '_cmpSnippet'));
167 $this->_isSorted = TRUE;
168 }
169
170 $smarty = CRM_Core_Smarty::singleton();
171 $html = '';
172 foreach ($this->_snippets as $snippet) {
173 if ($snippet['disabled']) {
174 continue;
175 }
22e263ad 176 switch ($snippet['type']) {
6a488035
TO
177 case 'markup':
178 $html .= $snippet['markup'];
179 break;
2aa397bc 180
6a488035
TO
181 case 'template':
182 $tmp = $smarty->get_template_vars('snippet');
183 $smarty->assign('snippet', $snippet);
184 $html .= $smarty->fetch($snippet['template']);
185 $smarty->assign('snippet', $tmp);
186 break;
2aa397bc 187
6a488035 188 case 'callback':
16fff44f 189 $args = isset($snippet['arguments']) ? $snippet['arguments'] : array(&$snippet, &$html);
6a488035
TO
190 $html .= call_user_func_array($snippet['callback'], $args);
191 break;
2aa397bc 192
6a488035
TO
193 case 'scriptUrl':
194 if (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) {
195 $html .= sprintf("<script type=\"text/javascript\" src=\"%s\">\n</script>\n", $snippet['scriptUrl']);
196 }
197 break;
2aa397bc 198
6a488035 199 case 'jquery':
3cc60a06 200 $snippet['script'] = sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']);
6a488035
TO
201 // no break - continue processing as script
202 case 'script':
203 if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) {
204 $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']);
205 }
206 break;
2aa397bc 207
6a488035
TO
208 case 'styleUrl':
209 if (!$allowCmsOverride || !$cms->addStyleUrl($snippet['styleUrl'], $this->_name)) {
210 $html .= sprintf("<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\"/>\n", $snippet['styleUrl']);
211 }
212 break;
2aa397bc 213
6a488035
TO
214 case 'style':
215 if (!$allowCmsOverride || !$cms->addStyle($snippet['style'], $this->_name)) {
216 $html .= sprintf("<style type=\"text/css\">\n%s\n</style>\n", $snippet['style']);
217 }
218 break;
2aa397bc 219
6a488035
TO
220 default:
221 require_once 'CRM/Core/Error.php';
481a74f4
TO
222 CRM_Core_Error::fatal(ts('Snippet type %1 is unrecognized',
223 array(1 => $snippet['type'])));
6a488035
TO
224 }
225 }
226 return $html;
227 }
228
a0ee3941
EM
229 /**
230 * @param $a
231 * @param $b
232 *
233 * @return int
234 */
00be9182 235 public static function _cmpSnippet($a, $b) {
4f99ca55
TO
236 if ($a['weight'] < $b['weight']) {
237 return -1;
2aa397bc 238 }
4f99ca55
TO
239 if ($a['weight'] > $b['weight']) {
240 return 1;
2aa397bc 241 }
6a488035 242 // fallback to name sort; don't really want to do this, but it makes results more stable
4f99ca55
TO
243 if ($a['name'] < $b['name']) {
244 return -1;
2aa397bc 245 }
4f99ca55
TO
246 if ($a['name'] > $b['name']) {
247 return 1;
2aa397bc 248 }
6a488035
TO
249 return 0;
250 }
251
252 /**
253 * Add block of static HTML to a region
254 *
6a0b768e
TO
255 * @param string $markup
256 * HTML.
6a488035
TO
257 *
258 public function addMarkup($markup) {
2aa397bc
TO
259 return $this->add(array(
260 'type' => 'markup',
261 'markup' => $markup,
262 ));
6a488035
TO
263 }
264
265 /**
266 * Add a Smarty template file to a region
267 *
268 * Note: File is not evaluated until the page is rendered
269 *
6a0b768e
TO
270 * @param string $template
271 * Path to the Smarty template file.
6a488035
TO
272 *
273 public function addTemplate($template) {
2aa397bc
TO
274 return $this->add(array(
275 'type' => 'template',
276 'template' => $template,
277 ));
6a488035
TO
278 }
279
280 /**
281 * Use a callback function to extend a region
282 *
283 * @param mixed $callback
6a0b768e
TO
284 * @param array $arguments
285 * Optional, array of parameters for callback; if omitted, the default arguments are ($snippetSpec, $html).
6a488035
TO
286 *
287 public function addCallback($callback, $arguments = FALSE) {
2aa397bc
TO
288 return $this->add(array(
289 'type' => 'callback',
290 'callback' => $callback,
291 'arguments' => $arguments,
292 ));
6a488035
TO
293 }
294 */
295}