/** * params must contain at least id=xx & {one of the fields from getfields}=value */ function civicrm_api3_generic_setValue($apiRequest) { $entity = $apiRequest['entity']; $params = $apiRequest['params']; // we can't use _spec, doesn't work with generic civicrm_api3_verify_mandatory($params, NULL, array('id', 'field', 'value')); $id = $params['id']; if (!is_numeric($id)) { return civicrm_api3_create_error(ts('Please enter a number'), array('error_code' => 'NaN', 'field' => "id")); } $field = CRM_Utils_String::munge($params['field']); $value = $params['value']; $fields = civicrm_api($entity, 'getFields', array("version" => 3, "sequential")); // getfields error, shouldn't happen. if ($fields['is_error']) { return $fields; } $fields = $fields['values']; if (!array_key_exists($field, $fields)) { return civicrm_api3_create_error("Param 'field' ({$field}) is invalid. must be an existing field", array("error_code" => "invalid_field", "fields" => array_keys($fields))); } $def = $fields[$field]; if (array_key_exists('required', $def) && empty($value)) { return civicrm_api3_create_error(ts("This can't be empty, please provide a value"), array("error_code" => "required", "field" => $field)); } switch ($def['type']) { case 1: //int if (!is_numeric($value)) { return civicrm_api3_create_error("Param '{$field}' must be a number", array('error_code' => 'NaN')); } case 2: //string require_once "CRM/Utils/Rule.php"; if (!CRM_Utils_Rule::xssString($value)) { return civicrm_api3_create_error(ts('Illegal characters in input (potential scripting attack)'), array('error_code' => 'XSS')); } if (array_key_exists('maxlength', $def)) { $value = substr($value, 0, $def['maxlength']); } break; case 16: //boolean $value = (bool) $value; break; case 4: //date //date default: return civicrm_api3_create_error("Param '{$field}' is of a type not managed yet. Join the API team and help us implement it", array('error_code' => 'NOT_IMPLEMENTED')); } if (CRM_Core_DAO::setFieldValue(_civicrm_api3_get_DAO($entity), $id, $field, $value)) { $entity = array('id' => $id, $field => $value); CRM_Utils_Hook::post('edit', $entity, $id, $entity); return civicrm_api3_create_success($entity); } else { return civicrm_api3_create_error("error assigning {$field}={$value} for {$entity} (id={$id})"); } }
/** * create/update contact_type * * This API is used to create new contact_type or update any of the existing * In case of updating existing contact_type, id of that particular contact_type must * be in $params array. * * @param array $params (referance) Associative array of property * name/value pairs to insert in new 'contact_type' * * @return array contact_type array * * @access public */ function civicrm_api3_contact_type_create($params) { civicrm_api3_verify_mandatory($params, _civicrm_api3_get_DAO(__FUNCTION__), array('name', 'parent_id')); if (!array_key_exists('label', $params)) { $params['label'] = $params['name']; } if (!array_key_exists('is_active', $params)) { $params['is_active'] = TRUE; } $params['name'] = CRM_Utils_String::munge($params['name']); return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); }
/** * Create or update case type. * * @param array $params * Input parameters. * * @throws API_Exception * @return array * API result array */ function civicrm_api3_case_type_create($params) { civicrm_api3_verify_mandatory($params, _civicrm_api3_get_DAO(__FUNCTION__)); // Computed properties. unset($params['is_forkable']); unset($params['is_forked']); if (!array_key_exists('is_active', $params) && empty($params['id'])) { $params['is_active'] = TRUE; } // This is an existing case-type. if (!empty($params['id']) && !CRM_Case_BAO_CaseType::isForked($params['id']) && !CRM_Case_BAO_CaseType::isForkable($params['id'])) { unset($params['definition']); } $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'CaseType'); return _civicrm_api3_case_type_get_formatResult($result); }
/** * @param entityName */ private function getMockableBAOObjects($entityName, $count = 2) { $baoString = _civicrm_api3_get_DAO($entityName); if (empty($baoString)) { $this->markTestIncomplete("Entity [{$entityName}] cannot be mocked - no known DAO"); return; } $baos = array(); $i = 0; while ($i < $count) { // create entities $baoObj = CRM_Core_DAO::createTestObject($baoString, array('currency' => 'USD')); $this->assertTrue(is_integer($baoObj->id), 'check first id'); $this->deletableTestObjects[$baoString][] = $baoObj->id; $baos[] = $baoObj; $i++; } return $baos; }
function testGetDAO() { $params = array('civicrm_api3_custom_group_get' => 'CRM_Core_DAO_CustomGroup', 'custom_group' => 'CRM_Core_DAO_CustomGroup', 'CustomGroup' => 'CRM_Core_DAO_CustomGroup', 'civicrm_api3_custom_field_get' => 'CRM_Core_DAO_CustomField', 'civicrm_api3_survey_get' => 'CRM_Campaign_DAO_Survey', 'civicrm_api3_pledge_payment_get' => 'CRM_Pledge_DAO_PledgePayment', 'civicrm_api3_website_get' => 'CRM_Core_DAO_Website', 'Membership' => 'CRM_Member_DAO_Membership'); foreach ($params as $input => $expected) { $result = _civicrm_api3_get_DAO($input); $this->assertEquals($expected, $result); } }
/** * Returns fields allowable by api. * * @param $entity * String Entity to query. * @param bool $unique * Index by unique fields?. * @param array $params * * @return array */ function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = array()) { $unsetIfEmpty = array('dataPattern', 'headerPattern', 'default', 'export', 'import'); $dao = _civicrm_api3_get_DAO($entity); if (empty($dao)) { return array(); } $d = new $dao(); $fields = $d->fields(); // replace uniqueNames by the normal names as the key if (empty($unique)) { foreach ($fields as $name => &$field) { //getting rid of unused attributes foreach ($unsetIfEmpty as $attr) { if (empty($field[$attr])) { unset($field[$attr]); } } if ($name == $field['name']) { continue; } if (array_key_exists($field['name'], $fields)) { $field['error'] = 'name conflict'; // it should never happen, but better safe than sorry continue; } $fields[$field['name']] = $field; $fields[$field['name']]['uniqueName'] = $name; unset($fields[$name]); } } // Translate FKClassName to the corresponding api foreach ($fields as $name => &$field) { if (!empty($field['FKClassName'])) { $FKApi = CRM_Core_DAO_AllCoreTables::getBriefName($field['FKClassName']); if ($FKApi) { $field['FKApiName'] = $FKApi; } } } $fields += _civicrm_api_get_custom_fields($entity, $params); return $fields; }
/** * params must contain at least id=xx & {one of the fields from getfields}=value */ function civicrm_api3_generic_setValue($apiRequest) { $entity = $apiRequest['entity']; $params = $apiRequest['params']; // we can't use _spec, doesn't work with generic civicrm_api3_verify_mandatory($params, NULL, array('id', 'field', 'value')); $id = $params['id']; if (!is_numeric($id)) { return civicrm_api3_create_error(ts('Please enter a number'), array('error_code' => 'NaN', 'field' => "id")); } $field = CRM_Utils_String::munge($params['field']); $value = $params['value']; $fields = civicrm_api($entity, 'getFields', array('version' => 3, 'action' => 'create', "sequential")); // getfields error, shouldn't happen. if ($fields['is_error']) { return $fields; } $fields = $fields['values']; if (!array_key_exists($field, $fields)) { return civicrm_api3_create_error("Param 'field' ({$field}) is invalid. must be an existing field", array("error_code" => "invalid_field", "fields" => array_keys($fields))); } $def = $fields[$field]; // Disallow empty values except for the number zero. // TODO: create a utility for this since it's needed in many places // if (array_key_exists('required', $def) && CRM_Utils_System::isNull($value)) { if (array_key_exists('required', $def) && empty($value) && $value !== '0' && $value !== 0) { return civicrm_api3_create_error(ts("This can't be empty, please provide a value"), array("error_code" => "required", "field" => $field)); } switch ($def['type']) { case CRM_Utils_Type::T_INT: if (!is_numeric($value)) { return civicrm_api3_create_error("Param '{$field}' must be a number", array('error_code' => 'NaN')); } case CRM_Utils_Type::T_STRING: case CRM_Utils_Type::T_TEXT: if (!CRM_Utils_Rule::xssString($value)) { return civicrm_api3_create_error(ts('Illegal characters in input (potential scripting attack)'), array('error_code' => 'XSS')); } if (array_key_exists('maxlength', $def)) { $value = substr($value, 0, $def['maxlength']); } break; case CRM_Utils_Type::T_DATE: $value = CRM_Utils_Type::escape($value, "Date", false); if (!$value) { return civicrm_api3_create_error("Param '{$field}' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS"); } break; case CRM_Utils_Type::T_BOOLEAN: $value = (bool) $value; break; default: return civicrm_api3_create_error("Param '{$field}' is of a type not managed yet (" . $def['type'] . "). Join the API team and help us implement it", array('error_code' => 'NOT_IMPLEMENTED')); } $dao_name = _civicrm_api3_get_DAO($entity); if (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $value)) { $params = array('id' => $id, $field => $value); $entityDAO = new $dao_name(); $entityDAO->copyValues($params); CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO); return civicrm_api3_create_success($params); } else { return civicrm_api3_create_error("error assigning {$field}={$value} for {$entity} (id={$id})"); } }
function _civicrm_api_get_fields($entity, $unique = FALSE, &$params = array()) { $unsetIfEmpty = array('dataPattern', 'headerPattern', 'default', 'export', 'import'); $dao = _civicrm_api3_get_DAO($entity); if (empty($dao)) { return array(); } $file = str_replace('_', '/', $dao) . ".php"; require_once $file; $d = new $dao(); $fields = $d->fields(); // replace uniqueNames by the normal names as the key if (empty($unique)) { foreach ($fields as $name => &$field) { //getting rid of unused attributes foreach ($unsetIfEmpty as $attr) { if (empty($field[$attr])) { unset($field[$attr]); } } if ($name == $field['name']) { continue; } if (array_key_exists($field['name'], $fields)) { $field['error'] = 'name conflict'; // it should never happen, but better safe than sorry continue; } $fields[$field['name']] = $field; $fields[$field['name']]['uniqueName'] = $name; unset($fields[$name]); } } $fields += _civicrm_api_get_custom_fields($entity, $params); return $fields; }
/** * Retrieve one or more countryies. * * @param array $params * Array per getfields metadata. * * @return array * api result array */ function civicrm_api3_country_get($params) { return _civicrm_api3_basic_get(_civicrm_api3_get_DAO(__FUNCTION__), $params); }
/** * Set a single value using the api. * * This function is called when no specific setvalue api exists. * Params must contain at least id=xx & {one of the fields from getfields}=value * * @param array $apiRequest * * @throws API_Exception * @return array */ function civicrm_api3_generic_setValue($apiRequest) { $entity = $apiRequest['entity']; $params = $apiRequest['params']; $id = $params['id']; if (!is_numeric($id)) { return civicrm_api3_create_error(ts('Please enter a number'), array('error_code' => 'NaN', 'field' => "id")); } $field = CRM_Utils_String::munge($params['field']); $value = $params['value']; $fields = civicrm_api($entity, 'getFields', array('version' => 3, 'action' => 'create', "sequential")); // getfields error, shouldn't happen. if ($fields['is_error']) { return $fields; } $fields = $fields['values']; $isCustom = strpos($field, 'custom_') === 0; // Trim off the id portion of a multivalued custom field name $fieldKey = $isCustom && substr_count($field, '_') > 1 ? rtrim(rtrim($field, '1234567890'), '_') : $field; if (!array_key_exists($fieldKey, $fields)) { return civicrm_api3_create_error("Param 'field' ({$field}) is invalid. must be an existing field", array("error_code" => "invalid_field", "fields" => array_keys($fields))); } $def = $fields[$fieldKey]; $title = CRM_Utils_Array::value('title', $def, ts('Field')); // Disallow empty values except for the number zero. // TODO: create a utility for this since it's needed in many places if (!empty($def['required']) || !empty($def['is_required'])) { if ((empty($value) || $value === 'null') && $value !== '0' && $value !== 0) { return civicrm_api3_create_error(ts('%1 is a required field.', array(1 => $title)), array("error_code" => "required", "field" => $field)); } } switch ($def['type']) { case CRM_Utils_Type::T_FLOAT: if (!is_numeric($value) && !empty($value) && $value !== 'null') { return civicrm_api3_create_error(ts('%1 must be a number.', array(1 => $title)), array('error_code' => 'NaN')); } break; case CRM_Utils_Type::T_INT: if (!CRM_Utils_Rule::integer($value) && !empty($value) && $value !== 'null') { return civicrm_api3_create_error(ts('%1 must be a number.', array(1 => $title)), array('error_code' => 'NaN')); } break; case CRM_Utils_Type::T_STRING: case CRM_Utils_Type::T_TEXT: if (!CRM_Utils_Rule::xssString($value)) { return civicrm_api3_create_error(ts('Illegal characters in input (potential scripting attack)'), array('error_code' => 'XSS')); } if (array_key_exists('maxlength', $def)) { $value = substr($value, 0, $def['maxlength']); } break; case CRM_Utils_Type::T_DATE: $value = CRM_Utils_Type::escape($value, "Date", FALSE); if (!$value) { return civicrm_api3_create_error("Param '{$field}' is not a date. format YYYYMMDD or YYYYMMDDHHMMSS"); } break; case CRM_Utils_Type::T_BOOLEAN: // Allow empty value for non-required fields if ($value === '' || $value === 'null') { $value = ''; } else { $value = (bool) $value; } break; default: return civicrm_api3_create_error("Param '{$field}' is of a type not managed yet (" . $def['type'] . "). Join the API team and help us implement it", array('error_code' => 'NOT_IMPLEMENTED')); } $dao_name = _civicrm_api3_get_DAO($entity); $params = array('id' => $id, $field => $value); if ((!empty($def['pseudoconstant']) || !empty($def['option_group_id'])) && $value !== '' && $value !== 'null') { _civicrm_api3_api_match_pseudoconstant($params[$field], $entity, $field, $def); } CRM_Utils_Hook::pre('edit', $entity, $id, $params); // Custom fields if ($isCustom) { CRM_Utils_Array::crmReplaceKey($params, 'id', 'entityID'); // Treat 'null' as empty value. This is awful but the rest of the code supports it. if ($params[$field] === 'null') { $params[$field] = ''; } CRM_Core_BAO_CustomValueTable::setValues($params); CRM_Utils_Hook::post('edit', $entity, $id, CRM_Core_DAO::$_nullObject); } elseif (CRM_Core_DAO::setFieldValue($dao_name, $id, $field, $params[$field])) { $entityDAO = new $dao_name(); $entityDAO->copyValues($params); CRM_Utils_Hook::post('edit', $entity, $entityDAO->id, $entityDAO); } else { return civicrm_api3_create_error("error assigning {$field}={$value} for {$entity} (id={$id})"); } // Add changelog entry - TODO: Should we do this for other entities as well? if (strtolower($entity) === 'contact') { CRM_Core_BAO_Log::register($id, 'civicrm_contact', $id); } return civicrm_api3_create_success($params); }
function civicrm_api3_mailing_group_getfields($params) { $dao = _civicrm_api3_get_DAO('Subscribe'); $d = new $dao(); $fields = $d->fields(); $d->free(); $dao = _civicrm_api3_get_DAO('Unsubscribe'); $d = new $dao(); $fields = $fields + $d->fields(); $d->free(); // CRM-13830 - prevent the api wrapper from helping out with pseudoconstants // Since these fields don't belong to this entity it will fail foreach ($fields as &$field) { unset($field['pseudoconstant']); } return civicrm_api3_create_success($fields); }
function civicrm_api3_mailing_group_getfields($params) { $dao = _civicrm_api3_get_DAO('Subscribe'); $file = str_replace('_', '/', $dao) . ".php"; require_once $file; $d = new $dao(); $fields = $d->fields(); $d->free(); $dao = _civicrm_api3_get_DAO('Unsubscribe'); $file = str_replace('_', '/', $dao) . ".php"; require_once $file; $d = new $dao(); $fields = $fields + $d->fields(); $d->free(); return civicrm_api3_create_success($fields); }
function civicrm_api3_profile_getfields($params) { $dao = _civicrm_api3_get_DAO('UFGroup'); $file = str_replace('_', '/', $dao) . ".php"; require_once $file; $d = new $dao(); $fields = $d->fields(); return civicrm_api3_create_success($fields); }