| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * This api exposes CiviCRM message_template. |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Create message template. |
| 36 | * |
| 37 | * @param array $params |
| 38 | * |
| 39 | * @return array |
| 40 | * @throws \API_Exception |
| 41 | */ |
| 42 | function civicrm_api3_message_template_create($params) { |
| 43 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'MessageTemplate'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Adjust Metadata for Create action. |
| 48 | * |
| 49 | * The metadata is used for setting defaults, documentation & validation. |
| 50 | * |
| 51 | * @param array $params |
| 52 | * Array of parameters determined by getfields. |
| 53 | */ |
| 54 | function _civicrm_api3_message_template_create_spec(&$params) { |
| 55 | $params['msg_title']['api.required'] = 1; |
| 56 | $params['is_active']['api.default'] = TRUE; |
| 57 | /* $params['entity_id']['api.required'] = 1; |
| 58 | $params['entity_table']['api.default'] = "civicrm_contribution_recur"; |
| 59 | $params['type']['api.default'] = "R"; |
| 60 | */ |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Delete message template. |
| 65 | * |
| 66 | * @param array $params |
| 67 | * |
| 68 | * @return bool |
| 69 | * API result array |
| 70 | */ |
| 71 | function civicrm_api3_message_template_delete($params) { |
| 72 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Adjust metadata for message_template get action. |
| 77 | * |
| 78 | * @param array $params |
| 79 | */ |
| 80 | function _civicrm_api3_message_template_get_spec(&$params) { |
| 81 | // fetch active records by default |
| 82 | $params['is_active']['api.default'] = 1; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Retrieve one or more message_template. |
| 87 | * |
| 88 | * @param array $params |
| 89 | * Array of name/value pairs. |
| 90 | * |
| 91 | * @return array |
| 92 | * API result array. |
| 93 | */ |
| 94 | function civicrm_api3_message_template_get($params) { |
| 95 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sends a template. |
| 100 | * |
| 101 | * @param array $params |
| 102 | * @throws API_Exception |
| 103 | */ |
| 104 | function civicrm_api3_message_template_send($params) { |
| 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); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Adjust Metadata for Create action. |
| 130 | * |
| 131 | * The metadata is used for setting defaults, documentation & |
| 132 | * validation. |
| 133 | * |
| 134 | * @param array $params |
| 135 | * Array of parameters determined by getfields. |
| 136 | */ |
| 137 | function _civicrm_api3_message_template_send_spec(&$params) { |
| 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'); |
| 141 | $params['id']['type'] = CRM_Utils_Type::T_INT; |
| 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'); |
| 146 | $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING; |
| 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'); |
| 151 | $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING; |
| 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'); |
| 156 | $params['contact_id']['type'] = CRM_Utils_Type::T_INT; |
| 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'); |
| 161 | // FIXME: Type?? |
| 162 | |
| 163 | $params['from']['description'] = 'the From: header'; |
| 164 | $params['from']['title'] = 'From'; |
| 165 | $params['from']['type'] = CRM_Utils_Type::T_STRING; |
| 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'); |
| 170 | $params['to_name']['type'] = CRM_Utils_Type::T_STRING; |
| 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'); |
| 175 | $params['to_email']['type'] = CRM_Utils_Type::T_STRING; |
| 176 | |
| 177 | $params['cc']['description'] = 'the Cc: header'; |
| 178 | $params['cc']['title'] = 'CC'; |
| 179 | $params['cc']['type'] = CRM_Utils_Type::T_STRING; |
| 180 | |
| 181 | $params['bcc']['description'] = 'the Bcc: header'; |
| 182 | $params['bcc']['title'] = 'BCC'; |
| 183 | $params['bcc']['type'] = CRM_Utils_Type::T_STRING; |
| 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'); |
| 188 | $params['reply_to']['type'] = CRM_Utils_Type::T_STRING; |
| 189 | |
| 190 | $params['attachments']['description'] = 'email attachments'; |
| 191 | $params['attachments']['title'] = 'Attachments'; |
| 192 | // FIXME: Type?? |
| 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'); |
| 197 | $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN; |
| 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'); |
| 202 | $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING; |
| 203 | } |