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