updated example & a couple of tidy ups to throw API_Exception rather than legacy
[civicrm-core.git] / api / v3 / examples / Contact / GetFieldsOptions.php
CommitLineData
6a488035 1<?php
fb32de45 2/**
3 * Test Generated example of using contact GetFields API
4 * Demonstrate retrieving metadata with custom field options *
6a488035
TO
5 */
6function contact_getfields_example(){
7a7cc10b 7$params = array(
8 'options' => array(
6a488035
TO
9 'get_options' => 'custom_1',
10 ),
6a488035
TO
11 'action' => 'create',
12);
13
fb32de45 14try{
15 $result = civicrm_api3('contact', 'GetFields', $params);
16}
17catch (CiviCRM_API3_Exception $e) {
18 // handle error here
19 $errorMessage = $e->getMessage();
20 $errorCode = $e->getErrorCode();
21 $errorData = $e->getExtraParams();
22 return array('error' => $errorMessage, 'error_code' => $errorCode, 'error_data' => $errorData);
23}
6a488035 24
fb32de45 25return $result;
6a488035
TO
26}
27
fb32de45 28/**
6a488035
TO
29 * Function returns array of result expected from previous function
30 */
31function contact_getfields_expectedresult(){
32
7a7cc10b 33 $expectedResult = array(
6a488035
TO
34 'is_error' => 0,
35 'version' => 3,
7a7cc10b 36 'count' => 53,
37 'values' => array(
38 'id' => array(
6a488035
TO
39 'name' => 'id',
40 'type' => 1,
41 'title' => 'Internal Contact ID',
42 'required' => true,
43 'import' => true,
44 'where' => 'civicrm_contact.id',
45 'headerPattern' => '/internal|contact?|id$/i',
46 'export' => true,
7a7cc10b 47 'api.aliases' => array(
6a488035
TO
48 '0' => 'contact_id',
49 ),
50 ),
7a7cc10b 51 'contact_type' => array(
6a488035
TO
52 'name' => 'contact_type',
53 'type' => 2,
54 'title' => 'Contact Type',
55 'maxlength' => 64,
56 'size' => 30,
57 'export' => true,
58 'where' => 'civicrm_contact.contact_type',
7a7cc10b 59 'pseudoconstant' => array(
60 'table' => 'civicrm_contact_type',
6a488035
TO
61 'keyColumn' => 'name',
62 'labelColumn' => 'label',
7a7cc10b 63 'condition' => 'parent_id IS NULL',
6a488035
TO
64 ),
65 'api.required' => 1,
6a488035 66 ),
7a7cc10b 67 'contact_sub_type' => array(
6a488035
TO
68 'name' => 'contact_sub_type',
69 'type' => 2,
70 'title' => 'Contact Subtype',
71 'maxlength' => 255,
72 'size' => 45,
73 'import' => true,
74 'where' => 'civicrm_contact.contact_sub_type',
75 'headerPattern' => '/C(ontact )?(subtype|sub-type|sub type)/i',
76 'export' => true,
7a7cc10b 77 'pseudoconstant' => array(
78 'table' => 'civicrm_contact_type',
79 'keyColumn' => 'name',
80 'labelColumn' => 'label',
81 'condition' => 'parent_id IS NOT NULL',
82 ),
6a488035 83 ),
7a7cc10b 84 'do_not_email' => array(
6a488035
TO
85 'name' => 'do_not_email',
86 'type' => 16,
87 'title' => 'Do Not Email',
88 'import' => true,
89 'where' => 'civicrm_contact.do_not_email',
90 'headerPattern' => '/d(o )?(not )?(email)/i',
91 'dataPattern' => '/^\d{1,}$/',
92 'export' => true,
93 ),
7a7cc10b 94 'do_not_phone' => array(
6a488035
TO
95 'name' => 'do_not_phone',
96 'type' => 16,
97 'title' => 'Do Not Phone',
98 'import' => true,
99 'where' => 'civicrm_contact.do_not_phone',
100 'headerPattern' => '/d(o )?(not )?(call|phone)/i',
101 'dataPattern' => '/^\d{1,}$/',
102 'export' => true,
103 ),
7a7cc10b 104 'do_not_mail' => array(
6a488035
TO
105 'name' => 'do_not_mail',
106 'type' => 16,
107 'title' => 'Do Not Mail',
108 'import' => true,
109 'where' => 'civicrm_contact.do_not_mail',
110 'headerPattern' => '/^(d(o\s)?n(ot\s)?mail)|(\w*)?bulk\s?(\w*)$/i',
111 'dataPattern' => '/^\d{1,}$/',
112 'export' => true,
113 ),
7a7cc10b 114 'do_not_sms' => array(
6a488035
TO
115 'name' => 'do_not_sms',
116 'type' => 16,
117 'title' => 'Do Not Sms',
118 'import' => true,
119 'where' => 'civicrm_contact.do_not_sms',
120 'headerPattern' => '/d(o )?(not )?(sms)/i',
121 'dataPattern' => '/^\d{1,}$/',
122 'export' => true,
123 ),
7a7cc10b 124 'do_not_trade' => array(
6a488035
TO
125 'name' => 'do_not_trade',
126 'type' => 16,
127 'title' => 'Do Not Trade',
128 'import' => true,
129 'where' => 'civicrm_contact.do_not_trade',
130 'headerPattern' => '/d(o )?(not )?(trade)/i',
131 'dataPattern' => '/^\d{1,}$/',
132 'export' => true,
133 ),
7a7cc10b 134 'is_opt_out' => array(
6a488035
TO
135 'name' => 'is_opt_out',
136 'type' => 16,
137 'title' => 'No Bulk Emails (User Opt Out)',
138 'required' => true,
139 'import' => true,
140 'where' => 'civicrm_contact.is_opt_out',
141 'export' => true,
142 ),
7a7cc10b 143 'legal_identifier' => array(
6a488035
TO
144 'name' => 'legal_identifier',
145 'type' => 2,
146 'title' => 'Legal Identifier',
147 'maxlength' => 32,
148 'size' => 20,
149 'import' => true,
150 'where' => 'civicrm_contact.legal_identifier',
151 'headerPattern' => '/legal\s?id/i',
152 'dataPattern' => '/\w+?\d{5,}/',
153 'export' => true,
154 ),
7a7cc10b 155 'external_identifier' => array(
6a488035
TO
156 'name' => 'external_identifier',
157 'type' => 2,
158 'title' => 'External Identifier',
159 'maxlength' => 32,
160 'size' => 8,
161 'import' => true,
162 'where' => 'civicrm_contact.external_identifier',
163 'headerPattern' => '/external\s?id/i',
164 'dataPattern' => '/^\d{11,}$/',
165 'export' => true,
166 ),
7a7cc10b 167 'sort_name' => array(
6a488035
TO
168 'name' => 'sort_name',
169 'type' => 2,
170 'title' => 'Sort Name',
171 'maxlength' => 128,
172 'size' => 30,
173 'export' => true,
174 'where' => 'civicrm_contact.sort_name',
175 ),
7a7cc10b 176 'display_name' => array(
6a488035
TO
177 'name' => 'display_name',
178 'type' => 2,
179 'title' => 'Display Name',
180 'maxlength' => 128,
181 'size' => 30,
182 'export' => true,
183 'where' => 'civicrm_contact.display_name',
184 ),
7a7cc10b 185 'nick_name' => array(
6a488035
TO
186 'name' => 'nick_name',
187 'type' => 2,
7a7cc10b 188 'title' => 'Nickname',
6a488035
TO
189 'maxlength' => 128,
190 'size' => 30,
191 'import' => true,
192 'where' => 'civicrm_contact.nick_name',
193 'headerPattern' => '/n(ick\s)name|nick$/i',
194 'dataPattern' => '/^\w+$/',
195 'export' => true,
196 ),
7a7cc10b 197 'legal_name' => array(
6a488035
TO
198 'name' => 'legal_name',
199 'type' => 2,
200 'title' => 'Legal Name',
201 'maxlength' => 128,
202 'size' => 30,
203 'import' => true,
204 'where' => 'civicrm_contact.legal_name',
205 'headerPattern' => '/^legal|(l(egal\s)?name)$/i',
206 'export' => true,
207 ),
7a7cc10b 208 'image_URL' => array(
6a488035
TO
209 'name' => 'image_URL',
210 'type' => 2,
211 'title' => 'Image Url',
212 'maxlength' => 255,
213 'size' => 45,
214 'import' => true,
215 'where' => 'civicrm_contact.image_URL',
216 'export' => true,
217 ),
7a7cc10b 218 'preferred_communication_method' => array(
6a488035
TO
219 'name' => 'preferred_communication_method',
220 'type' => 2,
221 'title' => 'Preferred Communication Method',
222 'maxlength' => 255,
223 'size' => 45,
224 'import' => true,
225 'where' => 'civicrm_contact.preferred_communication_method',
226 'headerPattern' => '/^p(ref\w*\s)?c(omm\w*)|( meth\w*)$/i',
227 'dataPattern' => '/^\w+$/',
228 'export' => true,
7a7cc10b 229 'pseudoconstant' => array(
230 'optionGroupName' => 'preferred_communication_method',
231 ),
6a488035 232 ),
7a7cc10b 233 'preferred_language' => array(
6a488035
TO
234 'name' => 'preferred_language',
235 'type' => 2,
236 'title' => 'Preferred Language',
237 'maxlength' => 5,
238 'size' => 6,
239 'import' => true,
240 'where' => 'civicrm_contact.preferred_language',
241 'headerPattern' => '/^lang/i',
242 'export' => true,
7a7cc10b 243 'pseudoconstant' => array(
244 'optionGroupName' => 'languages',
245 'keyColumn' => 'name',
246 ),
6a488035 247 ),
7a7cc10b 248 'preferred_mail_format' => array(
6a488035
TO
249 'name' => 'preferred_mail_format',
250 'type' => 2,
251 'title' => 'Preferred Mail Format',
252 'import' => true,
253 'where' => 'civicrm_contact.preferred_mail_format',
254 'headerPattern' => '/^p(ref\w*\s)?m(ail\s)?f(orm\w*)$/i',
255 'export' => true,
256 'default' => 'Both',
257 'enumValues' => 'Text, HTML, Both',
6a488035 258 ),
7a7cc10b 259 'hash' => array(
6a488035
TO
260 'name' => 'hash',
261 'type' => 2,
262 'title' => 'Contact Hash',
263 'maxlength' => 32,
264 'size' => 20,
265 'export' => true,
266 'where' => 'civicrm_contact.hash',
267 ),
7a7cc10b 268 'api_key' => array(
6a488035
TO
269 'name' => 'api_key',
270 'type' => 2,
271 'title' => 'Api Key',
272 'maxlength' => 32,
273 'size' => 20,
274 ),
7a7cc10b 275 'first_name' => array(
6a488035
TO
276 'name' => 'first_name',
277 'type' => 2,
278 'title' => 'First Name',
279 'maxlength' => 64,
280 'size' => 30,
281 'import' => true,
282 'where' => 'civicrm_contact.first_name',
283 'headerPattern' => '/^first|(f(irst\s)?name)$/i',
284 'dataPattern' => '/^\w+$/',
285 'export' => true,
286 ),
7a7cc10b 287 'middle_name' => array(
6a488035
TO
288 'name' => 'middle_name',
289 'type' => 2,
290 'title' => 'Middle Name',
291 'maxlength' => 64,
292 'size' => 20,
293 'import' => true,
294 'where' => 'civicrm_contact.middle_name',
295 'headerPattern' => '/^middle|(m(iddle\s)?name)$/i',
296 'dataPattern' => '/^\w+$/',
297 'export' => true,
298 ),
7a7cc10b 299 'last_name' => array(
6a488035
TO
300 'name' => 'last_name',
301 'type' => 2,
302 'title' => 'Last Name',
303 'maxlength' => 64,
304 'size' => 30,
305 'import' => true,
306 'where' => 'civicrm_contact.last_name',
307 'headerPattern' => '/^last|(l(ast\s)?name)$/i',
308 'dataPattern' => '/^\w+(\s\w+)?+$/',
309 'export' => true,
310 ),
9f1b81e0 311 'prefix_id' => array(
312 'name' => 'prefix_id',
313 'type' => 1,
314 'title' => 'Individual Prefix',
315 'import' => true,
316 'where' => 'civicrm_contact.prefix_id',
317 'headerPattern' => '/^(prefix|title)/i',
318 'dataPattern' => '/^(mr|ms|mrs|sir|dr)\.?$/i',
319 'export' => true,
320 'pseudoconstant' => array(
321 'optionGroupName' => 'individual_prefix',
322 ),
b422b715 323 'api.aliases' => array(
324 '0' => 'individual_prefix',
325 '1' => 'individual_prefix_id',
326 ),
9f1b81e0 327 ),
328 'suffix_id' => array(
329 'name' => 'suffix_id',
330 'type' => 1,
331 'title' => 'Individual Suffix',
332 'import' => true,
333 'where' => 'civicrm_contact.suffix_id',
334 'headerPattern' => '/^suffix$/i',
335 'dataPattern' => '/^(sr|jr)\.?|i{2,}$/',
336 'export' => true,
337 'pseudoconstant' => array(
338 'optionGroupName' => 'individual_suffix',
339 ),
b422b715 340 'api.aliases' => array(
341 '0' => 'individual_suffix',
342 '1' => 'individual_suffix_id',
343 ),
9f1b81e0 344 ),
7a7cc10b 345 'email_greeting_id' => array(
6a488035
TO
346 'name' => 'email_greeting_id',
347 'type' => 1,
348 'title' => 'Email Greeting ID',
349 ),
7a7cc10b 350 'email_greeting_custom' => array(
6a488035
TO
351 'name' => 'email_greeting_custom',
352 'type' => 2,
353 'title' => 'Email Greeting Custom',
354 'maxlength' => 128,
355 'size' => 45,
356 'import' => true,
357 'where' => 'civicrm_contact.email_greeting_custom',
358 ),
7a7cc10b 359 'email_greeting_display' => array(
6a488035
TO
360 'name' => 'email_greeting_display',
361 'type' => 2,
362 'title' => 'Email Greeting',
363 'maxlength' => 255,
364 'size' => 45,
365 ),
7a7cc10b 366 'postal_greeting_id' => array(
6a488035
TO
367 'name' => 'postal_greeting_id',
368 'type' => 1,
369 'title' => 'Postal Greeting ID',
370 ),
7a7cc10b 371 'postal_greeting_custom' => array(
6a488035
TO
372 'name' => 'postal_greeting_custom',
373 'type' => 2,
374 'title' => 'Postal Greeting Custom',
375 'maxlength' => 128,
376 'size' => 45,
377 'import' => true,
378 'where' => 'civicrm_contact.postal_greeting_custom',
379 ),
7a7cc10b 380 'postal_greeting_display' => array(
6a488035
TO
381 'name' => 'postal_greeting_display',
382 'type' => 2,
383 'title' => 'Postal Greeting',
384 'maxlength' => 255,
385 'size' => 45,
386 ),
7a7cc10b 387 'addressee_id' => array(
6a488035
TO
388 'name' => 'addressee_id',
389 'type' => 1,
390 'title' => 'Addressee ID',
391 ),
7a7cc10b 392 'addressee_custom' => array(
6a488035
TO
393 'name' => 'addressee_custom',
394 'type' => 2,
395 'title' => 'Addressee Custom',
396 'maxlength' => 128,
397 'size' => 45,
398 'import' => true,
399 'where' => 'civicrm_contact.addressee_custom',
400 ),
7a7cc10b 401 'addressee_display' => array(
6a488035
TO
402 'name' => 'addressee_display',
403 'type' => 2,
404 'title' => 'Addressee',
405 'maxlength' => 255,
406 'size' => 45,
407 ),
7a7cc10b 408 'job_title' => array(
6a488035
TO
409 'name' => 'job_title',
410 'type' => 2,
411 'title' => 'Job Title',
412 'maxlength' => 255,
413 'size' => 20,
414 'import' => true,
415 'where' => 'civicrm_contact.job_title',
416 'headerPattern' => '/^job|(j(ob\s)?title)$/i',
417 'dataPattern' => '//',
418 'export' => true,
419 ),
7a7cc10b 420 'gender_id' => array(
6a488035
TO
421 'name' => 'gender_id',
422 'type' => 1,
423 'title' => 'Gender',
7a7cc10b 424 'import' => true,
425 'where' => 'civicrm_contact.gender_id',
426 'headerPattern' => '/^gender$/i',
427 'export' => true,
428 'pseudoconstant' => array(
6a488035
TO
429 'optionGroupName' => 'gender',
430 ),
6a488035 431 ),
7a7cc10b 432 'birth_date' => array(
6a488035
TO
433 'name' => 'birth_date',
434 'type' => 4,
435 'title' => 'Birth Date',
436 'import' => true,
437 'where' => 'civicrm_contact.birth_date',
438 'headerPattern' => '/^birth|(b(irth\s)?date)|D(\W*)O(\W*)B(\W*)$/i',
439 'dataPattern' => '/\d{4}-?\d{2}-?\d{2}/',
440 'export' => true,
441 ),
7a7cc10b 442 'is_deceased' => array(
6a488035
TO
443 'name' => 'is_deceased',
444 'type' => 16,
445 'title' => 'Is Deceased',
446 'import' => true,
447 'where' => 'civicrm_contact.is_deceased',
448 'headerPattern' => '/i(s\s)?d(eceased)$/i',
449 'export' => true,
450 ),
7a7cc10b 451 'deceased_date' => array(
6a488035
TO
452 'name' => 'deceased_date',
453 'type' => 4,
454 'title' => 'Deceased Date',
455 'import' => true,
456 'where' => 'civicrm_contact.deceased_date',
457 'headerPattern' => '/^deceased|(d(eceased\s)?date)$/i',
458 'export' => true,
459 ),
7a7cc10b 460 'household_name' => array(
6a488035
TO
461 'name' => 'household_name',
462 'type' => 2,
463 'title' => 'Household Name',
464 'maxlength' => 128,
465 'size' => 30,
466 'import' => true,
467 'where' => 'civicrm_contact.household_name',
468 'headerPattern' => '/^household|(h(ousehold\s)?name)$/i',
469 'dataPattern' => '/^\w+$/',
470 'export' => true,
471 ),
7a7cc10b 472 'primary_contact_id' => array(
6a488035
TO
473 'name' => 'primary_contact_id',
474 'type' => 1,
475 'title' => 'Household Primary Contact ID',
476 'FKClassName' => 'CRM_Contact_DAO_Contact',
477 ),
7a7cc10b 478 'organization_name' => array(
6a488035
TO
479 'name' => 'organization_name',
480 'type' => 2,
481 'title' => 'Organization Name',
482 'maxlength' => 128,
483 'size' => 30,
484 'import' => true,
485 'where' => 'civicrm_contact.organization_name',
486 'headerPattern' => '/^organization|(o(rganization\s)?name)$/i',
487 'dataPattern' => '/^\w+$/',
488 'export' => true,
489 ),
7a7cc10b 490 'sic_code' => array(
6a488035
TO
491 'name' => 'sic_code',
492 'type' => 2,
493 'title' => 'Sic Code',
494 'maxlength' => 8,
495 'size' => 8,
496 'import' => true,
497 'where' => 'civicrm_contact.sic_code',
498 'headerPattern' => '/^sic|(s(ic\s)?code)$/i',
499 'export' => true,
500 ),
7a7cc10b 501 'user_unique_id' => array(
6a488035
TO
502 'name' => 'user_unique_id',
503 'type' => 2,
504 'title' => 'Unique ID (OpenID)',
505 'maxlength' => 255,
506 'size' => 45,
507 'import' => true,
508 'where' => 'civicrm_contact.user_unique_id',
509 'headerPattern' => '/^Open\s?ID|u(niq\w*)?\s?ID/i',
510 'dataPattern' => '/^[\w\/\:\.]+$/',
511 'export' => true,
512 'rule' => 'url',
513 ),
7a7cc10b 514 'created_date' => array(
6a488035
TO
515 'name' => 'created_date',
516 'type' => 256,
517 'title' => 'Created Date',
518 'required' => '',
519 'default' => 'UL',
520 ),
7a7cc10b 521 'modified_date' => array(
6a488035
TO
522 'name' => 'modified_date',
523 'type' => 256,
524 'title' => 'Modified Date',
525 'required' => '',
526 'default' => 'URRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAM',
527 ),
7a7cc10b 528 'source' => array(
6a488035
TO
529 'name' => 'source',
530 'type' => 2,
531 'title' => 'Source of Contact Data',
532 'maxlength' => 255,
533 'size' => 30,
534 'import' => true,
535 'where' => 'civicrm_contact.source',
536 'headerPattern' => '/(S(ource\s)?o(f\s)?C(ontact\s)?Data)$/i',
537 'export' => true,
538 'uniqueName' => 'contact_source',
539 ),
7a7cc10b 540 'employer_id' => array(
6a488035
TO
541 'name' => 'employer_id',
542 'type' => 1,
543 'title' => 'Current Employer ID',
544 'export' => true,
545 'where' => 'civicrm_contact.employer_id',
546 'FKClassName' => 'CRM_Contact_DAO_Contact',
547 'uniqueName' => 'current_employer_id',
548 ),
7a7cc10b 549 'is_deleted' => array(
6a488035
TO
550 'name' => 'is_deleted',
551 'type' => 16,
552 'title' => 'Contact is in Trash',
553 'required' => true,
554 'export' => true,
555 'where' => 'civicrm_contact.is_deleted',
556 'uniqueName' => 'contact_is_deleted',
557 ),
7a7cc10b 558 'custom_1' => array(
7f6d3dd3 559 'label' => 'Our special field',
6a488035
TO
560 'groupTitle' => 'select_test_g',
561 'data_type' => 'String',
562 'html_type' => 'Select',
b422b715 563 'default_value' => '',
6a488035
TO
564 'text_length' => '',
565 'options_per_line' => '',
566 'custom_group_id' => '1',
567 'extends' => 'Contact',
568 'is_search_range' => 0,
569 'extends_entity_column_value' => '',
570 'extends_entity_column_id' => '',
571 'is_view' => 0,
572 'is_multiple' => 0,
fb32de45 573 'option_group_id' => '99',
6a488035
TO
574 'date_format' => '',
575 'time_format' => '',
53ca8fd7 576 'name' => 'custom_1',
3f1a97cd 577 'type' => 2,
7a7cc10b 578 'options' => array(
6a488035
TO
579 '1' => 'Label1',
580 '2' => 'Label2',
581 ),
582 ),
7a7cc10b 583 'current_employer' => array(
6a488035
TO
584 'title' => 'Current Employer',
585 'description' => 'Name of Current Employer',
3f1a97cd 586 'type' => 2,
6a488035 587 ),
7a7cc10b 588 'dupe_check' => array(
589 'title' => 'Check for Duplicates',
590 'description' => 'Throw error if contact create matches dedupe rule',
591 ),
6a488035
TO
592 ),
593);
594
fb32de45 595 return $expectedResult;
6a488035
TO
596}
597
598
599/*
600* This example has been generated from the API test suite. The test that created it is called
601*
602* testCustomFieldCreateWithOptionValues and can be found in
603* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/ContactTest.php
604*
605* You can see the outcome of the API tests at
606* http://tests.dev.civicrm.org/trunk/results-api_v3
607*
608* To Learn about the API read
609* http://book.civicrm.org/developer/current/techniques/api/
610*
611* and review the wiki at
612* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs
613*
614* Read more about testing here
615* http://wiki.civicrm.org/confluence/display/CRM/Testing
616*
617* API Standards documentation:
618* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
619*/