Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * Maintain a set of markup/templates to inject inside various regions | |
5 | */ | |
060617e9 | 6 | class CRM_Core_Region implements CRM_Core_Resources_CollectionInterface, CRM_Core_Resources_CollectionAdderInterface { |
6a488035 TO |
7 | |
8 | /** | |
d09edf64 | 9 | * Obtain the content for a given region. |
6a488035 TO |
10 | * |
11 | * @param string $name | |
6a0b768e TO |
12 | * @param bool $autocreate |
13 | * Whether to automatically create an empty region. | |
6a488035 TO |
14 | * @return CRM_Core_Region |
15 | */ | |
00be9182 | 16 | public static function &instance($name, $autocreate = TRUE) { |
832eb60a TO |
17 | if ($autocreate && !isset(Civi::$statics[__CLASS__][$name])) { |
18 | Civi::$statics[__CLASS__][$name] = new CRM_Core_Region($name); | |
6a488035 | 19 | } |
832eb60a | 20 | return Civi::$statics[__CLASS__][$name]; |
6a488035 TO |
21 | } |
22 | ||
060617e9 | 23 | use CRM_Core_Resources_CollectionTrait; |
8dbd7691 | 24 | |
6a488035 TO |
25 | /** |
26 | * Symbolic name of this region | |
27 | * | |
28 | * @var string | |
29 | */ | |
518fa0ee | 30 | public $_name; |
6a488035 | 31 | |
a0ee3941 | 32 | /** |
100fef9d | 33 | * @param string $name |
a0ee3941 | 34 | */ |
6a488035 | 35 | public function __construct($name) { |
6a488035 | 36 | $this->_name = $name; |
bac2c5e4 | 37 | $this->types = ['markup', 'template', 'callback', 'scriptFile', 'scriptUrl', 'script', 'jquery', 'settings', 'style', 'styleFile', 'styleUrl']; |
8dbd7691 | 38 | $this->defaults['region'] = $name; |
6a488035 TO |
39 | |
40 | // Placeholder which represents any of the default content generated by the main Smarty template | |
be2fb01f | 41 | $this->add([ |
6a488035 TO |
42 | 'name' => 'default', |
43 | 'type' => 'markup', | |
44 | 'markup' => '', | |
45 | 'weight' => 0, | |
be2fb01f | 46 | ]); |
6a488035 TO |
47 | } |
48 | ||
49 | /** | |
d09edf64 | 50 | * Render all the snippets in a region. |
6a488035 | 51 | * |
6a0b768e TO |
52 | * @param string $default |
53 | * HTML, the initial content of the region. | |
54 | * @param bool $allowCmsOverride | |
55 | * Allow CMS to override rendering of region. | |
6a488035 TO |
56 | * @return string, HTML |
57 | */ | |
58 | public function render($default, $allowCmsOverride = TRUE) { | |
59 | // $default is just another part of the region | |
8dbd7691 TO |
60 | if (is_array($this->snippets['default'])) { |
61 | $this->snippets['default']['markup'] = $default; | |
6a488035 | 62 | } |
6a488035 | 63 | |
ac27ff2e TO |
64 | Civi::dispatcher()->dispatch('civi.region.render', \Civi\Core\Event\GenericHookEvent::create(['region' => $this])); |
65 | ||
8dbd7691 | 66 | $this->sort(); |
6a488035 | 67 | |
b41dbde6 | 68 | $cms = CRM_Core_Config::singleton()->userSystem; |
6a488035 TO |
69 | $smarty = CRM_Core_Smarty::singleton(); |
70 | $html = ''; | |
b41dbde6 TO |
71 | |
72 | $renderSnippet = function($snippet) use (&$html, $smarty, $cms, $allowCmsOverride, &$renderSnippet) { | |
22e263ad | 73 | switch ($snippet['type']) { |
6a488035 TO |
74 | case 'markup': |
75 | $html .= $snippet['markup']; | |
76 | break; | |
2aa397bc | 77 | |
6a488035 TO |
78 | case 'template': |
79 | $tmp = $smarty->get_template_vars('snippet'); | |
80 | $smarty->assign('snippet', $snippet); | |
81 | $html .= $smarty->fetch($snippet['template']); | |
82 | $smarty->assign('snippet', $tmp); | |
83 | break; | |
2aa397bc | 84 | |
6a488035 | 85 | case 'callback': |
2e1f50d6 | 86 | $args = $snippet['arguments'] ?? array(&$snippet, &$html); |
6a488035 TO |
87 | $html .= call_user_func_array($snippet['callback'], $args); |
88 | break; | |
2aa397bc | 89 | |
6a488035 TO |
90 | case 'scriptUrl': |
91 | if (!$allowCmsOverride || !$cms->addScriptUrl($snippet['scriptUrl'], $this->_name)) { | |
92 | $html .= sprintf("<script type=\"text/javascript\" src=\"%s\">\n</script>\n", $snippet['scriptUrl']); | |
93 | } | |
94 | break; | |
2aa397bc | 95 | |
6a488035 | 96 | case 'jquery': |
b41dbde6 TO |
97 | $renderSnippet([ |
98 | 'type' => 'script', | |
99 | 'script' => sprintf("CRM.\$(function(\$) {\n%s\n});", $snippet['jquery']), | |
100 | ]); | |
101 | break; | |
102 | ||
bac2c5e4 TO |
103 | case 'scriptFile': |
104 | foreach ($snippet['scriptFileUrls'] as $url) { | |
105 | $html .= $renderSnippet(['type' => 'scriptUrl', 'scriptUrl' => $url] + $snippet); | |
106 | } | |
107 | break; | |
108 | ||
6a488035 TO |
109 | case 'script': |
110 | if (!$allowCmsOverride || !$cms->addScript($snippet['script'], $this->_name)) { | |
111 | $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $snippet['script']); | |
112 | } | |
113 | break; | |
2aa397bc | 114 | |
bac2c5e4 TO |
115 | case 'styleFile': |
116 | foreach ($snippet['styleFileUrls'] as $url) { | |
117 | $html .= $renderSnippet(['type' => 'styleUrl', 'styleUrl' => $url] + $snippet); | |
118 | } | |
119 | break; | |
120 | ||
6a488035 TO |
121 | case 'styleUrl': |
122 | if (!$allowCmsOverride || !$cms->addStyleUrl($snippet['styleUrl'], $this->_name)) { | |
123 | $html .= sprintf("<link href=\"%s\" rel=\"stylesheet\" type=\"text/css\"/>\n", $snippet['styleUrl']); | |
124 | } | |
125 | break; | |
2aa397bc | 126 | |
6a488035 TO |
127 | case 'style': |
128 | if (!$allowCmsOverride || !$cms->addStyle($snippet['style'], $this->_name)) { | |
129 | $html .= sprintf("<style type=\"text/css\">\n%s\n</style>\n", $snippet['style']); | |
130 | } | |
131 | break; | |
2aa397bc | 132 | |
832eb60a | 133 | case 'settings': |
f55f8f17 | 134 | $settingsData = json_encode($this->getSettings(), JSON_UNESCAPED_SLASHES); |
832eb60a TO |
135 | $js = "(function(vars) { |
136 | if (window.CRM) CRM.$.extend(true, CRM, vars); else window.CRM = vars; | |
137 | })($settingsData)"; | |
138 | $html .= sprintf("<script type=\"text/javascript\">\n%s\n</script>\n", $js); | |
139 | break; | |
140 | ||
6a488035 | 141 | default: |
79e11805 | 142 | throw new CRM_Core_Exception(ts('Snippet type %1 is unrecognized', |
be2fb01f | 143 | [1 => $snippet['type']])); |
6a488035 | 144 | } |
b41dbde6 TO |
145 | }; |
146 | ||
147 | foreach ($this->snippets as $snippet) { | |
148 | if (empty($snippet['disabled'])) { | |
149 | $renderSnippet($snippet); | |
150 | } | |
6a488035 TO |
151 | } |
152 | return $html; | |
153 | } | |
154 | ||
6a488035 | 155 | } |