3 // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
6 * The ExtensionUtil class provides small stubs for accessing resources of this
9 class CRM_Afform_ExtensionUtil
{
10 const SHORT_NAME
= "afform";
11 const LONG_NAME
= "org.civicrm.afform";
12 const CLASS_PREFIX
= "CRM_Afform";
15 * Translate a string using the extension's domain.
17 * If the extension doesn't have a specific translation
18 * for the string, fallback to the default translations.
21 * Canonical message text (generally en_US).
22 * @param array $params
27 public static function ts($text, $params = array()) {
28 if (!array_key_exists('domain', $params)) {
29 $params['domain'] = array(self
::LONG_NAME
, NULL);
31 return ts($text, $params);
35 * Get the URL of a resource file (in this extension).
37 * @param string|NULL $file
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'.
44 public static function url($file = NULL) {
46 return rtrim(CRM_Core_Resources
::singleton()->getUrl(self
::LONG_NAME
), '/');
48 return CRM_Core_Resources
::singleton()->getUrl(self
::LONG_NAME
, $file);
52 * Get the path of a resource file (in this extension).
54 * @param string|NULL $file
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'.
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));
67 * Get the name of a class within this extension.
69 * @param string $suffix
70 * Ex: 'Page_HelloWorld' or 'Page\\HelloWorld'.
72 * Ex: 'CRM_Foo_Page_HelloWorld'.
74 public static function findClass($suffix) {
75 return self
::CLASS_PREFIX
. '_' . str_replace('\\', '_', $suffix);
80 use CRM_Afform_ExtensionUtil
as E
;
83 * (Delegated) Implements hook_civicrm_config().
85 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
87 function _afform_civix_civicrm_config(&$config = NULL) {
88 static $configured = FALSE;
94 $template =& CRM_Core_Smarty
::singleton();
96 $extRoot = dirname(__FILE__
) . DIRECTORY_SEPARATOR
;
97 $extDir = $extRoot . 'templates';
99 if (is_array($template->template_dir
)) {
100 array_unshift($template->template_dir
, $extDir);
103 $template->template_dir
= array($extDir, $template->template_dir
);
106 $include_path = $extRoot . PATH_SEPARATOR
. get_include_path();
107 set_include_path($include_path);
111 * (Delegated) Implements hook_civicrm_xmlMenu().
113 * @param $files array(string)
115 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
117 function _afform_civix_civicrm_xmlMenu(&$files) {
118 foreach (_afform_civix_glob(__DIR__
. '/xml/Menu/*.xml') as $file) {
124 * Implements hook_civicrm_install().
126 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
128 function _afform_civix_civicrm_install() {
129 _afform_civix_civicrm_config();
130 if ($upgrader = _afform_civix_upgrader()) {
131 $upgrader->onInstall();
136 * Implements hook_civicrm_postInstall().
138 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
140 function _afform_civix_civicrm_postInstall() {
141 _afform_civix_civicrm_config();
142 if ($upgrader = _afform_civix_upgrader()) {
143 if (is_callable(array($upgrader, 'onPostInstall'))) {
144 $upgrader->onPostInstall();
150 * Implements hook_civicrm_uninstall().
152 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
154 function _afform_civix_civicrm_uninstall() {
155 _afform_civix_civicrm_config();
156 if ($upgrader = _afform_civix_upgrader()) {
157 $upgrader->onUninstall();
162 * (Delegated) Implements hook_civicrm_enable().
164 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
166 function _afform_civix_civicrm_enable() {
167 _afform_civix_civicrm_config();
168 if ($upgrader = _afform_civix_upgrader()) {
169 if (is_callable(array($upgrader, 'onEnable'))) {
170 $upgrader->onEnable();
176 * (Delegated) Implements hook_civicrm_disable().
178 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
181 function _afform_civix_civicrm_disable() {
182 _afform_civix_civicrm_config();
183 if ($upgrader = _afform_civix_upgrader()) {
184 if (is_callable(array($upgrader, 'onDisable'))) {
185 $upgrader->onDisable();
191 * (Delegated) Implements hook_civicrm_upgrade().
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
196 * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
197 * for 'enqueue', returns void
199 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
201 function _afform_civix_civicrm_upgrade($op, CRM_Queue_Queue
$queue = NULL) {
202 if ($upgrader = _afform_civix_upgrader()) {
203 return $upgrader->onUpgrade($op, $queue);
208 * @return CRM_Afform_Upgrader
210 function _afform_civix_upgrader() {
211 if (!file_exists(__DIR__
. '/CRM/Afform/Upgrader.php')) {
215 return CRM_Afform_Upgrader_Base
::instance();
220 * Search directory tree for files which match a glob pattern
222 * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored.
223 * Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles()
225 * @param $dir string, base dir
226 * @param $pattern string, glob pattern, eg "*.txt"
227 * @return array(string)
229 function _afform_civix_find_files($dir, $pattern) {
230 if (is_callable(array('CRM_Utils_File', 'findFiles'))) {
231 return CRM_Utils_File
::findFiles($dir, $pattern);
234 $todos = array($dir);
236 while (!empty($todos)) {
237 $subdir = array_shift($todos);
238 foreach (_afform_civix_glob("$subdir/$pattern") as $match) {
239 if (!is_dir($match)) {
243 if ($dh = opendir($subdir)) {
244 while (FALSE !== ($entry = readdir($dh))) {
245 $path = $subdir . DIRECTORY_SEPARATOR
. $entry;
246 if ($entry{0} == '.') {
248 elseif (is_dir($path)) {
258 * (Delegated) Implements hook_civicrm_managed().
260 * Find any *.mgd.php files, merge their content, and return.
262 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
264 function _afform_civix_civicrm_managed(&$entities) {
265 $mgdFiles = _afform_civix_find_files(__DIR__
, '*.mgd.php');
266 foreach ($mgdFiles as $file) {
268 foreach ($es as $e) {
269 if (empty($e['module'])) {
270 $e['module'] = E
::LONG_NAME
;
273 if (empty($e['params']['version'])) {
274 $e['params']['version'] = '3';
281 * (Delegated) Implements hook_civicrm_caseTypes().
283 * Find any and return any files matching "xml/case/*.xml"
285 * Note: This hook only runs in CiviCRM 4.4+.
287 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
289 function _afform_civix_civicrm_caseTypes(&$caseTypes) {
290 if (!is_dir(__DIR__
. '/xml/case')) {
294 foreach (_afform_civix_glob(__DIR__
. '/xml/case/*.xml') as $file) {
295 $name = preg_replace('/\.xml$/', '', basename($file));
296 if ($name != CRM_Case_XMLProcessor
::mungeCaseType($name)) {
297 $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor
::mungeCaseType($name));
298 CRM_Core_Error
::fatal($errorMessage);
299 // throw new CRM_Core_Exception($errorMessage);
301 $caseTypes[$name] = array(
302 'module' => E
::LONG_NAME
,
310 * (Delegated) Implements hook_civicrm_angularModules().
312 * Find any and return any files matching "ang/*.ang.php"
314 * Note: This hook only runs in CiviCRM 4.5+.
316 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
318 function _afform_civix_civicrm_angularModules(&$angularModules) {
319 if (!is_dir(__DIR__
. '/ang')) {
323 $files = _afform_civix_glob(__DIR__
. '/ang/*.ang.php');
324 foreach ($files as $file) {
325 $name = preg_replace(':\.ang\.php$:', '', basename($file));
326 $module = include $file;
327 if (empty($module['ext'])) {
328 $module['ext'] = E
::LONG_NAME
;
330 $angularModules[$name] = $module;
335 * Glob wrapper which is guaranteed to return an array.
337 * The documentation for glob() says, "On some systems it is impossible to
338 * distinguish between empty match and an error." Anecdotally, the return
339 * result for an empty match is sometimes array() and sometimes FALSE.
340 * This wrapper provides consistency.
342 * @link http://php.net/glob
343 * @param string $pattern
344 * @return array, possibly empty
346 function _afform_civix_glob($pattern) {
347 $result = glob($pattern);
348 return is_array($result) ?
$result : array();
352 * Inserts a navigation menu item at a given place in the hierarchy.
354 * @param array $menu - menu hierarchy
355 * @param string $path - path to parent of this item, e.g. 'my_extension/submenu'
356 * 'Mailing', or 'Administer/System Settings'
357 * @param array $item - the item to insert (parent/child attributes will be
360 function _afform_civix_insert_navigation_menu(&$menu, $path, $item) {
361 // If we are done going down the path, insert menu
364 'attributes' => array_merge(array(
365 'label' => CRM_Utils_Array
::value('name', $item),
372 // Find an recurse into the next level down
374 $path = explode('/', $path);
375 $first = array_shift($path);
376 foreach ($menu as $key => &$entry) {
377 if ($entry['attributes']['name'] == $first) {
378 if (!isset($entry['child'])) {
379 $entry['child'] = array();
381 $found = _afform_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key);
389 * (Delegated) Implements hook_civicrm_navigationMenu().
391 function _afform_civix_navigationMenu(&$nodes) {
392 if (!is_callable(array('CRM_Core_BAO_Navigation', 'fixNavigationMenu'))) {
393 _afform_civix_fixNavigationMenu($nodes);
398 * Given a navigation menu, generate navIDs for any items which are
401 function _afform_civix_fixNavigationMenu(&$nodes) {
403 array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) {
404 if ($key === 'navID') {
405 $maxNavID = max($maxNavID, $item);
408 _afform_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL);
411 function _afform_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) {
412 $origKeys = array_keys($nodes);
413 foreach ($origKeys as $origKey) {
414 if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) {
415 $nodes[$origKey]['attributes']['parentID'] = $parentID;
417 // If no navID, then assign navID and fix key.
418 if (!isset($nodes[$origKey]['attributes']['navID'])) {
419 $newKey = ++
$maxNavID;
420 $nodes[$origKey]['attributes']['navID'] = $newKey;
421 $nodes[$newKey] = $nodes[$origKey];
422 unset($nodes[$origKey]);
425 if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) {
426 _afform_civix_fixNavigationMenuItems($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']);
432 * (Delegated) Implements hook_civicrm_alterSettingsFolders().
434 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
436 function _afform_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
437 static $configured = FALSE;
443 $settingsDir = __DIR__
. DIRECTORY_SEPARATOR
. 'settings';
444 if (is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) {
445 $metaDataFolders[] = $settingsDir;
450 * (Delegated) Implements hook_civicrm_entityTypes().
452 * Find any *.entityType.php files, merge their content, and return.
454 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
457 function _afform_civix_civicrm_entityTypes(&$entityTypes) {
458 $entityTypes = array_merge($entityTypes, array (