Merge pull request #15351 from civicrm/5.18
[civicrm-core.git] / api / v3 / examples / Mailing / Create.ex.php
1 <?php
2 /**
3 * Test Generated example demonstrating the Mailing.create API.
4 *
5 * @return array
6 * API result array
7 */
8 function mailing_create_example() {
9 $params = [
10 'subject' => 'Hello {contact.display_name}',
11 'body_text' => 'This is {contact.display_name}.
12 https://civicrm.org
13 {domain.address}{action.optOutUrl}',
14 'body_html' => '<link href=\'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700\' rel=\'stylesheet\' type=\'text/css\'><p><a href=\"http://{action.forward}\">Forward this email</a><a href=\"{action.forward}\">Forward this email with no protocol</a></p<p>This is {contact.display_name}.</p><p><a href=\'https://civicrm.org/\'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>',
15 'name' => 'mailing name',
16 'created_id' => 15,
17 'header_id' => '',
18 'footer_id' => '',
19 'groups' => [
20 'include' => [
21 '0' => 13,
22 ],
23 'exclude' => [
24 '0' => 14,
25 ],
26 ],
27 'mailings' => [
28 'include' => [],
29 'exclude' => [],
30 ],
31 'options' => [
32 'force_rollback' => 1,
33 ],
34 'api.MailingRecipients.get' => [
35 'mailing_id' => '$value.id',
36 'api.contact.getvalue' => [
37 'return' => 'display_name',
38 ],
39 'api.email.getvalue' => [
40 'return' => 'email',
41 ],
42 ],
43 ];
44
45 try{
46 $result = civicrm_api3('Mailing', 'create', $params);
47 }
48 catch (CiviCRM_API3_Exception $e) {
49 // Handle error here.
50 $errorMessage = $e->getMessage();
51 $errorCode = $e->getErrorCode();
52 $errorData = $e->getExtraParams();
53 return [
54 'is_error' => 1,
55 'error_message' => $errorMessage,
56 'error_code' => $errorCode,
57 'error_data' => $errorData,
58 ];
59 }
60
61 return $result;
62 }
63
64 /**
65 * Function returns array of result expected from previous function.
66 *
67 * @return array
68 * API result array
69 */
70 function mailing_create_expectedresult() {
71
72 $expectedResult = [
73 'is_error' => 0,
74 'version' => 3,
75 'count' => 1,
76 'id' => 9,
77 'values' => [
78 '9' => [
79 'id' => '9',
80 'domain_id' => '1',
81 'header_id' => '',
82 'footer_id' => '',
83 'reply_id' => '8',
84 'unsubscribe_id' => '5',
85 'resubscribe_id' => '6',
86 'optout_id' => '7',
87 'name' => 'mailing name',
88 'mailing_type' => 'standalone',
89 'from_name' => 'FIXME',
90 'from_email' => 'info@EXAMPLE.ORG',
91 'replyto_email' => 'info@EXAMPLE.ORG',
92 'template_type' => 'traditional',
93 'template_options' => '',
94 'subject' => 'Hello {contact.display_name}',
95 'body_text' => 'This is {contact.display_name}.
96 https://civicrm.org
97 {domain.address}{action.optOutUrl}',
98 'body_html' => '<link href=\'https://fonts.googleapis.com/css?family=Roboto+Condensed:400,700|Zilla+Slab:500,700\' rel=\'stylesheet\' type=\'text/css\'><p><a href=\"http://{action.forward}\">Forward this email</a><a href=\"{action.forward}\">Forward this email with no protocol</a></p<p>This is {contact.display_name}.</p><p><a href=\'https://civicrm.org/\'>CiviCRM.org</a></p><p>{domain.address}{action.optOutUrl}</p>',
99 'url_tracking' => '1',
100 'forward_replies' => 0,
101 'auto_responder' => 0,
102 'open_tracking' => '1',
103 'is_completed' => '',
104 'msg_template_id' => '',
105 'override_verp' => '1',
106 'created_id' => '15',
107 'created_date' => '2013-07-28 08:49:19',
108 'modified_date' => '2012-11-14 16:02:35',
109 'scheduled_id' => '',
110 'scheduled_date' => '',
111 'approver_id' => '',
112 'approval_date' => '',
113 'approval_status_id' => '',
114 'approval_note' => '',
115 'is_archived' => 0,
116 'visibility' => 'Public Pages',
117 'campaign_id' => '',
118 'dedupe_email' => '1',
119 'sms_provider_id' => '',
120 'hash' => '',
121 'location_type_id' => '',
122 'email_selection_method' => 'automatic',
123 'language' => '',
124 'api.MailingRecipients.get' => [
125 'is_error' => 0,
126 'version' => 3,
127 'count' => 1,
128 'id' => 4,
129 'values' => [
130 '0' => [
131 'id' => '4',
132 'mailing_id' => '9',
133 'contact_id' => '16',
134 'email_id' => '16',
135 'api.contact.getvalue' => 'Mr. Includer Person II',
136 'api.email.getvalue' => 'include.me@example.org',
137 ],
138 ],
139 ],
140 ],
141 ],
142 ];
143
144 return $expectedResult;
145 }
146
147 /*
148 * This example has been generated from the API test suite.
149 * The test that created it is called "testMailerPreviewRecipients"
150 * and can be found at:
151 * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/MailingTest.php
152 *
153 * You can see the outcome of the API tests at
154 * https://test.civicrm.org/job/CiviCRM-Core-Matrix/
155 *
156 * To Learn about the API read
157 * https://docs.civicrm.org/dev/en/latest/api/
158 *
159 * Browse the API on your own site with the API Explorer. It is in the main
160 * CiviCRM menu, under: Support > Development > API Explorer.
161 *
162 * Read more about testing here
163 * https://docs.civicrm.org/dev/en/latest/testing/
164 *
165 * API Standards documentation:
166 * https://docs.civicrm.org/dev/en/latest/framework/api-architecture/
167 */