/** * Create a new Activity. * * Creates a new Activity record and returns the newly created * activity object (including the contact_id property). Minimum * required data values for the various contact_type are: * * Properties which have administratively assigned sets of values * If an unrecognized value is passed, an error * will be returned. * * Modules may invoke crm_get_contact_values($contactID) to * retrieve a list of currently available values for a given * property. * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param string $activity_type Which class of contact is being created. * Valid values = 'SMS', 'Meeting', 'Event', 'PhoneCall'. * {@schema Activity/Activity.xml} * * @return CRM_Activity|CRM_Error Newly created Activity object * */ function &civicrm_activity_create(&$params) { _civicrm_initialize(); $errors = array(); // check for various error and required conditions $errors = _civicrm_activity_check_params($params, true); if (!empty($errors)) { return $errors; } // processing for custom data $values = array(); _civicrm_custom_format_params($params, $values, 'Activity'); if (!empty($values['custom'])) { $params['custom'] = $values['custom']; } if (!CRM_Utils_Array::value('activity_type_id', $params)) { $params['activity_type_id'] = CRM_Core_OptionGroup::getValue('activity_type', $params['activity_name'], 'name'); } // create activity $activity = CRM_Activity_BAO_Activity::create($params); if (!is_a($activity, 'CRM_Core_Error') && isset($activity->id)) { $activityArray = array('is_error' => 0); } else { $activityArray = array('is_error' => 1); } _civicrm_object_to_array($activity, $activityArray); return $activityArray; }
/** * 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|error * @access public */ function _civicrm_membership_format_params(&$params, &$values, $create = false) { require_once "CRM/Member/DAO/Membership.php"; $fields =& CRM_Member_DAO_Membership::fields(); _civicrm_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 civicrm_create_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 civicrm_create_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 civicrm_create_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; } } _civicrm_custom_format_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; }
/** * @todo Write sth * @todo Serious FIXMES in the code! File issues. */ function civicrm_contact_update(&$params, $create_new = FALSE) { _civicrm_initialize(); try { civicrm_api_check_permission(__FUNCTION__, $params, TRUE); } catch (Exception $e) { return civicrm_create_error($e->getMessage()); } require_once 'CRM/Utils/Array.php'; $entityId = CRM_Utils_Array::value('contact_id', $params, NULL); if (!CRM_Utils_Array::value('contact_type', $params) && $entityId) { $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId); } $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); $values = civicrm_contact_check_params($params, $dupeCheck); if ($values) { return $values; } if ($create_new) { // Make sure nothing is screwed up before we create a new contact if (!empty($entityId)) { return civicrm_create_error('Cannot create new contact when contact_id is present'); } if (empty($params['contact_type'])) { return civicrm_create_error('Contact Type not specified'); } // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { require_once 'CRM/Core/BAO/LocationType.php'; $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } if ($homeUrl = CRM_Utils_Array::value('home_url', $params)) { require_once 'CRM/Core/PseudoConstant.php'; $websiteTypes = CRM_Core_PseudoConstant::websiteType(); $params['website'] = array(1 => array('website_type_id' => key($websiteTypes), 'url' => $homeUrl)); } // FIXME: Some legacy support cruft, should get rid of this in 3.1 $change = array('individual_prefix' => 'prefix', 'prefix' => 'prefix_id', 'individual_suffix' => 'suffix', 'suffix' => 'suffix_id', 'gender' => 'gender_id'); foreach ($change as $field => $changeAs) { if (array_key_exists($field, $params)) { $params[$changeAs] = $params[$field]; unset($params[$field]); } } // End legacy support cruft if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) { $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix()); } if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) { $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix()); } if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) { $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender()); } $error = _civicrm_greeting_format_params($params); if (civicrm_error($error)) { return $error; } $values = array(); if (!($csType = CRM_Utils_Array::value('contact_sub_type', $params)) && $entityId) { require_once 'CRM/Contact/BAO/Contact.php'; $csType = CRM_Contact_BAO_Contact::getContactSubType($entityId); } $customValue = civicrm_contact_check_custom_params($params, $csType); if ($customValue) { return $customValue; } _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId); $params = array_merge($params, $values); $contact =& _civicrm_contact_update($params, $entityId); if (is_a($contact, 'CRM_Core_Error')) { return civicrm_create_error($contact->_errors[0]['message']); } else { $values = array(); $values['contact_id'] = $contact->id; $values['is_error'] = 0; } return $values; }
/** * Update a specified activity. * * Updates activity with the values passed in the 'params' array. An * error is returned if an invalid id or activity Name is passed * @param CRM_Activity $activity A valid Activity object * @param array $params Associative array of property * name/value pairs to be updated. * * @return CRM_Activity|CRM_Core_Error Return the updated ActivtyType Object else * Error Object (if integrity violation) * * @access public * */ function &civicrm_activity_update(&$params) { $errors = array(); //check for various error and required conditions $errors = _civicrm_activity_check_params($params); if (!empty($errors)) { return $errors; } // processing for custom data $values = array(); _civicrm_custom_format_params($params, $values, 'Activity'); if (!empty($values['custom'])) { $params['custom'] = $values['custom']; } $activity = CRM_Activity_BAO_Activity::create($params); $activityArray = array(); _civicrm_object_to_array($activity, $activityArray); return $activityArray; }
/** * 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 * ' * * @return array|CRM_Error * @access public */ function _civicrm_pledge_format_params(&$params, &$values, $create = FALSE) { // based on contribution apis - copy all the pledge fields - this function filters out non -valid fields but unfortunately // means we have to put them back where there are 2 names for the field (name in table & unique name) // since there is no clear std to use one or the other. Generally either works ? but not for create date // perhaps we should just copy $params across rather than run it through the 'filter'? // but at least the filter forces anomalies into the open. In several cases it turned out the unique names wouldn't work // even though they are 'generally' what is returned in the GET - implying they should $fields = CRM_Pledge_DAO_Pledge::fields(); _civicrm_store_values($fields, $params, $values); //add back the fields we know of that got dropped by the previous function if ($params['pledge_create_date']) { //pledge_create_date will not be formatted by the format params function so change back to create_date $values['create_date'] = $params['pledge_create_date']; } if ($params['create_date']) { //create_date may have been dropped by the $fields function so retrieve it $values['create_date'] = $params['create_date']; } if (array_key_exists('installment_amount', $params)) { //field has been renamed - don't lose it! Note that this must be called // installment amount not pledge_installment_amount, pledge_original_installment_amount // or original_installment_amount to avoid error // Division by zero in CRM\Pledge\BAO\PledgePayment.php:162 // but we should accept the variant because they are all 'logical assumptions' based on the // 'standards' $values['installment_amount'] = $params['installment_amount']; } if (array_key_exists('original_installment_amount', $params)) { $values['installment_amount'] = $params['original_installment_amount']; } if (array_key_exists('pledge_original_installment_amount', $params)) { $values['installment_amount'] = $params['pledge_original_installment_amount']; } if (array_key_exists('status_id', $params)) { $values['pledge_status_id'] = $params['status_id']; } if ($params['contact_id']) { //this is validity checked further down to make sure the contact exists $values['pledge_contact_id'] = $params['contact_id']; } if (array_key_exists('id', $params)) { //retrieve the id key dropped from params. Note we can't use pledge_id because it //causes an error in CRM_Pledge_BAO_PledgePayment - approx line 302 $values['id'] = $params['id']; } if (array_key_exists('pledge_id', $params)) { //retrieve the id key dropped from params. Note we can't use pledge_id because it //causes an error in CRM_Pledge_BAO_PledgePayment - approx line 302 $values['id'] = $params['pledge_id']; unset($values['pledge_id']); } if (array_key_exists('status_id', $params)) { $values['pledge_status_id'] = $params['status_id']; } if (empty($values['id'])) { //at this point both should be the same so unset both if not set - passing in empty //value causes crash rather creating new - do it before next section as null values ignored in 'switch' unset($values['id']); } if (!empty($params['scheduled_date'])) { //scheduled date is required to set next payment date - defaults to start date $values['scheduled_date'] = $params['scheduled_date']; } elseif (array_key_exists('start_date', $params)) { $values['scheduled_date'] = $params['start_date']; } if (CRM_Utils_Array::value('contribution_type_id', $params)) { $values['contribution_type_id'] = $params['contribution_type_id']; } foreach ($values as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'pledge_contact_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_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 civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['pledge_contact_id']; unset($values['pledge_contact_id']); break; case 'pledge_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_error("contact_id not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_pledge WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } break; case 'create_date': case 'scheduled_date': case 'start_date': if (!CRM_Utils_Rule::datetime($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'installment_amount': case 'amount': if (!CRM_Utils_Rule::money($value)) { return civicrm_create_error("{$key} not a valid amount: {$value}"); } break; case 'currency': if (!CRM_Utils_Rule::currencyCode($value)) { return civicrm_create_error("currency not a valid code: {$value}"); } break; case 'contribution_type_id': require_once 'CRM/Contribute/PseudoConstant.php'; $typeId = CRM_Contribute_PseudoConstant::contributionType($value); if (!CRM_Utils_Rule::integer($value) || !$typeId) { return civicrm_create_error("contribution type id is not valid: {$value}"); } default: break; } } //format the parameters _civicrm_custom_format_params($params, $values, 'Pledge'); return array(); }
/** * 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 * ' * * @return array|CRM_Error * @access public */ function _civicrm_relationship_format_params(&$params, &$values) { // copy all the relationship fields as is $fields = CRM_Contact_DAO_Relationship::fields(); _civicrm_store_values($fields, $params, $values); $relationTypes = CRM_Core_PseudoConstant::relationshipType('name', TRUE); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc require_once 'CRM/Utils/System.php'; if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'contact_id_a': case 'contact_id_b': require_once 'CRM/Utils/Rule.php'; if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_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 civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } break; case 'start_date': case 'end_date': if (!CRM_Utils_Rule::qfDate($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'relationship_type': foreach ($relationTypes as $relTypId => $relValue) { if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { $relationshipTypeId = $relTypId; break; } } if ($relationshipTypeId) { if (CRM_Utils_Array::value('relationship_type_id', $values) && $relationshipTypeId != $values['relationship_type_id']) { return civicrm_create_error('Mismatched Relationship Type and Relationship Type Id'); } $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; } else { return civicrm_create_error('Invalid Relationship Type'); } case 'relationship_type_id': if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { return civicrm_create_error("{$key} not a valid: {$value}"); } // execute for both relationship_type and relationship_type_id $relation = $relationTypes[$params['relationship_type_id']]; require_once 'CRM/Contact/BAO/Contact.php'; if ($relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) { return civicrm_create_error("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); } if ($relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) { return civicrm_create_error("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}"); } break; default: break; } } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } _civicrm_custom_format_params($params, $values, 'Relationship'); return array(); }
/** * @todo Write sth * @todo Serious FIXMES in the code! File issues. */ function civicrm_contact_update(&$params, $create_new = false) { _civicrm_initialize(); require_once 'CRM/Utils/Array.php'; $contactID = CRM_Utils_Array::value('contact_id', $params); if ($create_new) { // Make sure nothing is screwed up before we create a new contact if (!empty($contactID)) { return civicrm_create_error('Cannot create new contact when contact_id is present'); } if (empty($params['contact_type'])) { return civicrm_create_error('Contact Type not specified'); } $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, false); $values = civicrm_contact_check_params($params, $dupeCheck); if ($values) { return $values; } // If we get here, we're ready to create a new contact if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { require_once 'CRM/Core/BAO/LocationType.php'; $defLocType = CRM_Core_BAO_LocationType::getDefault(); $params['email'] = array(1 => array('email' => $email, 'is_primary' => 1, 'location_type_id' => $defLocType->id ? $defLocType->id : 1)); } } // FIXME: Some legacy support cruft, should get rid of this in 3.1 $change = array('individual_prefix' => 'prefix', 'prefix' => 'prefix_id', 'individual_suffix' => 'suffix', 'suffix' => 'suffix_id', 'gender' => 'gender_id'); foreach ($change as $field => $changeAs) { if (array_key_exists($field, $params)) { $params[$changeAs] = $params[$field]; unset($params[$field]); } } // End legacy support cruft if (isset($params['suffix_id']) && !is_numeric($params['suffix_id'])) { $params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix()); } if (isset($params['prefix_id']) && !is_numeric($params['prefix_id'])) { $params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix()); } if (isset($params['gender_id']) && !is_numeric($params['gender_id'])) { $params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender()); } $values = array(); $entityId = CRM_Utils_Array::value('contact_id', $params, null); _civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId); $params = array_merge($params, $values); $contact =& _civicrm_contact_update($params, $contactID); if (is_a($contact, 'CRM_Core_Error')) { return civicrm_create_error($contact->_errors[0]['message']); } else { $values = array(); $values['contact_id'] = $contact->id; $values['is_error'] = 0; } return $values; }
/** * 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 * ' * * @return array|CRM_Error * @access public */ function _civicrm_pledgepayment_format_params(&$params, &$values, $create = FALSE) { // copy all the pledge fields as is require_once 'CRM/Pledge/BAO/PledgePayment.php'; require_once 'CRM/Pledge/DAO/Pledge.php'; $fields = CRM_Pledge_DAO_Pledge::fields(); _civicrm_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 'pledge_contact_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_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 civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['pledge_contact_id']; unset($values['pledge_contact_id']); break; case 'receive_date': case 'end_date': case 'pledge_create_date': case 'cancel_date': case 'receipt_date': case 'thankyou_date': if (!CRM_Utils_Rule::date($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'non_deductible_amount': case 'total_amount': case 'fee_amount': case 'net_amount': if (!CRM_Utils_Rule::money($value)) { return civicrm_create_error("{$key} not a valid amount: {$value}"); } break; case 'currency': if (!CRM_Utils_Rule::currencyCode($value)) { return civicrm_create_error("currency not a valid code: {$value}"); } break; case 'pledge_type': $values['pledge_type_id'] = CRM_Utils_Array::key(ucfirst($value), CRM_Pledge_PseudoConstant::pledgeType()); break; case 'payment_instrument': require_once 'CRM/Core/OptionGroup.php'; $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value); break; default: break; } } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } if (array_key_exists('installment_amount', $params)) { $values['installment_amount'] = $params['installment_amount']; } // testing testing - how do I make it take a create_date? It needs $values['create_date'] set but doesn't seem to like it because $fields calls it $pledge_create_date //ditto scheduled date. I don't know why this is needs to be done because I don't fully understand the code above if (array_key_exists('pledge_create_date', $params)) { $values['create_date'] = $params['pledge_create_date']; } if (array_key_exists('pledge_scheduled_date', $params)) { $values['scheduled_date'] = $params['pledge_scheduled_date']; } if (array_key_exists('pledge_create_date', $params)) { $values['create_date'] = $params['pledge_create_date']; } if (array_key_exists('status_id', $params)) { $values['status_id'] = $params['status_id']; $values['pledge_status_id'] = $params['status_id']; } _civicrm_custom_format_params($params, $values, 'Pledge'); if ($create) { // CRM_pledge_BAO_Pledge::add() handles Pledge_source // So, if $values contains Pledge_source, convert it to source $changes = array('pledge_source' => 'source'); foreach ($changes as $orgVal => $changeVal) { if (isset($values[$orgVal])) { $values[$changeVal] = $values[$orgVal]; unset($values[$orgVal]); } } } return array(); }
/** * 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 * ' * @return array|CRM_Error * @access public */ function _civicrm_contribute_format_params(&$params, &$values, $create = false) { // copy all the contribution fields as is $fields =& CRM_Contribute_DAO_Contribution::fields(); _civicrm_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 'contribution_contact_id': if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_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 civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } $values['contact_id'] = $values['contribution_contact_id']; unset($values['contribution_contact_id']); break; case 'receive_date': case 'cancel_date': case 'receipt_date': case 'thankyou_date': if (!CRM_Utils_Rule::date($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'non_deductible_amount': case 'total_amount': case 'fee_amount': case 'net_amount': if (!CRM_Utils_Rule::money($value)) { return civicrm_create_error("{$key} not a valid amount: {$value}"); } break; case 'currency': if (!CRM_Utils_Rule::currencyCode($value)) { return civicrm_create_error("currency not a valid code: {$value}"); } break; case 'contribution_type': $values['contribution_type_id'] = CRM_Utils_Array::key(ucfirst($value), CRM_Contribute_PseudoConstant::contributionType()); break; case 'payment_instrument': require_once 'CRM/Core/OptionGroup.php'; $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value); break; default: break; } } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } _civicrm_custom_format_params($params, $values, 'Contribution'); if ($create) { // CRM_Contribute_BAO_Contribution::add() handles contribution_source // So, if $values contains contribution_source, convert it to source $changes = array('contribution_source' => 'source'); foreach ($changes as $orgVal => $changeVal) { if (isset($values[$orgVal])) { $values[$changeVal] = $values[$orgVal]; unset($values[$orgVal]); } } } return array(); }
/** * 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|error * @access public */ function _civicrm_membership_format_params(&$params, &$values, $create = FALSE) { require_once "CRM/Member/DAO/Membership.php"; require_once "CRM/Member/PseudoConstant.php"; $fields = CRM_Member_DAO_Membership::fields(); _civicrm_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 civicrm_create_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 civicrm_create_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 'start_date': case 'end_date': case 'reminder_date': case 'membership_start_date': case 'membership_end_date': if (!CRM_Utils_Rule::date($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } // make sure we format dates to mysql friendly format $values[$key] = CRM_Utils_Date::processDate($value, NULL, FALSE, 'Ymd'); break; case 'membership_type_id': if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipType())) { return civicrm_create_error('Invalid Membership Type Id'); } $values[$key] = $value; break; case 'membership_type': $membershipTypeId = CRM_Utils_Array::key(ucfirst($value), CRM_Member_PseudoConstant::membershipType()); if ($membershipTypeId) { if (CRM_Utils_Array::value('membership_type_id', $values) && $membershipTypeId != $values['membership_type_id']) { return civicrm_create_error('Mismatched membership Type and Membership Type Id'); } } else { return civicrm_create_error('Invalid Membership Type'); } $values['membership_type_id'] = $membershipTypeId; break; case 'status_id': if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipStatus())) { return civicrm_create_error('Invalid Membership Status Id'); } $values[$key] = $value; break; default: break; } } _civicrm_custom_format_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; }