| 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 | $params['id']['type'] = CRM_Utils_Type::T_INT; |
| 140 | |
| 141 | $params['option_group_name']['description'] = 'option group name of the template (required if no id supplied)'; |
| 142 | $params['option_group_name']['title'] = 'Option Group Name'; |
| 143 | $params['option_group_name']['api.aliases'] = array('groupName'); |
| 144 | $params['option_group_name']['type'] = CRM_Utils_Type::T_STRING; |
| 145 | |
| 146 | $params['option_value_name']['description'] = 'option value name of the template (required if no id supplied)'; |
| 147 | $params['option_value_name']['title'] = 'Option Value Name'; |
| 148 | $params['option_value_name']['api.aliases'] = array('valueName'); |
| 149 | $params['option_value_name']['type'] = CRM_Utils_Type::T_STRING; |
| 150 | |
| 151 | $params['contact_id']['description'] = 'contact id if contact tokens are to be replaced'; |
| 152 | $params['contact_id']['title'] = 'Contact ID'; |
| 153 | $params['contact_id']['api.aliases'] = array('contactId'); |
| 154 | $params['contact_id']['type'] = CRM_Utils_Type::T_INT; |
| 155 | |
| 156 | $params['template_params']['description'] = 'additional template params (other than the ones already set in the template singleton)'; |
| 157 | $params['template_params']['title'] = 'Template Params'; |
| 158 | $params['template_params']['api.aliases'] = array('tplParams'); |
| 159 | // FIXME: Type?? |
| 160 | |
| 161 | $params['from']['description'] = 'the From: header'; |
| 162 | $params['from']['title'] = 'From'; |
| 163 | $params['from']['type'] = CRM_Utils_Type::T_STRING; |
| 164 | |
| 165 | $params['to_name']['description'] = 'the recipient’s name'; |
| 166 | $params['to_name']['title'] = 'Recipient Name'; |
| 167 | $params['to_name']['api.aliases'] = array('toName'); |
| 168 | $params['to_name']['type'] = CRM_Utils_Type::T_STRING; |
| 169 | |
| 170 | $params['to_email']['description'] = 'the recipient’s email - mail is sent only if set'; |
| 171 | $params['to_email']['title'] = 'Recipient Email'; |
| 172 | $params['to_email']['api.aliases'] = array('toEmail'); |
| 173 | $params['to_email']['type'] = CRM_Utils_Type::T_STRING; |
| 174 | |
| 175 | $params['cc']['description'] = 'the Cc: header'; |
| 176 | $params['cc']['title'] = 'CC'; |
| 177 | $params['cc']['type'] = CRM_Utils_Type::T_STRING; |
| 178 | |
| 179 | $params['bcc']['description'] = 'the Bcc: header'; |
| 180 | $params['bcc']['title'] = 'BCC'; |
| 181 | $params['bcc']['type'] = CRM_Utils_Type::T_STRING; |
| 182 | |
| 183 | $params['reply_to']['description'] = 'the Reply-To: header'; |
| 184 | $params['reply_to']['title'] = 'Reply To'; |
| 185 | $params['reply_to']['api.aliases'] = array('replyTo'); |
| 186 | $params['reply_to']['type'] = CRM_Utils_Type::T_STRING; |
| 187 | |
| 188 | $params['attachments']['description'] = 'email attachments'; |
| 189 | $params['attachments']['title'] = 'Attachments'; |
| 190 | // FIXME: Type?? |
| 191 | |
| 192 | $params['is_test']['description'] = 'whether this is a test email (and hence should include the test banner)'; |
| 193 | $params['is_test']['title'] = 'Is Test'; |
| 194 | $params['is_test']['api.aliases'] = array('isTest'); |
| 195 | $params['is_test']['type'] = CRM_Utils_Type::T_BOOLEAN; |
| 196 | |
| 197 | $params['pdf_filename']['description'] = 'filename of optional PDF version to add as attachment (do not include path)'; |
| 198 | $params['pdf_filename']['title'] = 'PDF Filename'; |
| 199 | $params['pdf_filename']['api.aliases'] = array('PDFFilename'); |
| 200 | $params['pdf_filename']['type'] = CRM_Utils_Type::T_STRING; |
| 201 | } |