Commit | Line | Data |
---|---|---|
a807dfa4 TO |
1 | <?php |
2 | ||
3 | // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file | |
4 | ||
5 | /** | |
6 | * The ExtensionUtil class provides small stubs for accessing resources of this | |
7 | * extension. | |
8 | */ | |
9 | class CRM_AfformHtml_ExtensionUtil { | |
10 | const SHORT_NAME = "afform_html"; | |
11 | const LONG_NAME = "org.civicrm.afform-html"; | |
12 | const CLASS_PREFIX = "CRM_AfformHtml"; | |
13 | ||
14 | /** | |
15 | * Translate a string using the extension's domain. | |
16 | * | |
17 | * If the extension doesn't have a specific translation | |
18 | * for the string, fallback to the default translations. | |
19 | * | |
20 | * @param string $text | |
21 | * Canonical message text (generally en_US). | |
22 | * @param array $params | |
23 | * @return string | |
24 | * Translated text. | |
25 | * @see ts | |
26 | */ | |
0a496a3f | 27 | public static function ts($text, $params = []) { |
a807dfa4 | 28 | if (!array_key_exists('domain', $params)) { |
0a496a3f | 29 | $params['domain'] = [self::LONG_NAME, NULL]; |
a807dfa4 TO |
30 | } |
31 | return ts($text, $params); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Get the URL of a resource file (in this extension). | |
36 | * | |
37 | * @param string|NULL $file | |
38 | * Ex: NULL. | |
39 | * Ex: 'css/foo.css'. | |
40 | * @return string | |
41 | * Ex: 'http://example.org/sites/default/ext/org.example.foo'. | |
42 | * Ex: 'http://example.org/sites/default/ext/org.example.foo/css/foo.css'. | |
43 | */ | |
44 | public static function url($file = NULL) { | |
45 | if ($file === NULL) { | |
46 | return rtrim(CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME), '/'); | |
47 | } | |
48 | return CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME, $file); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Get the path of a resource file (in this extension). | |
53 | * | |
54 | * @param string|NULL $file | |
55 | * Ex: NULL. | |
56 | * Ex: 'css/foo.css'. | |
57 | * @return string | |
58 | * Ex: '/var/www/example.org/sites/default/ext/org.example.foo'. | |
59 | * Ex: '/var/www/example.org/sites/default/ext/org.example.foo/css/foo.css'. | |
60 | */ | |
61 | public static function path($file = NULL) { | |
62 | // return CRM_Core_Resources::singleton()->getPath(self::LONG_NAME, $file); | |
63 | return __DIR__ . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file)); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Get the name of a class within this extension. | |
68 | * | |
69 | * @param string $suffix | |
70 | * Ex: 'Page_HelloWorld' or 'Page\\HelloWorld'. | |
71 | * @return string | |
72 | * Ex: 'CRM_Foo_Page_HelloWorld'. | |
73 | */ | |
74 | public static function findClass($suffix) { | |
75 | return self::CLASS_PREFIX . '_' . str_replace('\\', '_', $suffix); | |
76 | } | |
77 | ||
78 | } | |
79 | ||
80 | use CRM_AfformHtml_ExtensionUtil as E; | |
81 | ||
82 | /** | |
83 | * (Delegated) Implements hook_civicrm_config(). | |
84 | * | |
0a496a3f | 85 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config |
a807dfa4 TO |
86 | */ |
87 | function _afform_html_civix_civicrm_config(&$config = NULL) { | |
88 | static $configured = FALSE; | |
89 | if ($configured) { | |
90 | return; | |
91 | } | |
92 | $configured = TRUE; | |
93 | ||
94 | $template =& CRM_Core_Smarty::singleton(); | |
95 | ||
96 | $extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR; | |
97 | $extDir = $extRoot . 'templates'; | |
98 | ||
99 | if (is_array($template->template_dir)) { | |
100 | array_unshift($template->template_dir, $extDir); | |
101 | } | |
102 | else { | |
0a496a3f | 103 | $template->template_dir = [$extDir, $template->template_dir]; |
a807dfa4 TO |
104 | } |
105 | ||
106 | $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); | |
107 | set_include_path($include_path); | |
108 | } | |
109 | ||
110 | /** | |
111 | * (Delegated) Implements hook_civicrm_xmlMenu(). | |
112 | * | |
113 | * @param $files array(string) | |
114 | * | |
0a496a3f | 115 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu |
a807dfa4 TO |
116 | */ |
117 | function _afform_html_civix_civicrm_xmlMenu(&$files) { | |
118 | foreach (_afform_html_civix_glob(__DIR__ . '/xml/Menu/*.xml') as $file) { | |
119 | $files[] = $file; | |
120 | } | |
121 | } | |
122 | ||
123 | /** | |
124 | * Implements hook_civicrm_install(). | |
125 | * | |
0a496a3f | 126 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install |
a807dfa4 TO |
127 | */ |
128 | function _afform_html_civix_civicrm_install() { | |
129 | _afform_html_civix_civicrm_config(); | |
130 | if ($upgrader = _afform_html_civix_upgrader()) { | |
131 | $upgrader->onInstall(); | |
132 | } | |
133 | } | |
134 | ||
135 | /** | |
136 | * Implements hook_civicrm_postInstall(). | |
137 | * | |
0a496a3f | 138 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall |
a807dfa4 TO |
139 | */ |
140 | function _afform_html_civix_civicrm_postInstall() { | |
141 | _afform_html_civix_civicrm_config(); | |
142 | if ($upgrader = _afform_html_civix_upgrader()) { | |
0a496a3f | 143 | if (is_callable([$upgrader, 'onPostInstall'])) { |
a807dfa4 TO |
144 | $upgrader->onPostInstall(); |
145 | } | |
146 | } | |
147 | } | |
148 | ||
149 | /** | |
150 | * Implements hook_civicrm_uninstall(). | |
151 | * | |
0a496a3f | 152 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall |
a807dfa4 TO |
153 | */ |
154 | function _afform_html_civix_civicrm_uninstall() { | |
155 | _afform_html_civix_civicrm_config(); | |
156 | if ($upgrader = _afform_html_civix_upgrader()) { | |
157 | $upgrader->onUninstall(); | |
158 | } | |
159 | } | |
160 | ||
161 | /** | |
162 | * (Delegated) Implements hook_civicrm_enable(). | |
163 | * | |
0a496a3f | 164 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable |
a807dfa4 TO |
165 | */ |
166 | function _afform_html_civix_civicrm_enable() { | |
167 | _afform_html_civix_civicrm_config(); | |
168 | if ($upgrader = _afform_html_civix_upgrader()) { | |
0a496a3f | 169 | if (is_callable([$upgrader, 'onEnable'])) { |
a807dfa4 TO |
170 | $upgrader->onEnable(); |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * (Delegated) Implements hook_civicrm_disable(). | |
177 | * | |
0a496a3f | 178 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable |
a807dfa4 TO |
179 | * @return mixed |
180 | */ | |
181 | function _afform_html_civix_civicrm_disable() { | |
182 | _afform_html_civix_civicrm_config(); | |
183 | if ($upgrader = _afform_html_civix_upgrader()) { | |
0a496a3f | 184 | if (is_callable([$upgrader, 'onDisable'])) { |
a807dfa4 TO |
185 | $upgrader->onDisable(); |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | /** | |
191 | * (Delegated) Implements hook_civicrm_upgrade(). | |
192 | * | |
193 | * @param $op string, the type of operation being performed; 'check' or 'enqueue' | |
194 | * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks | |
195 | * | |
196 | * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) | |
197 | * for 'enqueue', returns void | |
198 | * | |
0a496a3f | 199 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade |
a807dfa4 TO |
200 | */ |
201 | function _afform_html_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { | |
202 | if ($upgrader = _afform_html_civix_upgrader()) { | |
203 | return $upgrader->onUpgrade($op, $queue); | |
204 | } | |
205 | } | |
206 | ||
207 | /** | |
208 | * @return CRM_AfformHtml_Upgrader | |
209 | */ | |
210 | function _afform_html_civix_upgrader() { | |
211 | if (!file_exists(__DIR__ . '/CRM/AfformHtml/Upgrader.php')) { | |
212 | return NULL; | |
213 | } | |
214 | else { | |
215 | return CRM_AfformHtml_Upgrader_Base::instance(); | |
216 | } | |
217 | } | |
218 | ||
219 | /** | |
0a496a3f | 220 | * Search directory tree for files which match a glob pattern. |
a807dfa4 TO |
221 | * |
222 | * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored. | |
223 | * Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles() | |
224 | * | |
0a496a3f TO |
225 | * @param string $dir base dir |
226 | * @param string $pattern , glob pattern, eg "*.txt" | |
227 | * | |
a807dfa4 TO |
228 | * @return array(string) |
229 | */ | |
230 | function _afform_html_civix_find_files($dir, $pattern) { | |
0a496a3f | 231 | if (is_callable(['CRM_Utils_File', 'findFiles'])) { |
a807dfa4 TO |
232 | return CRM_Utils_File::findFiles($dir, $pattern); |
233 | } | |
234 | ||
0a496a3f TO |
235 | $todos = [$dir]; |
236 | $result = []; | |
a807dfa4 TO |
237 | while (!empty($todos)) { |
238 | $subdir = array_shift($todos); | |
239 | foreach (_afform_html_civix_glob("$subdir/$pattern") as $match) { | |
240 | if (!is_dir($match)) { | |
241 | $result[] = $match; | |
242 | } | |
243 | } | |
244 | if ($dh = opendir($subdir)) { | |
245 | while (FALSE !== ($entry = readdir($dh))) { | |
246 | $path = $subdir . DIRECTORY_SEPARATOR . $entry; | |
247 | if ($entry{0} == '.') { | |
248 | } | |
249 | elseif (is_dir($path)) { | |
250 | $todos[] = $path; | |
251 | } | |
252 | } | |
253 | closedir($dh); | |
254 | } | |
255 | } | |
256 | return $result; | |
257 | } | |
258 | /** | |
259 | * (Delegated) Implements hook_civicrm_managed(). | |
260 | * | |
261 | * Find any *.mgd.php files, merge their content, and return. | |
262 | * | |
0a496a3f | 263 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed |
a807dfa4 TO |
264 | */ |
265 | function _afform_html_civix_civicrm_managed(&$entities) { | |
266 | $mgdFiles = _afform_html_civix_find_files(__DIR__, '*.mgd.php'); | |
267 | sort($mgdFiles); | |
268 | foreach ($mgdFiles as $file) { | |
269 | $es = include $file; | |
270 | foreach ($es as $e) { | |
271 | if (empty($e['module'])) { | |
272 | $e['module'] = E::LONG_NAME; | |
273 | } | |
274 | if (empty($e['params']['version'])) { | |
275 | $e['params']['version'] = '3'; | |
276 | } | |
277 | $entities[] = $e; | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | /** | |
283 | * (Delegated) Implements hook_civicrm_caseTypes(). | |
284 | * | |
285 | * Find any and return any files matching "xml/case/*.xml" | |
286 | * | |
287 | * Note: This hook only runs in CiviCRM 4.4+. | |
288 | * | |
0a496a3f | 289 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes |
a807dfa4 TO |
290 | */ |
291 | function _afform_html_civix_civicrm_caseTypes(&$caseTypes) { | |
292 | if (!is_dir(__DIR__ . '/xml/case')) { | |
293 | return; | |
294 | } | |
295 | ||
296 | foreach (_afform_html_civix_glob(__DIR__ . '/xml/case/*.xml') as $file) { | |
297 | $name = preg_replace('/\.xml$/', '', basename($file)); | |
298 | if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) { | |
299 | $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name)); | |
0a496a3f | 300 | throw new CRM_Core_Exception($errorMessage); |
a807dfa4 | 301 | } |
0a496a3f | 302 | $caseTypes[$name] = [ |
a807dfa4 TO |
303 | 'module' => E::LONG_NAME, |
304 | 'name' => $name, | |
305 | 'file' => $file, | |
0a496a3f | 306 | ]; |
a807dfa4 TO |
307 | } |
308 | } | |
309 | ||
310 | /** | |
311 | * (Delegated) Implements hook_civicrm_angularModules(). | |
312 | * | |
313 | * Find any and return any files matching "ang/*.ang.php" | |
314 | * | |
315 | * Note: This hook only runs in CiviCRM 4.5+. | |
316 | * | |
0a496a3f | 317 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules |
a807dfa4 TO |
318 | */ |
319 | function _afform_html_civix_civicrm_angularModules(&$angularModules) { | |
320 | if (!is_dir(__DIR__ . '/ang')) { | |
321 | return; | |
322 | } | |
323 | ||
324 | $files = _afform_html_civix_glob(__DIR__ . '/ang/*.ang.php'); | |
325 | foreach ($files as $file) { | |
326 | $name = preg_replace(':\.ang\.php$:', '', basename($file)); | |
327 | $module = include $file; | |
328 | if (empty($module['ext'])) { | |
329 | $module['ext'] = E::LONG_NAME; | |
330 | } | |
331 | $angularModules[$name] = $module; | |
332 | } | |
333 | } | |
334 | ||
0a496a3f TO |
335 | /** |
336 | * (Delegated) Implements hook_civicrm_themes(). | |
337 | * | |
338 | * Find any and return any files matching "*.theme.php" | |
339 | */ | |
340 | function _afform_html_civix_civicrm_themes(&$themes) { | |
341 | $files = _afform_html_civix_glob(__DIR__ . '/*.theme.php'); | |
342 | foreach ($files as $file) { | |
343 | $themeMeta = include $file; | |
344 | if (empty($themeMeta['name'])) { | |
345 | $themeMeta['name'] = preg_replace(':\.theme\.php$:', '', basename($file)); | |
346 | } | |
347 | if (empty($themeMeta['ext'])) { | |
348 | $themeMeta['ext'] = E::LONG_NAME; | |
349 | } | |
350 | $themes[$themeMeta['name']] = $themeMeta; | |
351 | } | |
352 | } | |
353 | ||
a807dfa4 TO |
354 | /** |
355 | * Glob wrapper which is guaranteed to return an array. | |
356 | * | |
357 | * The documentation for glob() says, "On some systems it is impossible to | |
358 | * distinguish between empty match and an error." Anecdotally, the return | |
359 | * result for an empty match is sometimes array() and sometimes FALSE. | |
360 | * This wrapper provides consistency. | |
361 | * | |
362 | * @link http://php.net/glob | |
363 | * @param string $pattern | |
0a496a3f | 364 | * |
a807dfa4 TO |
365 | * @return array, possibly empty |
366 | */ | |
367 | function _afform_html_civix_glob($pattern) { | |
368 | $result = glob($pattern); | |
0a496a3f | 369 | return is_array($result) ? $result : []; |
a807dfa4 TO |
370 | } |
371 | ||
372 | /** | |
373 | * Inserts a navigation menu item at a given place in the hierarchy. | |
374 | * | |
375 | * @param array $menu - menu hierarchy | |
376 | * @param string $path - path to parent of this item, e.g. 'my_extension/submenu' | |
377 | * 'Mailing', or 'Administer/System Settings' | |
378 | * @param array $item - the item to insert (parent/child attributes will be | |
379 | * filled for you) | |
0a496a3f TO |
380 | * |
381 | * @return bool | |
a807dfa4 TO |
382 | */ |
383 | function _afform_html_civix_insert_navigation_menu(&$menu, $path, $item) { | |
384 | // If we are done going down the path, insert menu | |
385 | if (empty($path)) { | |
0a496a3f TO |
386 | $menu[] = [ |
387 | 'attributes' => array_merge([ | |
a807dfa4 TO |
388 | 'label' => CRM_Utils_Array::value('name', $item), |
389 | 'active' => 1, | |
0a496a3f TO |
390 | ], $item), |
391 | ]; | |
a807dfa4 TO |
392 | return TRUE; |
393 | } | |
394 | else { | |
395 | // Find an recurse into the next level down | |
396 | $found = FALSE; | |
397 | $path = explode('/', $path); | |
398 | $first = array_shift($path); | |
399 | foreach ($menu as $key => &$entry) { | |
400 | if ($entry['attributes']['name'] == $first) { | |
401 | if (!isset($entry['child'])) { | |
0a496a3f | 402 | $entry['child'] = []; |
a807dfa4 | 403 | } |
0a496a3f | 404 | $found = _afform_html_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item); |
a807dfa4 TO |
405 | } |
406 | } | |
407 | return $found; | |
408 | } | |
409 | } | |
410 | ||
411 | /** | |
412 | * (Delegated) Implements hook_civicrm_navigationMenu(). | |
413 | */ | |
414 | function _afform_html_civix_navigationMenu(&$nodes) { | |
0a496a3f | 415 | if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) { |
a807dfa4 TO |
416 | _afform_html_civix_fixNavigationMenu($nodes); |
417 | } | |
418 | } | |
419 | ||
420 | /** | |
421 | * Given a navigation menu, generate navIDs for any items which are | |
422 | * missing them. | |
423 | */ | |
424 | function _afform_html_civix_fixNavigationMenu(&$nodes) { | |
425 | $maxNavID = 1; | |
426 | array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) { | |
427 | if ($key === 'navID') { | |
428 | $maxNavID = max($maxNavID, $item); | |
429 | } | |
430 | }); | |
431 | _afform_html_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL); | |
432 | } | |
433 | ||
434 | function _afform_html_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) { | |
435 | $origKeys = array_keys($nodes); | |
436 | foreach ($origKeys as $origKey) { | |
437 | if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) { | |
438 | $nodes[$origKey]['attributes']['parentID'] = $parentID; | |
439 | } | |
440 | // If no navID, then assign navID and fix key. | |
441 | if (!isset($nodes[$origKey]['attributes']['navID'])) { | |
442 | $newKey = ++$maxNavID; | |
443 | $nodes[$origKey]['attributes']['navID'] = $newKey; | |
444 | $nodes[$newKey] = $nodes[$origKey]; | |
445 | unset($nodes[$origKey]); | |
446 | $origKey = $newKey; | |
447 | } | |
448 | if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) { | |
449 | _afform_html_civix_fixNavigationMenuItems($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']); | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | /** | |
455 | * (Delegated) Implements hook_civicrm_alterSettingsFolders(). | |
456 | * | |
0a496a3f | 457 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders |
a807dfa4 TO |
458 | */ |
459 | function _afform_html_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { | |
a807dfa4 | 460 | $settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings'; |
0a496a3f | 461 | if (!in_array($settingsDir, $metaDataFolders) && is_dir($settingsDir)) { |
a807dfa4 TO |
462 | $metaDataFolders[] = $settingsDir; |
463 | } | |
464 | } | |
465 | ||
466 | /** | |
467 | * (Delegated) Implements hook_civicrm_entityTypes(). | |
468 | * | |
469 | * Find any *.entityType.php files, merge their content, and return. | |
470 | * | |
0a496a3f | 471 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes |
a807dfa4 TO |
472 | */ |
473 | ||
474 | function _afform_html_civix_civicrm_entityTypes(&$entityTypes) { | |
475 | $entityTypes = array_merge($entityTypes, array ( | |
476 | )); | |
477 | } |