Skip to content

Commit

Permalink
Merge pull request civicrm#4 from twomice/search
Browse files Browse the repository at this point in the history
Search
  • Loading branch information
dlobo committed Apr 30, 2013
2 parents bc75d48 + c22c6aa commit 104c432
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 60 deletions.
2 changes: 1 addition & 1 deletion CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ static function resolveDefaults(&$defaults, $reverse = FALSE) {

CRM_Utils_Array::lookupValue($defaults, 'prefix', CRM_Core_PseudoConstant::individualPrefix(), $reverse);
CRM_Utils_Array::lookupValue($defaults, 'suffix', CRM_Core_PseudoConstant::individualSuffix(), $reverse);
CRM_Utils_Array::lookupValue($defaults, 'gender', CRM_Core_PseudoConstant::gender(), $reverse);
CRM_Utils_Array::lookupValue($defaults, 'gender', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), $reverse);

//lookup value of email/postal greeting, addressee, CRM-4575
foreach (self::$_greetingTypes as $greeting) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Edit/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CRM_Contact_Form_Edit_Demographics {
static function buildQuickForm(&$form) {
// radio button for gender
$genderOptions = array();
$gender = CRM_Core_PseudoConstant::gender(TRUE);
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id', array('localize' => TRUE));
foreach ($gender as $key => $var) {
$genderOptions[$key] = $form->createElement('radio', NULL,
ts('Gender'), $var, $key,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ static function demographics(&$form) {
$form->add('hidden', 'hidden_demographics', 1);
// radio button for gender
$genderOptions = array();
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
foreach ($gender as $key => $var) {
$genderOptions[$key] = $form->createElement('radio', NULL,
ts('Gender'), $var, $key,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Page/Inline/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function run() {
CRM_Contact_BAO_Contact::getValues( $params, $defaults );

if (CRM_Utils_Array::value('gender_id', $defaults)) {
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
$defaults['gender_display'] = $gender[CRM_Utils_Array::value('gender_id', $defaults)];
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Page/View/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function view() {
}

if (CRM_Utils_Array::value('gender_id', $defaults)) {
$gender = CRM_Core_PseudoConstant::gender(TRUE);
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id', array('localize' => TRUE));
$defaults['gender_display'] = $gender[CRM_Utils_Array::value('gender_id', $defaults)];
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ static function buildProfile(
}
elseif ($fieldName === 'gender') {
$genderOptions = array();
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
foreach ($gender as $key => $var) {
$genderOptions[$key] = $form->createElement('radio', NULL, ts('Gender'), $var, $key);
}
Expand Down
20 changes: 15 additions & 5 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class CRM_Core_DAO extends DB_DataObject {
// special value for mail bulk inserts to avoid
// potential duplication, assuming a smaller number reduces number of queries
// by some factor, so some tradeoff. CRM-8678
BULK_MAIL_INSERT_COUNT = 10;
BULK_MAIL_INSERT_COUNT = 10,
QUERY_FORMAT_WILDCARD = 1,
QUERY_FORMAT_NO_QUOTES = 2;

/*
* Define entities that shouldn't be created or deleted when creating/ deleting
* test objects - this prevents world regions, countries etc from being added / deleted
Expand Down Expand Up @@ -965,10 +968,17 @@ static function composeQuery($query, &$params, $abort = TRUE) {
$item[1] == 'Memo' ||
$item[1] == 'Link'
) {
if (isset($item[2]) &&
$item[2]
) {
$item[0] = "'%{$item[0]}%'";
// Support class constants stipulating wildcard characters and/or
// non-quoting of strings. Also support legacy code which may be
// passing in TRUE or 1 for $item[2], which used to indicate the
// use of wildcard characters.
if (!empty($item[2])) {
if ($item[2] & CRM_Core_DAO::QUERY_FORMAT_WILDCARD || $item[2] === TRUE) {
$item[0] = "'%{$item[0]}%'";
}
elseif (!($item[2] & CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES)) {
$item[0] = "'{$item[0]}'";
}
}
else {
$item[0] = "'{$item[0]}'";
Expand Down
123 changes: 96 additions & 27 deletions CRM/Core/PseudoConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
*/
class CRM_Core_PseudoConstant {

/**
* static cache for pseudoconstant arrays
* @var array
* @static
*/
private static $cache;

/**
* static cache for pseudoconstant arrays
* @var array
* @static
*/
private static $cache;

/**
* location type
* @var array
Expand Down Expand Up @@ -105,13 +119,6 @@ class CRM_Core_PseudoConstant {
*/
private static $individualSuffix;

/**
* gender
* @var array
* @static
*/
private static $gender;

/**
* im protocols
* @var array
Expand Down Expand Up @@ -399,6 +406,88 @@ class CRM_Core_PseudoConstant {
*/
private static $accountOptionValues;

/**
* Get options for a given field.
* @param String $daoName
* @param String $fieldName
* @param Array $params
*
* @return Array on success, FALSE on error.
*
* @static
*/
public static function get($daoName, $fieldName, $params = array()) {
$dao = new $daoName;
$fields = $dao->fields();
if (empty($fields[$fieldName])) {
return FALSE;
}
$fieldSpec = $fields[$fieldName];

// If the field is an enum, use explode the enum definition and return the array.
if (array_key_exists('enumValues', $fieldSpec)) {
// use of a space after the comma is inconsistent in xml
$enumStr = str_replace(', ', ',', $fieldSpec['enumValues']);
return explode(',', $enumStr);
}
elseif (!empty($fieldSpec['pseudoconstant'])) {
$pseudoconstant = $fieldSpec['pseudoconstant'];
if(!empty($pseudoconstant['optionGroupName'])) {
// Translate $params array into function arguments;
// populate default function params if not supplied in the array.
$ret = CRM_Core_OptionGroup::values(
$pseudoconstant['optionGroupName'],
CRM_Utils_Array::value('flip', $params, FALSE),
CRM_Utils_Array::value('grouping', $params, FALSE),
CRM_Utils_Array::value('localize', $params, FALSE),
CRM_Utils_Array::value('condition', $params, NULL),
CRM_Utils_Array::value('labelColumnName', $params, 'label'),
CRM_Utils_Array::value('onlyActive', $params, TRUE),
CRM_Utils_Array::value('fresh', $params, FALSE)
);
return $ret;
}
if (!empty($pseudoconstant['table'])) {
// Sort params so the serialized string will be consistent.
ksort($params);
$cacheKey = "{$daoName}{$fieldName}" . serialize($params);

if (isset(self::$cache[$cacheKey])) {
return self::$cache[$cacheKey];
}

$query = "
SELECT
%1 AS id, %2 AS label
FROM
%3
";
if (!empty($pseudoconstant['condition'])) {
$query .= " WHERE {$pseudoconstant['condition']}";
}
$query .= " ORDER BY %2";
$queryParams = array(
1 => array($pseudoconstant['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
2 => array($pseudoconstant['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
);

self::$cache[$cacheKey] = array();
$dao = CRM_Core_DAO::executeQuery($query, $queryParams);
while ($dao->fetch()) {
self::$cache[$cacheKey][$dao->id] = $dao->label;
}
if (CRM_Utils_Array::value('localize', $params)) {
$i18n = CRM_Core_I18n::singleton();
$i18n->localizeArray(self::$cache[$cacheKey]);
}
return self::$cache[$cacheKey];
}
}
// If we're still here, it's an error. Return FALSE.
return FALSE;
}

/**
* populate the object from the database. generic populate
* method
Expand Down Expand Up @@ -708,26 +797,6 @@ public static function &individualSuffix() {
return self::$individualSuffix;
}

/**
* Get all Gender.
*
* The static array gender is returned
*
* @access public
* @static
*
* @param boolean $all - get All Gender - default is to get only active ones.
*
* @return array - array reference of all gender.
*
*/
public static function &gender($localize = FALSE) {
if (!self::$gender) {
self::$gender = CRM_Core_OptionGroup::values('gender', FALSE, FALSE, $localize);
}
return self::$gender;
}

/**
* Get all the IM Providers from database.
*
Expand Down
2 changes: 1 addition & 1 deletion CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$profile
}
}
elseif ('gender' == substr($name, 0, 6)) {
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
$values[$index] = $gender[$params[$name]];
}
elseif ('individual_prefix' == substr($name, 0, 17)) {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ public function checkGender($gender) {
return FALSE;
}

$allGenders = CRM_Core_PseudoConstant::gender();
$allGenders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
foreach ($allGenders as $key => $value) {
if (strlen($gender) > strlen($value)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Logging/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function titlesAndValuesForTable($table) {
'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(),
'financial_type_id' => CRM_Contribute_PseudoConstant::financialType(),
'country_id' => CRM_Core_PseudoConstant::country(),
'gender_id' => CRM_Core_PseudoConstant::gender(),
'gender_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
'location_type_id' => CRM_Core_PseudoConstant::locationType(),
'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(),
'phone_type_id' => CRM_Core_PseudoConstant::phoneType(),
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Case/Demographics.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function __construct() {
);
}

$this->_genders = CRM_Core_PseudoConstant::gender();
$this->_genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');

parent::__construct();
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contact/CurrentEmployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function alterDisplay(&$rows) {
//handle gender
if (array_key_exists('civicrm_contact_gender_id', $row)) {
if ($value = $row['civicrm_contact_gender_id']) {
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
$rows[$rowNum]['civicrm_contact_gender_id'] = $gender[$value];
}
$entryFound = TRUE;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Grant/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function __construct() {
'gender_id' =>
array('title' => ts('Gender'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Core_PseudoConstant::gender(),
'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
),
'id' =>
array('title' => ts('Contact ID'),
Expand Down
4 changes: 2 additions & 2 deletions CRM/Report/Form/Grant/Statistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function __construct() {
'name' => 'gender_id',
'title' => ts('Gender'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Core_PseudoConstant::gender(),
'options' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
),
'contact_type' =>
array(
Expand Down Expand Up @@ -384,7 +384,7 @@ function alterDisplay(&$rows) {

$grantTypes = CRM_Grant_PseudoConstant::grantType();
$countries = CRM_Core_PseudoConstant::country();
$gender = CRM_Core_PseudoConstant::gender();
$gender = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');

$grantAmountTotal = "
SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count ,
Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/DeprecatedUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
if (isset($values['gender'])) {
if (CRM_Utils_Array::value('gender_id', $params)) {
$genders = array();
$genders = CRM_Core_PseudoConstant::gender();
$genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
$params['gender'] = $genders[$params['gender_id']];
}
else {
Expand Down
2 changes: 1 addition & 1 deletion api/v3/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function civicrm_api3_contact_create($params) {
}

if (isset($params['gender_id']) && !(is_numeric($params['gender_id']))) {
$params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender());
$params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'));
}

$error = _civicrm_api3_greeting_format_params($params);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Contact/BAO/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function testResolveDefaults() {
CRM_Contact_BAO_Contact::resolveDefaults($params);

//check the resolve values.
$genders = CRM_Core_PseudoConstant::gender();
$genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
$this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
$prefix = CRM_Core_PseudoConstant::individualPrefix();
$this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
Expand Down
Loading

0 comments on commit 104c432

Please sign in to comment.