}
}
}
- //if ( array_key_exists ('debug',$params) && is_object ($dao)) {
- if (is_array($params) && array_key_exists('debug', $params)) {
+
+ if (is_array($params) && !empty($params['debug'])) {
if (is_string($action) && $action != 'getfields') {
$apiFields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => $action) + $params);
}
$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';
}
}
/**
- * This is a wrapper for api_store_values which will check the suitable fields using getfields
+ * This is a legacy wrapper for api_store_values which will check the suitable fields using getfields
* rather than DAO->fields
*
* Getfields has handling for how to deal with uniquenames which dao->fields doesn't
function _civicrm_api3_basic_create($bao_name, &$params, $entity = NULL) {
$args = array(&$params);
- if(!empty($entity)){
+ if (!empty($entity)) {
$ids = array($entity => CRM_Utils_Array::value('id', $params));
$args[] = &$ids;
}
+
if (method_exists($bao_name, 'create')) {
$fct = 'create';
+ $fct_name = $bao_name . '::' . $fct;
+ $bao = call_user_func_array(array($bao_name, $fct), $args);
}
elseif (method_exists($bao_name, 'add')) {
$fct = 'add';
+ $fct_name = $bao_name . '::' . $fct;
+ $bao = call_user_func_array(array($bao_name, $fct), $args);
}
- if (!isset($fct)) {
- return civicrm_api3_create_error('Entity not created, missing create or add method for ' . $bao_name);
+ else {
+ $fct_name = '_civicrm_api3_basic_create_fallback';
+ $bao = _civicrm_api3_basic_create_fallback($bao_name, $params);
}
- $bao = call_user_func_array(array($bao_name, $fct), $args);
+
if (is_null($bao)) {
- return civicrm_api3_create_error('Entity not created ' . $bao_name . '::' . $fct);
+ return civicrm_api3_create_error('Entity not created (' . $fct_name . ')');
}
else {
$values = array();
}
}
+/**
+ * For BAO's which don't have a create() or add() functions, use this fallback implementation.
+ *
+ * FIXME There's an intuitive sense that this behavior should be defined somehow in the BAO/DAO class
+ * structure. In practice, that requires a fair amount of refactoring and/or kludgery.
+ *
+ * @param string $bao_name
+ * @param array $params
+ * @return CRM_Core_DAO|NULL an instance of the BAO
+ */
+function _civicrm_api3_basic_create_fallback($bao_name, &$params) {
+ $entityName = CRM_Core_DAO_AllCoreTables::getBriefName(get_parent_class($bao_name));
+ if (empty($entityName)) {
+ throw new API_Exception("Class \"$bao_name\" does not map to an entity name", "unmapped_class_to_entity", array(
+ 'class_name' => $bao_name,
+ ));
+ }
+ $hook = empty($params['id']) ? 'create' : 'edit';
+
+ CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
+ $instance = new $bao_name();
+ $instance->copyValues($params);
+ $instance->save();
+ CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);
+
+ return $instance;
+}
+
/**
* Function to do a 'standard' api del - when the api is only doing a $bao::del then use this
* if api::del doesn't exist it will try DAO delete method
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'];
}