Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / CustomValue.php
index 8d79caf6dba957551a9b571174b1e614d4e79f23..bf23c7477693b8571b4df5f1cd41502ab175a344 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
 
   /**
-   * Validate a value against a CustomField type
+   * Validate a value against a CustomField type.
    *
    * @param string $type
    *   The type of the data.
    * @param string $value
    *   The data to be validated.
    *
-   * @return boolean
+   * @return bool
    *   True if the value is of the specified type
-   * @static
    */
   public static function typecheck($type, $value) {
     switch ($type) {
@@ -127,7 +126,6 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
    *
    * @return string|null
    *   the mysql data store placeholder
-   * @static
    */
   public static function typeToField($type) {
     switch ($type) {
@@ -167,7 +165,7 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
    * @param array $formValues
    * @return null
    */
-  public static function fixFieldValueOfTypeMemo(&$formValues) {
+  public static function fixCustomFieldValue(&$formValues) {
     if (empty($formValues)) {
       return NULL;
     }
@@ -182,19 +180,29 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
       $htmlType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
         substr($key, 7), 'html_type'
       );
-      if (($htmlType == 'TextArea') &&
+      $dataType = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_CustomField',
+        substr($key, 7), 'data_type'
+      );
+
+      if (is_array($formValues[$key])) {
+        if (!in_array(key($formValues[$key]), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
+          $formValues[$key] = array('IN' => $formValues[$key]);
+        }
+      }
+      elseif (($htmlType == 'TextArea' ||
+          ($htmlType == 'Text' && $dataType == 'String')
+        ) &&
         !((substr($formValues[$key], 0, 1) == '%') ||
           (substr($formValues[$key], -1, 1) == '%')
         )
       ) {
-        $formValues[$key] = '%' . $formValues[$key] . '%';
+        $formValues[$key] = array('LIKE' => '%' . $formValues[$key] . '%');
       }
-
     }
   }
 
   /**
-   * Delete option value give an option value and custom group id
+   * Delete option value give an option value and custom group id.
    *
    * @param int $customValueID
    *   Custom value ID.
@@ -202,7 +210,6 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
    *   Custom group ID.
    *
    * @return void
-   * @static
    */
   public static function deleteCustomValue($customValueID, $customGroupID) {
     // first we need to find custom value table, from custom group ID
@@ -218,4 +225,5 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
       $customValueID
     );
   }
+
 }