Merge pull request #17522 from seamuslee001/remove_deprecated_methods
[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
c4d7103b 86 *
8089541a 87 * @throws API_Exception
c4d7103b 88 * @throws \CRM_Core_Exception
f471d149
CB
89 */
90function civicrm_api3_message_template_send($params) {
1d05b6d8 91 // Change external param names to internal ones
cf8f0fff 92 $fieldSpec = [];
1d05b6d8
CW
93 _civicrm_api3_message_template_send_spec($fieldSpec);
94
95 foreach ($fieldSpec as $field => $spec) {
96 if (isset($spec['api.aliases']) && array_key_exists($field, $params)) {
97 $params[CRM_Utils_Array::first($spec['api.aliases'])] = $params[$field];
98 unset($params[$field]);
99 }
100 }
101 if (empty($params['messageTemplateID'])) {
c4d7103b 102 if (empty($params['valueName'])) {
1d05b6d8
CW
103 // Can't use civicrm_api3_verify_mandatory for this because it would give the wrong field names
104 throw new API_Exception(
c4d7103b 105 'Mandatory key(s) missing from params array: requires id or option_value_name',
106 'mandatory_missing',
107 ['fields' => ['id', 'option_value_name']]
1d05b6d8
CW
108 );
109 }
110 }
111 CRM_Core_BAO_MessageTemplate::sendTemplate($params);
f471d149
CB
112}
113
114/**
0aa0303c 115 * Adjust Metadata for Create action.
f471d149
CB
116 *
117 * The metadata is used for setting defaults, documentation &
118 * validation.
119 *
cf470720 120 * @param array $params
b081365f 121 * Array of parameters determined by getfields.
f471d149
CB
122 */
123function _civicrm_api3_message_template_send_spec(&$params) {
1d05b6d8
CW
124 $params['id']['description'] = 'ID of the template';
125 $params['id']['title'] = 'Message Template ID';
cf8f0fff 126 $params['id']['api.aliases'] = ['messageTemplateID', 'message_template_id'];
d142432b 127 $params['id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8 128
1d05b6d8
CW
129 $params['option_value_name']['description'] = 'option value name of the template (required if no id supplied)';
130 $params['option_value_name']['title'] = 'Option Value Name';
cf8f0fff 131 $params['option_value_name']['api.aliases'] = ['valueName'];
d142432b 132 $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
133
134 $params['contact_id']['description'] = 'contact id if contact tokens are to be replaced';
135 $params['contact_id']['title'] = 'Contact ID';
cf8f0fff 136 $params['contact_id']['api.aliases'] = ['contactId'];
d142432b 137 $params['contact_id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8
CW
138
139 $params['template_params']['description'] = 'additional template params (other than the ones already set in the template singleton)';
140 $params['template_params']['title'] = 'Template Params';
cf8f0fff 141 $params['template_params']['api.aliases'] = ['tplParams'];
d142432b 142 // FIXME: Type??
1d05b6d8
CW
143
144 $params['from']['description'] = 'the From: header';
145 $params['from']['title'] = 'From';
d142432b 146 $params['from']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
147
148 $params['to_name']['description'] = 'the recipient’s name';
149 $params['to_name']['title'] = 'Recipient Name';
cf8f0fff 150 $params['to_name']['api.aliases'] = ['toName'];
d142432b 151 $params['to_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
152
153 $params['to_email']['description'] = 'the recipient’s email - mail is sent only if set';
154 $params['to_email']['title'] = 'Recipient Email';
cf8f0fff 155 $params['to_email']['api.aliases'] = ['toEmail'];
d142432b 156 $params['to_email']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
157
158 $params['cc']['description'] = 'the Cc: header';
159 $params['cc']['title'] = 'CC';
d142432b 160 $params['cc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
161
162 $params['bcc']['description'] = 'the Bcc: header';
163 $params['bcc']['title'] = 'BCC';
d142432b 164 $params['bcc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
165
166 $params['reply_to']['description'] = 'the Reply-To: header';
167 $params['reply_to']['title'] = 'Reply To';
cf8f0fff 168 $params['reply_to']['api.aliases'] = ['replyTo'];
d142432b 169 $params['reply_to']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
170
171 $params['attachments']['description'] = 'email attachments';
172 $params['attachments']['title'] = 'Attachments';
d142432b 173 // FIXME: Type??
1d05b6d8
CW
174
175 $params['is_test']['description'] = 'whether this is a test email (and hence should include the test banner)';
176 $params['is_test']['title'] = 'Is Test';
cf8f0fff 177 $params['is_test']['api.aliases'] = ['isTest'];
d142432b 178 $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN;
1d05b6d8
CW
179
180 $params['pdf_filename']['description'] = 'filename of optional PDF version to add as attachment (do not include path)';
181 $params['pdf_filename']['title'] = 'PDF Filename';
cf8f0fff 182 $params['pdf_filename']['api.aliases'] = ['PDFFilename'];
d142432b 183 $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING;
f471d149 184}