/** * Function to get all relationship type * retruns An array of Relationship_type * * @access public */ function civicrm_relationship_types_get($params = null) { _civicrm_initialize(); require_once 'CRM/Contact/DAO/RelationshipType.php'; $relationshipTypes = array(); $relationshipType = array(); $relationType = new CRM_Contact_DAO_RelationshipType(); if (!empty($params) && is_array($params)) { $properties = array_keys($relationType->fields()); foreach ($properties as $name) { if (array_key_exists($name, $params)) { $relationType->{$name} = $params[$name]; } } } $relationType->find(); while ($relationType->fetch()) { _civicrm_object_to_array(clone $relationType, $relationshipType); $relationshipTypes[] = $relationshipType; } return $relationshipTypes; }
/** * Build the form object. */ public function buildQuickForm() { //to save the current mappings if (!$this->get('savedMapping')) { $saveDetailsName = ts('Save this field mapping'); $this->applyFilter('saveMappingName', 'trim'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } else { $savedMapping = $this->get('savedMapping'); list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue, $mappingWebsiteType) = CRM_Core_BAO_Mapping::getMappingFields($savedMapping); //get loaded Mapping Fields $mappingName = CRM_Utils_Array::value(1, $mappingName); $mappingContactType = CRM_Utils_Array::value(1, $mappingContactType); $mappingLocation = CRM_Utils_Array::value(1, $mappingLocation); $mappingPhoneType = CRM_Utils_Array::value(1, $mappingPhoneType); $mappingImProvider = CRM_Utils_Array::value(1, $mappingImProvider); $mappingRelation = CRM_Utils_Array::value(1, $mappingRelation); $mappingWebsiteType = CRM_Utils_Array::value(1, $mappingWebsiteType); $this->assign('loadedMapping', $savedMapping); $this->set('loadedMapping', $savedMapping); $params = array('id' => $savedMapping); $temp = array(); $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp); $this->assign('savedName', $mappingDetails->name); $this->add('hidden', 'mappingId', $savedMapping); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), NULL); $saveDetailsName = ts('Save as a new field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, array('onclick' => "showSaveDetails(this)")); $this->addFormRule(array('CRM_Contact_Import_Form_MapField', 'formRule')); //-------- end of saved mapping stuff --------- $defaults = array(); $mapperKeys = array_keys($this->_mapperFields); $hasColumnNames = !empty($this->_columnNames); $columnPatterns = $this->get('columnPatterns'); $dataPatterns = $this->get('dataPatterns'); $hasLocationTypes = $this->get('fieldTypes'); $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); // Pass default location to js if ($defaultLocationType) { $this->assign('defaultLocationType', $defaultLocationType->id); $this->assign('defaultLocationTypeLabel', $this->_location_types[$defaultLocationType->id]); } /* Initialize all field usages to false */ foreach ($mapperKeys as $key) { $this->_fieldUsed[$key] = FALSE; } $sel1 = $this->_mapperFields; $sel2[''] = NULL; $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); foreach ($this->_location_types as $key => $value) { $sel3['phone'][$key] =& $phoneTypes; //build array for IM service provider type for contact $sel3['im'][$key] =& $imProviders; } $sel4 = NULL; // store and cache all relationship types $contactRelation = new CRM_Contact_DAO_RelationshipType(); $contactRelation->find(); while ($contactRelation->fetch()) { $contactRelationCache[$contactRelation->id] = array(); $contactRelationCache[$contactRelation->id]['contact_type_a'] = $contactRelation->contact_type_a; $contactRelationCache[$contactRelation->id]['contact_sub_type_a'] = $contactRelation->contact_sub_type_a; $contactRelationCache[$contactRelation->id]['contact_type_b'] = $contactRelation->contact_type_b; $contactRelationCache[$contactRelation->id]['contact_sub_type_b'] = $contactRelation->contact_sub_type_b; } $highlightedFields = $highlightedRelFields = array(); $highlightedFields['email'] = 'All'; $highlightedFields['external_identifier'] = 'All'; $highlightedFields['first_name'] = 'Individual'; $highlightedFields['last_name'] = 'Individual'; $highlightedFields['household_name'] = 'Household'; $highlightedFields['organization_name'] = 'Organization'; foreach ($mapperKeys as $key) { // check if there is a _a_b or _b_a in the key if (strpos($key, '_a_b') || strpos($key, '_b_a')) { list($id, $first, $second) = explode('_', $key); } else { $id = $first = $second = NULL; } if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $cType = $contactRelationCache[$id]["contact_type_{$second}"]; //CRM-5125 for contact subtype specific relationshiptypes $cSubType = NULL; if (!empty($contactRelationCache[$id]["contact_sub_type_{$second}"])) { $cSubType = $contactRelationCache[$id]["contact_sub_type_{$second}"]; } if (!$cType) { $cType = 'All'; } $relatedFields = array(); $relatedFields = CRM_Contact_BAO_Contact::importableFields($cType); unset($relatedFields['']); $values = array(); foreach ($relatedFields as $name => $field) { $values[$name] = $field['title']; if (isset($hasLocationTypes[$name])) { $sel3[$key][$name] = $this->_location_types; } elseif ($name == 'url') { $sel3[$key][$name] = $websiteTypes; } else { $sel3[$name] = NULL; } } //fix to append custom group name to field name, CRM-2676 if (empty($this->_formattedFieldNames[$cType]) || $cType == $this->_contactType) { $this->_formattedFieldNames[$cType] = $this->formatCustomFieldName($values); } $this->_formattedFieldNames[$cType] = array_merge($values, $this->_formattedFieldNames[$cType]); //Modified the Relationship fields if the fields are //present in dedupe rule if ($this->_onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK && !empty($this->_dedupeFields[$cType]) && is_array($this->_dedupeFields[$cType])) { static $cTypeArray = array(); if ($cType != $this->_contactType && !in_array($cType, $cTypeArray)) { foreach ($this->_dedupeFields[$cType] as $val) { if ($valTitle = CRM_Utils_Array::value($val, $this->_formattedFieldNames[$cType])) { $this->_formattedFieldNames[$cType][$val] = $valTitle . ' (match to contact)'; } } $cTypeArray[] = $cType; } } foreach ($highlightedFields as $k => $v) { if ($v == $cType || $v == 'All') { $highlightedRelFields[$key][] = $k; } } $this->assign('highlightedRelFields', $highlightedRelFields); $sel2[$key] = $this->_formattedFieldNames[$cType]; if (!empty($cSubType)) { //custom fields for sub type $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($cSubType); if (!empty($subTypeFields)) { $subType = NULL; foreach ($subTypeFields as $customSubTypeField => $details) { $subType[$customSubTypeField] = $details['title']; $sel2[$key] = array_merge($sel2[$key], $this->formatCustomFieldName($subType)); } } } foreach ($this->_location_types as $k => $value) { $sel4[$key]['phone'][$k] =& $phoneTypes; //build array of IM service provider for related contact $sel4[$key]['im'][$k] =& $imProviders; } } else { $options = NULL; if (!empty($hasLocationTypes[$key])) { $options = $this->_location_types; } elseif ($key == 'url') { $options = $websiteTypes; } $sel2[$key] = $options; } } $js = "<script type='text/javascript'>\n"; $formName = 'document.forms.' . $this->_name; //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($i = 0; $i < $this->_columnCount; $i++) { $sel =& $this->addElement('hierselect', "mapper[{$i}]", ts('Mapper for Field %1', array(1 => $i)), NULL); $jsSet = FALSE; if ($this->get('savedMapping')) { if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { if (isset($mappingRelation[$i])) { // relationship mapping switch ($this->get('contactType')) { case CRM_Import_Parser::CONTACT_INDIVIDUAL: $contactType = 'Individual'; break; case CRM_Import_Parser::CONTACT_HOUSEHOLD: $contactType = 'Household'; break; case CRM_Import_Parser::CONTACT_ORGANIZATION: $contactType = 'Organization'; } //CRM-5125 $contactSubType = NULL; if ($this->get('contactSubType')) { $contactSubType = $this->get('contactSubType'); } $relations = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $contactType, FALSE, 'label', TRUE, $contactSubType); foreach ($relations as $key => $var) { if ($key == $mappingRelation[$i]) { $relation = $key; break; } } $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i])); $websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL; $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL; //get provider id from saved mappings $imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL; if ($websiteTypeId) { $defaults["mapper[{$i}]"] = array($relation, $contactDetails, $websiteTypeId); if (!$websiteTypeId) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } } else { // default for IM/phone when mapping with relation is true $typeId = NULL; if (isset($phoneType)) { $typeId = $phoneType; } elseif (isset($imProvider)) { $typeId = $imProvider; } $defaults["mapper[{$i}]"] = array($relation, $contactDetails, $locationId, $typeId); if (!$locationId) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } } // fix for edge cases, CRM-4954 if ($contactDetails == 'image_url') { $contactDetails = str_replace('url', 'URL', $contactDetails); } if (!$contactDetails) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } if (!$phoneType && !$imProvider) { $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; } //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; $jsSet = TRUE; } else { $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); $websiteTypeId = isset($mappingWebsiteType[$i]) ? $mappingWebsiteType[$i] : NULL; $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : NULL; // get IM service provider id $imProvider = isset($mappingImProvider[$i]) ? $mappingImProvider[$i] : NULL; if ($websiteTypeId) { if (!$websiteTypeId) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } $defaults["mapper[{$i}]"] = array($mappingHeader[0], $websiteTypeId); } else { if (!$locationId) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } //default for IM/phone without related contact $typeId = NULL; if (isset($phoneType)) { $typeId = $phoneType; } elseif (isset($imProvider)) { $typeId = $imProvider; } $defaults["mapper[{$i}]"] = array($mappingHeader[0], $locationId, $typeId); } if (!$phoneType && !$imProvider) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; $jsSet = TRUE; } } else { $defaults["mapper[{$i}]"] = array(); } if (!$jsSet) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } } else { // this load section to help mapping if we ran out of saved columns when doing Load Mapping $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_0_');\n"; if ($hasColumnNames) { $defaults["mapper[{$i}]"] = array($this->defaultFromColumnName($this->_columnNames[$i], $columnPatterns)); } else { $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i)); } } //end of load mapping } else { $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_0_');\n"; if ($hasColumnNames) { // do array search first to see if has mapped key $columnKey = ''; $columnKey = array_search($this->_columnNames[$i], $this->_mapperFields); if (isset($this->_fieldUsed[$columnKey])) { $defaults["mapper[{$i}]"] = $columnKey; $this->_fieldUsed[$key] = TRUE; } else { // Infer the default from the column names if we have them $defaults["mapper[{$i}]"] = array($this->defaultFromColumnName($this->_columnNames[$i], $columnPatterns), 0); } } else { // Otherwise guess the default from the form of the data $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i), 0); } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); //set warning if mismatch in more than if (isset($mappingName) && $this->_columnCount != count($mappingName)) { $warning++; } if ($warning != 0 && $this->get('savedMapping')) { $session = CRM_Core_Session::singleton(); $session->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.')); } else { $session = CRM_Core_Session::singleton(); $session->setStatus(NULL); } $this->setDefaults($defaults); $this->addButtons(array(array('type' => 'back', 'name' => ts('Previous')), array('type' => 'next', 'name' => ts('Continue'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); }
/** * Get all Relationship Types from database. * * The static array group is returned, and if it's * called the first time, the <b>RelationshipType DAO</b> is used * to get all the relationship types. * * Note: any database errors will be trapped by the DAO. * * @param string $valueColumnName db column name/label. * @param boolean $reset reset relationship types if true * * @access public * @static * * @return array - array reference of all relationship types. */ public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) { if (!CRM_Utils_Array::value($valueColumnName, self::$relationshipType) || $reset) { self::$relationshipType[$valueColumnName] = array(); //now we have name/label columns CRM-3336 $column_a_b = "{$valueColumnName}_a_b"; $column_b_a = "{$valueColumnName}_b_a"; $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType(); $relationshipTypeDAO->selectAdd(); $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b"); $relationshipTypeDAO->is_active = 1; $relationshipTypeDAO->find(); while ($relationshipTypeDAO->fetch()) { self::$relationshipType[$valueColumnName][$relationshipTypeDAO->id] = array('id' => $relationshipTypeDAO->id, $column_a_b => $relationshipTypeDAO->{$column_a_b}, $column_b_a => $relationshipTypeDAO->{$column_b_a}, 'contact_type_a' => "{$relationshipTypeDAO->contact_type_a}", 'contact_type_b' => "{$relationshipTypeDAO->contact_type_b}", 'contact_sub_type_a' => "{$relationshipTypeDAO->contact_sub_type_a}", 'contact_sub_type_b' => "{$relationshipTypeDAO->contact_sub_type_b}"); } } return self::$relationshipType[$valueColumnName]; }