Merge pull request #16903 from eileenmcnaughton/settting_better
[civicrm-core.git] / api / v3 / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM email records.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Add an Email for a contact.
20 *
21 * @param array $params
22 * Array per getfields metadata.
23 *
24 * @return array
25 * API result array
26 *
27 * @throws \API_Exception
28 * @throws \Civi\API\Exception\UnauthorizedException
29 */
30 function civicrm_api3_email_create($params) {
31 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Email');
32 }
33
34 /**
35 * Adjust Metadata for Create action.
36 *
37 * The metadata is used for setting defaults, documentation & validation.
38 *
39 * @param array $params
40 * Array of parameters determined by getfields.
41 */
42 function _civicrm_api3_email_create_spec(&$params) {
43 $params['is_primary']['api.default'] = 0;
44 $params['email']['api.required'] = 1;
45 $params['contact_id']['api.required'] = 1;
46 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
47 if ($defaultLocation) {
48 $params['location_type_id']['api.default'] = $defaultLocation->id;
49 }
50 }
51
52 /**
53 * Deletes an existing Email.
54 *
55 * @param array $params
56 * Array per getfields metadata.
57 *
58 * @return array
59 * API result array.
60 *
61 * @throws \API_Exception
62 * @throws \CiviCRM_API3_Exception
63 * @throws \Civi\API\Exception\UnauthorizedException
64 */
65 function civicrm_api3_email_delete($params) {
66 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
67 }
68
69 /**
70 * Retrieve one or more emails.
71 *
72 * @param array $params
73 * Array per getfields metadata.
74 *
75 * @return array
76 * api result array
77 */
78 function civicrm_api3_email_get($params) {
79 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
80 }
81
82 /**
83 * Set default getlist parameters.
84 *
85 * @see _civicrm_api3_generic_getlist_defaults
86 *
87 * @param array $request
88 *
89 * @return array
90 */
91 function _civicrm_api3_email_getlist_defaults(&$request) {
92 return [
93 'description_field' => [
94 'contact_id.sort_name',
95 'email',
96 ],
97 'params' => [
98 'on_hold' => 0,
99 'contact_id.is_deleted' => 0,
100 'contact_id.is_deceased' => 0,
101 'contact_id.do_not_email' => 0,
102 ],
103 'label_field' => 'contact_id.sort_name',
104 // If no results from sort_name try email.
105 'search_field' => 'contact_id.sort_name',
106 'search_field_fallback' => 'email',
107 ];
108
109 }