Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / CustomValue.php
index b667468997d47a33efbadc39d29a0cad0ca42b1d..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
+   * @param string $type
+   *   The type of the data.
+   * @param string $value
+   *   The data to be validated.
    *
-   * @return boolean True if the value is of the specified type
-   * @static
+   * @return bool
+   *   True if the value is of the specified type
    */
   public static function typecheck($type, $value) {
     switch ($type) {
@@ -83,10 +85,10 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
         $mulValues = explode(',', $value);
         foreach ($mulValues as $key => $state) {
           $valid = array_key_exists(strtolower(trim($state)),
-            array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)
-          ) || array_key_exists(strtolower(trim($state)),
-            array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER)
-          );
+              array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)
+            ) || array_key_exists(strtolower(trim($state)),
+              array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER)
+            );
           if (!$valid) {
             break;
           }
@@ -100,10 +102,10 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
         $mulValues = explode(',', $value);
         foreach ($mulValues as $key => $country) {
           $valid = array_key_exists(strtolower(trim($country)),
-            array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)
-          ) || array_key_exists(strtolower(trim($country)),
-            array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER)
-          );
+              array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)
+            ) || array_key_exists(strtolower(trim($country)),
+              array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER)
+            );
           if (!$valid) {
             break;
           }
@@ -119,10 +121,11 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
   /**
    * Given a 'civicrm' type string, return the mysql data store area
    *
-   * @param string $type the civicrm type string
+   * @param string $type
+   *   The civicrm type string.
    *
-   * @return the mysql data store placeholder
-   * @static
+   * @return string|null
+   *   the mysql data store placeholder
    */
   public static function typeToField($type) {
     switch ($type) {
@@ -159,9 +162,10 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
 
 
   /**
-   * @param $formValues
+   * @param array $formValues
+   * @return null
    */
-  public static function fixFieldValueOfTypeMemo(&$formValues) {
+  public static function fixCustomFieldValue(&$formValues) {
     if (empty($formValues)) {
       return NULL;
     }
@@ -176,25 +180,36 @@ 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] . '%');
       }
-
     }
   }
 
   /**
-   * Delet 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
-   * @param int $customGroupID custom group ID
+   * @param int $customValueID
+   *   Custom value ID.
+   * @param int $customGroupID
+   *   Custom group ID.
    *
    * @return void
-   * @static
    */
   public static function deleteCustomValue($customValueID, $customGroupID) {
     // first we need to find custom value table, from custom group ID
@@ -210,4 +225,5 @@ class CRM_Core_BAO_CustomValue extends CRM_Core_DAO {
       $customValueID
     );
   }
+
 }