First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,45 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CiviCRM_APIv3
*/
/**
* Get available api actions.
*
* @param array $apiRequest
*
* @return array
* @throws API_Exception
*/
function civicrm_api3_generic_getActions($apiRequest) {
civicrm_api3_verify_mandatory($apiRequest, NULL, array('entity'));
$mfp = \Civi::service('magic_function_provider');
$actions = $mfp->getActionNames($apiRequest['version'], $apiRequest['entity']);
return civicrm_api3_create_success($actions, $apiRequest['params'], $apiRequest['entity'], 'getactions');
}

View file

@ -0,0 +1,290 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CiviCRM_APIv3
*/
/**
* Generic api wrapper used for quicksearch and autocomplete.
*
* @param array $apiRequest
*
* @return mixed
*/
function civicrm_api3_generic_getList($apiRequest) {
$entity = _civicrm_api_get_entity_name_from_camel($apiRequest['entity']);
$request = $apiRequest['params'];
$meta = civicrm_api3_generic_getfields(array('action' => 'get') + $apiRequest, FALSE);
// Hey api, would you like to provide default values?
$fnName = "_civicrm_api3_{$entity}_getlist_defaults";
$defaults = function_exists($fnName) ? $fnName($request) : array();
_civicrm_api3_generic_getList_defaults($entity, $request, $defaults, $meta['values']);
// Hey api, would you like to format the search params?
$fnName = "_civicrm_api3_{$entity}_getlist_params";
$fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_params';
$fnName($request);
$request['params']['check_permissions'] = !empty($apiRequest['params']['check_permissions']);
$result = civicrm_api3($entity, 'get', $request['params']);
// Hey api, would you like to format the output?
$fnName = "_civicrm_api3_{$entity}_getlist_output";
$fnName = function_exists($fnName) ? $fnName : '_civicrm_api3_generic_getlist_output';
$values = $fnName($result, $request, $entity, $meta['values']);
_civicrm_api3_generic_getlist_postprocess($result, $request, $values);
$output = array('page_num' => $request['page_num']);
// Limit is set for searching but not fetching by id
if (!empty($request['params']['options']['limit'])) {
// If we have an extra result then this is not the last page
$last = $request['params']['options']['limit'] - 1;
$output['more_results'] = isset($values[$last]);
unset($values[$last]);
}
return civicrm_api3_create_success($values, $request['params'], $entity, 'getlist', CRM_Core_DAO::$_nullObject, $output);
}
/**
* Set defaults for api.getlist.
*
* @param string $entity
* @param array $request
* @param array $apiDefaults
* @param array $fields
*/
function _civicrm_api3_generic_getList_defaults($entity, &$request, $apiDefaults, $fields) {
$defaults = array(
'page_num' => 1,
'input' => '',
'image_field' => NULL,
'color_field' => isset($fields['color']) ? 'color' : NULL,
'id_field' => $entity == 'option_value' ? 'value' : 'id',
'description_field' => array(),
'add_wildcard' => Civi::settings()->get('includeWildCardInName'),
'params' => array(),
'extra' => array(),
);
// Find main field from meta
foreach (array('sort_name', 'title', 'label', 'name', 'subject') as $field) {
if (isset($fields[$field])) {
$defaults['label_field'] = $defaults['search_field'] = $field;
break;
}
}
// Find fields to be used for the description
foreach (array('description') as $field) {
if (isset($fields[$field])) {
$defaults['description_field'][] = $field;
}
}
$resultsPerPage = Civi::settings()->get('search_autocomplete_count');
if (isset($request['params']) && isset($apiDefaults['params'])) {
$request['params'] += $apiDefaults['params'];
}
$request += $apiDefaults + $defaults;
// Default api params
$params = array(
'sequential' => 1,
'options' => array(),
);
// When searching e.g. autocomplete
if ($request['input']) {
$params[$request['search_field']] = array('LIKE' => ($request['add_wildcard'] ? '%' : '') . $request['input'] . '%');
}
// When looking up a field e.g. displaying existing record
if (!empty($request['id'])) {
if (is_string($request['id']) && strpos($request['id'], ',')) {
$request['id'] = explode(',', trim($request['id'], ', '));
}
// Don't run into search limits when prefilling selection
$params['options']['limit'] = NULL;
unset($params['options']['offset'], $request['params']['options']['limit'], $request['params']['options']['offset']);
$params[$request['id_field']] = is_array($request['id']) ? array('IN' => $request['id']) : $request['id'];
}
$request['params'] += $params;
$request['params']['options'] += array(
// Add pagination parameters
'sort' => $request['label_field'],
// Adding one extra result allows us to see if there are any more
'limit' => $resultsPerPage + 1,
// Because sql is zero-based
'offset' => ($request['page_num'] - 1) * $resultsPerPage,
);
}
/**
* Fallback implementation of getlist_params. May be overridden by individual apis.
*
* @param array $request
*/
function _civicrm_api3_generic_getlist_params(&$request) {
$fieldsToReturn = array($request['id_field'], $request['label_field']);
if (!empty($request['image_field'])) {
$fieldsToReturn[] = $request['image_field'];
}
if (!empty($request['color_field'])) {
$fieldsToReturn[] = $request['color_field'];
}
if (!empty($request['description_field'])) {
$fieldsToReturn = array_merge($fieldsToReturn, (array) $request['description_field']);
}
$request['params']['return'] = array_unique(array_merge($fieldsToReturn, $request['extra']));
}
/**
* Fallback implementation of getlist_output. May be overridden by individual api functions.
*
* @param array $result
* @param array $request
* @param string $entity
* @param array $fields
*
* @return array
*/
function _civicrm_api3_generic_getlist_output($result, $request, $entity, $fields) {
$output = array();
if (!empty($result['values'])) {
foreach ($result['values'] as $row) {
$data = array(
'id' => $row[$request['id_field']],
'label' => $row[$request['label_field']],
);
if (!empty($request['description_field'])) {
$data['description'] = array();
foreach ((array) $request['description_field'] as $field) {
if (!empty($row[$field])) {
if (!isset($fields[$field]['pseudoconstant'])) {
$data['description'][] = $row[$field];
}
else {
$data['description'][] = CRM_Core_PseudoConstant::getLabel(
_civicrm_api3_get_BAO($entity),
$field,
$row[$field]
);
}
}
}
};
if (!empty($request['image_field'])) {
$data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
}
if (isset($row[$request['color_field']])) {
$data['color'] = $row[$request['color_field']];
}
$output[] = $data;
}
}
return $output;
}
/**
* Common postprocess for getlist output
*
* @param $result
* @param $request
* @param $values
*/
function _civicrm_api3_generic_getlist_postprocess($result, $request, &$values) {
$chains = array();
foreach ($request['params'] as $field => $param) {
if (substr($field, 0, 4) === 'api.') {
$chains[] = $field;
}
}
if (!empty($result['values'])) {
foreach (array_values($result['values']) as $num => $row) {
foreach ($request['extra'] as $field) {
$values[$num]['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
}
foreach ($chains as $chain) {
$values[$num][$chain] = isset($row[$chain]) ? $row[$chain] : NULL;
}
}
}
}
/**
* Provide metadata for this api
*
* @param array $params
* @param array $apiRequest
*/
function _civicrm_api3_generic_getlist_spec(&$params, $apiRequest) {
$params += array(
'page_num' => array(
'title' => 'Page Number',
'description' => "Current page of a multi-page lookup",
'type' => CRM_Utils_Type::T_INT,
),
'input' => array(
'title' => 'Search Input',
'description' => "String to search on",
'type' => CRM_Utils_Type::T_TEXT,
),
'params' => array(
'title' => 'API Params',
'description' => "Additional filters to send to the {$apiRequest['entity']} API.",
),
'extra' => array(
'title' => 'Extra',
'description' => 'Array of additional fields to return.',
),
'image_field' => array(
'title' => 'Image Field',
'description' => "Field that this entity uses to store icons (usually automatic)",
'type' => CRM_Utils_Type::T_TEXT,
),
'id_field' => array(
'title' => 'ID Field',
'description' => "Field that uniquely identifies this entity (usually automatic)",
'type' => CRM_Utils_Type::T_TEXT,
),
'description_field' => array(
'title' => 'Description Field',
'description' => "Field that this entity uses to store summary text (usually automatic)",
'type' => CRM_Utils_Type::T_TEXT,
),
'label_field' => array(
'title' => 'Label Field',
'description' => "Field to display as title of results (usually automatic)",
'type' => CRM_Utils_Type::T_TEXT,
),
'search_field' => array(
'title' => 'Search Field',
'description' => "Field to search on (assumed to be the same as label field unless otherwise specified)",
'type' => CRM_Utils_Type::T_TEXT,
),
);
}

View file

@ -0,0 +1,164 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CiviCRM_APIv3
*/
/**
* 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 = (boolean) $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);
}
// Core fields
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);
}

View file

@ -0,0 +1,82 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CiviCRM_APIv3
*/
/**
* Update function is basically a hack.
*
* We want to remove it but must resolve issues in
* http://issues.civicrm.org/jira/browse/CRM-12144
*
* It is not recommended & if update doesn't work & fix does then update will not be fixed
*
* To do this, perform a 'get' action to load the existing values, then merge in the updates
* and call 'create' to save the revised entity.
*
* @deprecated
*
* @param array $apiRequest
* Array with keys:
* - entity: string
* - action: string
* - version: string
* - function: callback (mixed)
* - params: array, varies
*
* @return array|int|mixed
*/
function civicrm_api3_generic_update($apiRequest) {
//$key_id = strtolower ($apiRequest['entity'])."_id";
$key_id = "id";
if (!array_key_exists($key_id, $apiRequest['params'])) {
return civicrm_api3_create_error("Mandatory parameter missing $key_id");
}
// @fixme
// tests show that contribution works better with create
// this is horrible but to make it work we'll just handle it separately
if (strtolower($apiRequest['entity']) == 'contribution') {
return civicrm_api($apiRequest['entity'], 'create', $apiRequest['params']);
}
$seek = array($key_id => $apiRequest['params'][$key_id], 'version' => $apiRequest['version']);
$existing = civicrm_api($apiRequest['entity'], 'get', $seek);
if ($existing['is_error']) {
return $existing;
}
if ($existing['count'] > 1) {
return civicrm_api3_create_error("More than one " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
}
if ($existing['count'] == 0) {
return civicrm_api3_create_error("No " . $apiRequest['entity'] . " with id " . $apiRequest['params'][$key_id]);
}
$existing = array_pop($existing['values']);
$p = array_merge($existing, $apiRequest['params']);
return civicrm_api($apiRequest['entity'], 'create', $p);
}

View file

@ -0,0 +1,53 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* @package CiviCRM_APIv3
*/
/**
* Provide meta-data for this api.
*
* @param array $params
*/
function _civicrm_api3_generic_validate_spec(&$params) {
$params['action']['api.required'] = TRUE;
$params['action']['title'] = ts('API Action');
}
/**
* Generic api wrapper used for validation of entity-action pair.
*
* @param array $apiRequest
*
* @return mixed
*/
function civicrm_api3_generic_validate($apiRequest) {
$errors = _civicrm_api3_validate($apiRequest['entity'], $apiRequest['params']['action'], $apiRequest['params']);
return civicrm_api3_create_success($errors, $apiRequest['params'], $apiRequest['entity'], 'validate');
}