/** * Defines 'custom value' within a field for a specific entity table/id combination. * * @param $entity_table String Name of the table that this value is attached to * * @param $entity_id int ID of the object in the relevant table * * @param $custom_field object field type of the value * * @param $data Array data appropriate value for the above custom field * * @param $separator String separator for values for ckeckbox. * * @return newly created custom_value object * * @access public * * */ function crm_create_custom_value($entity_table, $entity_id, &$custom_field, &$data, $separator = null) { _crm_initialize(); if (!isset($entity_table)) { return _crm_error("parameter entity_table is not set "); } if (!isset($entity_id)) { return _crm_error("parameter entity_id is not set "); } if (!isset($custom_field->id) && !isset($custom_field->type)) { return _crm_error("field id ot type is not set in custom_field object"); } if ($separator) { $values = explode($separator, $data['value']); require_once 'CRM/Core/BAO/CustomOption.php'; $data['value'] = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $values); } $data['type'] = $custom_field->data_type; $data['custom_field_id'] = $custom_field->id; $data['entity_table'] = $entity_table; $data['entity_id'] = $entity_id; require_once 'CRM/Core/BAO/CustomValue.php'; return CRM_Core_BAO_CustomValue::create($data); }
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; }
/** * takes an associative array and creates a contribution object * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contribute_BAO_Contribution object * @access public * @static */ function &create(&$params, &$ids) { require_once 'CRM/Utils/Money.php'; require_once 'CRM/Utils/Date.php'; // FIXME: a cludgy hack to fix the dates to MySQL format $dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date'); foreach ($dateFields as $df) { if (isset($params[$df])) { $params[$df] = CRM_Utils_Date::isoToMysql($params[$df]); } } CRM_Core_DAO::transaction('BEGIN'); $contribution = CRM_Contribute_BAO_Contribution::add($params, $ids); if (is_a($contribution, 'CRM_Core_Error')) { CRM_Core_DAO::transaction('ROLLBACK'); return $contribution; } $params['contribution_id'] = $contribution->id; // add custom field values if (CRM_Utils_Array::value('custom', $params)) { foreach ($params['custom'] as $customValue) { $cvParams = array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution->id, 'value' => $customValue['value'], 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']); if ($customValue['id']) { $cvParams['id'] = $customValue['id']; } CRM_Core_BAO_CustomValue::create($cvParams); } } // let's create an (or update the relevant) Acitivity History record $contributionType = CRM_Contribute_PseudoConstant::contributionType($contribution->contribution_type_id); if (!$contributionType) { $contributionType = ts('Contribution'); } if (!$GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['insertDate']) { $GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['insertDate'] = CRM_Utils_Date::customFormat(date('Y-m-d H:i')); } $activitySummary = ts('%1 - %2 (updated on %3)', array(1 => CRM_Utils_Money::format($contribution->total_amount, $contribution->currency), 2 => $contributionType, 3 => $GLOBALS['_CRM_CONTRIBUTE_BAO_CONTRIBUTION']['insertDate'])); $historyParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contribution->contact_id, 'activity_type' => $contributionType, 'module' => 'CiviContribute', 'callback' => 'CRM_Contribute_Page_Contribution::details', 'activity_id' => $contribution->id, 'activity_summary' => $activitySummary, 'activity_date' => $contribution->receive_date); if (CRM_Utils_Array::value('contribution', $ids)) { // this contribution should have an Activity History record already $getHistoryParams = array('module' => 'CiviContribute', 'activity_id' => $contribution->id); $getHistoryValues =& CRM_Core_BAO_History::getHistory($getHistoryParams, 0, 1, null, 'Activity'); if (!empty($getHistoryValues)) { $tmp = array_keys($getHistoryValues); $ids['activity_history'] = $tmp[0]; } } $historyDAO =& CRM_Core_BAO_History::create($historyParams, $ids, 'Activity'); if (is_a($historyDAO, 'CRM_Core_Error')) { CRM_Core_Error::fatal("Failed creating Activity History for contribution of id {$contribution->id}"); } CRM_Core_DAO::transaction('COMMIT'); return $contribution; }
/** * takes an associative array and creates a contact object and all the associated * derived objects (i.e. individual, location, email, phone etc) * * This function is invoked from within the web form layer and also from the api layer * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param int $maxLocationBlocks the maximum number of location blocks to process * * @return object CRM_Contact_BAO_Contact object * @access public * @static */ function &create(&$params, &$ids, $maxLocationBlocks) { require_once 'CRM/Utils/Hook.php'; if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::pre('edit', $params['contact_type'], $ids['contact'], $params); } else { CRM_Utils_Hook::pre('create', $params['contact_type'], null, $params); } CRM_Core_DAO::transaction('BEGIN'); $contact = CRM_Contact_BAO_Contact::add($params, $ids); $params['contact_id'] = $contact->id; // invoke the add operator on the contact_type class require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $params['contact_type']) . ".php"; eval('$contact->contact_type_object =& CRM_Contact_BAO_' . $params['contact_type'] . '::add($params, $ids);'); $location = array(); for ($locationId = 1; $locationId <= $maxLocationBlocks; $locationId++) { // start of for loop for location $location[$locationId] = CRM_Core_BAO_Location::add($params, $ids, $locationId); } $contact->location = $location; // add notes if (CRM_Utils_Array::value('note', $params)) { if (is_array($params['note'])) { foreach ($params['note'] as $note) { $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $note['note']); CRM_Core_BAO_Note::add($noteParams); } } else { $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $params['note']); CRM_Core_BAO_Note::add($noteParams); } } // update the UF email if that has changed require_once 'CRM/Core/BAO/UFMatch.php'; CRM_Core_BAO_UFMatch::updateUFEmail($contact->id); // add custom field values if (CRM_Utils_Array::value('custom', $params)) { foreach ($params['custom'] as $customValue) { $cvParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contact->id, 'value' => $customValue['value'], 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']); if ($customValue['id']) { $cvParams['id'] = $customValue['id']; } CRM_Core_BAO_CustomValue::create($cvParams); } } // make a civicrm_subscription_history entry only on contact create (CRM-777) if (!CRM_Utils_Array::value('contact', $ids)) { $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin'); CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams); } CRM_Core_DAO::transaction('COMMIT'); if (CRM_Utils_Array::value('contact', $ids)) { CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact); } else { CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact); } $contact->contact_type_display = CRM_Contact_DAO_Contact::tsEnum('contact_type', $contact->contact_type); return $contact; }