Commit 821d46b7 authored by eileen's avatar eileen 🎱

Merge pull request #3069 from eileenmcnaughton/CRM-14449

CRM-14449 api custom data behaviour - preliminary tidy up
parents c946e381 8295042e
......@@ -283,7 +283,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
public static function create(&$params) {
// check required params
if (!self::dataExists($params)) {
CRM_Core_Error::fatal('Not enough data to create activity object,');
throw new CRM_Core_Exception('Not enough data to create activity object');
}
$activity = new CRM_Activity_DAO_Activity();
......@@ -494,7 +494,9 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
$t = array_slice($params['target_contact_id'], 0, 1);
$recentContactId = $t[0];
}
elseif (isset($params['target_contact_id'])) {
//is array check fixes warning without degrading functionality but it seems this bit of code may no longer work
// as it may always be an array
elseif (isset($params['target_contact_id']) && !is_array($params['target_contact_id'])) {
$msgs[] = "target={$params['target_contact_id']}";
// will be used for recently viewed display
$recentContactId = $params['target_contact_id'];
......
......@@ -56,7 +56,7 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship {
*/
static function create(&$params, $ids = array()) {
$valid = $invalid = $duplicate = $saved = 0;
$relationships = array();
$relationships = $relationshipIds = array();
$relationshipId = CRM_Utils_Array::value('relationship', $ids, CRM_Utils_Array::value('id', $params));
//CRM-9015 - the hooks are called here & in add (since add doesn't call create)
// but in future should be tidied per ticket
......
......@@ -239,7 +239,7 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution {
* @access public
* @static
*/
static function &create(&$params, $ids = array()) {
static function create(&$params, $ids = array()) {
$dateFields = array('receive_date', 'cancel_date', 'receipt_date', 'thankyou_date');
foreach ($dateFields as $df) {
if (isset($params[$df])) {
......
......@@ -192,7 +192,7 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
* @access public
* @static
*/
static function &create(&$params) {
static function create(&$params) {
$transaction = new CRM_Core_Transaction();
$status = NULL;
......@@ -209,7 +209,7 @@ class CRM_Event_BAO_Participant extends CRM_Event_DAO_Participant {
}
if ((!CRM_Utils_Array::value('id', $params)) ||
($params['status_id'] != $status)
(isset($params['status_id']) && $params['status_id'] != $status)
) {
CRM_Activity_BAO_Activity::addActivity($participant);
}
......
......@@ -134,6 +134,7 @@ function civicrm_api3_case_create($params) {
function _civicrm_api3_case_get_spec(&$params) {
$params['contact_id']['api.aliases'] = array('client_id');
$params['contact_id']['title'] = 'Case Client';
$params['creator_id']['api.default'] = 'user_contact_id';
}
/**
......@@ -234,7 +235,7 @@ SELECT DISTINCT case_id
WHERE (contact_id_a = $contact
OR contact_id_b = $contact)
AND case_id IS NOT NULL";
$dao = &CRM_Core_DAO::executeQuery($sql);
$dao = CRM_Core_DAO::executeQuery($sql);
$cases = array();
while ($dao->fetch()) {
......@@ -372,8 +373,11 @@ function civicrm_api3_case_delete($params) {
*
* @param int $caseId
*
* @return array (reference) case object
* @param $params
*
* @internal param $options
*
* @return array (reference) case object
*/
function _civicrm_api3_case_read($caseId, $options) {
$return = CRM_Utils_Array::value('return', $options, array());
......@@ -412,13 +416,6 @@ function _civicrm_api3_case_read($caseId, $options) {
* Internal function to format create params for processing
*/
function _civicrm_api3_case_format_params(&$params) {
if (!array_key_exists('creator_id', $params)) {
$session = CRM_Core_Session::singleton();
$params['creator_id'] = $session->get('userID');
}
if (empty($params['start_date'])) {
$params['start_date'] = date('YmdHis');
}
// figure out case type id from case type and vice-versa
$caseTypes = CRM_Case_PseudoConstant::caseType('label', FALSE);
if (empty($params['case_type_id'])) {
......
......@@ -49,10 +49,7 @@
* @access public
*/
function civicrm_api3_grant_create($params) {
$values = array();
_civicrm_api3_custom_format_params($params, $values, 'Grant');
$params = array_merge($values, $params);
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'grant');
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Grant');
}
/**
......@@ -77,7 +74,7 @@ function _civicrm_api3_grant_create_spec(&$params) {
* @access public
*/
function civicrm_api3_grant_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Grant');
}
/**
......
......@@ -200,7 +200,7 @@ function civicrm_api3_membership_get($params) {
if ($options['is_count']) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
$membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
$membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE, 'Membership');
$return = $options['return'];
if(empty($membershipValues) ||
......
......@@ -711,7 +711,7 @@ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $
'sort' => CRM_Utils_Rule::string($sort) ? $sort : NULL,
'limit' => CRM_Utils_Rule::integer($limit) ? $limit : NULL,
'is_count' => $is_count,
'return' => !empty($returnProperties) ? $returnProperties : NULL,
'return' => !empty($returnProperties) ? $returnProperties : array(),
);
if ($options['sort'] && stristr($options['sort'], 'SELECT')) {
......@@ -831,15 +831,6 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE,
if(isset($dao->count)) {
return $dao->count;
}
//if custom fields are required we will endeavour to set them . NB passing $entity in might be a bit clunky / unrequired
if (!empty($entity) && !empty($params['return']) && is_array($params['return'])) {
foreach ($params['return'] as $return) {
if (substr($return, 0, 6) == 'custom') {
$custom = TRUE;
}
}
}
$fields = array_keys(_civicrm_api3_build_fields_array($dao, $uniqueFields));
......@@ -854,7 +845,8 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE,
}
}
$result[$dao->id] = $tmp;
if (!empty($custom)) {
if(_civicrm_api3_custom_fields_are_required($entity, $params)) {
_civicrm_api3_custom_data_get($result[$dao->id], $entity, $dao->id);
}
}
......@@ -863,6 +855,27 @@ function _civicrm_api3_dao_to_array($dao, $params = NULL, $uniqueFields = TRUE,
return $result;
}
/**
* We currently retrieve all custom fields or none at this level so if we know the entity
* && it can take custom fields & there is the string 'custom' in their return request we get them all, they are filtered on the way out
* @todo filter so only required fields are queried
*
* @param $params
* @param string $entity - entity name in CamelCase
*
* @return bool
*/
function _civicrm_api3_custom_fields_are_required($entity, $params) {
if (!array_key_exists($entity, CRM_Core_BAO_CustomQuery::$extendsMap)) {
return FALSE;
}
$options = _civicrm_api3_get_options_from_params($params);
//we check for possibility of 'custom' => 1 as well as specific custom fields
$returnString = implode('', $options['return']) . implode('', array_keys($options['return']));
if(stristr($returnString, 'custom')) {
return TRUE;
}
}
/**
* Converts an object to an array
*
......@@ -910,6 +923,22 @@ function _civicrm_api3_custom_format_params($params, &$values, $extends, $entity
}
}
/**
* @param $params
* @param $entity
*/
function _civicrm_api3_format_params_for_create(&$params, $entity) {
$nonGenericEntities = array('Contact', 'Individual', 'Household', 'Organization');
$customFieldEntities = array_diff_key(CRM_Core_BAO_CustomQuery::$extendsMap, array_fill_keys($nonGenericEntities, 1));
if(!array_key_exists($entity, $customFieldEntities)) {
return;
}
$values = array();
_civicrm_api3_custom_format_params($params, $values, $entity);
$params = array_merge($params, $values);
}
/**
* @deprecated
* This function ensures that we have the right input parameters
......@@ -993,7 +1022,7 @@ function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $
return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity, 'get');
}
else {
return _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity);
return _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity, 'get');
}
}
......@@ -1005,7 +1034,7 @@ function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $
* @return array
*/
function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
_civicrm_api3_format_params_for_create($params, $entity);
$args = array(&$params);
if (!empty($entity)) {
$ids = array($entity => CRM_Utils_Array::value('id', $params));
......@@ -1125,7 +1154,7 @@ function _civicrm_api3_basic_delete($bao_name, &$params) {
* @param string $subName - Subtype of entity
*/
function _civicrm_api3_custom_data_get(&$returnArray, $entity, $entity_id, $groupID = NULL, $subType = NULL, $subName = NULL) {
$groupTree = &CRM_Core_BAO_CustomGroup::getTree($entity,
$groupTree = CRM_Core_BAO_CustomGroup::getTree($entity,
CRM_Core_DAO::$_nullObject,
$entity_id,
$groupID,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment