Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
244bbdd8 | 13 | * This api exposes CiviCRM email records. |
6a488035 TO |
14 | * |
15 | * @package CiviCRM_APIv3 | |
6a488035 TO |
16 | */ |
17 | ||
6a488035 | 18 | /** |
2e66abf8 | 19 | * Add an Email for a contact. |
6a488035 | 20 | * |
c490a46a | 21 | * @param array $params |
2e66abf8 | 22 | * Array per getfields metadata. |
77b97be7 | 23 | * |
a6c01b45 | 24 | * @return array |
72b3a70c | 25 | * API result array |
30420aa3 | 26 | * |
27 | * @throws \API_Exception | |
28 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 TO |
29 | */ |
30 | function civicrm_api3_email_create($params) { | |
ddaf2161 | 31 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Email'); |
6a488035 | 32 | } |
11e09c59 TO |
33 | |
34 | /** | |
0aa0303c EM |
35 | * Adjust Metadata for Create action. |
36 | * | |
37 | * The metadata is used for setting defaults, documentation & validation. | |
1c88e578 | 38 | * |
cf470720 | 39 | * @param array $params |
b081365f | 40 | * Array of parameters determined by getfields. |
6a488035 TO |
41 | */ |
42 | function _civicrm_api3_email_create_spec(&$params) { | |
6a488035 TO |
43 | $params['is_primary']['api.default'] = 0; |
44 | $params['email']['api.required'] = 1; | |
45 | $params['contact_id']['api.required'] = 1; | |
2890c0f9 AS |
46 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); |
47 | if ($defaultLocation) { | |
48 | $params['location_type_id']['api.default'] = $defaultLocation->id; | |
49 | } | |
6a488035 TO |
50 | } |
51 | ||
52 | /** | |
2e66abf8 | 53 | * Deletes an existing Email. |
6a488035 | 54 | * |
cf470720 | 55 | * @param array $params |
2e66abf8 | 56 | * Array per getfields metadata. |
6a488035 | 57 | * |
2e66abf8 EM |
58 | * @return array |
59 | * API result array. | |
30420aa3 | 60 | * |
61 | * @throws \API_Exception | |
62 | * @throws \CiviCRM_API3_Exception | |
63 | * @throws \Civi\API\Exception\UnauthorizedException | |
6a488035 TO |
64 | */ |
65 | function civicrm_api3_email_delete($params) { | |
66 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
67 | } | |
68 | ||
69 | /** | |
2e66abf8 | 70 | * Retrieve one or more emails. |
6a488035 | 71 | * |
cf470720 | 72 | * @param array $params |
2e66abf8 | 73 | * Array per getfields metadata. |
6a488035 | 74 | * |
a6c01b45 | 75 | * @return array |
72b3a70c | 76 | * api result array |
6a488035 TO |
77 | */ |
78 | function civicrm_api3_email_get($params) { | |
6a488035 TO |
79 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
80 | } | |
30420aa3 | 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' => [ | |
30420aa3 | 94 | 'email', |
95 | ], | |
96 | 'params' => [ | |
97 | 'on_hold' => 0, | |
98 | 'contact_id.is_deleted' => 0, | |
99 | 'contact_id.is_deceased' => 0, | |
100 | 'contact_id.do_not_email' => 0, | |
101 | ], | |
5b4b9509 | 102 | // Note that changing this to display name affects query performance. The label field is used |
103 | // for sorting & mysql will prefer to use the index on the ORDER BY field. So if this is changed | |
104 | // to display name then the filtering will bypass the index. In testing this took around 30 times | |
105 | // as long. | |
30420aa3 | 106 | 'label_field' => 'contact_id.sort_name', |
107 | // If no results from sort_name try email. | |
108 | 'search_field' => 'contact_id.sort_name', | |
109 | 'search_field_fallback' => 'email', | |
110 | ]; | |
111 | ||
112 | } |