4 * Maintain a set of markup/templates to inject inside various regions
6 class CRM_Core_Region
implements CRM_Core_Resources_CollectionInterface
, CRM_Core_Resources_CollectionAdderInterface
{
9 * Obtain the content for a given region.
12 * @param bool $autocreate
13 * Whether to automatically create an empty region.
14 * @return CRM_Core_Region
16 public static function &instance($name, $autocreate = TRUE) {
17 if ($autocreate && !isset(Civi
::$statics[__CLASS__
][$name])) {
18 Civi
::$statics[__CLASS__
][$name] = new CRM_Core_Region($name);
20 return Civi
::$statics[__CLASS__
][$name];
23 use CRM_Core_Resources_CollectionTrait
;
26 * Symbolic name of this region
35 public function __construct($name) {
37 $this->types
= ['markup', 'template', 'callback', 'scriptFile', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleFile', 'styleUrl'];
38 $this->defaults
['region'] = $name;
40 // Placeholder which represents any of the default content generated by the main Smarty template
50 * Render all the snippets in a region.
52 * @param string $default
53 * HTML, the initial content of the region.
54 * @param bool $allowCmsOverride
55 * Allow CMS to override rendering of region.
56 * @return string, HTML
58 public function render($default, $allowCmsOverride = TRUE) {
59 // $default is just another part of the region
60 if (is_array($this->snippets
['default'])) {
61 $this->snippets
['default']['markup'] = $default;
64 Civi
::dispatcher()->dispatch('civi.region.render', \Civi\Core\Event\GenericHookEvent
::create(['region' => $this]));
68 $cms = CRM_Core_Config
::singleton()->userSystem
;
69 $smarty = CRM_Core_Smarty
::singleton();
72 $renderSnippet = function($snippet) use (&$html, $smarty, $cms, $allowCmsOverride, &$renderSnippet) {
73 switch ($snippet['type']) {
75 $html .= $snippet['markup'];
79 $tmp = $smarty->get_template_vars('snippet');
80 $smarty->assign('snippet', $snippet);
81 $html .= $smarty->fetch($snippet['template']);
82 $smarty->assign('snippet', $tmp);
86 $args = $snippet['arguments'] ??
array(&$snippet, &$html);
87 $html .= call_user_func_array($snippet['callback'], $args);
91 if (!$allowCmsOverride ||
!$cms->addScriptUrl($snippet['scriptUrl'], $this->_name
)) {
92 $html .= sprintf("<script type=\"text/javascript\" src=\"%s\">\n</script>\n", $snippet['scriptUrl']);
99 'script' => sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']),
104 foreach ($snippet['scriptFileUrls'] as $url) {
105 $html .= $renderSnippet(['type' => 'scriptUrl', 'scriptUrl' => $url] +
$snippet);
110 if (!$allowCmsOverride ||
!$cms->addScript($snippet['script'], $this->_name
)) {
111 $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']);
116 foreach ($snippet['styleFileUrls'] as $url) {
117 $html .= $renderSnippet(['type' => 'styleUrl', 'styleUrl' => $url] +
$snippet);
122 if (!$allowCmsOverride ||
!$cms->addStyleUrl($snippet['styleUrl'], $this->_name
)) {
123 $html .= sprintf("<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\"/>\n", $snippet['styleUrl']);
128 if (!$allowCmsOverride ||
!$cms->addStyle($snippet['style'], $this->_name
)) {
129 $html .= sprintf("<style type=\"text/css\">\n%s\n</style>\n", $snippet['style']);
134 $settingsData = json_encode($this->getSettings(), JSON_UNESCAPED_SLASHES
);
135 $js = "(function(vars) {
136 if (window.CRM) CRM.$.extend(true, CRM, vars); else window.CRM = vars;
138 $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js);
142 throw new CRM_Core_Exception(ts('Snippet type %1 is unrecognized',
143 [1 => $snippet['type']]));
147 foreach ($this->snippets
as $snippet) {
148 if (empty($snippet['disabled'])) {
149 $renderSnippet($snippet);