Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Add in functions to ensure that the profile type gets set to grant if…
Browse files Browse the repository at this point in the history
… it contains grant fields and also replace a deprecated function with non deprecated function
  • Loading branch information
seamuslee001 authored and monishdeb committed Jul 14, 2020
1 parent c60cd11 commit f4f17a8
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
170 changes: 165 additions & 5 deletions CRM/Grantapplications/BAO/GrantApplicationProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'),
Expand Down Expand Up @@ -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;
}
}

/**
* 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);
}

}
4 changes: 2 additions & 2 deletions grantapplications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit f4f17a8

Please sign in to comment.