PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/api/v3/CustomValue.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 396 lines | 259 code | 14 blank | 123 comment | 51 complexity | 18c876c37bf0f086a87c01012200e74a MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. * This api exposes CiviCRM custom value.
  29. *
  30. * @package CiviCRM_APIv3
  31. */
  32. /**
  33. * Sets custom values for an entity.
  34. *
  35. * @param array $params
  36. * Expected keys are in format custom_fieldID:recordID or custom_groupName:fieldName:recordID.
  37. *
  38. * @example:
  39. * @code
  40. * // entity ID. You do not need to specify entity type, we figure it out based on the fields you're using
  41. * 'entity_id' => 123,
  42. * // (omitting :id) inserts or updates a field in a single-valued group
  43. * 'custom_6' => 'foo',
  44. * // custom_24 is checkbox or multiselect, so pass items as an array
  45. * 'custom_24' => array('bar', 'baz'),
  46. * // in this case custom_33 is part of a multi-valued group, and we're updating record id 5
  47. * 'custom_33:5' => value,
  48. * // inserts new record in multi-valued group
  49. * 'custom_33:-1' => value,
  50. * // inserts another new record in multi-valued group
  51. * 'custom_33:-2' => value,
  52. * // you can use group_name:field_name instead of ID
  53. * 'custom_some_group:my_field' => 'myinfo',
  54. * // updates record ID 8 in my_other_field in multi-valued some_big_group
  55. * 'custom_some_big_group:my_other_field:8' => 'myinfo',
  56. * @endcode
  57. *
  58. * @throws Exception
  59. * @return array
  60. * ['values' => TRUE] or ['is_error' => 1, 'error_message' => 'what went wrong']
  61. */
  62. function civicrm_api3_custom_value_create($params) {
  63. // @todo it's not clear where the entity_table is used as CRM_Core_BAO_CustomValueTable::setValues($create)
  64. // didn't seem to use it
  65. // so not clear if it's relevant
  66. if (!empty($params['entity_table']) && substr($params['entity_table'], 0, 7) == 'civicrm') {
  67. $params['entity_table'] = substr($params['entity_table'], 8, 7);
  68. }
  69. $create = ['entityID' => $params['entity_id']];
  70. // Translate names and
  71. //Convert arrays to multi-value strings
  72. $sp = CRM_Core_DAO::VALUE_SEPARATOR;
  73. foreach ($params as $id => $param) {
  74. if (is_array($param)) {
  75. $param = $sp . implode($sp, $param) . $sp;
  76. }
  77. list($c, $id) = CRM_Utils_System::explode('_', $id, 2);
  78. if ($c != 'custom') {
  79. continue;
  80. }
  81. list($i, $n, $x) = CRM_Utils_System::explode(':', $id, 3);
  82. if (is_numeric($i)) {
  83. $key = $i;
  84. $x = $n;
  85. }
  86. else {
  87. // Lookup names if ID was not supplied
  88. $key = CRM_Core_BAO_CustomField::getCustomFieldID($n, $i);
  89. if (!$key) {
  90. continue;
  91. }
  92. }
  93. if ($x && is_numeric($x)) {
  94. $key .= '_' . $x;
  95. }
  96. $create['custom_' . $key] = $param;
  97. }
  98. $result = CRM_Core_BAO_CustomValueTable::setValues($create);
  99. if ($result['is_error']) {
  100. throw new Exception($result['error_message']);
  101. }
  102. return civicrm_api3_create_success(TRUE, $params, 'CustomValue');
  103. }
  104. /**
  105. * Adjust Metadata for Create action.
  106. *
  107. * The metadata is used for setting defaults, documentation & validation.
  108. *
  109. * @param array $params
  110. * Array of parameters determined by getfields.
  111. */
  112. function _civicrm_api3_custom_value_create_spec(&$params) {
  113. $params['entity_id']['api.required'] = 1;
  114. $params['entity_id']['title'] = 'Entity ID';
  115. }
  116. /**
  117. * Use this API to get existing custom values for an entity.
  118. *
  119. * @param array $params
  120. * Array specifying the entity_id.
  121. * Optionally include entity_type param, i.e. 'entity_type' => 'Activity'
  122. * If no entity_type is supplied, it will be determined based on the fields you request.
  123. * If no entity_type is supplied and no fields are specified, 'Contact' will be assumed.
  124. * Optionally include the desired custom data to be fetched (or else all custom data for this entity will be returned)
  125. * Example: 'entity_id' => 123, 'return.custom_6' => 1, 'return.custom_33' => 1
  126. * If you do not know the ID, you may use group name : field name, for example 'return.foo_stuff:my_field' => 1
  127. *
  128. * @throws API_Exception
  129. * @return array
  130. */
  131. function civicrm_api3_custom_value_get($params) {
  132. $getParams = [
  133. 'entityID' => $params['entity_id'],
  134. 'entityType' => CRM_Utils_Array::value('entity_table', $params, ''),
  135. ];
  136. if (strstr($getParams['entityType'], 'civicrm_')) {
  137. $getParams['entityType'] = ucfirst(substr($getParams['entityType'], 8));
  138. }
  139. unset($params['entity_id'], $params['entity_table']);
  140. foreach ($params as $id => $param) {
  141. if ($param && substr($id, 0, 6) == 'return') {
  142. $returnVal = $param;
  143. if (!empty(substr($id, 7))) {
  144. $returnVal = substr($id, 7);
  145. }
  146. if (!is_array($returnVal)) {
  147. $returnVal = explode(',', $returnVal);
  148. }
  149. foreach ($returnVal as $value) {
  150. list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
  151. if ($c == 'custom' && is_numeric($i)) {
  152. $names['custom_' . $i] = 'custom_' . $i;
  153. $fldId = $i;
  154. }
  155. else {
  156. // Lookup names if ID was not supplied
  157. list($group, $field) = CRM_Utils_System::explode(':', $value, 2);
  158. $fldId = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
  159. if (!$fldId) {
  160. continue;
  161. }
  162. $names['custom_' . $fldId] = 'custom_' . $i;
  163. }
  164. $getParams['custom_' . $fldId] = 1;
  165. }
  166. }
  167. }
  168. $result = CRM_Core_BAO_CustomValueTable::getValues($getParams);
  169. if ($result['is_error']) {
  170. if ($result['error_message'] == "No values found for the specified entity ID and custom field(s).") {
  171. $values = [];
  172. return civicrm_api3_create_success($values, $params, 'CustomValue');
  173. }
  174. else {
  175. throw new API_Exception($result['error_message']);
  176. }
  177. }
  178. else {
  179. $entity_id = $result['entityID'];
  180. unset($result['is_error'], $result['entityID']);
  181. // Convert multi-value strings to arrays
  182. $sp = CRM_Core_DAO::VALUE_SEPARATOR;
  183. foreach ($result as $id => $value) {
  184. if (strpos($value, $sp) !== FALSE) {
  185. $value = explode($sp, trim($value, $sp));
  186. }
  187. $idArray = explode('_', $id);
  188. if ($idArray[0] != 'custom') {
  189. continue;
  190. }
  191. $fieldNumber = $idArray[1];
  192. $customFieldInfo = CRM_Core_BAO_CustomField::getNameFromID($fieldNumber);
  193. $info = array_pop($customFieldInfo);
  194. // id is the index for returned results
  195. if (empty($idArray[2])) {
  196. $n = 0;
  197. $id = $fieldNumber;
  198. }
  199. else {
  200. $n = $idArray[2];
  201. $id = $fieldNumber . "." . $idArray[2];
  202. }
  203. if (!empty($params['format.field_names'])) {
  204. $id = $info['field_name'];
  205. }
  206. else {
  207. $id = $fieldNumber;
  208. }
  209. $values[$id]['entity_id'] = $getParams['entityID'];
  210. if (!empty($getParams['entityType'])) {
  211. $values[$id]['entity_table'] = $getParams['entityType'];
  212. }
  213. //set 'latest' -useful for multi fields but set for single for consistency
  214. $values[$id]['latest'] = $value;
  215. $values[$id]['id'] = $id;
  216. $values[$id][$n] = $value;
  217. }
  218. return civicrm_api3_create_success($values, $params, 'CustomValue');
  219. }
  220. }
  221. /**
  222. * Adjust Metadata for Get action.
  223. *
  224. * The metadata is used for setting defaults, documentation & validation.
  225. *
  226. * @param array $params
  227. * Array of parameters determined by getfields.
  228. */
  229. function _civicrm_api3_custom_value_get_spec(&$params) {
  230. $params['entity_id']['api.required'] = 1;
  231. $params['entity_id']['title'] = 'Entity ID';
  232. }
  233. /**
  234. * CustomValue.gettree API specification
  235. *
  236. * @param array $spec description of fields supported by this API call
  237. *
  238. * @throws \CiviCRM_API3_Exception
  239. */
  240. function _civicrm_api3_custom_value_gettree_spec(&$spec) {
  241. $spec['entity_id'] = [
  242. 'title' => 'Entity Id',
  243. 'description' => 'Id of entity',
  244. 'type' => CRM_Utils_Type::T_INT,
  245. 'api.required' => 1,
  246. ];
  247. $entities = civicrm_api3('Entity', 'get');
  248. $entities = array_diff($entities['values'], $entities['deprecated']);
  249. $spec['entity_type'] = [
  250. 'title' => 'Entity Type',
  251. 'description' => 'API name of entity type, e.g. "Contact"',
  252. 'type' => CRM_Utils_Type::T_STRING,
  253. 'api.required' => 1,
  254. 'options' => array_combine($entities, $entities),
  255. ];
  256. // Return params for custom group, field & value
  257. foreach (CRM_Core_DAO_CustomGroup::fields() as $field) {
  258. $name = 'custom_group.' . $field['name'];
  259. $spec[$name] = ['name' => $name] + $field;
  260. }
  261. foreach (CRM_Core_DAO_CustomField::fields() as $field) {
  262. $name = 'custom_field.' . $field['name'];
  263. $spec[$name] = ['name' => $name] + $field;
  264. }
  265. $spec['custom_value.id'] = [
  266. 'title' => 'Custom Value Id',
  267. 'description' => 'Id of record in custom value table',
  268. 'type' => CRM_Utils_Type::T_INT,
  269. ];
  270. $spec['custom_value.data'] = [
  271. 'title' => 'Custom Value (Raw)',
  272. 'description' => 'Raw value as stored in the database',
  273. 'type' => CRM_Utils_Type::T_STRING,
  274. ];
  275. $spec['custom_value.display'] = [
  276. 'title' => 'Custom Value (Formatted)',
  277. 'description' => 'Custom value formatted for display',
  278. 'type' => CRM_Utils_Type::T_STRING,
  279. ];
  280. }
  281. /**
  282. * CustomValue.gettree API
  283. *
  284. * @param array $params
  285. *
  286. * @return array API result
  287. * @throws \API_Exception
  288. * @throws \CRM_Core_Exception
  289. * @throws \CiviCRM_API3_Exception
  290. */
  291. function civicrm_api3_custom_value_gettree($params) {
  292. $ret = [];
  293. $options = _civicrm_api3_get_options_from_params($params);
  294. $toReturn = [
  295. 'custom_group' => [],
  296. 'custom_field' => [],
  297. 'custom_value' => [],
  298. ];
  299. foreach (array_keys($options['return']) as $r) {
  300. list($type, $field) = explode('.', $r);
  301. if (isset($toReturn[$type])) {
  302. $toReturn[$type][] = $field;
  303. }
  304. }
  305. // We must have a name if not indexing sequentially
  306. if (empty($params['sequential']) && $toReturn['custom_field']) {
  307. $toReturn['custom_field'][] = 'name';
  308. }
  309. switch ($params['entity_type']) {
  310. case 'Contact':
  311. $ret = ['entityType' => 'contact_type', 'subTypes' => 'contact_sub_type'];
  312. break;
  313. case 'Activity':
  314. case 'Campaign':
  315. case 'Case':
  316. case 'Contribution':
  317. case 'Event':
  318. case 'Grant':
  319. case 'Membership':
  320. case 'Relationship':
  321. $ret = ['subTypes' => strtolower($params['entity_type']) . '_type_id'];
  322. break;
  323. case 'Participant':
  324. // todo
  325. }
  326. $treeParams = [
  327. 'entityType' => $params['entity_type'],
  328. 'subTypes' => [],
  329. 'subName' => NULL,
  330. ];
  331. // Fetch entity data for custom group type/sub-type
  332. // Also verify access permissions (api3 will throw an exception if permission denied)
  333. if ($ret || !empty($params['check_permissions'])) {
  334. $entityData = civicrm_api3($params['entity_type'], 'getsingle', [
  335. 'id' => $params['entity_id'],
  336. 'check_permissions' => !empty($params['check_permissions']),
  337. 'return' => array_merge(['id'], array_values($ret)),
  338. ]);
  339. foreach ($ret as $param => $key) {
  340. if (isset($entityData[$key])) {
  341. $treeParams[$param] = $entityData[$key];
  342. }
  343. }
  344. }
  345. $tree = CRM_Core_BAO_CustomGroup::getTree($treeParams['entityType'], $toReturn, $params['entity_id'], NULL, $treeParams['subTypes'], $treeParams['subName'], TRUE, NULL, FALSE, CRM_Utils_Array::value('check_permissions', $params, TRUE));
  346. unset($tree['info']);
  347. $result = [];
  348. foreach ($tree as $group) {
  349. $result[$group['name']] = [];
  350. $groupToReturn = $toReturn['custom_group'] ? $toReturn['custom_group'] : array_keys($group);
  351. foreach ($groupToReturn as $item) {
  352. $result[$group['name']][$item] = CRM_Utils_Array::value($item, $group);
  353. }
  354. $result[$group['name']]['fields'] = [];
  355. foreach ($group['fields'] as $fieldInfo) {
  356. $field = ['value' => NULL];
  357. $fieldToReturn = $toReturn['custom_field'] ? $toReturn['custom_field'] : array_keys($fieldInfo);
  358. foreach ($fieldToReturn as $item) {
  359. $field[$item] = CRM_Utils_Array::value($item, $fieldInfo);
  360. }
  361. unset($field['customValue']);
  362. if (!empty($fieldInfo['customValue'])) {
  363. $field['value'] = CRM_Utils_Array::first($fieldInfo['customValue']);
  364. if (!$toReturn['custom_value'] || in_array('display', $toReturn['custom_value'])) {
  365. $field['value']['display'] = CRM_Core_BAO_CustomField::displayValue($field['value']['data'], $fieldInfo);
  366. }
  367. foreach (array_keys($field['value']) as $key) {
  368. if ($toReturn['custom_value'] && !in_array($key, $toReturn['custom_value'])) {
  369. unset($field['value'][$key]);
  370. }
  371. }
  372. }
  373. if (empty($params['sequential'])) {
  374. $result[$group['name']]['fields'][$fieldInfo['name']] = $field;
  375. }
  376. else {
  377. $result[$group['name']]['fields'][] = $field;
  378. }
  379. }
  380. }
  381. return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
  382. }