From dc5a7701013ad2bc15045b4e596b5bd464369d6d Mon Sep 17 00:00:00 2001 From: Eileen Date: Thu, 23 Jan 2014 16:16:04 +0000 Subject: [PATCH] CRM-14159 API Add generic options['metadata'] = labels functionality ---------------------------------------- * CRM-14159: Add ability to return labels as top level metadata http://issues.civicrm.org/jira/browse/CRM-14159 --- api/v3/Website.php | 2 +- .../examples/Website/GetWithLabelMetadata.php | 104 ++++++++++++++++++ api/v3/examples/WebsiteGetFields.php | 97 ++++++++++++++++ api/v3/utils.php | 10 +- tests/phpunit/api/v3/WebsiteTest.php | 18 ++- xml/schema/Core/Website.xml | 2 + 6 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 api/v3/examples/Website/GetWithLabelMetadata.php create mode 100644 api/v3/examples/WebsiteGetFields.php diff --git a/api/v3/Website.php b/api/v3/Website.php index 5aff8ea485..3c2030697c 100644 --- a/api/v3/Website.php +++ b/api/v3/Website.php @@ -109,6 +109,6 @@ function civicrm_api3_website_delete($params) { * @access public */ function civicrm_api3_website_get($params) { - return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); + return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'website'); } diff --git a/api/v3/examples/Website/GetWithLabelMetadata.php b/api/v3/examples/Website/GetWithLabelMetadata.php new file mode 100644 index 0000000000..650e99071d --- /dev/null +++ b/api/v3/examples/Website/GetWithLabelMetadata.php @@ -0,0 +1,104 @@ + array( + 'metadata' => array( + '0' => 'fields', + ), + ), +); + +try{ + $result = civicrm_api3('website', 'get', $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 + */ +function website_get_expectedresult(){ + + $expectedResult = array( + 'is_error' => 0, + 'version' => 3, + 'count' => 0, + 'values' => array(), + 'metadata' => array( + 'fields' => array( + 'id' => array( + 'name' => 'id', + 'type' => '1', + 'title' => 'Website ID', + 'required' => '1', + 'api.aliases' => array( + '0' => 'website_id', + ), + ), + 'contact_id' => array( + 'name' => 'contact_id', + 'type' => '1', + 'title' => 'Contact', + 'FKClassName' => 'CRM_Contact_DAO_Contact', + ), + 'url' => array( + 'name' => 'url', + 'type' => '2', + 'title' => 'Website', + 'maxlength' => '128', + 'size' => '30', + 'import' => '1', + 'where' => 'civicrm_website.url', + 'headerPattern' => '/Website/i', + 'dataPattern' => '/^[A-Za-z][0-9A-Za-z]{20,}$/', + 'export' => '1', + ), + 'website_type_id' => array( + 'name' => 'website_type_id', + 'type' => '1', + 'title' => 'Website Type', + 'pseudoconstant' => array( + 'optionGroupName' => 'website_type', + ), + ), + ), + ), +); + + return $expectedResult; +} + + +/* +* This example has been generated from the API test suite. The test that created it is called +* +* testGetMetadata and can be found in +* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/WebsiteTest.php +* +* You can see the outcome of the API tests at +* http://tests.dev.civicrm.org/trunk/results-api_v3 +* +* To Learn about the API read +* http://book.civicrm.org/developer/current/techniques/api/ +* +* and review the wiki at +* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs +* +* 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 +*/ diff --git a/api/v3/examples/WebsiteGetFields.php b/api/v3/examples/WebsiteGetFields.php new file mode 100644 index 0000000000..5e1669e5c7 --- /dev/null +++ b/api/v3/examples/WebsiteGetFields.php @@ -0,0 +1,97 @@ + 'get', +); + +try{ + $result = civicrm_api3('website', '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; +} + +/** + * Function returns array of result expected from previous function + */ +function website_getfields_expectedresult(){ + + $expectedResult = array( + 'is_error' => 0, + 'version' => 3, + 'count' => 4, + 'values' => array( + 'id' => array( + 'name' => 'id', + 'type' => 1, + 'title' => 'Website ID', + 'required' => true, + 'api.aliases' => array( + '0' => 'website_id', + ), + ), + 'contact_id' => array( + 'name' => 'contact_id', + 'type' => 1, + 'title' => 'Contact', + 'FKClassName' => 'CRM_Contact_DAO_Contact', + ), + 'url' => array( + 'name' => 'url', + 'type' => 2, + 'title' => 'Website', + 'maxlength' => 128, + 'size' => 30, + 'import' => true, + 'where' => 'civicrm_website.url', + 'headerPattern' => '/Website/i', + 'dataPattern' => '/^[A-Za-z][0-9A-Za-z]{20,}$/', + 'export' => true, + ), + 'website_type_id' => array( + 'name' => 'website_type_id', + 'type' => 1, + 'title' => 'Website Type', + 'pseudoconstant' => array( + 'optionGroupName' => 'website_type', + ), + ), + ), +); + + return $expectedResult; +} + + +/* +* This example has been generated from the API test suite. The test that created it is called +* +* testGetFields and can be found in +* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/WebsiteTest.php +* +* You can see the outcome of the API tests at +* http://tests.dev.civicrm.org/trunk/results-api_v3 +* +* To Learn about the API read +* http://book.civicrm.org/developer/current/techniques/api/ +* +* and review the wiki at +* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs +* +* 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 +*/ diff --git a/api/v3/utils.php b/api/v3/utils.php index f810fcbf42..65a6579872 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -253,7 +253,13 @@ function civicrm_api3_create_success($values = 1, $params = array( else { $result['values'] = $values; } - + if(!empty($params['options']) && !empty($params['options']['metadata'])) { + // we've made metadata an array but only supporting labels atm + if(in_array('fields', $params['options']['metadata'])) { + $fields = civicrm_api3($entity, 'getfields', array('action' => $action)); + $result['metadata']['fields'] = $fields['values']; + } + } return array_merge($result, $extraReturnValues); } @@ -967,7 +973,7 @@ function _civicrm_api3_basic_get($bao_name, &$params, $returnAsSuccess = TRUE, $ $bao = new $bao_name(); _civicrm_api3_dao_set_filter($bao, $params, TRUE,$entity); if ($returnAsSuccess) { - return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity); + return civicrm_api3_create_success(_civicrm_api3_dao_to_array($bao, $params, FALSE, $entity), $params, $entity, 'get'); } else { return _civicrm_api3_dao_to_array($bao, $params, FALSE, $entity); diff --git a/tests/phpunit/api/v3/WebsiteTest.php b/tests/phpunit/api/v3/WebsiteTest.php index 7484c2077b..86ff8b18da 100644 --- a/tests/phpunit/api/v3/WebsiteTest.php +++ b/tests/phpunit/api/v3/WebsiteTest.php @@ -90,6 +90,22 @@ class api_v3_WebsiteTest extends CiviUnitTestCase { $result = $this->callAPIFailure($this->_entity, 'delete', $deleteParams); $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array()); $this->assertEquals(1, $checkDeleted['count'], 'In line ' . __LINE__); -} + } + /** + * Test retrieval of label metadata + */ + public function testGetMetadata() { + $result = $this->callAPIAndDocument($this->_entity, 'get', array( + 'options' => array('metadata' => array('fields') + )), __FUNCTION__, __FILE__, 'Demonostrates returning label metadata', 'GetWithLabelMetadata'); + $this->assertEquals('Website', $result['metadata']['fields']['url']['title']); + } + /** + * Test retrieval of label metadata + */ + public function testGetFields() { + $result = $this->callAPIAndDocument($this->_entity, 'getfields', array('action' => 'get'), __FUNCTION__, __FILE__); + $this->assertArrayKeyExists('url', $result['values']); + } } diff --git a/xml/schema/Core/Website.xml b/xml/schema/Core/Website.xml index ab79cfe66b..e9caa12e20 100644 --- a/xml/schema/Core/Website.xml +++ b/xml/schema/Core/Website.xml @@ -9,6 +9,7 @@ id int unsigned + Website ID true Unique Website ID 3.2 @@ -19,6 +20,7 @@ contact_id + Contact int unsigned FK to Contact ID 3.2 -- 2.25.1