Merge pull request #6363 from civicrm/CRM-16944
[civicrm-core.git] / api / v3 / MessageTemplate.php
CommitLineData
4a092902 1<?php
4a092902
XD
2/*
3 +--------------------------------------------------------------------+
1e3c76b6 4 | CiviCRM version 4.6 |
4a092902 5 +--------------------------------------------------------------------+
1e3c76b6 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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) {
4a092902
XD
43 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
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
f471d149
CB
102 */
103function civicrm_api3_message_template_send($params) {
1d05b6d8
CW
104 // Change external param names to internal ones
105 $fieldSpec = array();
106 _civicrm_api3_message_template_send_spec($fieldSpec);
107
108 foreach ($fieldSpec as $field => $spec) {
109 if (isset($spec['api.aliases']) && array_key_exists($field, $params)) {
110 $params[CRM_Utils_Array::first($spec['api.aliases'])] = $params[$field];
111 unset($params[$field]);
112 }
113 }
114 if (empty($params['messageTemplateID'])) {
115 if (empty($params['groupName']) || empty($params['valueName'])) {
116 // Can't use civicrm_api3_verify_mandatory for this because it would give the wrong field names
117 throw new API_Exception(
118 "Mandatory key(s) missing from params array: requires id or option_group_name + option_value_name",
119 "mandatory_missing",
120 array("fields" => array('id', 'option_group_name', 'option_value_name'))
121 );
122 }
123 }
124 CRM_Core_BAO_MessageTemplate::sendTemplate($params);
f471d149
CB
125}
126
127/**
0aa0303c 128 * Adjust Metadata for Create action.
f471d149
CB
129 *
130 * The metadata is used for setting defaults, documentation &
131 * validation.
132 *
cf470720 133 * @param array $params
b081365f 134 * Array of parameters determined by getfields.
f471d149
CB
135 */
136function _civicrm_api3_message_template_send_spec(&$params) {
1d05b6d8
CW
137 $params['id']['description'] = 'ID of the template';
138 $params['id']['title'] = 'Message Template ID';
139 $params['id']['api.aliases'] = array('messageTemplateID', 'message_template_id');
d142432b 140 $params['id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8
CW
141
142 $params['option_group_name']['description'] = 'option group name of the template (required if no id supplied)';
143 $params['option_group_name']['title'] = 'Option Group Name';
144 $params['option_group_name']['api.aliases'] = array('groupName');
d142432b 145 $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
146
147 $params['option_value_name']['description'] = 'option value name of the template (required if no id supplied)';
148 $params['option_value_name']['title'] = 'Option Value Name';
149 $params['option_value_name']['api.aliases'] = array('valueName');
d142432b 150 $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
151
152 $params['contact_id']['description'] = 'contact id if contact tokens are to be replaced';
153 $params['contact_id']['title'] = 'Contact ID';
154 $params['contact_id']['api.aliases'] = array('contactId');
d142432b 155 $params['contact_id']['type'] = CRM_Utils_Type::T_INT;
1d05b6d8
CW
156
157 $params['template_params']['description'] = 'additional template params (other than the ones already set in the template singleton)';
158 $params['template_params']['title'] = 'Template Params';
159 $params['template_params']['api.aliases'] = array('tplParams');
d142432b 160 // FIXME: Type??
1d05b6d8
CW
161
162 $params['from']['description'] = 'the From: header';
163 $params['from']['title'] = 'From';
d142432b 164 $params['from']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
165
166 $params['to_name']['description'] = 'the recipient’s name';
167 $params['to_name']['title'] = 'Recipient Name';
168 $params['to_name']['api.aliases'] = array('toName');
d142432b 169 $params['to_name']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
170
171 $params['to_email']['description'] = 'the recipient’s email - mail is sent only if set';
172 $params['to_email']['title'] = 'Recipient Email';
173 $params['to_email']['api.aliases'] = array('toEmail');
d142432b 174 $params['to_email']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
175
176 $params['cc']['description'] = 'the Cc: header';
177 $params['cc']['title'] = 'CC';
d142432b 178 $params['cc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
179
180 $params['bcc']['description'] = 'the Bcc: header';
181 $params['bcc']['title'] = 'BCC';
d142432b 182 $params['bcc']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
183
184 $params['reply_to']['description'] = 'the Reply-To: header';
185 $params['reply_to']['title'] = 'Reply To';
186 $params['reply_to']['api.aliases'] = array('replyTo');
d142432b 187 $params['reply_to']['type'] = CRM_Utils_Type::T_STRING;
1d05b6d8
CW
188
189 $params['attachments']['description'] = 'email attachments';
190 $params['attachments']['title'] = 'Attachments';
d142432b 191 // FIXME: Type??
1d05b6d8
CW
192
193 $params['is_test']['description'] = 'whether this is a test email (and hence should include the test banner)';
194 $params['is_test']['title'] = 'Is Test';
195 $params['is_test']['api.aliases'] = array('isTest');
d142432b 196 $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN;
1d05b6d8
CW
197
198 $params['pdf_filename']['description'] = 'filename of optional PDF version to add as attachment (do not include path)';
199 $params['pdf_filename']['title'] = 'PDF Filename';
200 $params['pdf_filename']['api.aliases'] = array('PDFFilename');
d142432b 201 $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING;
f471d149 202}