/** * 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(); }
/** * 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 * ' * * @throws Exception * @return array|CRM_Error * @access public */ function _civicrm_api3_relationship_format_params($params, &$values) { // copy all the relationship fields as is $fields = CRM_Contact_DAO_Relationship::fields(); _civicrm_api3_store_values($fields, $params, $values); $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); if (!empty($params['id'])) { $relation = new CRM_Contact_BAO_Relationship(); $relation->id = $params['id']; if (!$relation->find(TRUE)) { throw new Exception('Relationship id is not valid'); } else { if (isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a || isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) { throw new Exception('Cannot change the contacts once relationship has been created'); } else { // since the BAO function is not std & won't accept just 'id' (aargh) let's // at least return our BAO here $values = array(); _civicrm_api3_object_to_array($relation, $values); $values = array_merge($values, $params); // and we need to reformat our date fields.... $dateFields = array('start_date', 'end_date'); foreach ($dateFields as $dateField) { $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]); } } } } foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'contact_id_a': case 'contact_id_b': if (!CRM_Utils_Rule::integer($value)) { throw new Exception("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) { throw new Exception("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } break; case 'relationship_type': foreach ($relationTypes as $relTypId => $relValue) { if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { $relationshipTypeId = $relTypId; break; } } if ($relationshipTypeId) { if (!empty($values['relationship_type_id']) && $relationshipTypeId != $values['relationship_type_id']) { throw new Exception('Mismatched Relationship Type and Relationship Type Id'); } $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; } else { throw new Exception('Invalid Relationship Type'); } case 'relationship_type_id': if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { throw new Exception("{$key} not a valid: {$value}"); } // execute for both relationship_type and relationship_type_id $relation = $relationTypes[$params['relationship_type_id']]; if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) { throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); } if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) { throw new Exception("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_api3_custom_format_params($params, $values, 'Relationship'); return array(); }
/** * returns the list of fields that can be exported * * @access public * return array */ function &export($prefix = false) { if (!$GLOBALS['_CRM_CONTACT_DAO_RELATIONSHIP']['_export']) { $GLOBALS['_CRM_CONTACT_DAO_RELATIONSHIP']['_export'] = array(); $fields =& CRM_Contact_DAO_Relationship::fields(); foreach ($fields as $name => $field) { if (CRM_Utils_Array::value('export', $field)) { if ($prefix) { $GLOBALS['_CRM_CONTACT_DAO_RELATIONSHIP']['_export']['relationship'] =& $fields[$name]; } else { $GLOBALS['_CRM_CONTACT_DAO_RELATIONSHIP']['_export'][$name] =& $fields[$name]; } } } } return $GLOBALS['_CRM_CONTACT_DAO_RELATIONSHIP']['_export']; }