/** * Get contact membership record. * * This api is used for finding an existing membership record. * This api will also return the mebership records for the contacts * having mebership based on the relationship with the direct members. * * @param Array $params key/value pairs for contact_id and some * options affecting the desired results; has legacy support * for just passing the contact_id itself as the argument * * @return Array of all found membership property values. * @access public */ function civicrm_membership_contact_get(&$params) { _civicrm_initialize(); $activeOnly = false; if (is_array($params)) { $contactID = CRM_Utils_Array::value('contact_id', $params); $activeOnly = CRM_Utils_Array::value('active_only', $params, false); if ($activeOnly == 1) { $activeOnly = true; } else { $activeOnly = false; } } elseif (CRM_Utils_Rule::integer($params)) { $contactID = $params; } else { return civicrm_create_error('Paramers can be only of type array or integer'); } if (empty($contactID)) { return civicrm_create_error('Invalid value for ContactID.'); } // get the membership for the given contact ID require_once 'CRM/Member/BAO/Membership.php'; $membership = array('contact_id' => $contactID); $membershipValues = array(); CRM_Member_BAO_Membership::getValues($membership, $membershipValues, $activeOnly); $recordCount = 0; if (empty($membershipValues)) { # No results is NOT an error! # return civicrm_create_error('No memberships for this contact.'); $membershipValues['record_count'] = $recordCount; return $membershipValues; } $members[$contactID] = array(); $relationships = array(); foreach ($membershipValues as $membershipId => $values) { // populate the membership type name for the membership type id require_once 'CRM/Member/BAO/MembershipType.php'; $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { $relationships[$membershipType['relationship_type_id']] = $membershipId; } // populating relationship type name. require_once 'CRM/Contact/BAO/RelationshipType.php'; $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); if ($relationshipType->find(true)) { $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; } require_once 'CRM/Core/BAO/CustomGroup.php'; $groupTree =& CRM_Core_BAO_CustomGroup::getTree('Membership', CRM_Core_DAO::$_nullObject, $membershipId, false, $values['membership_type_id']); $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, CRM_Core_DAO::$_nullObject); $defaults = array(); CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults); if (!empty($defaults)) { foreach ($defaults as $key => $val) { $membershipValues[$membershipId][$key] = $val; } } $recordCount++; } $members[$contactID] = $membershipValues; // populating contacts in members array based on their relationship with direct members. require_once 'CRM/Contact/BAO/Relationship.php'; if (!empty($relationships)) { foreach ($relationships as $relTypeId => $membershipId) { // As members are not direct members, there should not be // membership id in the result array. unset($membershipValues[$membershipId]['id']); $relationship = new CRM_Contact_BAO_Relationship(); $relationship->contact_id_b = $contactID; $relationship->relationship_type_id = $relTypeId; if ($relationship->find()) { while ($relationship->fetch()) { clone $relationship; $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; $members[$contactID][$relationship->contact_id_a] = $membershipValues[$membershipId]; } } $recordCount++; } } $members['record_count'] = $recordCount; return $members; }
/** * Build the mapping form. * * @param CRM_Core_Form $form * @param string $mappingType * (Export/Import/Search Builder). * @param int $mappingId * @param int $columnNo * @param int $blockCount * (no of blocks shown). * @param NULL $exportMode * * @return void */ public static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = NULL, $columnNo, $blockCount = 3, $exportMode = NULL) { if ($mappingType == 'Export') { $name = "Map"; $columnCount = array('1' => $columnNo); } elseif ($mappingType == 'Search Builder') { $name = "Builder"; $columnCount = $columnNo; } //get the saved mapping details if ($mappingType == 'Export') { $form->applyFilter('saveMappingName', 'trim'); //to save the current mappings if (!isset($mappingId)) { $saveDetailsName = ts('Save this field mapping'); $form->add('text', 'saveMappingName', ts('Name')); $form->add('text', 'saveMappingDesc', ts('Description')); } else { $form->assign('loadedMapping', $mappingId); $params = array('id' => $mappingId); $temp = array(); $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); $form->assign('savedName', $mappingDetails->name); $form->add('hidden', 'mappingId', $mappingId); $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); $saveDetailsName = ts('Save as a new field mapping'); $form->add('text', 'saveMappingName', ts('Name')); $form->add('text', 'saveMappingDesc', ts('Description')); } $form->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId')); } elseif ($mappingType == 'Search Builder') { $form->addElement('submit', 'addBlock', ts('Also include contacts where'), array('class' => 'submit-link')); } $defaults = array(); $hasLocationTypes = array(); $hasRelationTypes = array(); $fields = array(); if ($mappingType == 'Export') { $required = TRUE; } elseif ($mappingType == 'Search Builder') { $required = FALSE; } $contactType = array('Individual', 'Household', 'Organization'); foreach ($contactType as $value) { $contactFields = CRM_Contact_BAO_Contact::exportableFields($value, FALSE, $required); $contactFields = array_merge($contactFields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); // exclude the address options disabled in the Address Settings $fields[$value] = CRM_Core_BAO_Address::validateAddressOptions($contactFields); ksort($fields[$value]); if ($mappingType == 'Export') { $relationships = array(); $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $value); asort($relationshipTypes); foreach ($relationshipTypes as $key => $var) { list($type) = explode('_', $key); $relationships[$key]['title'] = $var; $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; $relationships[$key]['export'] = TRUE; $relationships[$key]['relationship_type_id'] = $type; $relationships[$key]['related'] = TRUE; $relationships[$key]['hasRelationType'] = 1; } if (!empty($relationships)) { $fields[$value] = array_merge($fields[$value], array('related' => array('title' => ts('- related contact info -'))), $relationships); } } } //get the current employer for mapping. if ($required) { $fields['Individual']['current_employer']['title'] = ts('Current Employer'); } // add component fields $compArray = array(); //we need to unset groups, tags, notes for component export if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) { foreach (array('groups', 'tags', 'notes') as $value) { unset($fields['Individual'][$value]); unset($fields['Household'][$value]); unset($fields['Organization'][$value]); } } if ($mappingType == 'Search Builder') { //build the common contact fields array. $fields['Contact'] = array(); foreach ($fields['Individual'] as $key => $value) { if (!empty($fields['Household'][$key]) && !empty($fields['Organization'][$key])) { $fields['Contact'][$key] = $value; unset($fields['Organization'][$key], $fields['Household'][$key], $fields['Individual'][$key]); } } if (array_key_exists('note', $fields['Contact'])) { $noteTitle = $fields['Contact']['note']['title']; $fields['Contact']['note']['title'] = $noteTitle . ': ' . ts('Body and Subject'); $fields['Contact']['note_body'] = array('title' => $noteTitle . ': ' . ts('Body Only'), 'name' => 'note_body'); $fields['Contact']['note_subject'] = array('title' => $noteTitle . ': ' . ts('Subject Only'), 'name' => 'note_subject'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT) { if (CRM_Core_Permission::access('CiviContribute')) { $fields['Contribution'] = CRM_Contribute_BAO_Contribution::exportableFields(); foreach (array('contribution_contact_id', 'contribution_soft_credit_name', 'contribution_soft_credit_amount', 'contribution_soft_credit_type', 'contribution_soft_credit_contribution_id') as $element) { unset($fields['Contribution'][$element]); } $compArray['Contribution'] = ts('Contribution'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { if (CRM_Core_Permission::access('CiviEvent')) { $fields['Participant'] = CRM_Event_BAO_Participant::exportableFields(); //get the component payment fields if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) { $componentPaymentFields = array(); foreach (CRM_Export_BAO_Export::componentPaymentFields() as $payField => $payTitle) { $componentPaymentFields[$payField] = array('title' => $payTitle); } $fields['Participant'] = array_merge($fields['Participant'], $componentPaymentFields); } $compArray['Participant'] = ts('Participant'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::MEMBER_EXPORT) { if (CRM_Core_Permission::access('CiviMember')) { $fields['Membership'] = CRM_Member_BAO_Membership::getMembershipFields($exportMode); unset($fields['Membership']['membership_contact_id']); $compArray['Membership'] = ts('Membership'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT) { if (CRM_Core_Permission::access('CiviPledge')) { $fields['Pledge'] = CRM_Pledge_BAO_Pledge::exportableFields(); unset($fields['Pledge']['pledge_contact_id']); $compArray['Pledge'] = ts('Pledge'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::CASE_EXPORT) { if (CRM_Core_Permission::access('CiviCase')) { $fields['Case'] = CRM_Case_BAO_Case::exportableFields(); $compArray['Case'] = ts('Case'); $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Case'); $compArray['Activity'] = ts('Case Activity'); unset($fields['Case']['case_contact_id']); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::GRANT_EXPORT) { if (CRM_Core_Permission::access('CiviGrant')) { $fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields(); unset($fields['Grant']['grant_contact_id']); if ($mappingType == 'Search Builder') { unset($fields['Grant']['grant_type_id']); } $compArray['Grant'] = ts('Grant'); } } if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) { $fields['Activity'] = CRM_Activity_BAO_Activity::exportableFields('Activity'); $compArray['Activity'] = ts('Activity'); } //Contact Sub Type For export $contactSubTypes = array(); $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo(); foreach ($subTypes as $subType => $val) { //adding subtype specific relationships CRM-5256 $csRelationships = array(); if ($mappingType == 'Export') { $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'], FALSE, 'label', TRUE, $subType); foreach ($subTypeRelationshipTypes as $key => $var) { if (!array_key_exists($key, $fields[$val['parent']])) { list($type) = explode('_', $key); $csRelationships[$key]['title'] = $var; $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/'; $csRelationships[$key]['export'] = TRUE; $csRelationships[$key]['relationship_type_id'] = $type; $csRelationships[$key]['related'] = TRUE; $csRelationships[$key]['hasRelationType'] = 1; } } } $fields[$subType] = $fields[$val['parent']] + $csRelationships; //custom fields for sub type $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType); $fields[$subType] += $subTypeFields; if (!empty($subTypeFields) || !empty($csRelationships)) { $contactSubTypes[$subType] = $val['label']; } } unset($subTypes); foreach ($fields as $key => $value) { foreach ($value as $key1 => $value1) { //CRM-2676, replacing the conflict for same custom field name from different custom group. $customGroupName = self::getCustomGroupName($key1); if ($customGroupName) { $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title']; } else { $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title']; } if (isset($value1['hasLocationType'])) { $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; } if (isset($value1['hasRelationType'])) { $hasRelationTypes[$key][$key1] = $value1['hasRelationType']; unset($relatedMapperFields[$key][$key1]); } } if (array_key_exists('related', $relatedMapperFields[$key])) { unset($relatedMapperFields[$key]['related']); } } $mapperKeys = array_keys($mapperFields); $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); // FIXME: dirty hack to make the default option show up first. This // avoids a mozilla browser bug with defaults on dynamically constructed // selector widgets. if ($defaultLocationType) { $defaultLocation = $locationTypes[$defaultLocationType->id]; unset($locationTypes[$defaultLocationType->id]); $locationTypes = array($defaultLocationType->id => $defaultLocation) + $locationTypes; } $locationTypes = array(' ' => ts('Primary')) + $locationTypes; // since we need a hierarchical list to display contact types & subtypes, // this is what we going to display in first selector $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); if ($mappingType == 'Search Builder') { $contactTypes = array('Contact' => ts('Contacts')) + $contactTypes; } $sel1 = array('' => ts('- select record type -')) + $contactTypes + $compArray; foreach ($sel1 as $key => $sel) { if ($key) { // sort everything BUT the contactType which is sorted seperately by // an initial commit of CRM-13278 (check ksort above) if (!in_array($key, $contactType)) { asort($mapperFields[$key]); } $sel2[$key] = array('' => ts('- select field -')) + $mapperFields[$key]; } } $sel3[''] = NULL; $sel5[''] = NULL; $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); asort($phoneTypes); foreach ($sel1 as $k => $sel) { if ($k) { foreach ($locationTypes as $key => $value) { if (trim($key) != '') { $sel4[$k]['phone'][$key] =& $phoneTypes; $sel4[$k]['im'][$key] =& $imProviders; } } } } foreach ($sel1 as $k => $sel) { if ($k) { foreach ($mapperFields[$k] as $key => $value) { if (isset($hasLocationTypes[$k][$key])) { $sel3[$k][$key] = $locationTypes; } else { $sel3[$key] = NULL; } } } } // Array for core fields and relationship custom data $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, NULL, TRUE); if ($mappingType == 'Export') { foreach ($sel1 as $k => $sel) { if ($k) { foreach ($mapperFields[$k] as $field => $dontCare) { if (isset($hasRelationTypes[$k][$field])) { list($id, $first, $second) = explode('_', $field); // FIX ME: For now let's not expose custom data related to relationship $relationshipCustomFields = array(); //$relationshipCustomFields = self::getRelationTypeCustomGroupData( $id ); //asort($relationshipCustomFields); $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $id; if ($relationshipType->find(TRUE)) { $direction = "contact_sub_type_{$second}"; if (isset($relationshipType->{$direction})) { $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->{$direction}], (array) $relationshipCustomFields); } else { $target_type = 'contact_type_' . $second; $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->{$target_type}], (array) $relationshipCustomFields); } } $relationshipType->free(); asort($relatedFields); $sel5[$k][$field] = $relatedFields; } } } } //Location Type for relationship fields foreach ($sel5 as $k => $v) { if ($v) { foreach ($v as $rel => $fields) { foreach ($fields as $field => $fieldLabel) { if (isset($hasLocationTypes[$k][$field])) { $sel6[$k][$rel][$field] = $locationTypes; } } } } } //PhoneTypes for relationship fields $sel7[''] = NULL; foreach ($sel6 as $k => $rel) { if ($k) { foreach ($rel as $phonekey => $phonevalue) { foreach ($locationTypes as $locType => $loc) { if (trim($locType) != '') { $sel7[$k][$phonekey]['phone'][$locType] =& $phoneTypes; $sel7[$k][$phonekey]['im'][$locType] =& $imProviders; } } } } } } //special fields that have location, hack for primary location $specialFields = array('street_address', 'supplemental_address_1', 'supplemental_address_2', 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'state_province', 'country', 'phone', 'email', 'im'); if (isset($mappingId)) { $colCnt = 0; list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue) = CRM_Core_BAO_Mapping::getMappingFields($mappingId); $blkCnt = count($mappingName); if ($blkCnt >= $blockCount) { $blockCount = $blkCnt + 1; } for ($x = 1; $x < $blockCount; $x++) { if (isset($mappingName[$x])) { $colCnt = count($mappingName[$x]); if ($colCnt >= $columnCount[$x]) { $columnCount[$x] = $colCnt; } } } } $form->_blockCount = $blockCount; $form->_columnCount = $columnCount; $form->set('blockCount', $form->_blockCount); $form->set('columnCount', $form->_columnCount); $defaults = $noneArray = $nullArray = array(); //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($x = 1; $x < $blockCount; $x++) { for ($i = 0; $i < $columnCount[$x]; $i++) { $sel =& $form->addElement('hierselect', "mapper[{$x}][{$i}]", ts('Mapper for Field %1', array(1 => $i)), NULL); $jsSet = FALSE; if (isset($mappingId)) { $locationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; if (isset($mappingName[$x][$i])) { if (is_array($mapperFields[$mappingContactType[$x][$i]])) { if (isset($mappingRelation[$x][$i])) { $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i])); $relLocationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0; if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) { $relLocationId = " "; } $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; $defaults["mapper[{$x}][{$i}]"] = array($mappingContactType[$x][$i], $mappingRelation[$x][$i], $locationId, $phoneType, $mappingName[$x][$i], $relLocationId, $relPhoneType); if (!$locationId) { $noneArray[] = array($x, $i, 2); } if (!$phoneType && !$imProvider) { $noneArray[] = array($x, $i, 3); } if (!$mappingName[$x][$i]) { $noneArray[] = array($x, $i, 4); } if (!$relLocationId) { $noneArray[] = array($x, $i, 5); } if (!$relPhoneType) { $noneArray[] = array($x, $i, 6); } $noneArray[] = array($x, $i, 2); } else { $phoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : NULL; $imProvider = isset($mappingImProvider[$x][$i]) ? $mappingImProvider[$x][$i] : NULL; if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) { $locationId = " "; } $defaults["mapper[{$x}][{$i}]"] = array($mappingContactType[$x][$i], $mappingName[$x][$i], $locationId, $phoneType); if (!$mappingName[$x][$i]) { $noneArray[] = array($x, $i, 1); } if (!$locationId) { $noneArray[] = array($x, $i, 2); } if (!$phoneType && !$imProvider) { $noneArray[] = array($x, $i, 3); } $noneArray[] = array($x, $i, 4); $noneArray[] = array($x, $i, 5); $noneArray[] = array($x, $i, 6); } $jsSet = TRUE; if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingOperator))) { $defaults["operator[{$x}][{$i}]"] = CRM_Utils_Array::value($i, $mappingOperator[$x]); } if (CRM_Utils_Array::value($i, CRM_Utils_Array::value($x, $mappingValue))) { $defaults["value[{$x}][{$i}]"] = CRM_Utils_Array::value($i, $mappingValue[$x]); } } } } //Fix for Search Builder if ($mappingType == 'Export') { $j = 7; } else { $j = 4; } $formValues = $form->exportValues(); if (!$jsSet) { if (empty($formValues)) { // Incremented length for third select box(relationship type) for ($k = 1; $k < $j; $k++) { $noneArray[] = array($x, $i, $k); } } else { if (!empty($formValues['mapper'][$x])) { foreach ($formValues['mapper'][$x] as $value) { for ($k = 1; $k < $j; $k++) { if (!isset($formValues['mapper'][$x][$i][$k]) || !$formValues['mapper'][$x][$i][$k]) { $noneArray[] = array($x, $i, $k); } else { $nullArray[] = array($x, $i, $k); } } } } else { for ($k = 1; $k < $j; $k++) { $noneArray[] = array($x, $i, $k); } } } } //Fix for Search Builder if ($mappingType == 'Export') { if (!isset($mappingId) || $i >= count(reset($mappingName))) { if (isset($formValues['mapper']) && isset($formValues['mapper'][$x][$i][1]) && array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)) { $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4)); } else { $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); } } else { $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7)); } } else { $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); } if ($mappingType == 'Search Builder') { //CRM -2292, restricted array set $operatorArray = array('' => ts('-operator-')) + CRM_Core_SelectValues::getSearchBuilderOperators(); $form->add('select', "operator[{$x}][{$i}]", '', $operatorArray); $form->add('text', "value[{$x}][{$i}]", ''); } } //end of columnCnt for if ($mappingType == 'Search Builder') { $title = ts('Another search field'); } else { $title = ts('Select more fields'); } $form->addElement('submit', "addMore[{$x}]", $title, array('class' => 'submit-link')); } //end of block for $js = "<script type='text/javascript'>\n"; $formName = "document.{$name}"; if (!empty($nullArray)) { $js .= "var nullArray = ["; $elements = array(); $seen = array(); foreach ($nullArray as $element) { $key = "{$element[0]}, {$element[1]}, {$element[2]}"; if (!isset($seen[$key])) { $elements[] = "[{$key}]"; $seen[$key] = 1; } } $js .= implode(', ', $elements); $js .= "]"; $js .= "\n for (var i=0;i<nullArray.length;i++) {\n if ( {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'] ) {\n {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';\n }\n }\n"; } if (!empty($noneArray)) { $js .= "var noneArray = ["; $elements = array(); $seen = array(); foreach ($noneArray as $element) { $key = "{$element[0]}, {$element[1]}, {$element[2]}"; if (!isset($seen[$key])) { $elements[] = "[{$key}]"; $seen[$key] = 1; } } $js .= implode(', ', $elements); $js .= "]"; $js .= "\n for (var i=0;i<noneArray.length;i++) {\n if ( {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'] ) {\n {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';\n }\n }\n"; } $js .= "</script>\n"; $form->assign('initHideBoxes', $js); $form->assign('columnCount', $columnCount); $form->assign('blockCount', $blockCount); $form->setDefaults($defaults); $form->setDefaultAction('refresh'); }
/** * Check if an error in custom data. * * @param array $params * @param string $errorMessage * A string containing all the error-fields. * * @param null $csType * @param null $relationships */ public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) { $session = CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); if (!empty($params['contact_sub_type'])) { $csType = CRM_Utils_Array::value('contact_sub_type', $params); } if (empty($params['contact_type'])) { $params['contact_type'] = 'Individual'; } $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType); $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); $customFields = $customFields + $addressCustomFields; foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { self::addToErrorMsg(ts('field ID'), $errorMessage); } // validate null values for required custom fields of type boolean if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') { self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); } //For address custom fields, we do get actual custom field value as an inner array of //values so need to modify if (array_key_exists($customFieldID, $addressCustomFields)) { $value = $value[0][$key]; } /* validate the data against the CF type */ if ($value) { if ($customFields[$customFieldID]['data_type'] == 'Date') { if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date::convertToDefaultDate($params[$key][0], $dateType, $key)) { $value = $params[$key][0][$key]; } elseif (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { $value = $params[$key]; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { if (CRM_Utils_String::strtoboolstr($value) === FALSE) { self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); } } // need not check for label filed import $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio', 'Multi-Select State/Province', 'Multi-Select Country'); if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) || $customFields[$customFieldID]['data_type'] == 'Boolean' || $customFields[$customFieldID]['data_type'] == 'ContactReference') { $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $value); if (!$valid) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } // check for values for custom fields for checkboxes and multiselect if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') { $value = trim($value); $value = str_replace('|', ',', $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); foreach ($mulValues as $v1) { if (strlen($v1) == 0) { continue; } $flag = FALSE; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($v1)) || strtolower(trim($v2['value'])) == strtolower(trim($v1))) { $flag = TRUE; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } elseif ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); $flag = FALSE; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($value)) || strtolower(trim($v2['value'])) == strtolower(trim($value))) { $flag = TRUE; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') { $mulValues = explode(',', $value); foreach ($mulValues as $stateValue) { if ($stateValue) { if (self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvince())) { continue; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') { $mulValues = explode(',', $value); foreach ($mulValues as $countryValue) { if ($countryValue) { CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); $config = CRM_Core_Config::singleton(); $limitCodes = $config->countryLimit(); $error = TRUE; foreach (array($countryNames, $countryIsoCodes, $limitCodes) as $values) { if (in_array(trim($countryValue), $values)) { $error = FALSE; break; } } if ($error) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } } } elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) { //CRM-5125 //supporting custom data of related contact subtypes $relation = NULL; if ($relationships) { if (array_key_exists($key, $relationships)) { $relation = $key; } elseif (CRM_Utils_Array::key($key, $relationships)) { $relation = CRM_Utils_Array::key($key, $relationships); } } if (!empty($relation)) { list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3); $direction = "contact_sub_type_{$second}"; $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $id; if ($relationshipType->find(TRUE)) { if (isset($relationshipType->{$direction})) { $params[$key]['contact_sub_type'] = $relationshipType->{$direction}; } } $relationshipType->free(); } self::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships); } } }
/** * non-standard behaviour inherited from v2 * @param array $params parameters passed into get function * @return array result for calling function */ function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) { $relationships = array(); foreach ($membershipValues as $membershipId => $values) { // populate the membership type name for the membership type id $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; if (!empty($membershipType['relationship_type_id'])) { $relationships[$membershipType['relationship_type_id']] = $membershipId; } // populating relationship type name. $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); if ($relationshipType->find(TRUE)) { $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; } _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']); } $members = $membershipValues; // populating contacts in members array based on their relationship with direct members. if (!empty($relationships)) { foreach ($relationships as $relTypeId => $membershipId) { // As members are not direct members, there should not be // membership id in the result array. unset($membershipValues[$membershipId]['id']); $relationship = new CRM_Contact_BAO_Relationship(); $relationship->contact_id_b = $contactID; $relationship->relationship_type_id = $relTypeId; if ($relationship->find()) { while ($relationship->fetch()) { clone $relationship; $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; } } } } return $members; }
/** * Get conatct membership record. * * This api is used for finding an existing membership record. * This api will also return the mebership records for the contacts * having mebership based on the relationship with the direct members. * * @param Int $contactID ID of a contact * * @return Array of all found membership property values. * @access public */ function crm_get_contact_memberships($contactID) { _crm_initialize(); if (empty($contactID)) { return _crm_error('Invalid value for ContactID.'); } // get the membership for the given contact ID require_once 'CRM/Member/BAO/Membership.php'; $membership = array('contact_id' => $contactID); $membershipValues = array(); CRM_Member_BAO_Membership::getValues($membership, $membershipValues); if (empty($membershipValues)) { return _crm_error('No memberships for this contact.'); } foreach ($membershipValues as $membershipId => $values) { // populate the membership type name for the membership type id require_once 'CRM/Member/BAO/MembershipType.php'; $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; $relationships[$membershipType['relationship_type_id']] = $membershipId; // populating relationship type name. require_once 'CRM/Contact/BAO/RelationshipType.php'; $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $membershipType['relationship_type_id']; if ($relationshipType->find(true)) { $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; } } $members[$contactID] = $membershipValues; // populating contacts in members array based on their relationship with direct members. require_once 'CRM/Contact/BAO/Relationship.php'; foreach ($relationships as $relTypeId => $membershipId) { // As members are not direct members, there should not be // membership id in the result array. unset($membershipValues[$membershipId]['id']); $relationship = new CRM_Contact_BAO_Relationship(); $relationship->contact_id_b = $contactID; $relationship->relationship_type_id = $relTypeId; if ($relationship->find()) { while ($relationship->fetch()) { clone $relationship; $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; $members[$contactID][$relationship->contact_id_a] = $membershipValues[$membershipId]; } } } return $members; }
/** * Get contact membership record. * * This api will return the membership records for the contacts * having membership based on the relationship with the direct members. * * @param Array $params key/value pairs for contact_id and some * options affecting the desired results; has legacy support * for just passing the contact_id itself as the argument * * @return Array of all found membership property values. * @access public * @todo needs some love - basically only a get for a given contact right now * {@getfields membership_get} */ function civicrm_api3_membership_get($params) { $contactID = $activeOnly = $membershipTypeId = $membershipType = NULL; $contactID = CRM_Utils_Array::value('contact_id', $params); if (is_array(CRM_Utils_Array::value('filters', $params)) && !empty($params['filters'])) { $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE); } $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly); //@todo replace this by handling in API layer - we should have enough info to do this // between pseudoconstant & fk - see comments in format_params $membershipTypeId = CRM_Utils_Array::value('membership_type_id', $params); if (!$membershipTypeId) { $membershipType = CRM_Utils_Array::value('membership_type', $params); if ($membershipType) { $membershipTypeId = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipType, 'id', 'name'); } } if (CRM_Utils_Array::value('contact_id', $params)) { $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly); } else { //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE); } if (empty($membershipValues)) { # No results is NOT an error! return civicrm_api3_create_success($membershipValues, $params); } $relationships = array(); foreach ($membershipValues as $membershipId => $values) { // populate the membership type name for the membership type id $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($values['membership_type_id']); $membershipValues[$membershipId]['membership_name'] = $membershipType['name']; if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) { $relationships[$membershipType['relationship_type_id']] = $membershipId; } // populating relationship type name. $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = CRM_Utils_Array::value('relationship_type_id', $membershipType); if ($relationshipType->find(TRUE)) { $membershipValues[$membershipId]['relationship_name'] = $relationshipType->name_a_b; } _civicrm_api3_custom_data_get($membershipValues[$membershipId], 'Membership', $membershipId, NULL, $values['membership_type_id']); } $members = $membershipValues; // populating contacts in members array based on their relationship with direct members. if (!empty($relationships)) { foreach ($relationships as $relTypeId => $membershipId) { // As members are not direct members, there should not be // membership id in the result array. unset($membershipValues[$membershipId]['id']); $relationship = new CRM_Contact_BAO_Relationship(); $relationship->contact_id_b = $contactID; $relationship->relationship_type_id = $relTypeId; if ($relationship->find()) { while ($relationship->fetch()) { clone $relationship; $membershipValues[$membershipId]['contact_id'] = $relationship->contact_id_a; $members[$membershipId]['related_contact_id'] = $relationship->contact_id_a; } } } } return civicrm_api3_create_success($members, $params, 'membership', 'get'); }