diff --git a/CRM/Wci/Upgrader.php b/CRM/Wci/Upgrader.php index b56d35e..6b9ba5a 100644 --- a/CRM/Wci/Upgrader.php +++ b/CRM/Wci/Upgrader.php @@ -25,7 +25,7 @@ /** * Collection of upgrade steps */ -class CRM_Wci_Upgrader extends CRM_Wci_Upgrader_Base { +class CRM_Wci_Upgrader extends CRM_Extension_Upgrader_Base { // By convention, functions that look like "function upgrade_NNNN()" are // upgrade tasks. They are executed in order (like Drupal's hook_update_N). diff --git a/CRM/Wci/Upgrader/Base.php b/CRM/Wci/Upgrader/Base.php deleted file mode 100644 index 64f2fc2..0000000 --- a/CRM/Wci/Upgrader/Base.php +++ /dev/null @@ -1,320 +0,0 @@ -ctx = array_shift($args); - $instance->queue = $instance->ctx->queue; - $method = array_shift($args); - return call_user_func_array(array($instance, $method), $args); - } - - public function __construct($extensionName, $extensionDir) { - $this->extensionName = $extensionName; - $this->extensionDir = $extensionDir; - } - - // ******** Task helpers ******** - - /** - * Run a CustomData file - * - * @param string $relativePath the CustomData XML file path (relative to this extension's dir) - * @return bool - */ - public function executeCustomDataFile($relativePath) { - $xml_file = $this->extensionDir . '/' . $relativePath; - return $this->executeCustomDataFileByAbsPath($xml_file); - } - - /** - * Run a CustomData file - * - * @param string $xml_file the CustomData XML file path (absolute path) - * @return bool - */ - protected static function executeCustomDataFileByAbsPath($xml_file) { - require_once 'CRM/Utils/Migrate/Import.php'; - $import = new CRM_Utils_Migrate_Import(); - $import->run($xml_file); - return TRUE; - } - - /** - * Run a SQL file - * - * @param string $relativePath the SQL file path (relative to this extension's dir) - * @return bool - */ - public function executeSqlFile($relativePath) { - CRM_Utils_File::sourceSQLFile( - CIVICRM_DSN, - $this->extensionDir . '/' . $relativePath - ); - return TRUE; - } - - /** - * Run one SQL query - * - * This is just a wrapper for CRM_Core_DAO::executeSql, but it - * provides syntatic sugar for queueing several tasks that - * run different queries - */ - public function executeSql($query, $params = array()) { - // FIXME verify that we raise an exception on error - CRM_Core_DAO::executeSql($query, $params); - return TRUE; - } - - /** - * Syntatic sugar for enqueuing a task which calls a function - * in this class. The task is weighted so that it is processed - * as part of the currently-pending revision. - * - * After passing the $funcName, you can also pass parameters that will go to - * the function. Note that all params must be serializable. - */ - public function addTask($title) { - $args = func_get_args(); - $title = array_shift($args); - $task = new CRM_Queue_Task( - array(get_class($this), '_queueAdapter'), - $args, - $title - ); - return $this->queue->createItem($task, array('weight' => -1)); - } - - // ******** Revision-tracking helpers ******** - - /** - * Determine if there are any pending revisions - * - * @return bool - */ - public function hasPendingRevisions() { - $revisions = $this->getRevisions(); - $currentRevision = $this->getCurrentRevision(); - - if (empty($revisions)) { - return FALSE; - } - if (empty($currentRevision)) { - return TRUE; - } - - return ($currentRevision < max($revisions)); - } - - /** - * Add any pending revisions to the queue - */ - public function enqueuePendingRevisions(CRM_Queue_Queue $queue) { - $this->queue = $queue; - - $currentRevision = $this->getCurrentRevision(); - foreach ($this->getRevisions() as $revision) { - if ($revision > $currentRevision) { - $title = ts('Upgrade %1 to revision %2', array( - 1 => $this->extensionName, - 2 => $revision, - )); - - // note: don't use addTask() because it sets weight=-1 - - $task = new CRM_Queue_Task( - array(get_class($this), '_queueAdapter'), - array('upgrade_' . $revision), - $title - ); - $this->queue->createItem($task); - - $task = new CRM_Queue_Task( - array(get_class($this), '_queueAdapter'), - array('setCurrentRevision', $revision), - $title - ); - $this->queue->createItem($task); - } - } - } - - /** - * Get a list of revisions - * - * @return array(revisionNumbers) sorted numerically - */ - public function getRevisions() { - if (! is_array($this->revisions)) { - $this->revisions = array(); - - $clazz = new ReflectionClass(get_class($this)); - $methods = $clazz->getMethods(); - foreach ($methods as $method) { - if (preg_match('/^upgrade_(.*)/', $method->name, $matches)) { - $this->revisions[] = $matches[1]; - } - } - sort($this->revisions, SORT_NUMERIC); - } - - return $this->revisions; - } - - public function getCurrentRevision() { - // return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName); - $key = $this->extensionName . ':version'; - return CRM_Core_BAO_Setting::getItem('Extension', $key); - } - - public function setCurrentRevision($revision) { - // We call this during hook_civicrm_install, but the underlying SQL - // UPDATE fails because the extension record hasn't been INSERTed yet. - // Instead, track revisions in our own namespace. - // CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision); - - $key = $this->extensionName . ':version'; - CRM_Core_BAO_Setting::setItem($revision, 'Extension', $key); - return TRUE; - } - - // ******** Hook delegates ******** - - public function onInstall() { - $files = glob($this->extensionDir . '/sql/*_install.sql'); - if (is_array($files)) { - foreach ($files as $file) { - CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); - } - } - $files = glob($this->extensionDir . '/xml/*_install.xml'); - if (is_array($files)) { - foreach ($files as $file) { - $this->executeCustomDataFileByAbsPath($file); - } - } - if (is_callable(array($this, 'install'))) { - $this->install(); - } - $revisions = $this->getRevisions(); - if (!empty($revisions)) { - $this->setCurrentRevision(max($revisions)); - } - } - - public function onUninstall() { - if (is_callable(array($this, 'uninstall'))) { - $this->uninstall(); - } - $files = glob($this->extensionDir . '/sql/*_uninstall.sql'); - if (is_array($files)) { - foreach ($files as $file) { - CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); - } - } - $this->setCurrentRevision(NULL); - } - - public function onEnable() { - // stub for possible future use - if (is_callable(array($this, 'enable'))) { - $this->enable(); - } - } - - public function onDisable() { - // stub for possible future use - if (is_callable(array($this, 'disable'))) { - $this->disable(); - } - } - - public function onUpgrade($op, CRM_Queue_Queue $queue = NULL) { - switch($op) { - case 'check': - return array($this->hasPendingRevisions()); - case 'enqueue': - return $this->enqueuePendingRevisions($queue); - default: - } - } -} diff --git a/info.xml b/info.xml index 5c5db17..9791b37 100644 --- a/info.xml +++ b/info.xml @@ -12,13 +12,24 @@ 1.0-rc4 beta - 4.4 + 5.38 Contributed by Zyxware Technologies - www.zyxware.com CRM/Wci + 23.02.1 https://github.com/zyxware/civiwci/wiki + + menu-xml@1.0.0 + setting-php@1.0.0 + smarty-v2@1.0.1 + + + + + + CRM_Wci_Upgrader diff --git a/mixin/menu-xml@1.0.0.mixin.php b/mixin/menu-xml@1.0.0.mixin.php new file mode 100644 index 0000000..4c0b227 --- /dev/null +++ b/mixin/menu-xml@1.0.0.mixin.php @@ -0,0 +1,31 @@ +addListener('hook_civicrm_xmlMenu', function ($e) use ($mixInfo) { + if (!$mixInfo->isActive()) { + return; + } + + $files = (array) glob($mixInfo->getPath('xml/Menu/*.xml')); + foreach ($files as $file) { + $e->files[] = $file; + } + }); + +}; diff --git a/mixin/polyfill.php b/mixin/polyfill.php new file mode 100644 index 0000000..17ba1df --- /dev/null +++ b/mixin/polyfill.php @@ -0,0 +1,101 @@ +')) { + $mixinVers[$name] = $ver; + } + } + $mixins = []; + foreach ($mixinVers as $name => $ver) { + $mixins[] = "$name@$ver"; + } + + // Imitate CRM_Extension_MixInfo. + $mixInfo = new class() { + + /** + * @var string + */ + public $longName; + + /** + * @var string + */ + public $shortName; + + public $_basePath; + + public function getPath($file = NULL) { + return $this->_basePath . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file)); + } + + public function isActive() { + return \CRM_Extension_System::singleton()->getMapper()->isActiveModule($this->shortName); + } + + }; + $mixInfo->longName = $longName; + $mixInfo->shortName = $shortName; + $mixInfo->_basePath = $basePath; + + // Imitate CRM_Extension_BootCache. + $bootCache = new class() { + + public function define($name, $callback) { + $envId = \CRM_Core_Config_Runtime::getId(); + $oldExtCachePath = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php"); + $stat = stat($oldExtCachePath); + $file = Civi::paths()->getPath('[civicrm.compile]/CachedMixin.' . md5($name . ($stat['mtime'] ?? 0)) . '.php'); + if (file_exists($file)) { + return include $file; + } + else { + $data = $callback(); + file_put_contents($file, '<' . "?php\nreturn " . var_export($data, 1) . ';'); + return $data; + } + } + + }; + + // Imitate CRM_Extension_MixinLoader::run() + // Parse all live mixins before trying to scan any classes. + global $_CIVIX_MIXIN_POLYFILL; + foreach ($mixins as $mixin) { + // If the exact same mixin is defined by multiple exts, just use the first one. + if (!isset($_CIVIX_MIXIN_POLYFILL[$mixin])) { + $_CIVIX_MIXIN_POLYFILL[$mixin] = include_once $basePath . '/mixin/' . $mixin . '.mixin.php'; + } + } + foreach ($mixins as $mixin) { + // If there's trickery about installs/uninstalls/resets, then we may need to register a second time. + if (!isset(\Civi::$statics[$longName][$mixin])) { + \Civi::$statics[$longName][$mixin] = 1; + $func = $_CIVIX_MIXIN_POLYFILL[$mixin]; + $func($mixInfo, $bootCache); + } + } +}; diff --git a/mixin/setting-php@1.0.0.mixin.php b/mixin/setting-php@1.0.0.mixin.php new file mode 100644 index 0000000..7195af4 --- /dev/null +++ b/mixin/setting-php@1.0.0.mixin.php @@ -0,0 +1,32 @@ +addListener('hook_civicrm_alterSettingsFolders', function ($e) use ($mixInfo) { + // When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically. + if (!$mixInfo->isActive()) { + return; + } + + $settingsDir = $mixInfo->getPath('settings'); + if (!in_array($settingsDir, $e->settingsFolders) && is_dir($settingsDir)) { + $e->settingsFolders[] = $settingsDir; + } + }); + +}; diff --git a/mixin/smarty-v2@1.0.1.mixin.php b/mixin/smarty-v2@1.0.1.mixin.php new file mode 100644 index 0000000..5972dbd --- /dev/null +++ b/mixin/smarty-v2@1.0.1.mixin.php @@ -0,0 +1,51 @@ +getPath('templates'); + if (!file_exists($dir)) { + return; + } + + $register = function() use ($dir) { + // This implementation has a theoretical edge-case bug on older versions of CiviCRM where a template could + // be registered more than once. + CRM_Core_Smarty::singleton()->addTemplateDir($dir); + }; + + // Let's figure out what environment we're in -- so that we know the best way to call $register(). + + if (!empty($GLOBALS['_CIVIX_MIXIN_POLYFILL'])) { + // Polyfill Loader (v<=5.45): We're already in the middle of firing `hook_config`. + if ($mixInfo->isActive()) { + $register(); + } + return; + } + + if (CRM_Extension_System::singleton()->getManager()->extensionIsBeingInstalledOrEnabled($mixInfo->longName)) { + // New Install, Standard Loader: The extension has just been enabled, and we're now setting it up. + // System has already booted. New templates may be needed for upcoming installation steps. + $register(); + return; + } + + // Typical Pageview, Standard Loader: Defer the actual registration for a moment -- to ensure that Smarty is online. + \Civi::dispatcher()->addListener('hook_civicrm_config', function() use ($mixInfo, $register) { + if ($mixInfo->isActive()) { + $register(); + } + }); + +}; diff --git a/wci.civix.php b/wci.civix.php index c9c2609..fcb597f 100644 --- a/wci.civix.php +++ b/wci.civix.php @@ -1,282 +1,162 @@ template_dir ) ) { - array_unshift( $template->template_dir, $extDir ); - } else { - $template->template_dir = array( $extDir, $template->template_dir ); +class CRM_Wci_ExtensionUtil { + const SHORT_NAME = 'wci'; + const LONG_NAME = 'com.zyxware.civiwci'; + const CLASS_PREFIX = 'CRM_Wci'; + + /** + * Translate a string using the extension's domain. + * + * If the extension doesn't have a specific translation + * for the string, fallback to the default translations. + * + * @param string $text + * Canonical message text (generally en_US). + * @param array $params + * @return string + * Translated text. + * @see ts + */ + public static function ts($text, $params = []): string { + if (!array_key_exists('domain', $params)) { + $params['domain'] = [self::LONG_NAME, NULL]; + } + return ts($text, $params); } - $include_path = $extRoot . PATH_SEPARATOR . get_include_path( ); - set_include_path( $include_path ); -} - -/** - * (Delegated) Implementation of hook_civicrm_xmlMenu - * - * @param $files array(string) - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu - */ -function _wci_civix_civicrm_xmlMenu(&$files) { - foreach (_wci_civix_glob(__DIR__ . '/xml/Menu/*.xml') as $file) { - $files[] = $file; + /** + * Get the URL of a resource file (in this extension). + * + * @param string|NULL $file + * Ex: NULL. + * Ex: 'css/foo.css'. + * @return string + * Ex: 'http://example.org/sites/default/ext/org.example.foo'. + * Ex: 'http://example.org/sites/default/ext/org.example.foo/css/foo.css'. + */ + public static function url($file = NULL): string { + if ($file === NULL) { + return rtrim(CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME), '/'); + } + return CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME, $file); } -} -/** - * Implementation of hook_civicrm_install - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install - */ -function _wci_civix_civicrm_install() { - _wci_civix_civicrm_config(); - if ($upgrader = _wci_civix_upgrader()) { - $upgrader->onInstall(); + /** + * Get the path of a resource file (in this extension). + * + * @param string|NULL $file + * Ex: NULL. + * Ex: 'css/foo.css'. + * @return string + * Ex: '/var/www/example.org/sites/default/ext/org.example.foo'. + * Ex: '/var/www/example.org/sites/default/ext/org.example.foo/css/foo.css'. + */ + public static function path($file = NULL) { + // return CRM_Core_Resources::singleton()->getPath(self::LONG_NAME, $file); + return __DIR__ . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file)); } -} -/** - * Implementation of hook_civicrm_uninstall - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall - */ -function _wci_civix_civicrm_uninstall() { - _wci_civix_civicrm_config(); - if ($upgrader = _wci_civix_upgrader()) { - $upgrader->onUninstall(); + /** + * Get the name of a class within this extension. + * + * @param string $suffix + * Ex: 'Page_HelloWorld' or 'Page\\HelloWorld'. + * @return string + * Ex: 'CRM_Foo_Page_HelloWorld'. + */ + public static function findClass($suffix) { + return self::CLASS_PREFIX . '_' . str_replace('\\', '_', $suffix); } -} -/** - * (Delegated) Implementation of hook_civicrm_enable - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable - */ -function _wci_civix_civicrm_enable() { - _wci_civix_civicrm_config(); - if ($upgrader = _wci_civix_upgrader()) { - if (is_callable(array($upgrader, 'onEnable'))) { - $upgrader->onEnable(); - } - } } -/** - * (Delegated) Implementation of hook_civicrm_disable - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable - * @return mixed - */ -function _wci_civix_civicrm_disable() { - _wci_civix_civicrm_config(); - if ($upgrader = _wci_civix_upgrader()) { - if (is_callable(array($upgrader, 'onDisable'))) { - $upgrader->onDisable(); - } - } -} +use CRM_Wci_ExtensionUtil as E; -/** - * (Delegated) Implementation of hook_civicrm_upgrade - * - * @param $op string, the type of operation being performed; 'check' or 'enqueue' - * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks - * - * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) - * for 'enqueue', returns void - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade - */ -function _wci_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { - if ($upgrader = _wci_civix_upgrader()) { - return $upgrader->onUpgrade($op, $queue); +function _wci_civix_mixin_polyfill() { + if (!class_exists('CRM_Extension_MixInfo')) { + $polyfill = __DIR__ . '/mixin/polyfill.php'; + (require $polyfill)(E::LONG_NAME, E::SHORT_NAME, E::path()); } } /** - * @return CRM_Wci_Upgrader - */ -function _wci_civix_upgrader() { - if (!file_exists(__DIR__.'/CRM/Wci/Upgrader.php')) { - return NULL; - } else { - return CRM_Wci_Upgrader_Base::instance(); - } -} - -/** - * Search directory tree for files which match a glob pattern - * - * Note: Dot-directories (like "..", ".git", or ".svn") will be ignored. - * Note: In Civi 4.3+, delegate to CRM_Utils_File::findFiles() + * (Delegated) Implements hook_civicrm_config(). * - * @param $dir string, base dir - * @param $pattern string, glob pattern, eg "*.txt" - * @return array(string) + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config */ -function _wci_civix_find_files($dir, $pattern) { - if (is_callable(array('CRM_Utils_File', 'findFiles'))) { - return CRM_Utils_File::findFiles($dir, $pattern); +function _wci_civix_civicrm_config($config = NULL) { + static $configured = FALSE; + if ($configured) { + return; } + $configured = TRUE; - $todos = array($dir); - $result = array(); - while (!empty($todos)) { - $subdir = array_shift($todos); - foreach (_wci_civix_glob("$subdir/$pattern") as $match) { - if (!is_dir($match)) { - $result[] = $match; - } - } - if ($dh = opendir($subdir)) { - while (FALSE !== ($entry = readdir($dh))) { - $path = $subdir . DIRECTORY_SEPARATOR . $entry; - if ($entry{0} == '.') { - } elseif (is_dir($path)) { - $todos[] = $path; - } - } - closedir($dh); - } - } - return $result; -} -/** - * (Delegated) Implementation of hook_civicrm_managed - * - * Find any *.mgd.php files, merge their content, and return. - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed - */ -function _wci_civix_civicrm_managed(&$entities) { - $mgdFiles = _wci_civix_find_files(__DIR__, '*.mgd.php'); - foreach ($mgdFiles as $file) { - $es = include $file; - foreach ($es as $e) { - if (empty($e['module'])) { - $e['module'] = 'com.zyxware.civiwci'; - } - $entities[] = $e; - } - } + $extRoot = __DIR__ . DIRECTORY_SEPARATOR; + $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); + set_include_path($include_path); + _wci_civix_mixin_polyfill(); } /** - * (Delegated) Implementation of hook_civicrm_caseTypes - * - * Find any and return any files matching "xml/case/*.xml" - * - * Note: This hook only runs in CiviCRM 4.4+. + * Implements hook_civicrm_install(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install */ -function _wci_civix_civicrm_caseTypes(&$caseTypes) { - if (!is_dir(__DIR__ . '/xml/case')) { - return; - } - - foreach (_wci_civix_glob(__DIR__ . '/xml/case/*.xml') as $file) { - $name = preg_replace('/\.xml$/', '', basename($file)); - if ($name != CRM_Case_XMLProcessor::mungeCaseType($name)) { - $errorMessage = sprintf("Case-type file name is malformed (%s vs %s)", $name, CRM_Case_XMLProcessor::mungeCaseType($name)); - CRM_Core_Error::fatal($errorMessage); - // throw new CRM_Core_Exception($errorMessage); - } - $caseTypes[$name] = array( - 'module' => 'com.zyxware.civiwci', - 'name' => $name, - 'file' => $file, - ); - } +function _wci_civix_civicrm_install() { + _wci_civix_civicrm_config(); + _wci_civix_mixin_polyfill(); } /** - * Glob wrapper which is guaranteed to return an array. - * - * The documentation for glob() says, "On some systems it is impossible to - * distinguish between empty match and an error." Anecdotally, the return - * result for an empty match is sometimes array() and sometimes FALSE. - * This wrapper provides consistency. + * (Delegated) Implements hook_civicrm_enable(). * - * @link http://php.net/glob - * @param string $pattern - * @return array, possibly empty + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable */ -function _wci_civix_glob($pattern) { - $result = glob($pattern); - return is_array($result) ? $result : array(); +function _wci_civix_civicrm_enable(): void { + _wci_civix_civicrm_config(); + _wci_civix_mixin_polyfill(); } /** - * Inserts a navigation menu item at a given place in the hierarchy + * Inserts a navigation menu item at a given place in the hierarchy. * - * $menu - menu hierarchy - * $path - path where insertion should happen (ie. Administer/System Settings) - * $item - menu you need to insert (parent/child attributes will be filled for you) - * $parentId - used internally to recurse in the menu structure + * @param array $menu - menu hierarchy + * @param string $path - path to parent of this item, e.g. 'my_extension/submenu' + * 'Mailing', or 'Administer/System Settings' + * @param array $item - the item to insert (parent/child attributes will be + * filled for you) + * + * @return bool */ -function _wci_civix_insert_navigation_menu(&$menu, $path, $item, $parentId = NULL) { - static $navId; - +function _wci_civix_insert_navigation_menu(&$menu, $path, $item) { // If we are done going down the path, insert menu if (empty($path)) { - if (!$navId) $navId = CRM_Core_DAO::singleValueQuery("SELECT max(id) FROM civicrm_navigation"); - $navId ++; - $menu[$navId] = array ( - 'attributes' => array_merge($item, array( - 'label' => CRM_Utils_Array::value('name', $item), - 'active' => 1, - 'parentID' => $parentId, - 'navID' => $navId, - )) - ); - return true; - } else { + $menu[] = [ + 'attributes' => array_merge([ + 'label' => $item['name'] ?? NULL, + 'active' => 1, + ], $item), + ]; + return TRUE; + } + else { // Find an recurse into the next level down - $found = false; + $found = FALSE; $path = explode('/', $path); $first = array_shift($path); foreach ($menu as $key => &$entry) { if ($entry['attributes']['name'] == $first) { - if (!$entry['child']) $entry['child'] = array(); - $found = _wci_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key); + if (!isset($entry['child'])) { + $entry['child'] = []; + } + $found = _wci_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item); } } return $found; @@ -284,17 +164,44 @@ function _wci_civix_insert_navigation_menu(&$menu, $path, $item, $parentId = NUL } /** - * (Delegated) Implementation of hook_civicrm_alterSettingsFolders - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders + * (Delegated) Implements hook_civicrm_navigationMenu(). */ -function _wci_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { - static $configured = FALSE; - if ($configured) return; - $configured = TRUE; +function _wci_civix_navigationMenu(&$nodes) { + if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) { + _wci_civix_fixNavigationMenu($nodes); + } +} - $settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings'; - if(is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) { - $metaDataFolders[] = $settingsDir; +/** + * Given a navigation menu, generate navIDs for any items which are + * missing them. + */ +function _wci_civix_fixNavigationMenu(&$nodes) { + $maxNavID = 1; + array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) { + if ($key === 'navID') { + $maxNavID = max($maxNavID, $item); + } + }); + _wci_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL); +} + +function _wci_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) { + $origKeys = array_keys($nodes); + foreach ($origKeys as $origKey) { + if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) { + $nodes[$origKey]['attributes']['parentID'] = $parentID; + } + // If no navID, then assign navID and fix key. + if (!isset($nodes[$origKey]['attributes']['navID'])) { + $newKey = ++$maxNavID; + $nodes[$origKey]['attributes']['navID'] = $newKey; + $nodes[$newKey] = $nodes[$origKey]; + unset($nodes[$origKey]); + $origKey = $newKey; + } + if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) { + _wci_civix_fixNavigationMenuItems($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']); + } } } diff --git a/wci.php b/wci.php index b882919..74ae060 100644 --- a/wci.php +++ b/wci.php @@ -32,17 +32,6 @@ function wci_civicrm_config(&$config) { _wci_civix_civicrm_config($config); } -/** - * Implementation of hook_civicrm_xmlMenu - * - * @param $files array(string) - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu - */ -function wci_civicrm_xmlMenu(&$files) { - _wci_civix_civicrm_xmlMenu($files); -} - /** * Implementation of hook_civicrm_install * @@ -52,15 +41,6 @@ function wci_civicrm_install() { return _wci_civix_civicrm_install(); } -/** - * Implementation of hook_civicrm_uninstall - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall - */ -function wci_civicrm_uninstall() { - return _wci_civix_civicrm_uninstall(); -} - /** * Implementation of hook_civicrm_enable * @@ -70,64 +50,6 @@ function wci_civicrm_enable() { return _wci_civix_civicrm_enable(); } -/** - * Implementation of hook_civicrm_disable - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable - */ -function wci_civicrm_disable() { - return _wci_civix_civicrm_disable(); -} - -/** - * Implementation of hook_civicrm_upgrade - * - * @param $op string, the type of operation being performed; 'check' or 'enqueue' - * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks - * - * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) - * for 'enqueue', returns void - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade - */ -function wci_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { - return _wci_civix_civicrm_upgrade($op, $queue); -} - -/** - * Implementation of hook_civicrm_managed - * - * Generate a list of entities to create/deactivate/delete when this module - * is installed, disabled, uninstalled. - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed - */ -function wci_civicrm_managed(&$entities) { - return _wci_civix_civicrm_managed($entities); -} - -/** - * Implementation of hook_civicrm_caseTypes - * - * Generate a list of case-types - * - * Note: This hook only runs in CiviCRM 4.4+. - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes - */ -function wci_civicrm_caseTypes(&$caseTypes) { - _wci_civix_civicrm_caseTypes($caseTypes); -} - -/** - * Implementation of hook_civicrm_alterSettingsFolders - * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders - */ -function wci_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { - _wci_civix_civicrm_alterSettingsFolders($metaDataFolders); -} - function _getMenuKeyMax($menuArray) { $max = array(max(array_keys($menuArray))); foreach($menuArray as $v) {