From b5a09f67b218f45f057cca8102cc188d7c1d8730 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 11 Mar 2020 19:01:24 +1300 Subject: [PATCH] Fix smart group custom field check to cope with api error As we have added new fields to the saved_search table this will lead to a fatal if it runs before the database update. This catches that error and handles it --- CRM/Utils/Check/Component/Schema.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/CRM/Utils/Check/Component/Schema.php b/CRM/Utils/Check/Component/Schema.php index c4b3a387ae..13803b63c8 100644 --- a/CRM/Utils/Check/Component/Schema.php +++ b/CRM/Utils/Check/Component/Schema.php @@ -89,15 +89,29 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { } /** + * Check that no smart groups exist that contain deleted custom fields. + * * @return array */ public function checkSmartGroupCustomFieldCriteria() { $messages = $problematicSG = []; $customFieldIds = array_keys(CRM_Core_BAO_CustomField::getFields('ANY', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE)); - $smartGroups = civicrm_api3('SavedSearch', 'get', [ - 'sequential' => 1, - 'options' => ['limit' => 0], - ]); + try { + $smartGroups = civicrm_api3('SavedSearch', 'get', [ + 'sequential' => 1, + 'options' => ['limit' => 0], + ]); + } + catch (CiviCRM_API3_Exception $e) { + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__, + ts('The smart group check was unable to run. This is likely to because a database upgrade is pending.'), + ts('Smart Group check did not run'), + \Psr\Log\LogLevel::INFO, + 'fa-server' + ); + return $messages; + } if (empty($smartGroups['values'])) { return $messages; } @@ -106,9 +120,9 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { continue; } foreach ($group['form_values'] as $formValues) { - if (isset($formValues[0]) && (substr($formValues[0], 0, 7) == 'custom_')) { + if (isset($formValues[0]) && (strpos($formValues[0], 'custom_') === 0)) { list(, $customFieldID) = explode('custom_', $formValues[0]); - if (!in_array($customFieldID, $customFieldIds)) { + if (!in_array($customFieldID, $customFieldIds, TRUE)) { $problematicSG[CRM_Contact_BAO_SavedSearch::getName($group['id'], 'id')] = [ 'title' => CRM_Contact_BAO_SavedSearch::getName($group['id'], 'title'), 'cfid' => $customFieldID, @@ -135,7 +149,7 @@ class CRM_Utils_Check_Component_Schema extends CRM_Utils_Check_Component { 2 => $customField['label'], ]); } - catch (Exception $e) { + catch (CiviCRM_API3_Exception $e) { $fieldName = ' - Deleted - '; } } -- 2.25.1