Merge pull request #2998 from yashodha/CRM-14500
[civicrm-core.git] / api / v3 / utils.php
index 3fde44b5aece31a7b904fc4376621eacb85e9920..31c5d626da28e574d3326e3b9c2164e80d13282f 100644 (file)
@@ -197,7 +197,7 @@ function civicrm_api3_create_success($values = 1, $params = array(), $entity = N
       $allFields = array_keys($apiFields['values']);
     }
     $paramFields = array_keys($params);
-    $undefined = array_diff($paramFields, $allFields, array_keys($_COOKIE), array('action', 'entity', 'debug', 'version', 'check_permissions', 'IDS_request_uri', 'IDS_user_agent', 'return', 'sequential', 'rowCount', 'option_offset', 'option_limit', 'custom', 'option_sort', 'options'));
+    $undefined = array_diff($paramFields, $allFields, array_keys($_COOKIE), array('action', 'entity', 'debug', 'version', 'check_permissions', 'IDS_request_uri', 'IDS_user_agent', 'return', 'sequential', 'rowCount', 'option_offset', 'option_limit', 'custom', 'option_sort', 'options', 'prettyprint'));
     if ($undefined) {
       $result['undefined_fields'] = array_merge($undefined);
     }
@@ -234,7 +234,7 @@ function civicrm_api3_create_success($values = 1, $params = array(), $entity = N
   }
   if(!empty($params['options']['metadata'])) {
     // we've made metadata an array but only supporting 'fields' atm
-    if(in_array('fields', $params['options']['metadata'])) {
+    if(in_array('fields', (array) $params['options']['metadata'])) {
       $fields = civicrm_api3($entity, 'getfields', array('action' => substr($action, 0, 3) == 'get' ? 'get' : 'create'));
       $result['metadata']['fields'] = $fields['values'];
     }
@@ -267,7 +267,7 @@ function _civicrm_api3_get_DAO($name) {
     // len ('civicrm_api3_') == 13
     $name = substr($name, 13, $last - 13);
   }
-  
+
   $name = _civicrm_api_get_camel_name($name, 3);
 
   if ($name == 'Individual' || $name == 'Household' || $name == 'Organization') {
@@ -476,6 +476,7 @@ function _civicrm_api3_get_using_query_object($entity, $params, $additional_opti
     }
 
   }
+
   $skipPermissions = !empty($params['check_permissions']) ? 0 :1;
 
   list($entities, $options) = CRM_Contact_BAO_Query::apiQuery(
@@ -569,9 +570,6 @@ function _civicrm_api3_dao_set_filter(&$dao, $params, $unique = TRUE, $entity) {
       }
     }
   }
-  // http://issues.civicrm.org/jira/browse/CRM-9150 - stick with 'simple' operators for now
-  // support for other syntaxes is discussed in ticket but being put off for now
-  $acceptedSQLOperators = array('=', '<=', '>=', '>', '<', 'LIKE', "<>", "!=", "NOT LIKE", 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN');
   if (!$fields) {
     $fields = array();
   }
@@ -1238,7 +1236,8 @@ function _civicrm_api3_validate_date(&$params, &$fieldName, &$fieldInfo) {
     if (strtotime($params[$fieldInfo['name']]) === FALSE) {
       throw new Exception($fieldInfo['name'] . " is not a valid date: " . $params[$fieldInfo['name']]);
     }
-    $params[$fieldInfo['name']] = CRM_Utils_Date::processDate($params[$fieldInfo['name']]);
+    $format = ($fieldInfo['type'] == CRM_Utils_Type::T_DATE) ? 'Ymd000000' : 'YmdHis';
+    $params[$fieldInfo['name']] = CRM_Utils_Date::processDate($params[$fieldInfo['name']], NULL, FALSE, $format);
   }
   if ((CRM_Utils_Array::value('name', $fieldInfo) != $fieldName) && !empty($params[$fieldName])) {
     //If the unique field name differs from the db name & is set handle it here
@@ -1447,23 +1446,17 @@ function _civicrm_api_get_custom_fields($entity, &$params) {
     FALSE,
     FALSE
   );
-  // find out if we have any requests to resolve options
-  $getoptions = CRM_Utils_Array::value('get_options', CRM_Utils_Array::value('options',$params));
-  if(!is_array($getoptions)){
-      $getoptions = array($getoptions);
-  }
+
+  $ret = array();
 
   foreach ($customfields as $key => $value) {
     // Regular fields have a 'name' property
     $value['name'] = 'custom_' . $key;
+    $value['title'] = $value['label'];
     $value['type'] = _getStandardTypeFromCustomDataType($value['data_type']);
-    $customfields['custom_' . $key] = $value;
-    if (in_array('custom_' . $key, $getoptions)) {
-      $customfields['custom_' . $key]['options'] = CRM_Core_BAO_CustomOption::valuesByID($key);
-    }
-    unset($customfields[$key]);
+    $ret['custom_' . $key] = $value;
   }
-  return $customfields;
+  return $ret;
 }
 /**
  * Translate the custom field data_type attribute into a std 'type'
@@ -1581,12 +1574,9 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent
 function  _civicrm_api3_resolve_contactID($contactIdExpr) {
   //if value = 'user_contact_id' replace value with logged in user id
   if ($contactIdExpr == "user_contact_id") {
-    $session = &CRM_Core_Session::singleton();
-    if (!is_numeric($session->get('userID'))) {
-      return NULL;
-    }
-    return $session->get('userID');
-  } elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) {
+    return CRM_Core_Session::getLoggedInContactID();
+  }
+  elseif (preg_match('/^@user:(.*)$/', $contactIdExpr, $matches)) {
     $config = CRM_Core_Config::singleton();
 
     $ufID = $config->userSystem->getUfId($matches[1]);