$name = 'Contact';
}
- //hack to deal with incorrectly named BAO/DAO - see CRM-10859 - remove after rename
- if($name == 'price_set' || $name == 'PriceSet'){
- return 'CRM_Price_DAO_Set';
- }
- if($name == 'price_field' || $name == 'PriceField'){
- return 'CRM_Price_DAO_Field';
- }
- if($name == 'price_field_value' || $name == 'PriceFieldValue'){
- return 'CRM_Price_DAO_FieldValue';
- }
- // these aren't listed on ticket CRM-10859 - but same problem - lack of standardisation
- if($name == 'mailing_job' || $name == 'MailingJob'){
- return 'CRM_Mailing_BAO_Job';
- }
+ //hack to deal with incorrectly named BAO/DAO - see CRM-10859 -
+ // several of these have been removed but am not confident mailing_recipients is
+ // tests so have not tackled.
+ // correct approach for im is unclear
if($name == 'mailing_recipients' || $name == 'MailingRecipients'){
return 'CRM_Mailing_BAO_Recipients';
}
function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $entity) {
//if fieldname exists in params
if (CRM_Utils_Array::value($fieldName, $params)) {
- //if value = 'user_contact_id' replace value with logged in user id
- if ($params[$fieldName] == "user_contact_id") {
- $session = &CRM_Core_Session::singleton();
- $params[$fieldName] = $session->get('userID');
+ // if value = 'user_contact_id' (or similar), replace value with contact id
+ if (!is_numeric($params[$fieldName]) && is_scalar($params[$fieldName])) {
+ $realContactId = _civicrm_api3_resolve_contactID($params[$fieldName]);
+ if ('unknown-user' === $realContactId) {
+ throw new API_Exception("\"$fieldName\" \"{$params[$fieldName]}\" cannot be resolved to a contact ID", 2002, array('error_field' => $fieldName,"type"=>"integer"));
+ } elseif (is_numeric($realContactId)) {
+ $params[$fieldName] = $realContactId;
+ }
}
if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) {
_civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo);
}
}
+/**
+ * Determine a contact ID using a string expression
+ *
+ * @param string $contactIdExpr e.g. "user_contact_id" or "@user:username"
+ * @return int|NULL|'unknown-user'
+ */
+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)) {
+ $config = CRM_Core_Config::singleton();
+
+ $ufID = $config->userSystem->getUfId($matches[1]);
+ if (!$ufID) {
+ return 'unknown-user';
+ }
+
+ $contactID = CRM_Core_BAO_UFMatch::getContactId($ufID);
+ if (!$contactID) {
+ return 'unknown-user';
+ }
+
+ return $contactID;
+ }
+ return NULL;
+}
+
function _civicrm_api3_validate_html(&$params, &$fieldName, &$fieldInfo) {
if ($value = CRM_Utils_Array::value($fieldName, $params)) {
if (!CRM_Utils_Rule::xssString($value)) {
'action' => 'create',
));
$meta = $result['values'];
+ if (!isset($meta[$fieldName]['name']) && isset($meta[$fieldName . '_id'])) {
+ $fieldName = $fieldName . '_id';
+ }
if (isset($meta[$fieldName])) {
return $meta[$fieldName]['name'];
}