diff --git a/CRM/CiviDiscount/Upgrader/Base.php b/CRM/CiviDiscount/Upgrader/Base.php index a6c8d4b..74155dc 100644 --- a/CRM/CiviDiscount/Upgrader/Base.php +++ b/CRM/CiviDiscount/Upgrader/Base.php @@ -1,12 +1,15 @@ extensionDir . '/' . $relativePath + $this->extensionDir . DIRECTORY_SEPARATOR . $relativePath ); return TRUE; } /** - * Run one SQL query + * @param string $tplFile + * The SQL file path (relative to this extension's dir). + * Ex: "sql/mydata.mysql.tpl". + * @return bool + */ + public function executeSqlTemplate($tplFile) { + // Assign multilingual variable to Smarty. + $upgrade = new CRM_Upgrade_Form(); + + $tplFile = CRM_Utils_File::isAbsolute($tplFile) ? $tplFile : $this->extensionDir . DIRECTORY_SEPARATOR . $tplFile; + $smarty = CRM_Core_Smarty::singleton(); + $smarty->assign('domainID', CRM_Core_Config::domainID()); + CRM_Utils_File::sourceSQLFile( + CIVICRM_DSN, $smarty->fetch($tplFile), NULL, TRUE + ); + 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 @@ -116,13 +146,14 @@ public function executeSqlFile($relativePath) { */ public function executeSql($query, $params = array()) { // FIXME verify that we raise an exception on error - CRM_Core_DAO::executeSql($query, $params); + CRM_Core_DAO::executeQuery($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 + * 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 @@ -142,7 +173,7 @@ public function addTask($title) { // ******** Revision-tracking helpers ******** /** - * Determine if there are any pending revisions + * Determine if there are any pending revisions. * * @return bool */ @@ -161,7 +192,7 @@ public function hasPendingRevisions() { } /** - * Add any pending revisions to the queue + * Add any pending revisions to the queue. */ public function enqueuePendingRevisions(CRM_Queue_Queue $queue) { $this->queue = $queue; @@ -194,7 +225,7 @@ public function enqueuePendingRevisions(CRM_Queue_Queue $queue) { } /** - * Get a list of revisions + * Get a list of revisions. * * @return array(revisionNumbers) sorted numerically */ @@ -216,24 +247,42 @@ public function getRevisions() { } public function getCurrentRevision() { - // return CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName); + $revision = CRM_Core_BAO_Extension::getSchemaVersion($this->extensionName); + if (!$revision) { + $revision = $this->getCurrentRevisionDeprecated(); + } + return $revision; + } + + private function getCurrentRevisionDeprecated() { $key = $this->extensionName . ':version'; - return CRM_Core_BAO_Setting::getItem('Extension', $key); + if ($revision = CRM_Core_BAO_Setting::getItem('Extension', $key)) { + $this->revisionStorageIsDeprecated = TRUE; + } + return $revision; } 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); + CRM_Core_BAO_Extension::setSchemaVersion($this->extensionName, $revision); + // clean up legacy schema version store (CRM-19252) + $this->deleteDeprecatedRevision(); return TRUE; } + private function deleteDeprecatedRevision() { + if ($this->revisionStorageIsDeprecated) { + $setting = new CRM_Core_BAO_Setting(); + $setting->name = $this->extensionName . ':version'; + $setting->delete(); + CRM_Core_Error::debug_log_message("Migrated extension schema revision ID for {$this->extensionName} from civicrm_setting (deprecated) to civicrm_extension.\n"); + } + } + // ******** Hook delegates ******** + /** + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install + */ public function onInstall() { $files = glob($this->extensionDir . '/sql/*_install.sql'); if (is_array($files)) { @@ -241,6 +290,12 @@ public function onInstall() { CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); } } + $files = glob($this->extensionDir . '/sql/*_install.mysql.tpl'); + if (is_array($files)) { + foreach ($files as $file) { + $this->executeSqlTemplate($file); + } + } $files = glob($this->extensionDir . '/xml/*_install.xml'); if (is_array($files)) { foreach ($files as $file) { @@ -250,13 +305,31 @@ public function onInstall() { if (is_callable(array($this, 'install'))) { $this->install(); } + } + + /** + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall + */ + public function onPostInstall() { $revisions = $this->getRevisions(); if (!empty($revisions)) { $this->setCurrentRevision(max($revisions)); } + if (is_callable(array($this, 'postInstall'))) { + $this->postInstall(); + } } + /** + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall + */ public function onUninstall() { + $files = glob($this->extensionDir . '/sql/*_uninstall.mysql.tpl'); + if (is_array($files)) { + foreach ($files as $file) { + $this->executeSqlTemplate($file); + } + } if (is_callable(array($this, 'uninstall'))) { $this->uninstall(); } @@ -266,9 +339,11 @@ public function onUninstall() { CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, $file); } } - $this->setCurrentRevision(NULL); } + /** + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable + */ public function onEnable() { // stub for possible future use if (is_callable(array($this, 'enable'))) { @@ -276,6 +351,9 @@ public function onEnable() { } } + /** + * @see https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable + */ public function onDisable() { // stub for possible future use if (is_callable(array($this, 'disable'))) { @@ -287,9 +365,12 @@ 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/cividiscount.civix.php b/cividiscount.civix.php index c70b4e1..a732372 100644 --- a/cividiscount.civix.php +++ b/cividiscount.civix.php @@ -24,9 +24,9 @@ class CRM_CiviDiscount_ExtensionUtil { * Translated text. * @see ts */ - public static function ts($text, $params = array()) { + public static function ts($text, $params = []) { if (!array_key_exists('domain', $params)) { - $params['domain'] = array(self::LONG_NAME, NULL); + $params['domain'] = [self::LONG_NAME, NULL]; } return ts($text, $params); } @@ -82,7 +82,7 @@ public static function findClass($suffix) { /** * (Delegated) Implements hook_civicrm_config(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config */ function _cividiscount_civix_civicrm_config(&$config = NULL) { static $configured = FALSE; @@ -100,7 +100,7 @@ function _cividiscount_civix_civicrm_config(&$config = NULL) { array_unshift($template->template_dir, $extDir); } else { - $template->template_dir = array($extDir, $template->template_dir); + $template->template_dir = [$extDir, $template->template_dir]; } $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); @@ -112,7 +112,7 @@ function _cividiscount_civix_civicrm_config(&$config = NULL) { * * @param $files array(string) * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu */ function _cividiscount_civix_civicrm_xmlMenu(&$files) { foreach (_cividiscount_civix_glob(__DIR__ . '/xml/Menu/*.xml') as $file) { @@ -123,7 +123,7 @@ function _cividiscount_civix_civicrm_xmlMenu(&$files) { /** * Implements hook_civicrm_install(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install */ function _cividiscount_civix_civicrm_install() { _cividiscount_civix_civicrm_config(); @@ -135,12 +135,12 @@ function _cividiscount_civix_civicrm_install() { /** * Implements hook_civicrm_postInstall(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall */ function _cividiscount_civix_civicrm_postInstall() { _cividiscount_civix_civicrm_config(); if ($upgrader = _cividiscount_civix_upgrader()) { - if (is_callable(array($upgrader, 'onPostInstall'))) { + if (is_callable([$upgrader, 'onPostInstall'])) { $upgrader->onPostInstall(); } } @@ -149,7 +149,7 @@ function _cividiscount_civix_civicrm_postInstall() { /** * Implements hook_civicrm_uninstall(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall */ function _cividiscount_civix_civicrm_uninstall() { _cividiscount_civix_civicrm_config(); @@ -161,12 +161,12 @@ function _cividiscount_civix_civicrm_uninstall() { /** * (Delegated) Implements hook_civicrm_enable(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable */ function _cividiscount_civix_civicrm_enable() { _cividiscount_civix_civicrm_config(); if ($upgrader = _cividiscount_civix_upgrader()) { - if (is_callable(array($upgrader, 'onEnable'))) { + if (is_callable([$upgrader, 'onEnable'])) { $upgrader->onEnable(); } } @@ -175,13 +175,13 @@ function _cividiscount_civix_civicrm_enable() { /** * (Delegated) Implements hook_civicrm_disable(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable * @return mixed */ function _cividiscount_civix_civicrm_disable() { _cividiscount_civix_civicrm_config(); if ($upgrader = _cividiscount_civix_upgrader()) { - if (is_callable(array($upgrader, 'onDisable'))) { + if (is_callable([$upgrader, 'onDisable'])) { $upgrader->onDisable(); } } @@ -196,7 +196,7 @@ function _cividiscount_civix_civicrm_disable() { * @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 + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade */ function _cividiscount_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { if ($upgrader = _cividiscount_civix_upgrader()) { @@ -217,22 +217,23 @@ function _cividiscount_civix_upgrader() { } /** - * Search directory tree for files which match a glob pattern + * 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() * - * @param $dir string, base dir - * @param $pattern string, glob pattern, eg "*.txt" + * @param string $dir base dir + * @param string $pattern , glob pattern, eg "*.txt" + * * @return array(string) */ function _cividiscount_civix_find_files($dir, $pattern) { - if (is_callable(array('CRM_Utils_File', 'findFiles'))) { + if (is_callable(['CRM_Utils_File', 'findFiles'])) { return CRM_Utils_File::findFiles($dir, $pattern); } - $todos = array($dir); - $result = array(); + $todos = [$dir]; + $result = []; while (!empty($todos)) { $subdir = array_shift($todos); foreach (_cividiscount_civix_glob("$subdir/$pattern") as $match) { @@ -259,20 +260,21 @@ function _cividiscount_civix_find_files($dir, $pattern) { * * Find any *.mgd.php files, merge their content, and return. * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed */ function _cividiscount_civix_civicrm_managed(&$entities) { $mgdFiles = _cividiscount_civix_find_files(__DIR__, '*.mgd.php'); + sort($mgdFiles); foreach ($mgdFiles as $file) { $es = include $file; foreach ($es as $e) { if (empty($e['module'])) { $e['module'] = E::LONG_NAME; } - $entities[] = $e; if (empty($e['params']['version'])) { $e['params']['version'] = '3'; } + $entities[] = $e; } } } @@ -284,7 +286,7 @@ function _cividiscount_civix_civicrm_managed(&$entities) { * * Note: This hook only runs in CiviCRM 4.4+. * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes */ function _cividiscount_civix_civicrm_caseTypes(&$caseTypes) { if (!is_dir(__DIR__ . '/xml/case')) { @@ -295,14 +297,13 @@ function _cividiscount_civix_civicrm_caseTypes(&$caseTypes) { $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); + throw new CRM_Core_Exception($errorMessage); } - $caseTypes[$name] = array( + $caseTypes[$name] = [ 'module' => E::LONG_NAME, 'name' => $name, 'file' => $file, - ); + ]; } } @@ -313,7 +314,7 @@ function _cividiscount_civix_civicrm_caseTypes(&$caseTypes) { * * Note: This hook only runs in CiviCRM 4.5+. * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules */ function _cividiscount_civix_civicrm_angularModules(&$angularModules) { if (!is_dir(__DIR__ . '/ang')) { @@ -331,6 +332,25 @@ function _cividiscount_civix_civicrm_angularModules(&$angularModules) { } } +/** + * (Delegated) Implements hook_civicrm_themes(). + * + * Find any and return any files matching "*.theme.php" + */ +function _cividiscount_civix_civicrm_themes(&$themes) { + $files = _cividiscount_civix_glob(__DIR__ . '/*.theme.php'); + foreach ($files as $file) { + $themeMeta = include $file; + if (empty($themeMeta['name'])) { + $themeMeta['name'] = preg_replace(':\.theme\.php$:', '', basename($file)); + } + if (empty($themeMeta['ext'])) { + $themeMeta['ext'] = E::LONG_NAME; + } + $themes[$themeMeta['name']] = $themeMeta; + } +} + /** * Glob wrapper which is guaranteed to return an array. * @@ -341,29 +361,34 @@ function _cividiscount_civix_civicrm_angularModules(&$angularModules) { * * @link http://php.net/glob * @param string $pattern + * * @return array, possibly empty */ function _cividiscount_civix_glob($pattern) { $result = glob($pattern); - return is_array($result) ? $result : array(); + return is_array($result) ? $result : []; } /** * Inserts a navigation menu item at a given place in the hierarchy. * * @param array $menu - menu hierarchy - * @param string $path - path where insertion should happen (ie. Administer/System Settings) - * @param array $item - menu you need to insert (parent/child attributes will be filled for you) + * @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 _cividiscount_civix_insert_navigation_menu(&$menu, $path, $item) { // If we are done going down the path, insert menu if (empty($path)) { - $menu[] = array( - 'attributes' => array_merge(array( + $menu[] = [ + 'attributes' => array_merge([ 'label' => CRM_Utils_Array::value('name', $item), 'active' => 1, - ), $item), - ); + ], $item), + ]; return TRUE; } else { @@ -374,9 +399,9 @@ function _cividiscount_civix_insert_navigation_menu(&$menu, $path, $item) { foreach ($menu as $key => &$entry) { if ($entry['attributes']['name'] == $first) { if (!isset($entry['child'])) { - $entry['child'] = array(); + $entry['child'] = []; } - $found = _cividiscount_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item, $key); + $found = _cividiscount_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item); } } return $found; @@ -387,7 +412,7 @@ function _cividiscount_civix_insert_navigation_menu(&$menu, $path, $item) { * (Delegated) Implements hook_civicrm_navigationMenu(). */ function _cividiscount_civix_navigationMenu(&$nodes) { - if (!is_callable(array('CRM_Core_BAO_Navigation', 'fixNavigationMenu'))) { + if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) { _cividiscount_civix_fixNavigationMenu($nodes); } } @@ -429,17 +454,24 @@ function _cividiscount_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parent /** * (Delegated) Implements hook_civicrm_alterSettingsFolders(). * - * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders */ function _cividiscount_civix_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { - static $configured = FALSE; - if ($configured) { - return; - } - $configured = TRUE; - $settingsDir = __DIR__ . DIRECTORY_SEPARATOR . 'settings'; - if (is_dir($settingsDir) && !in_array($settingsDir, $metaDataFolders)) { + if (!in_array($settingsDir, $metaDataFolders) && is_dir($settingsDir)) { $metaDataFolders[] = $settingsDir; } } + +/** + * (Delegated) Implements hook_civicrm_entityTypes(). + * + * Find any *.entityType.php files, merge their content, and return. + * + * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes + */ + +function _cividiscount_civix_civicrm_entityTypes(&$entityTypes) { + $entityTypes = array_merge($entityTypes, array ( + )); +} diff --git a/info.xml b/info.xml index a102a3d..374b20e 100644 --- a/info.xml +++ b/info.xml @@ -18,10 +18,10 @@ CiviCRM LLC info@civicrm.org - 2018-12-17 - 3.7 + 2020-01-03 + 3.8 - 5.8 + 5.19 For support, please ask on chat.civicrm.org diff --git a/templates/CRM/CiviDiscount/Page/List.tpl b/templates/CRM/CiviDiscount/Page/List.tpl index 9c51f89..41f51ba 100644 --- a/templates/CRM/CiviDiscount/Page/List.tpl +++ b/templates/CRM/CiviDiscount/Page/List.tpl @@ -47,7 +47,7 @@ {foreach from=$rows item=row} - {$row.code}
{$row.description} + {$row.code}
{$row.description|purify} {if $row.amount_type eq '1'}{$row.amount} %{else}{$row.amount|crmMoney}{/if} {$row.count_use} / {if $row.count_max eq 0}{ts}Unlimited{/ts}{else}{$row.count_max}{/if} diff --git a/templates/CRM/CiviDiscount/Page/View.tpl b/templates/CRM/CiviDiscount/Page/View.tpl index 639ed0a..4ef873d 100644 --- a/templates/CRM/CiviDiscount/Page/View.tpl +++ b/templates/CRM/CiviDiscount/Page/View.tpl @@ -49,7 +49,7 @@ {ts}Description{/ts} - {$description} + {$description|purify} {ts}Discount{/ts}