Merge pull request #15843 from totten/master-simplehead
[civicrm-core.git] / api / v3 / MessageTemplate.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 message_template.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Create message template.
20 *
21 * @param array $params
22 *
23 * @return array
24 * @throws \API_Exception
25 */
26 function civicrm_api3_message_template_create($params) {
27 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MessageTemplate');
28 }
29
30 /**
31 * Adjust Metadata for Create action.
32 *
33 * The metadata is used for setting defaults, documentation & validation.
34 *
35 * @param array $params
36 * Array of parameters determined by getfields.
37 */
38 function _civicrm_api3_message_template_create_spec(&$params) {
39 $params['msg_title']['api.required'] = 1;
40 $params['is_active']['api.default'] = TRUE;
41 /* $params['entity_id']['api.required'] = 1;
42 $params['entity_table']['api.default'] = "civicrm_contribution_recur";
43 $params['type']['api.default'] = "R";
44 */
45 }
46
47 /**
48 * Delete message template.
49 *
50 * @param array $params
51 *
52 * @return bool
53 * API result array
54 */
55 function civicrm_api3_message_template_delete($params) {
56 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
57 }
58
59 /**
60 * Adjust metadata for message_template get action.
61 *
62 * @param array $params
63 */
64 function _civicrm_api3_message_template_get_spec(&$params) {
65 // fetch active records by default
66 $params['is_active']['api.default'] = 1;
67 }
68
69 /**
70 * Retrieve one or more message_template.
71 *
72 * @param array $params
73 * Array of name/value pairs.
74 *
75 * @return array
76 * API result array.
77 */
78 function civicrm_api3_message_template_get($params) {
79 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
80 }
81
82 /**
83 * Sends a template.
84 *
85 * @param array $params
86 * @throws API_Exception
87 */
88 function civicrm_api3_message_template_send($params) {
89 // Change external param names to internal ones
90 $fieldSpec = [];
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",
105 ["fields" => ['id', 'option_group_name', 'option_value_name']]
106 );
107 }
108 }
109 CRM_Core_BAO_MessageTemplate::sendTemplate($params);
110 }
111
112 /**
113 * Adjust Metadata for Create action.
114 *
115 * The metadata is used for setting defaults, documentation &
116 * validation.
117 *
118 * @param array $params
119 * Array of parameters determined by getfields.
120 */
121 function _civicrm_api3_message_template_send_spec(&$params) {
122 $params['id']['description'] = 'ID of the template';
123 $params['id']['title'] = 'Message Template ID';
124 $params['id']['api.aliases'] = ['messageTemplateID', 'message_template_id'];
125 $params['id']['type'] = CRM_Utils_Type::T_INT;
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';
129 $params['option_group_name']['api.aliases'] = ['groupName'];
130 $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING;
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';
134 $params['option_value_name']['api.aliases'] = ['valueName'];
135 $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING;
136
137 $params['contact_id']['description'] = 'contact id if contact tokens are to be replaced';
138 $params['contact_id']['title'] = 'Contact ID';
139 $params['contact_id']['api.aliases'] = ['contactId'];
140 $params['contact_id']['type'] = CRM_Utils_Type::T_INT;
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';
144 $params['template_params']['api.aliases'] = ['tplParams'];
145 // FIXME: Type??
146
147 $params['from']['description'] = 'the From: header';
148 $params['from']['title'] = 'From';
149 $params['from']['type'] = CRM_Utils_Type::T_STRING;
150
151 $params['to_name']['description'] = 'the recipient’s name';
152 $params['to_name']['title'] = 'Recipient Name';
153 $params['to_name']['api.aliases'] = ['toName'];
154 $params['to_name']['type'] = CRM_Utils_Type::T_STRING;
155
156 $params['to_email']['description'] = 'the recipient’s email - mail is sent only if set';
157 $params['to_email']['title'] = 'Recipient Email';
158 $params['to_email']['api.aliases'] = ['toEmail'];
159 $params['to_email']['type'] = CRM_Utils_Type::T_STRING;
160
161 $params['cc']['description'] = 'the Cc: header';
162 $params['cc']['title'] = 'CC';
163 $params['cc']['type'] = CRM_Utils_Type::T_STRING;
164
165 $params['bcc']['description'] = 'the Bcc: header';
166 $params['bcc']['title'] = 'BCC';
167 $params['bcc']['type'] = CRM_Utils_Type::T_STRING;
168
169 $params['reply_to']['description'] = 'the Reply-To: header';
170 $params['reply_to']['title'] = 'Reply To';
171 $params['reply_to']['api.aliases'] = ['replyTo'];
172 $params['reply_to']['type'] = CRM_Utils_Type::T_STRING;
173
174 $params['attachments']['description'] = 'email attachments';
175 $params['attachments']['title'] = 'Attachments';
176 // FIXME: Type??
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';
180 $params['is_test']['api.aliases'] = ['isTest'];
181 $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN;
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';
185 $params['pdf_filename']['api.aliases'] = ['PDFFilename'];
186 $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING;
187 }