Reorganize and update api examples
[civicrm-core.git] / api / v3 / examples / Contact / GetFieldsOptions.php
index fac0bae4e7eb9524e2ad6c61d0cec7a176e345d7..e41ec7d2818ea30a730feaf5312f851c6495418c 100644 (file)
@@ -1,23 +1,31 @@
 <?php
-
-/*
- Demonstrate retrieving metadata with custom field options
+/**
+ * Test Generated example of using contact GetFields API
+ * Demonstrate retrieving metadata with custom field options *
  */
 function contact_getfields_example(){
 $params = array(
   'options' => array(
       'get_options' => 'custom_1',
     ),
-  'version' => 3,
   'action' => 'create',
 );
 
-  $result = civicrm_api( 'contact','GetFields',$params );
+try{
+  $result = civicrm_api3('contact', 'GetFields', $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;
+return $result;
 }
 
-/*
+/**
  * Function returns array of result expected from previous function
  */
 function contact_getfields_expectedresult(){
@@ -80,7 +88,7 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.do_not_email',
           'headerPattern' => '/d(o )?(not )?(email)/i',
-          'dataPattern' => '/^\d{1,}$/',
+          'dataPattern' => '/^\\d{1,}$/',
           'export' => true,
         ),
       'do_not_phone' => array(
@@ -90,7 +98,7 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.do_not_phone',
           'headerPattern' => '/d(o )?(not )?(call|phone)/i',
-          'dataPattern' => '/^\d{1,}$/',
+          'dataPattern' => '/^\\d{1,}$/',
           'export' => true,
         ),
       'do_not_mail' => array(
@@ -99,8 +107,8 @@ function contact_getfields_expectedresult(){
           'title' => 'Do Not Mail',
           'import' => true,
           'where' => 'civicrm_contact.do_not_mail',
-          'headerPattern' => '/^(d(o\s)?n(ot\s)?mail)|(\w*)?bulk\s?(\w*)$/i',
-          'dataPattern' => '/^\d{1,}$/',
+          'headerPattern' => '/^(d(o\\s)?n(ot\\s)?mail)|(\\w*)?bulk\\s?(\\w*)$/i',
+          'dataPattern' => '/^\\d{1,}$/',
           'export' => true,
         ),
       'do_not_sms' => array(
@@ -110,7 +118,7 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.do_not_sms',
           'headerPattern' => '/d(o )?(not )?(sms)/i',
-          'dataPattern' => '/^\d{1,}$/',
+          'dataPattern' => '/^\\d{1,}$/',
           'export' => true,
         ),
       'do_not_trade' => array(
@@ -120,7 +128,7 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.do_not_trade',
           'headerPattern' => '/d(o )?(not )?(trade)/i',
-          'dataPattern' => '/^\d{1,}$/',
+          'dataPattern' => '/^\\d{1,}$/',
           'export' => true,
         ),
       'is_opt_out' => array(
@@ -140,8 +148,8 @@ function contact_getfields_expectedresult(){
           'size' => 20,
           'import' => true,
           'where' => 'civicrm_contact.legal_identifier',
-          'headerPattern' => '/legal\s?id/i',
-          'dataPattern' => '/\w+?\d{5,}/',
+          'headerPattern' => '/legal\\s?id/i',
+          'dataPattern' => '/\\w+?\\d{5,}/',
           'export' => true,
         ),
       'external_identifier' => array(
@@ -152,8 +160,8 @@ function contact_getfields_expectedresult(){
           'size' => 8,
           'import' => true,
           'where' => 'civicrm_contact.external_identifier',
-          'headerPattern' => '/external\s?id/i',
-          'dataPattern' => '/^\d{11,}$/',
+          'headerPattern' => '/external\\s?id/i',
+          'dataPattern' => '/^\\d{11,}$/',
           'export' => true,
         ),
       'sort_name' => array(
@@ -182,8 +190,8 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.nick_name',
-          'headerPattern' => '/n(ick\s)name|nick$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/n(ick\\s)name|nick$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
         ),
       'legal_name' => array(
@@ -194,7 +202,7 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.legal_name',
-          'headerPattern' => '/^legal|(l(egal\s)?name)$/i',
+          'headerPattern' => '/^legal|(l(egal\\s)?name)$/i',
           'export' => true,
         ),
       'image_URL' => array(
@@ -215,8 +223,8 @@ function contact_getfields_expectedresult(){
           'size' => 45,
           'import' => true,
           'where' => 'civicrm_contact.preferred_communication_method',
-          'headerPattern' => '/^p(ref\w*\s)?c(omm\w*)|( meth\w*)$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/^p(ref\\w*\\s)?c(omm\\w*)|( meth\\w*)$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
           'pseudoconstant' => array(
               'optionGroupName' => 'preferred_communication_method',
@@ -243,7 +251,7 @@ function contact_getfields_expectedresult(){
           'title' => 'Preferred Mail Format',
           'import' => true,
           'where' => 'civicrm_contact.preferred_mail_format',
-          'headerPattern' => '/^p(ref\w*\s)?m(ail\s)?f(orm\w*)$/i',
+          'headerPattern' => '/^p(ref\\w*\\s)?m(ail\\s)?f(orm\\w*)$/i',
           'export' => true,
           'default' => 'Both',
           'enumValues' => 'Text, HTML, Both',
@@ -272,8 +280,8 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.first_name',
-          'headerPattern' => '/^first|(f(irst\s)?name)$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/^first|(f(irst\\s)?name)$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
         ),
       'middle_name' => array(
@@ -284,8 +292,8 @@ function contact_getfields_expectedresult(){
           'size' => 20,
           'import' => true,
           'where' => 'civicrm_contact.middle_name',
-          'headerPattern' => '/^middle|(m(iddle\s)?name)$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/^middle|(m(iddle\\s)?name)$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
         ),
       'last_name' => array(
@@ -296,8 +304,8 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.last_name',
-          'headerPattern' => '/^last|(l(ast\s)?name)$/i',
-          'dataPattern' => '/^\w+(\s\w+)?+$/',
+          'headerPattern' => '/^last|(l(ast\\s)?name)$/i',
+          'dataPattern' => '/^\\w+(\\s\\w+)?+$/',
           'export' => true,
         ),
       'prefix_id' => array(
@@ -307,11 +315,15 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.prefix_id',
           'headerPattern' => '/^(prefix|title)/i',
-          'dataPattern' => '/^(mr|ms|mrs|sir|dr)\.?$/i',
+          'dataPattern' => '/^(mr|ms|mrs|sir|dr)\\.?$/i',
           'export' => true,
           'pseudoconstant' => array(
               'optionGroupName' => 'individual_prefix',
             ),
+          'api.aliases' => array(
+              '0' => 'individual_prefix',
+              '1' => 'individual_prefix_id',
+            ),
         ),
       'suffix_id' => array(
           'name' => 'suffix_id',
@@ -320,11 +332,15 @@ function contact_getfields_expectedresult(){
           'import' => true,
           'where' => 'civicrm_contact.suffix_id',
           'headerPattern' => '/^suffix$/i',
-          'dataPattern' => '/^(sr|jr)\.?|i{2,}$/',
+          'dataPattern' => '/^(sr|jr)\\.?|i{2,}$/',
           'export' => true,
           'pseudoconstant' => array(
               'optionGroupName' => 'individual_suffix',
             ),
+          'api.aliases' => array(
+              '0' => 'individual_suffix',
+              '1' => 'individual_suffix_id',
+            ),
         ),
       'email_greeting_id' => array(
           'name' => 'email_greeting_id',
@@ -397,7 +413,7 @@ function contact_getfields_expectedresult(){
           'size' => 20,
           'import' => true,
           'where' => 'civicrm_contact.job_title',
-          'headerPattern' => '/^job|(j(ob\s)?title)$/i',
+          'headerPattern' => '/^job|(j(ob\\s)?title)$/i',
           'dataPattern' => '//',
           'export' => true,
         ),
@@ -419,8 +435,8 @@ function contact_getfields_expectedresult(){
           'title' => 'Birth Date',
           'import' => true,
           'where' => 'civicrm_contact.birth_date',
-          'headerPattern' => '/^birth|(b(irth\s)?date)|D(\W*)O(\W*)B(\W*)$/i',
-          'dataPattern' => '/\d{4}-?\d{2}-?\d{2}/',
+          'headerPattern' => '/^birth|(b(irth\\s)?date)|D(\\W*)O(\\W*)B(\\W*)$/i',
+          'dataPattern' => '/\\d{4}-?\\d{2}-?\\d{2}/',
           'export' => true,
         ),
       'is_deceased' => array(
@@ -429,7 +445,7 @@ function contact_getfields_expectedresult(){
           'title' => 'Is Deceased',
           'import' => true,
           'where' => 'civicrm_contact.is_deceased',
-          'headerPattern' => '/i(s\s)?d(eceased)$/i',
+          'headerPattern' => '/i(s\\s)?d(eceased)$/i',
           'export' => true,
         ),
       'deceased_date' => array(
@@ -438,7 +454,7 @@ function contact_getfields_expectedresult(){
           'title' => 'Deceased Date',
           'import' => true,
           'where' => 'civicrm_contact.deceased_date',
-          'headerPattern' => '/^deceased|(d(eceased\s)?date)$/i',
+          'headerPattern' => '/^deceased|(d(eceased\\s)?date)$/i',
           'export' => true,
         ),
       'household_name' => array(
@@ -449,8 +465,8 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.household_name',
-          'headerPattern' => '/^household|(h(ousehold\s)?name)$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/^household|(h(ousehold\\s)?name)$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
         ),
       'primary_contact_id' => array(
@@ -467,8 +483,8 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.organization_name',
-          'headerPattern' => '/^organization|(o(rganization\s)?name)$/i',
-          'dataPattern' => '/^\w+$/',
+          'headerPattern' => '/^organization|(o(rganization\\s)?name)$/i',
+          'dataPattern' => '/^\\w+$/',
           'export' => true,
         ),
       'sic_code' => array(
@@ -479,7 +495,7 @@ function contact_getfields_expectedresult(){
           'size' => 8,
           'import' => true,
           'where' => 'civicrm_contact.sic_code',
-          'headerPattern' => '/^sic|(s(ic\s)?code)$/i',
+          'headerPattern' => '/^sic|(s(ic\\s)?code)$/i',
           'export' => true,
         ),
       'user_unique_id' => array(
@@ -490,8 +506,8 @@ function contact_getfields_expectedresult(){
           'size' => 45,
           'import' => true,
           'where' => 'civicrm_contact.user_unique_id',
-          'headerPattern' => '/^Open\s?ID|u(niq\w*)?\s?ID/i',
-          'dataPattern' => '/^[\w\/\:\.]+$/',
+          'headerPattern' => '/^Open\\s?ID|u(niq\\w*)?\\s?ID/i',
+          'dataPattern' => '/^[\\w\\/\\:\\.]+$/',
           'export' => true,
           'rule' => 'url',
         ),
@@ -500,14 +516,14 @@ function contact_getfields_expectedresult(){
           'type' => 256,
           'title' => 'Created Date',
           'required' => '',
-          'default' => 'UL',
+          'default' => 'NULL',
         ),
       'modified_date' => array(
           'name' => 'modified_date',
           'type' => 256,
           'title' => 'Modified Date',
           'required' => '',
-          'default' => 'URRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAM',
+          'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
         ),
       'source' => array(
           'name' => 'source',
@@ -517,7 +533,7 @@ function contact_getfields_expectedresult(){
           'size' => 30,
           'import' => true,
           'where' => 'civicrm_contact.source',
-          'headerPattern' => '/(S(ource\s)?o(f\s)?C(ontact\s)?Data)$/i',
+          'headerPattern' => '/(S(ource\\s)?o(f\\s)?C(ontact\\s)?Data)$/i',
           'export' => true,
           'uniqueName' => 'contact_source',
         ),
@@ -544,6 +560,7 @@ function contact_getfields_expectedresult(){
           'groupTitle' => 'select_test_g',
           'data_type' => 'String',
           'html_type' => 'Select',
+          'default_value' => '',
           'text_length' => '',
           'options_per_line' => '',
           'custom_group_id' => '1',
@@ -553,10 +570,11 @@ function contact_getfields_expectedresult(){
           'extends_entity_column_id' => '',
           'is_view' => 0,
           'is_multiple' => 0,
-          'option_group_id' => '97',
+          'option_group_id' => '99',
           'date_format' => '',
           'time_format' => '',
           'name' => 'custom_1',
+          'type' => 2,
           'options' => array(
               '1' => 'Label1',
               '2' => 'Label2',
@@ -565,6 +583,7 @@ function contact_getfields_expectedresult(){
       'current_employer' => array(
           'title' => 'Current Employer',
           'description' => 'Name of Current Employer',
+          'type' => 2,
         ),
       'dupe_check' => array(
           'title' => 'Check for Duplicates',
@@ -573,7 +592,7 @@ function contact_getfields_expectedresult(){
     ),
 );
 
-  return $expectedResult  ;
+  return $expectedResult;
 }
 
 
@@ -597,4 +616,4 @@ function contact_getfields_expectedresult(){
 *
 * API Standards documentation:
 * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
-*/
\ No newline at end of file
+*/