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