public function testTypeToFieldWithWrongInput() { $values = array('String' => 'memo_data', 'File' => 'date_data', 'Boolean' => 'char_data'); foreach ($values as $type => $value) { $valid = CRM_Core_BAO_CustomValue::typeToField($type); $this->assertNotEquals($valid, $value, 'Checking type ' . $type . ' for returned CustomField Type.'); } }
/** * class constructor * * Takes in a set of custom field ids andsets up the data structures to * generate a query * * @param array $ids the set of custom field ids * * @access public */ function CRM_Core_BAO_CustomQuery($ids) { $this->_ids =& $ids; $this->_select = array(); $this->_element = array(); $this->_tables = array(); $this->_whereTables = array(); $this->_where = array(); $this->_qill = array(); $this->_options = array(); $this->_fields = array(); if (empty($this->_ids)) { return; } // initialize the field array $tmpArray = array_keys($this->_ids); $query = 'select * from civicrm_custom_field where is_active = 1 AND id IN ( ' . implode(',', $tmpArray) . ' ) '; $dao =& CRM_Core_DAO::executeQuery($query); $optionIds = array(); while ($dao->fetch()) { // get the group dao to figure which class this custom field extends $extends =& CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $dao->custom_group_id, 'extends'); $extendsTable = $GLOBALS['_CRM_CORE_BAO_CUSTOMQUERY']['extendsMap'][$extends]; $this->_fields[$dao->id] = array('id' => $dao->id, 'label' => $dao->label, 'extends' => $extendsTable, 'data_type' => $dao->data_type, 'html_type' => $dao->html_type, 'is_search_range' => $dao->is_search_range, 'db_field' => CRM_Core_BAO_CustomValue::typeToField($dao->data_type)); // store it in the options cache to make things easier // during option lookup $this->_options[$dao->id] = array(); $this->_options[$dao->id]['attributes'] = array('label' => $dao->label, 'data_type' => $dao->data_type, 'html_type' => $dao->html_type); if ($dao->html_type == 'CheckBox' || $dao->html_type == 'Radio' || $dao->html_type == 'Select' || $dao->html_type == 'Multi-Select') { $optionIds[] = $dao->id; } } // build the cache for custom values with options (label => value) if (!empty($optionIds)) { $query = 'select entity_id, label, value from civicrm_custom_option where entity_id IN ( ' . implode(',', $optionIds) . ' ) AND entity_table = \'civicrm_custom_field\''; $dao =& CRM_Core_DAO::executeQuery($query); while ($dao->fetch()) { $this->_options[$dao->entity_id][$dao->value] = $dao->label; } } // CRM_Core_Error::debug( 'q', $this ); }
function _crm_update_contact($contact, $values, $overwrite = true) { // first check to make sure the location arrays sync up $param = array("contact_id" => $contact->id); $contact = crm_get_contact($param); $locMatch = _crm_location_match($contact, $values); if (!$locMatch) { return _crm_error('Cannot update contact location'); } // it is possible that an contact type object record does not exist // if the contact_type_object is null etc, if so we create one if ($contact->contact_type_object == null) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contact->contact_type) . ".php"; eval('$contact->contact_type_object =& new CRM_Contact_BAO_' . $contact->contact_type . '( );'); $contact->contact_type_object->contact_id = $contact->id; } $sortNameArray = array(); // fix sort_name and display_name if ($contact->contact_type == 'Individual') { if ($overwrite || !isset($contact->contact_type_object->first_name)) { $firstName = CRM_Utils_Array::value('first_name', $values); } else { $firstName = null; } if (!$firstName) { $firstName = isset($contact->contact_type_object->first_name) ? $contact->contact_type_object->first_name : ''; } if ($overwrite || !isset($contact->contact_type_object->middle_name)) { $middleName = CRM_Utils_Array::value('middle_name', $values); } else { $middleName = null; } if (!$middleName) { $middleName = isset($contact->contact_type_object->middle_name) ? $contact->contact_type_object->middle_name : ''; } if ($overwrite || !isset($contact->contact_type_object->last_name)) { $lastName = CRM_Utils_Array::value('last_name', $values); } else { $lastName = null; } if (!$lastName) { $lastName = isset($contact->contact_type_object->last_name) ? $contact->contact_type_object->last_name : ''; } if ($overwrite || !isset($contact->contact_type_object->prefix_id)) { $prefix = CRM_Utils_Array::value('prefix', $values); } else { $prefix = null; } if (!$prefix) { if (isset($contact->contact_type_object->prefix_id)) { $prefix =& new CRM_Core_DAO_IndividualPrefix(); $prefix->id = $contact->contact_type_object->prefix_id; $prefix->find(); $prefix->fetch(); $prefix = $prefix->name; } else { $prefix = ""; } } if ($overwrite || !isset($contact->contact_type_object->suffix_id)) { $suffix = CRM_Utils_Array::value('suffix', $values); } else { $suffix = null; } if (!$suffix) { if (isset($contact->contact_type_object->suffix_id)) { $suffix =& new CRM_Core_DAO_IndividualSuffix(); $suffix->id = $contact->contact_type_object->suffix_id; $suffix->find(); $suffix->fetch(); $suffix = $suffix->name; } else { $suffix = ""; } } if ($overwrite) { $gender = CRM_Utils_Array::value('gender', $values); } else { $gender = null; } if ($gender) { $genderDao =& new CRM_Core_DAO_Gender(); $genderDao->name = $gender; $genderDao->find(true); $values['gender_id'] = $genderDao->id; } if ($lastName != "" && $firstName != "") { $values['sort_name'] = "{$lastName}, {$firstName}"; } else { if ($lastName != "") { $values['sort_name'] = "{$lastName}"; } else { if ($firstName != "") { $values['sort_name'] = "{$firstName}"; } } } $values['display_name'] = "{$prefix} {$firstName} {$middleName} {$lastName} {$suffix} "; } else { if ($contact->contact_type == 'Household') { if ($overwrite || !isset($contact->contact_type_object->household_name)) { $householdName = CRM_Utils_Array::value('household_name', $values); } else { $householdName = null; } if (!$householdName) { $householdName = isset($contact->contact_type_object->household_name) ? $contact->contact_type_object->household_name : ''; } $values['sort_name'] = $householdName; } else { if ($overwrite || !isset($contact->contact_type_object->organization_name)) { $organizationName = CRM_Utils_Array::value('organization_name', $values); } else { $organizationName = null; } if (!$organizationName) { $organizationName = isset($contact->contact_type_object->organization_name) ? $contact->contact_type_object->organization_name : ''; } $values['sort_name'] = $organizationName; } } _crm_update_object($contact, $values); _crm_update_object($contact->contact_type_object, $values); if (!isset($contact->location)) { $contact->location = array(); } if (!array_key_exists(1, $contact->location) || empty($contact->location[1])) { $contact->location[1] =& new CRM_Core_BAO_Location(); } $primary_location = null; foreach ($contact->location as $key => $loc) { if ($loc->is_primary) { $primary_location = $key; break; } } if (is_array($values['location'])) { foreach ($values['location'] as $updateLocation) { $emptyBlock = $contactLocationBlock = null; /* Scan the location array for the correct block to update */ foreach ($contact->location as $key => $loc) { if ($loc->location_type_id == $updateLocation['location_type_id']) { $contactLocationBlock = $key; break; } else { if (!isset($loc->location_type_id)) { $emptyBlock = $key; } } } if ($contactLocationBlock == null) { if ($emptyBlock != null) { $contactLocationBlock = $emptyBlock; } else { /* no matching blocks and no empty blocks, make a new one */ $contact->location[] =& new CRM_Core_BAO_Location(); $contactLocationBlock = count($contact->location); } } $updateLocation['entity_id'] = $contact->id; $updateLocation['entity_table'] = CRM_Contact_BAO_Contact::getTableName(); /* If we're not overwriting, copy old data back before updating */ if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock], $updateLocation, true); } /* Make sure we only have one primary location */ if ($primary_location == null && $updateLocation['is_primary']) { $primary_location = $contactLocationBlock; } else { if ($primary_location != $contactLocationBlock) { $updateLocation['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock], $updateLocation); if (!isset($contact->location[$contactLocationBlock]->address)) { $contact->location[$contactLocationBlock]->address =& new CRM_Core_BAO_Address(); } $updateLocation['address']['location_id'] = $contact->location[$contactLocationBlock]->id; if ($updateLocation['address']['state_province']) { $state_province =& new CRM_Core_DAO_StateProvince(); $state_province->name = $updateLocation['address']['state_province']; if (!$state_province->find(true)) { $state_province->name = null; $state_province->abbreviation = $updateLocation['address']['state_province']; $state_province->find(true); } $updateLocation['address']['state_province_id'] = $state_province->id; } if ($updateLocation['address']['country']) { $country =& new CRM_Core_DAO_Country(); $country->name = $updateLocation['address']['country']; if (!$country->find(true)) { $country->name = null; $country->iso_code = $updateLocation['address']['country']; $country->find(true); } $updateLocation['address']['country_id'] = $country->id; } if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->address, $updateLocation['address'], true); } _crm_update_object($contact->location[$contactLocationBlock]->address, $updateLocation['address']); $blocks = array('Email', 'IM'); foreach ($blocks as $block) { $name = strtolower($block); if (!is_array($updateLocation[$name])) { continue; } if (!isset($contact->location[$contactLocationBlock]->{$name})) { $contact->location[$contactLocationBlock]->{$name} = array(); } $primary = null; foreach ($contact->location[$contactLocationBlock]->{$name} as $key => $value) { if ($value->is_primary) { $primary = $key; break; } } $propertyBlock = 1; foreach ($updateLocation[$name] as $property) { if (!isset($contact->location[$contactLocationBlock]->{$name}[$propertyBlock])) { require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_BAO_" . $block) . ".php"; eval('$contact->location[$contactLocationBlock]->{$name}[$propertyBlock] =& new CRM_Core_BAO_' . $block . '( );'); } $property['location_id'] = $contact->location[$contactLocationBlock]->id; if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->{$name}[$propertyBlock], $property, true); } if ($primary == null && $property['is_primary']) { $primary = $propertyBlock; } else { if ($primary != $propertyBlock) { $property['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock]->{$name}[$propertyBlock], $property); $propertyBlock++; } } /* handle multiple phones */ if (is_array($updateLocation['phone'])) { if (!isset($contact->location[$contactLocationBlock]->phone)) { $contact->location[$contactLocationBlock]->phone = array(); } $primary_phone = null; foreach ($contact->location[$contactLocationBlock]->phone as $key => $value) { if ($value->is_primary) { $primary_phone = $key; break; } } foreach ($updateLocation['phone'] as $phone) { /* scan through the contact record for matching phone type at this location */ $contactPhoneBlock = null; foreach ($contact->location[$contactLocationBlock]->phone as $key => $contactPhoneBlock) { if ($contactPhoneBlock->phone_type_id == $phone['phone_type_id']) { $contactPhoneBlock = $key; break; } } if ($contactPhoneBlock == null) { if (empty($contact->location[$contactLocationBlock]->phone)) { $contactPhoneBlock = 1; } else { $contactPhoneBlock = count($contact->location[$contactLocationBlock]->phone) + 1; } $contact->location[$contactLocationBlock]->phone[$contactPhoneBlock] =& new CRM_Core_BAO_Phone(); } $phone['location_id'] = $contact->location[$contactLocationBlock]->id; if (!$overwrite) { _crm_update_from_object($contact->location[$contactLocationBlock]->phone[$contactPhoneBlock], $phone, true); } if ($primary_phone == null && $phone['is_primary']) { $primary_phone = $contactPhoneBlock; } else { if ($primary_phone != $contactPhoneBlock) { $phone['is_primary'] = false; } } _crm_update_object($contact->location[$contactLocationBlock]->phone[$contactPhoneBlock], $phone); } } } } /* Custom data */ if (is_array($values['custom'])) { foreach ($values['custom'] as $customValue) { /* get the field for the data type */ $field = CRM_Core_BAO_CustomValue::typeToField($customValue['type']); if (!$field) { /* FIXME failure! */ continue; } /* adjust the value if it's boolean */ if ($customValue['type'] == 'Boolean') { $value = CRM_Utils_String::strtobool($customValue['value']); } else { $value = $customValue['value']; } /* look for a matching existing custom value */ $match = false; foreach ($contact->custom_values as $cv) { if ($cv->custom_field_id == $customValue['custom_field_id']) { /* match */ $match = true; if ($overwrite) { $cv->{$field} = $value; $cv->save(); break; } } } if (!$match) { /* no match, so create a new CustomValue */ $cvParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contact->id, 'value' => $value, 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']); CRM_Core_BAO_CustomValue::create($cvParams); } } } return $contact; }