From 091fe2a8036531e96097d16caffc50c4b2f91b3e Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 1 May 2013 18:54:32 -0700 Subject: [PATCH] Support contact_type and sub_type CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Core/PseudoConstant.php | 71 +++++++------------ CRM/Financial/Form/FinancialBatch.php | 2 +- tests/phpunit/CRM/Core/PseudoConstantTest.php | 54 ++++++++++++-- xml/GenCode.php | 10 ++- xml/schema/Contact/Contact.xml | 10 ++- xml/schema/Core/MappingField.xml | 5 ++ 6 files changed, 97 insertions(+), 55 deletions(-) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 01619183b6..94b25c6ff6 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -249,7 +249,8 @@ class CRM_Core_PseudoConstant { * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value) * - localize boolean if true, localize the results before returning * - condition string add another condition to the sql query - * - labelColumnName string the column to use for 'label' + * - keyColumn string the column to use for 'id' + * - labelColumn string the column to use for 'label' * - onlyActive boolean return only the action option values * - fresh boolean ignore cache entries and go back to DB * @@ -258,27 +259,17 @@ class CRM_Core_PseudoConstant { * @static */ public static function get($daoName, $fieldName, $params = array()) { - // Merge defaults - $params += array( - 'flip' => FALSE, - 'grouping' => FALSE, - 'localize' => FALSE, - 'condition' => NULL, - 'labelColumnName' => NULL, - 'onlyActive' => TRUE, - 'fresh' => FALSE, - ); - $flip = $params['flip']; - $dao = new $daoName; $fields = $dao->fields(); + $dao->free(); if (empty($fields[$fieldName])) { return FALSE; } $fieldSpec = $fields[$fieldName]; + $flip = !empty($params['flip']); // If the field is an enum, explode the enum definition and return the array. - if (array_key_exists('enumValues', $fieldSpec)) { + if (isset($fieldSpec['enumValues'])) { // use of a space after the comma is inconsistent in xml $enumStr = str_replace(', ', ',', $fieldSpec['enumValues']); $values = explode(',', $enumStr); @@ -287,6 +278,18 @@ class CRM_Core_PseudoConstant { elseif (!empty($fieldSpec['pseudoconstant'])) { $pseudoconstant = $fieldSpec['pseudoconstant']; + // Merge params with defaults + $params += array( + 'grouping' => FALSE, + 'localize' => FALSE, + 'condition' => CRM_Utils_Array::value('condition', $pseudoconstant), + 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant), + 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant), + 'onlyActive' => TRUE, + 'fresh' => FALSE, + ); + + // Fetch option group from option_value table if(!empty($pseudoconstant['optionGroupName'])) { // Call our generic fn for retrieving from the option_value table return CRM_Core_OptionGroup::values( @@ -294,12 +297,14 @@ class CRM_Core_PseudoConstant { $flip, $params['grouping'], $params['localize'], - $params['condition'], - $params['labelColumnName'] ? $params['labelColumnName'] : 'label', + $params['condition'] ? $params['condition'] : $pseudoconstant['condition'], + $params['labelColumn'] ? $params['labelColumn'] : 'label', $params['onlyActive'], $params['fresh'] ); } + + // Fetch options from other tables if (!empty($pseudoconstant['table'])) { // Normalize params so the serialized cache string will be consistent. CRM_Utils_Array::remove($params, 'flip', 'fresh'); @@ -308,7 +313,7 @@ class CRM_Core_PseudoConstant { // Return cached options if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) { - return self::$cache[$cacheKey]; + return $flip ? array_flip(self::$cache[$cacheKey]) : self::$cache[$cacheKey]; } $select = "SELECT %1 AS id, %2 AS label"; @@ -328,11 +333,9 @@ class CRM_Core_PseudoConstant { } $dao->free(); } - // Support labelColumnName param - $labelColumnName = $params['labelColumnName'] ? $params['labelColumnName'] : $pseudoconstant['labelColumn']; $queryParams = array( - 1 => array($pseudoconstant['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), - 2 => array($labelColumnName, 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), + 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), ); @@ -1635,13 +1638,9 @@ WHERE id = %1 public static function &greetingDefaults() { if (!self::$greetingDefaults) { $defaultGreetings = array(); - $contactTypes = array( - 'Individual' => 1, - 'Household' => 2, - 'Organization' => 3, - ); + $contactTypes = self::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name')); - foreach ($contactTypes as $contactType => $filter) { + foreach ($contactTypes as $filter => $contactType) { $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 "; foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { @@ -1743,23 +1742,5 @@ WHERE id = %1 return self::$accountOptionValues[$cacheKey]; } - - /* - * The static array contactType is returned - * - * @access public - * @static - * @param string $column db column name/label. - * - * @return array - array reference of all Types - * - */ - - public static function &contactType($column = 'label') { - if (!self::$contactType) { - self::$contactType = CRM_Contact_BAO_ContactType::basicTypePairs(TRUE); - } - return self::$contactType; - } } diff --git a/CRM/Financial/Form/FinancialBatch.php b/CRM/Financial/Form/FinancialBatch.php index 5eca23bdc2..a06021dcc9 100644 --- a/CRM/Financial/Form/FinancialBatch.php +++ b/CRM/Financial/Form/FinancialBatch.php @@ -224,7 +224,7 @@ class CRM_Financial_Form_FinancialBatch extends CRM_Contribute_Form { } if ($this->_action & CRM_Core_Action::ADD) { - $batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumnName' => 'name')); + $batchMode = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'mode_id', array('labelColumn' => 'name')); $params['mode_id'] = CRM_Utils_Array::key('Manual Batch', $batchMode); $params['status_id'] = CRM_Utils_Array::key('Open', $batchStatus); $params['created_date'] = date('YmdHis'); diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index c3194df480..107164f1d7 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -44,11 +44,14 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { } function testOptionValues() { - // We'll test these daoName/field combinations. - // array[DAO Name] = properties, where properties can be: - // - fieldName: the SQL column name within the DAO table. - // - sample: Any one value which is expected in the list of option values. - // - max: integer (default = 10) maximum number of option values expected. + /* + * daoName/field combinations to test + * array[DAO Name] = properties, where properties can be: + * - fieldName: the SQL column name within the DAO table. + * - sample: Any one value which is expected in the list of option values. + * - exclude: Any one value which should not be in the list. + * - max: integer (default = 10) maximum number of option values expected. + */ $fields = array( 'CRM_Core_DAO_OptionValue' => array( array( @@ -148,6 +151,16 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { 'fieldName' => 'preferred_communication_method', 'sample' => 'Postal Mail', ), + array( + 'fieldName' => 'contact_type', + 'sample' => 'Individual', + 'exclude' => 'Team', + ), + array( + 'fieldName' => 'contact_sub_type', + 'sample' => 'Team', + 'exclude' => 'Individual', + ), ), 'CRM_Batch_DAO_Batch' => array( array( @@ -175,14 +188,43 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { foreach ($daoFields as $field) { $message = "DAO name: '{$daoName}', field: '{$field['fieldName']}'"; - // Ensure sample value is contained in the returned optionValues. $optionValues = CRM_Core_PseudoConstant::get($daoName, $field['fieldName']); + $this->assertNotEmpty($optionValues, $message); + + // Ensure sample value is contained in the returned optionValues. $this->assertContains($field['sample'], $optionValues, $message); + // Exclude test + if (!empty($field['exclude'])) { + $this->assertNotContains($field['exclude'], $optionValues, $message); + } + // Ensure count of optionValues is not extraordinarily high. $max = CRM_Utils_Array::value('max', $field, 10); $this->assertLessThanOrEqual($max, count($optionValues), $message); } } } + + function testContactTypes() { + $byName = array( + 'Individual' => 'Individual', + 'Household' => 'Household', + 'Organization' => 'Organization', + ); + $byId = array( + 1 => 'Individual', + 2 => 'Household', + 3 => 'Organization', + ); + // By default this should return by name + $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type'); + $this->assertEquals($byName, $result); + // But we can also fetch by ID + $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name')); + $this->assertEquals($byId, $result); + // Make sure flip param works + $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name', 'flip' => TRUE)); + $this->assertEquals(array_flip($byId), $result); + } } diff --git a/xml/GenCode.php b/xml/GenCode.php index ea7a62081b..600810b339 100644 --- a/xml/GenCode.php +++ b/xml/GenCode.php @@ -717,7 +717,15 @@ Alternatively you can get a version of CiviCRM that matches your PHP version if(!empty($fieldXML->pseudoconstant)){ //ok this is a bit long-winded but it gets there & is consistent with above approach $field['pseudoconstant'] = array(); - $validOptions = array('name', 'optionGroupName', 'table', 'keyColumn', 'labelColumn','class'); + $validOptions = array( + 'name', + 'optionGroupName', + 'table', + 'keyColumn', + 'labelColumn', + 'class', + 'condition', + ); foreach ($validOptions as $pseudoOption){ if(!empty($fieldXML->pseudoconstant->$pseudoOption)){ $field['pseudoconstant'][$pseudoOption] = $this->value($pseudoOption, $fieldXML->pseudoconstant); diff --git a/xml/schema/Contact/Contact.xml b/xml/schema/Contact/Contact.xml index e6dab38007..4ab58b1cbd 100644 --- a/xml/schema/Contact/Contact.xml +++ b/xml/schema/Contact/Contact.xml @@ -29,10 +29,10 @@ Type of Contact. true - contactType - civicrm_location_type
+ civicrm_contact_type
name label + parent_id IS NULL
1.1 3.1 @@ -51,6 +51,12 @@ true /C(ontact )?(subtype|sub-type|sub type)/i May be used to over-ride contact view and edit templates. + + civicrm_contact_type
+ name + label + parent_id IS NOT NULL +
1.5 diff --git a/xml/schema/Core/MappingField.xml b/xml/schema/Core/MappingField.xml index 1154e066b8..fbae7d3d50 100644 --- a/xml/schema/Core/MappingField.xml +++ b/xml/schema/Core/MappingField.xml @@ -43,6 +43,11 @@ varchar 64 Contact Type in mapping + + civicrm_contact_type
+ name + label +
1.2 -- 2.25.1