/** * This function adds the activity history variable in $values to the * parameter list $params. For most cases, $values should have length 1. * * @param array $values The variable(s) to be added * @param array $params The structured parameter list * * @return bool|CRM_Utils_Error * @access public */ function _crm_add_formatted_history_param(&$values, &$params) { /* Cache the various object fields */ static $fields = null; if ($fields == null) { $fields = array(); } //print_r($values); //print_r($params); if (isset($values['activity_type'])) { $params['activity_type'] = $values['activity_type']; return true; } if (isset($values['activity_date'])) { $params['activity_date'] = $values['activity_date']; return true; } if (isset($values['activity_id'])) { $params['activity_id'] = $values['activity_id']; return true; } /* Check for custom field values */ if ($fields['custom'] == null) { $fields['custom'] =& CRM_Core_BAO_CustomField::getFields('Contribution'); } foreach ($values 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, $fields['custom'])) { return _crm_error('Invalid custom field ID'); } if (!isset($params['custom'])) { $params['custom'] = array(); } // fixed for Import $newMulValues = array(); if ($fields['custom'][$customFieldID][3] == 'CheckBox' || $fields['custom'][$customFieldID][3] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($v1))) { $newMulValues[] = $v2['value']; } } } $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $newMulValues); } else { if ($fields['custom'][$customFieldID][3] == 'Select' || $fields['custom'][$customFieldID][3] == 'Radio') { $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($custuomOption as $v2) { if (strtolower($v2['label']) == strtolower(trim($value))) { $value = $v2['value']; break; } } } } $customBlock = count($params['custom']) + 1; $params['custom'][$customBlock] = array('custom_field_id' => $customFieldID, 'value' => $value, 'type' => $fields['custom'][$customFieldID][2], 'name' => $fields['custom'][$customFieldID][0]); } } /* Finally, check for contribution fields */ if (!isset($fields['History'])) { $fields['History'] =& CRM_Core_DAO_ActivityHistory::fields(); } _crm_store_values($fields['History'], $values, $params); }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * * @param array $create Is the formatted Values array going to * be used for CRM_Member_BAO_Membership:create() * * @return array|CRM_Error * @access public */ function _crm_format_membership_params(&$params, &$values, $create = false) { require_once "CRM/Member/DAO/Membership.php"; $fields =& CRM_Member_DAO_Membership::fields(); _crm_store_values($fields, $params, $values); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'membership_contact_id': if (!CRM_Utils_Rule::integer($value)) { return _crm_error("contact_id not valid: {$value}"); } $dao =& new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams); if (!$svq) { return _crm_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['membership_contact_id']; unset($values['membership_contact_id']); break; case 'join_date': case 'membership_start_date': case 'membership_end_date': if (!CRM_Utils_Rule::date($value)) { return _crm_error("{$key} not a valid date: {$value}"); } break; case 'membership_type_id': $id = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_MembershipType", $value, 'id', 'name'); $values[$key] = $id; break; case 'status_id': $id = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_MembershipStatus", $value, 'id', 'name'); $values[$key] = $id; break; default: break; } } _crm_format_custom_params($params, $values, 'Membership'); if ($create) { // CRM_Member_BAO_Membership::create() handles membership_start_date, // membership_end_date and membership_source. So, if $values contains // membership_start_date, membership_end_date or membership_source, // convert it to start_date, end_date or source $changes = array('membership_start_date' => 'start_date', 'membership_end_date' => 'end_date', 'membership_source' => 'source'); foreach ($changes as $orgVal => $changeVal) { if (isset($values[$orgVal])) { $values[$changeVal] = $values[$orgVal]; unset($values[$orgVal]); } } } return null; }
/** * Update a specified location with the provided property values. * * @param object $contact A valid Contact object (passed by reference). * @param string $location_id Valid (db-level) id for location to be updated. * @param Array $params Associative array of property name/value pairs to be updated * * @return Location object with updated property values * * @access public * */ function crm_update_location(&$contact, $location_id, $params) { _crm_initialize(); if (!isset($contact->id)) { return _crm_error('$contact is not valid contact datatype'); } $locationId = (int) $location_id; if ($locationId == 0) { return _crm_error('missing or invalid $location_id'); } // $locationNumber is the contact-level number of the location (1, 2, 3, etc.) $locationNumber = null; $locations =& crm_get_locations($contact); foreach ($locations as $locNumber => $locValue) { if ($locValue->id == $locationId) { $locationNumber = $locNumber; break; } } if (!$locationNumber) { return _crm_error('invalid $location_id'); } $values = array('contact_id' => $contact->id, 'location' => array($locationNumber => array())); $loc =& $values['location'][$locationNumber]; $loc['address'] = array(); require_once 'CRM/Core/DAO/Address.php'; $fields =& CRM_Core_DAO_Address::fields(); _crm_store_values($fields, $params, $loc['address']); //$ids = array( 'county', 'country_id', 'state_province_id', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name' ); $ids = array('county', 'country_id', 'country', 'state_province_id', 'state_province', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name', 'street_address'); foreach ($ids as $id) { if (array_key_exists($id, $params)) { $loc['address'][$id] = $params[$id]; } } if (is_numeric($loc['address']['state_province'])) { $loc['address']['state_province'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($loc['address']['state_province']); } if (is_numeric($loc['address']['country'])) { $loc['address']['country'] = CRM_Core_PseudoConstant::countryIsoCode($loc['address']['country']); } if (array_key_exists('location_type_id', $params)) { $loc['location_type_id'] = $params['location_type_id']; } if (array_key_exists('location_type', $params)) { $locTypes =& CRM_Core_PseudoConstant::locationType(); $loc['location_type_id'] = CRM_Utils_Array::key($params['location_type'], $locTypes); } if (array_key_exists('name', $params)) { $loc['name'] = $params['name']; } if (array_key_exists('is_primary', $params)) { $loc['is_primary'] = (int) $params['is_primary']; } $loc['id'] = $locationId; $blocks = array('Email', 'Phone', 'IM'); foreach ($blocks as $block) { $name = strtolower($block); $loc[$name] = array(); if ($params[$name]) { $count = 1; foreach ($params[$name] as $val) { CRM_Core_DAO::storeValues($val, $loc[$name][$count++]); } } } $par = array('id' => $contact->id, 'contact_id' => $contact->id); $contact = CRM_Contact_BAO_Contact::retrieve($par, $defaults, $ids); CRM_Contact_BAO_Contact::resolveDefaults($values, true); $location = CRM_Core_BAO_Location::add($values, $ids, $locationNumber); return $location; }