Merge pull request #16545 from eileenmcnaughton/part_pend
[civicrm-core.git] / api / v3 / MessageTemplate.php
CommitLineData
4a092902 1<?php
4a092902
XD
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
4a092902 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 |
4a092902 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
4a092902
XD
11
12/**
c28e1768 13 * This api exposes CiviCRM message_template.
4a092902 14 *
b081365f 15 * @package CiviCRM_APIv3
4a092902
XD
16 */
17
4a092902 18/**
61fe4988
EM
19 * Create message template.
20 *
d0997921 21 * @param array $params
61fe4988 22 *
645ee340
EM
23 * @return array
24 * @throws \API_Exception
4a092902 25 */
63c9befe 26function civicrm_api3_message_template_create($params) {
a25b46e9 27 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MessageTemplate');
4a092902
XD
28}
29
30/**
0aa0303c
EM
31 * Adjust Metadata for Create action.
32 *
33 * The metadata is used for setting defaults, documentation & validation.
8ef12e64 34 *
cf470720 35 * @param array $params
b081365f 36 * Array of parameters determined by getfields.
4a092902 37 */
63c9befe 38function _civicrm_api3_message_template_create_spec(&$params) {
4a092902 39 $params['msg_title']['api.required'] = 1;
35671d00
TO
40 $params['is_active']['api.default'] = TRUE;
41 /* $params['entity_id']['api.required'] = 1;
4a092902
XD
42 $params['entity_table']['api.default'] = "civicrm_contribution_recur";
43 $params['type']['api.default'] = "R";
e70a7fc0 44 */
4a092902
XD
45}
46
47/**
dc64d047
EM
48 * Delete message template.
49 *
cf470720 50 * @param array $params
4a092902 51 *
ae5ffbb7
TO
52 * @return bool
53 * API result array
4a092902 54 */
63c9befe 55function civicrm_api3_message_template_delete($params) {
4a092902
XD
56 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
57}
58
aa1b1481 59/**
1747ab99
EM
60 * Adjust metadata for message_template get action.
61 *
c490a46a 62 * @param array $params
aa1b1481 63 */
63c9befe 64function _civicrm_api3_message_template_get_spec(&$params) {
bd876613
CW
65 // fetch active records by default
66 $params['is_active']['api.default'] = 1;
4a092902 67}
f471d149 68
4a092902 69/**
1747ab99 70 * Retrieve one or more message_template.
4a092902 71 *
cf470720 72 * @param array $params
1747ab99 73 * Array of name/value pairs.
4a092902 74 *
a6c01b45 75 * @return array
1747ab99 76 * API result array.
4a092902 77 */
63c9befe 78function civicrm_api3_message_template_get($params) {
4a092902
XD
79 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
80}
81
f471d149
CB
82/**
83 * Sends a template.
1747ab99 84 *
d0997921 85 * @param array $params
8089541a 86 * @throws API_Exception
f471d149
CB
87 */
88function civicrm_api3_message_template_send($params) {
1d05b6d8 89 // Change external param names to internal ones
cf8f0fff 90 $fieldSpec = [];
1d05b6d8
CW
91 _civicrm_api3_message_template_send_spec($fieldSpec);
92
93 foreach ($fieldSpec as $field => $spec) {
94 if (isset($spec['api.aliases']) && array_key_exists($field, $params)) {
95 $params[CRM_Utils_Array::first($spec['api.aliases'])] = $params[$field];
96 unset($params[$field]);
97 }
98 }
99 if (empty($params['messageTemplateID'])) {
100 if (empty($params['groupName']) || empty($params['valueName'])) {
101 // Can't use civicrm_api3_verify_mandatory for this because it would give the wrong field names
102 throw new API_Exception(
103 "Mandatory key(s) missing from params array: requires id or option_group_name + option_value_name",
104 "mandatory_missing",
cf8f0fff 105 ["fields" => ['id', 'option_group_name', 'option_value_name']]
1d05b6d8
CW
106 );
107 }
108 }
109 CRM_Core_BAO_MessageTemplate::sendTemplate($params);
f471d149
CB
110}
111
112/**
0aa0303c 113 * Adjust Metadata for Create action.
f471d149
CB
114 *
115 * The metadata is used for setting defaults, documentation &
116 * validation.
117 *
cf470720 118 * @param array $params
b081365f 119 * Array of parameters determined by getfields.
f471d149
CB
120 */
121function _civicrm_api3_message_template_send_spec(&$params) {
1d05b6d8
CW
122 $params['id']['description'] = 'ID of the template';
123 $params['id']['title'] = 'Message Template ID';
cf8f0fff 124 $params['id']['api.aliases'] = ['messageTemplateID', 'message_template_id'];
d142432b 125 $params['id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8
CW
126
127 $params['option_group_name']['description'] = 'option group name of the template (required if no id supplied)';
128 $params['option_group_name']['title'] = 'Option Group Name';
cf8f0fff 129 $params['option_group_name']['api.aliases'] = ['groupName'];
d142432b 130 $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
131
132 $params['option_value_name']['description'] = 'option value name of the template (required if no id supplied)';
133 $params['option_value_name']['title'] = 'Option Value Name';
cf8f0fff 134 $params['option_value_name']['api.aliases'] = ['valueName'];
d142432b 135 $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
136
137 $params['contact_id']['description'] = 'contact id if contact tokens are to be replaced';
138 $params['contact_id']['title'] = 'Contact ID';
cf8f0fff 139 $params['contact_id']['api.aliases'] = ['contactId'];
d142432b 140 $params['contact_id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8
CW
141
142 $params['template_params']['description'] = 'additional template params (other than the ones already set in the template singleton)';
143 $params['template_params']['title'] = 'Template Params';
cf8f0fff 144 $params['template_params']['api.aliases'] = ['tplParams'];
d142432b 145 // FIXME: Type??
1d05b6d8
CW
146
147 $params['from']['description'] = 'the From: header';
148 $params['from']['title'] = 'From';
d142432b 149 $params['from']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
150
151 $params['to_name']['description'] = 'the recipient’s name';
152 $params['to_name']['title'] = 'Recipient Name';
cf8f0fff 153 $params['to_name']['api.aliases'] = ['toName'];
d142432b 154 $params['to_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
155
156 $params['to_email']['description'] = 'the recipient’s email - mail is sent only if set';
157 $params['to_email']['title'] = 'Recipient Email';
cf8f0fff 158 $params['to_email']['api.aliases'] = ['toEmail'];
d142432b 159 $params['to_email']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
160
161 $params['cc']['description'] = 'the Cc: header';
162 $params['cc']['title'] = 'CC';
d142432b 163 $params['cc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
164
165 $params['bcc']['description'] = 'the Bcc: header';
166 $params['bcc']['title'] = 'BCC';
d142432b 167 $params['bcc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
168
169 $params['reply_to']['description'] = 'the Reply-To: header';
170 $params['reply_to']['title'] = 'Reply To';
cf8f0fff 171 $params['reply_to']['api.aliases'] = ['replyTo'];
d142432b 172 $params['reply_to']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
173
174 $params['attachments']['description'] = 'email attachments';
175 $params['attachments']['title'] = 'Attachments';
d142432b 176 // FIXME: Type??
1d05b6d8
CW
177
178 $params['is_test']['description'] = 'whether this is a test email (and hence should include the test banner)';
179 $params['is_test']['title'] = 'Is Test';
cf8f0fff 180 $params['is_test']['api.aliases'] = ['isTest'];
d142432b 181 $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN;
1d05b6d8
CW
182
183 $params['pdf_filename']['description'] = 'filename of optional PDF version to add as attachment (do not include path)';
184 $params['pdf_filename']['title'] = 'PDF Filename';
cf8f0fff 185 $params['pdf_filename']['api.aliases'] = ['PDFFilename'];
d142432b 186 $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING;
f471d149 187}