From f4f17a836c9ca9109742339635e7582aed3ef362 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Tue, 14 Jul 2020 06:41:57 -0400 Subject: [PATCH] Add in functions to ensure that the profile type gets set to grant if it contains grant fields and also replace a deprecated function with non deprecated function --- CRM/Core/Controller.php | 2 +- .../BAO/GrantApplicationProfile.php | 170 +++++++++++++++++- grantapplications.php | 4 +- 3 files changed, 168 insertions(+), 8 deletions(-) diff --git a/CRM/Core/Controller.php b/CRM/Core/Controller.php index 32129ef..06accaf 100644 --- a/CRM/Core/Controller.php +++ b/CRM/Core/Controller.php @@ -442,7 +442,7 @@ public function addPages(&$stateMachine, $action = CRM_Core_Action::NONE) { $$stateName->setOptions($options); } if (property_exists($$stateName, 'urlPath')) { - $$stateName->urlPath = explode('/', (string) CRM_Utils_System::getUrlPath()); + $$stateName->urlPath = explode('/', (string) CRM_Utils_System::currentPath()); } $this->addPage($$stateName); $this->addAction($stateName, new HTML_QuickForm_Action_Direct()); diff --git a/CRM/Grantapplications/BAO/GrantApplicationProfile.php b/CRM/Grantapplications/BAO/GrantApplicationProfile.php index 16c874b..43f17a1 100755 --- a/CRM/Grantapplications/BAO/GrantApplicationProfile.php +++ b/CRM/Grantapplications/BAO/GrantApplicationProfile.php @@ -35,10 +35,10 @@ /** * This class contains function for Grant Applications - * */ class CRM_Grantapplications_BAO_GrantApplicationProfile extends CRM_Core_DAO { - static function getGrantFields() { + + public static function getGrantFields() { $exportableFields = self::exportableFields('Grant'); $skipFields = array('grant_id', 'grant_contact_id'); @@ -50,7 +50,7 @@ static function getGrantFields() { return $exportableFields; } - static function exportableFields() { + public static function exportableFields() { $grantFields = array( 'grant_status_id' => array( 'title' => ts('Grant Status'), @@ -87,10 +87,170 @@ static function exportableFields() { * * return array of enabled extensions */ - static function checkRelatedExtensions($name = 'biz.jmaconsulting.bugp') { + public static function checkRelatedExtensions($name = 'biz.jmaconsulting.bugp') { $enableDisable = NULL; $sql = "SELECT is_active FROM civicrm_extension WHERE full_name = '{name}'"; $enableDisable = CRM_Core_DAO::singleValueQuery($sql); return $enableDisable; } -} \ No newline at end of file + + /** + * Calculate the profile type 'group_type' as per profile fields. + * + * @param int $gId + * Profile id. + * @param bool $includeTypeValues + * @param int $ignoreFieldId + * Ignore particular profile field. + * + * @return array + * list of calculated group type + */ + public static function calculateGroupType($gId, $includeTypeValues = FALSE, $ignoreFieldId = NULL) { + //get the profile fields. + $ufFields = CRM_Core_BAO_UFGroup::getFields($gId, FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE); + return self::_calculateGroupType($ufFields, $includeTypeValues, $ignoreFieldId); + } + + /** + * Calculate the profile type 'group_type' as per profile fields. + * + * @param $ufFields + * @param bool $includeTypeValues + * @param int $ignoreFieldId + * Ignore perticular profile field. + * + * @return array + * list of calculated group type + */ + public static function _calculateGroupType($ufFields, $includeTypeValues = FALSE, $ignoreFieldId = NULL) { + $groupType = $groupTypeValues = $customFieldIds = []; + if (!empty($ufFields)) { + foreach ($ufFields as $fieldName => $fieldValue) { + //ignore field from group type when provided. + //in case of update profile field. + if ($ignoreFieldId && ($ignoreFieldId == $fieldValue['field_id'])) { + continue; + } + if (!in_array($fieldValue['field_type'], $groupType)) { + $groupType[$fieldValue['field_type']] = $fieldValue['field_type']; + } + + if ($includeTypeValues && ($fldId = CRM_Core_BAO_CustomField::getKeyID($fieldName))) { + $customFieldIds[$fldId] = $fldId; + } + } + } + + if (!empty($customFieldIds)) { + $query = 'SELECT DISTINCT(cg.id), cg.extends, cg.extends_entity_column_id, cg.extends_entity_column_value FROM civicrm_custom_group cg LEFT JOIN civicrm_custom_field cf ON cf.custom_group_id = cg.id WHERE cg.extends_entity_column_value IS NOT NULL AND cf.id IN (' . implode(',', $customFieldIds) . ')'; + + $customGroups = CRM_Core_DAO::executeQuery($query); + while ($customGroups->fetch()) { + if (!$customGroups->extends_entity_column_value) { + continue; + } + + $groupTypeName = "{$customGroups->extends}Type"; + if ($customGroups->extends == 'Participant' && $customGroups->extends_entity_column_id) { + $groupTypeName = CRM_Core_PseudoConstant::getName('CRM_Core_DAO_CustomGroup', 'extends_entity_column_id', $customGroups->extends_entity_column_id); + } + + foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $customGroups->extends_entity_column_value) as $val) { + if ($val) { + $groupTypeValues[$groupTypeName][$val] = $val; + } + } + } + + if (!empty($groupTypeValues)) { + $groupType = array_merge($groupType, $groupTypeValues); + } + } + + return $groupType; + } + + /** + * Update the profile type 'group_type' as per profile fields including group types and group subtype values. + * Build and store string like: group_type1,group_type2[VALUE_SEPERATOR]group_type1Type:1:2:3,group_type2Type:1:2 + * + * FIELDS GROUP_TYPE + * BirthDate + Email Individual,Contact + * BirthDate + Subject Individual,Activity + * BirthDate + Subject + SurveyOnlyField Individual,Activity\0ActivityType:28 + * BirthDate + Subject + SurveyOnlyField + PhoneOnlyField (Not allowed) + * BirthDate + SurveyOnlyField Individual,Activity\0ActivityType:28 + * BirthDate + Subject + SurveyOrPhoneField Individual,Activity\0ActivityType:2:28 + * BirthDate + SurveyOrPhoneField Individual,Activity\0ActivityType:2:28 + * BirthDate + SurveyOrPhoneField + SurveyOnlyField Individual,Activity\0ActivityType:2:28 + * BirthDate + StudentField + Subject + SurveyOnlyField Individual,Activity,Student\0ActivityType:28 + * + * @param int $gId + * @param array $groupTypes + * With key having group type names. + * + * @return bool + */ + public static function updateGroupTypes($gId, $groupTypes = []) { + if (!is_array($groupTypes) || !$gId) { + return FALSE; + } + + // If empty group types set group_type as 'null' + if (empty($groupTypes)) { + return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $gId, 'group_type', 'null'); + } + + $componentGroupTypes = ['Contribution', 'Participant', 'Membership', 'Activity', 'Case', 'Grant']; + $validGroupTypes = array_merge([ + 'Contact', + 'Individual', + 'Organization', + 'Household', + ], $componentGroupTypes, CRM_Contact_BAO_ContactType::subTypes()); + + $gTypes = $gTypeValues = []; + + $participantExtends = ['ParticipantRole', 'ParticipantEventName', 'ParticipantEventType']; + // Get valid group type and group subtypes + foreach ($groupTypes as $groupType => $value) { + if (in_array($groupType, $validGroupTypes) && !in_array($groupType, $gTypes)) { + $gTypes[] = $groupType; + } + + $subTypesOf = NULL; + + if (in_array($groupType, $participantExtends)) { + $subTypesOf = $groupType; + } + elseif (strpos($groupType, 'Type') > 0) { + $subTypesOf = substr($groupType, 0, strpos($groupType, 'Type')); + } + else { + continue; + } + + if (!empty($value) && + (in_array($subTypesOf, $componentGroupTypes) || + in_array($subTypesOf, $participantExtends) + ) + ) { + $gTypeValues[$subTypesOf] = $groupType . ":" . implode(':', $value); + } + } + + if (empty($gTypes)) { + return FALSE; + } + + // Build String to store group types and group subtypes + $groupTypeString = implode(',', $gTypes); + if (!empty($gTypeValues)) { + $groupTypeString .= CRM_Core_DAO::VALUE_SEPARATOR . implode(',', $gTypeValues); + } + + return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_UFGroup', $gId, 'group_type', $groupTypeString); + } + +} diff --git a/grantapplications.php b/grantapplications.php index 458f036..c30910e 100644 --- a/grantapplications.php +++ b/grantapplications.php @@ -599,8 +599,8 @@ function grantapplications_civicrm_alterUFFields(&$fields) { function grantapplications_civicrm_post($entity, $op, $id, $object) { if ($entity === 'UFField' && in_array($op, ['edit', 'create'])) { - $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($object->uf_group_id, TRUE); - CRM_Core_BAO_UFGroup::updateGroupTypes($object->uf_group_id, $fieldsType); + $fieldsType = CRM_Grantapplications_BAO_GrantApplicationProfile::calculateGroupType($object->uf_group_id, TRUE); + CRM_Grantapplications_BAO_GrantApplicationProfile::updateGroupTypes($object->uf_group_id, $fieldsType); } }