* - 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
*
* @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);
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(
$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');
// 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";
}
$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),
);
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) {
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;
- }
}
}
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');
}
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(
'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(
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);
+ }
}
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);
<comment>Type of Contact.</comment>
<export>true</export>
<pseudoconstant>
- <name>contactType</name>
- <table>civicrm_location_type</table>
+ <table>civicrm_contact_type</table>
<keyColumn>name</keyColumn>
<labelColumn>label</labelColumn>
+ <condition>parent_id IS NULL</condition>
</pseudoconstant>
<add>1.1</add>
<change>3.1</change>
<import>true</import>
<headerPattern>/C(ontact )?(subtype|sub-type|sub type)/i</headerPattern>
<comment>May be used to over-ride contact view and edit templates.</comment>
+ <pseudoconstant>
+ <table>civicrm_contact_type</table>
+ <keyColumn>name</keyColumn>
+ <labelColumn>label</labelColumn>
+ <condition>parent_id IS NOT NULL</condition>
+ </pseudoconstant>
<add>1.5</add>
</field>
<index>
<type>varchar</type>
<length>64</length>
<comment>Contact Type in mapping</comment>
+ <pseudoconstant>
+ <table>civicrm_contact_type</table>
+ <keyColumn>name</keyColumn>
+ <labelColumn>label</labelColumn>
+ </pseudoconstant>
<add>1.2</add>
</field>
<field>