Added generic method CRM_Core_PseudoConstant::get(). CRM-12464
authorAS <as@dell12.(none)>
Tue, 30 Apr 2013 03:59:05 +0000 (20:59 -0700)
committerColeman Watts <coleman@civicrm.org>
Wed, 29 May 2013 21:32:22 +0000 (14:32 -0700)
----------------------------------------
* CRM-12464: Search improvements in 4.4
  http://issues.civicrm.org/jira/browse/CRM-12464

CRM/Core/DAO.php
CRM/Core/PseudoConstant.php
tests/phpunit/CRM/Core/PseudoConstantTest.php [new file with mode: 0644]

index ba954aba61e739c522e40ef2571f5d2595441ea9..ea8b5db65f61947f0e1dad403bc3470f9f3240fd 100644 (file)
@@ -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
@@ -965,10 +968,17 @@ FROM   civicrm_domain
             $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]}'";
index d23081b2889b5444165acb0be33703db52f59e53..4c551a6c702a504d66298ef88dfc904e9e641dad 100644 (file)
  */
 class CRM_Core_PseudoConstant {
 
+  /**
+   * static cache for pseudoconstant arrays
+   * @var array
+   * @static
+   */
+  private static $cache;
+
   /**
    * location type
    * @var array
@@ -392,6 +399,81 @@ 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();
+    $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
diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php
new file mode 100644 (file)
index 0000000..3e6d0ce
--- /dev/null
@@ -0,0 +1,56 @@
+<?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);
+  }
+}