// 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
$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]}'";
*/
class CRM_Core_PseudoConstant {
+ /**
+ * static cache for pseudoconstant arrays
+ * @var array
+ * @static
+ */
+ private static $cache;
+
/**
* location type
* @var array
*/
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();
+ $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;
+ }
+ 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
--- /dev/null
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.3 |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2013 |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM. |
+ | |
+ | CiviCRM is free software; you can copy, modify, and distribute it |
+ | under the terms of the GNU Affero General Public License |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+ | |
+ | CiviCRM is distributed in the hope that it will be useful, but |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+ | See the GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public |
+ | License and the CiviCRM Licensing Exception along |
+ | with this program; if not, contact CiviCRM LLC |
+ | at info[AT]civicrm[DOT]org. If you have questions about the |
+ | GNU Affero General Public License or the licensing of CiviCRM, |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+*/
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ * Tests for linking to resource files
+ */
+class CRM_Core_PseudoConstantTest extends CiviUnitTestCase {
+ function get_info() {
+ return array(
+ 'name' => 'PseudoConstant',
+ 'description' => 'Tests for pseudoconstant option values',
+ 'group' => 'Core',
+ );
+ }
+
+ function setUp() {
+ parent::setUp();
+ }
+
+ function testGender() {
+ $expected = array(
+ 1 => 'Female',
+ 2 => 'Male',
+ 3 => 'Transgender',
+ );
+
+ $actual = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
+ $this->assertEquals($expected, $actual);
+ }
+}