* @return array
* @throws \API_Exception
*/
-function civicrm_api3_mailing_get_token($params) {
- if (!array_key_exists("usage", $params)) {
- throw new API_Exception('Mandatory keys missing from params array: entity');
+function civicrm_api3_mailing_gettokens($params) {
+ $tokens = array();
+ foreach ((array) $params['entity'] as $ent) {
+ $func = $ent . 'Tokens';
+ if (!method_exists('CRM_Core_SelectValues', $func)) {
+ throw new API_Exception('Unknown token entity: ' . $ent);
+ }
+ $tokens = array_merge(CRM_Core_SelectValues::$func(), $tokens);
}
-
- $tokens = CRM_Core_SelectValues::contactTokens();
- switch ($params['usage']) {
- case 'Mailing':
- $tokens = array_merge(CRM_Core_SelectValues::mailingTokens(), $tokens);
- break;
-
- case 'ScheduleEventReminder':
- $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens);
- $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
- $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens);
- break;
-
- case 'ManageEventScheduleReminder':
- $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens);
- break;
+ if (!empty($params['sequential'])) {
+ $tokens = CRM_Utils_Token::formatTokensForDisplay($tokens);
}
+ return civicrm_api3_create_success($tokens, $params, 'Mailing', 'gettokens');
+}
- return CRM_Utils_Token::formatTokensForDisplay($tokens);
+/**
+ * Adjust Metadata for Create action.
+ *
+ * The metadata is used for setting defaults, documentation & validation.
+ *
+ * @param array $params
+ * Array of parameters determined by getfields.
+ */
+function _civicrm_api3_mailing_gettokens_spec(&$params) {
+ $params['entity'] = array(
+ 'api.default' => array('contact'),
+ 'api.required' => 1,
+ 'title' => 'Entity',
+ 'options' => array(),
+ );
+ foreach (get_class_methods('CRM_Core_SelectValues') as $func) {
+ if (strpos($func, 'Tokens')) {
+ $ent = str_replace('Tokens', '', $func);
+ $params['entity']['options'][$ent] = $ent;
+ }
+ }
}
/**
--- /dev/null
+<?php
+/**
+ * Test Generated example demonstrating the Mailing.gettokens API.
+ *
+ * Demonstrates fetching tokens for one or more entities (in this case "contact" and "mailing").
+ * Optionally pass sequential=1 to have output ready-formatted for the select2 widget.
+ *
+ * @return array
+ * API result array
+ */
+function mailing_gettokens_example() {
+ $params = array(
+ 'entity' => array(
+ '0' => 'contact',
+ '1' => 'mailing',
+ ),
+ );
+
+ try{
+ $result = civicrm_api3('Mailing', 'gettokens', $params);
+ }
+ catch (CiviCRM_API3_Exception $e) {
+ // Handle error here.
+ $errorMessage = $e->getMessage();
+ $errorCode = $e->getErrorCode();
+ $errorData = $e->getExtraParams();
+ return array(
+ 'error' => $errorMessage,
+ 'error_code' => $errorCode,
+ 'error_data' => $errorData,
+ );
+ }
+
+ return $result;
+}
+
+/**
+ * Function returns array of result expected from previous function.
+ *
+ * @return array
+ * API result array
+ */
+function mailing_gettokens_expectedresult() {
+
+ $expectedResult = array(
+ 'is_error' => 0,
+ 'version' => 3,
+ 'count' => 82,
+ 'values' => array(
+ '{action.unsubscribe}' => 'Unsubscribe via email',
+ '{action.unsubscribeUrl}' => 'Unsubscribe via web page',
+ '{action.resubscribe}' => 'Resubscribe via email',
+ '{action.resubscribeUrl}' => 'Resubscribe via web page',
+ '{action.optOut}' => 'Opt out via email',
+ '{action.optOutUrl}' => 'Opt out via web page',
+ '{action.forward}' => 'Forward this email (link)',
+ '{action.reply}' => 'Reply to this email (link)',
+ '{action.subscribeUrl}' => 'Subscribe via web page',
+ '{domain.name}' => 'Domain name',
+ '{domain.address}' => 'Domain (organization) address',
+ '{domain.phone}' => 'Domain (organization) phone',
+ '{domain.email}' => 'Domain (organization) email',
+ '{mailing.name}' => 'Mailing name',
+ '{mailing.group}' => 'Mailing group',
+ '{mailing.viewUrl}' => 'Mailing permalink',
+ '{contact.contact_type}' => 'Contact Type',
+ '{contact.do_not_email}' => 'Do Not Email',
+ '{contact.do_not_phone}' => 'Do Not Phone',
+ '{contact.do_not_mail}' => 'Do Not Mail',
+ '{contact.do_not_sms}' => 'Do Not Sms',
+ '{contact.do_not_trade}' => 'Do Not Trade',
+ '{contact.is_opt_out}' => 'No Bulk Emails (User Opt Out)',
+ '{contact.external_identifier}' => 'External Identifier',
+ '{contact.sort_name}' => 'Sort Name',
+ '{contact.display_name}' => 'Display Name',
+ '{contact.nick_name}' => 'Nickname',
+ '{contact.image_URL}' => 'Image Url',
+ '{contact.preferred_communication_method}' => 'Preferred Communication Method',
+ '{contact.preferred_language}' => 'Preferred Language',
+ '{contact.preferred_mail_format}' => 'Preferred Mail Format',
+ '{contact.hash}' => 'Contact Hash',
+ '{contact.contact_source}' => 'Source of Contact Data',
+ '{contact.first_name}' => 'First Name',
+ '{contact.middle_name}' => 'Middle Name',
+ '{contact.last_name}' => 'Last Name',
+ '{contact.individual_prefix}' => 'Individual Prefix',
+ '{contact.individual_suffix}' => 'Individual Suffix',
+ '{contact.formal_title}' => 'Formal Title',
+ '{contact.communication_style}' => 'Communication Style',
+ '{contact.job_title}' => 'Job Title',
+ '{contact.gender}' => 'Gender',
+ '{contact.birth_date}' => 'Birth Date',
+ '{contact.current_employer_id}' => 'Current Employer ID',
+ '{contact.contact_is_deleted}' => 'Contact is in Trash',
+ '{contact.created_date}' => 'Created Date',
+ '{contact.modified_date}' => 'Modified Date',
+ '{contact.addressee}' => 'Addressee',
+ '{contact.email_greeting}' => 'Email Greeting',
+ '{contact.postal_greeting}' => 'Postal Greeting',
+ '{contact.current_employer}' => 'Current Employer',
+ '{contact.location_type}' => 'Location Type',
+ '{contact.street_address}' => 'Street Address',
+ '{contact.street_number}' => 'Street Number',
+ '{contact.street_number_suffix}' => 'Street Number Suffix',
+ '{contact.street_name}' => 'Street Name',
+ '{contact.street_unit}' => 'Street Unit',
+ '{contact.supplemental_address_1}' => 'Supplemental Address 1',
+ '{contact.supplemental_address_2}' => 'Supplemental Address 2',
+ '{contact.city}' => 'City',
+ '{contact.postal_code_suffix}' => 'Postal Code Suffix',
+ '{contact.postal_code}' => 'Postal Code',
+ '{contact.geo_code_1}' => 'Latitude',
+ '{contact.geo_code_2}' => 'Longitude',
+ '{contact.address_name}' => 'Address Name',
+ '{contact.master_id}' => 'Master Address Belongs To',
+ '{contact.county}' => 'County',
+ '{contact.state_province}' => 'State',
+ '{contact.country}' => 'Country',
+ '{contact.phone}' => 'Phone',
+ '{contact.phone_ext}' => 'Phone Extension',
+ '{contact.email}' => 'Email',
+ '{contact.on_hold}' => 'On Hold',
+ '{contact.signature_text}' => 'Signature Text',
+ '{contact.signature_html}' => 'Signature Html',
+ '{contact.im_provider}' => 'IM Provider',
+ '{contact.im}' => 'IM Screen Name',
+ '{contact.openid}' => 'OpenID',
+ '{contact.world_region}' => 'World Region',
+ '{contact.url}' => 'Website',
+ '{contact.checksum}' => 'Checksum',
+ '{contact.contact_id}' => 'Internal Contact ID',
+ ),
+ );
+
+ return $expectedResult;
+}
+
+/*
+* This example has been generated from the API test suite.
+* The test that created it is called "testMailGetTokens"
+* and can be found at:
+* https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/MailingTest.php
+*
+* You can see the outcome of the API tests at
+* https://test.civicrm.org/job/CiviCRM-master-git/
+*
+* To Learn about the API read
+* http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API
+*
+* Browse the api on your own site with the api explorer
+* http://MYSITE.ORG/path/to/civicrm/api
+*
+* Read more about testing here
+* http://wiki.civicrm.org/confluence/display/CRM/Testing
+*
+* API Standards documentation:
+* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
+*/