Merge branch 4.6 into master
[civicrm-core.git] / CRM / Core / BAO / CustomQuery.php
index e212a0a3acc080fc2529ee0126c5ad22e3a2c034..512b7595c4d85899d70025da6a330e9981648dce 100644 (file)
@@ -1,10 +1,9 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
  | 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$
  *
  */
@@ -38,21 +37,21 @@ class CRM_Core_BAO_CustomQuery {
   const PREFIX = 'custom_value_';
 
   /**
-   * The set of custom field ids
+   * The set of custom field ids.
    *
    * @var array
    */
   protected $_ids;
 
   /**
-   * The select clause
+   * The select clause.
    *
    * @var array
    */
   public $_select;
 
   /**
-   * The name of the elements that are in the select clause
+   * The name of the elements that are in the select clause.
    * used to extract the values
    *
    * @var array
@@ -60,7 +59,7 @@ class CRM_Core_BAO_CustomQuery {
   public $_element;
 
   /**
-   * The tables involved in the query
+   * The tables involved in the query.
    *
    * @var array
    */
@@ -68,28 +67,28 @@ class CRM_Core_BAO_CustomQuery {
   public $_whereTables;
 
   /**
-   * The where clause
+   * The where clause.
    *
    * @var array
    */
   public $_where;
 
   /**
-   * The english language version of the query
+   * The english language version of the query.
    *
    * @var array
    */
   public $_qill;
 
   /**
-   * The cache to translate the option values into labels
+   * The cache to translate the option values into labels.
    *
    * @var array
    */
   public $_options;
 
   /**
-   * The custom fields information
+   * The custom fields information.
    *
    * @var array
    */
@@ -105,10 +104,9 @@ class CRM_Core_BAO_CustomQuery {
   protected $_locationSpecificCustomFields;
 
   /**
-   * This stores custom data group types and tables that it extends
+   * This stores custom data group types and tables that it extends.
    *
    * @var array
-   * @static
    */
   static $extendsMap = array(
     'Contact' => 'civicrm_contact',
@@ -131,7 +129,7 @@ class CRM_Core_BAO_CustomQuery {
   );
 
   /**
-   * Class constructor
+   * Class constructor.
    *
    * Takes in a set of custom field ids andsets up the data structures to
    * generate a query
@@ -141,19 +139,18 @@ class CRM_Core_BAO_CustomQuery {
    *
    * @param bool $contactSearch
    * @param array $locationSpecificFields
-   *
    */
   public function __construct($ids, $contactSearch = FALSE, $locationSpecificFields = array()) {
     $this->_ids = &$ids;
     $this->_locationSpecificCustomFields = $locationSpecificFields;
 
-    $this->_select      = array();
-    $this->_element     = array();
-    $this->_tables      = array();
+    $this->_select = array();
+    $this->_element = array();
+    $this->_tables = array();
     $this->_whereTables = array();
-    $this->_where       = array();
-    $this->_qill        = array();
-    $this->_options     = array();
+    $this->_where = array();
+    $this->_qill = array();
+    $this->_options = array();
 
     $this->_fields = array();
     $this->_contactSearch = $contactSearch;
@@ -165,7 +162,7 @@ class CRM_Core_BAO_CustomQuery {
     // initialize the field array
     $tmpArray = array_keys($this->_ids);
     $idString = implode(',', $tmpArray);
-    $query    = "
+    $query = "
 SELECT f.id, f.label, f.data_type,
        f.html_type, f.is_search_range,
        f.option_group_id, f.custom_group_id,
@@ -256,11 +253,9 @@ SELECT label, value
   }
 
   /**
-   * Generate the select clause and the associated tables
+   * Generate the select clause and the associated tables.
    * for the from clause
    *
-   * @param NULL
-   *
    * @return void
    */
   public function select() {
@@ -323,13 +318,10 @@ SELECT label, value
   }
 
   /**
-   * Generate the where clause and also the english language
+   * Generate the where clause and also the english language.
    * equivalent
    *
-   * @param NULL
-   *
    * @return void
-   *
    */
   public function where() {
     foreach ($this->_ids as $id => $values) {
@@ -359,7 +351,10 @@ SELECT label, value
         if (is_array($value) && !$field['is_search_range']) {
           $isSerialized = CRM_Core_BAO_CustomField::isSerialized($field);
           $wildcard = $isSerialized ? $wildcard : TRUE;
-          $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array('field' => $name, 'context' => 'search'), array()));
+          $options = CRM_Utils_Array::value('values', civicrm_api3('contact', 'getoptions', array(
+                'field' => $name,
+                'context' => 'search',
+              ), array()));
           $qillValue = '';
           $sqlOP = $wildcard ? ' OR ' : ' AND ';
           $sqlValue = array();
@@ -408,7 +403,7 @@ SELECT label, value
               if ($wildcard) {
                 $val = $strtolower(CRM_Core_DAO::escapeString($val));
                 $val = "%$val%";
-                $op  = 'LIKE';
+                $op = 'LIKE';
               }
 
               //FIX for custom data query fired against no value(NULL/NOT NULL)
@@ -487,7 +482,7 @@ SELECT label, value
             $toValue = CRM_Utils_Array::value('to', $value);
 
             if (!$fromValue && !$toValue) {
-              if (!CRM_Utils_Date::processDate($value) && $op != 'IS NULL' && $op != 'IS NOT NULL') {
+              if (!CRM_Utils_Date::processDate($value) && !in_array($op, array('IS NULL', 'IS NOT NULL', 'IS EMPTY', 'IS NOT EMPTY'))) {
                 continue;
               }
 
@@ -553,11 +548,9 @@ SELECT label, value
   }
 
   /**
-   * Function that does the actual query generation
+   * Function that does the actual query generation.
    * basically ties all the above functions together
    *
-   * @param NULL
-   *
    * @return array
    *   array of strings
    */
@@ -579,7 +572,8 @@ SELECT label, value
       }
     }
 
-    return array(implode(' , ', $this->_select),
+    return array(
+      implode(' , ', $this->_select),
       implode(' ', $this->_tables),
       $whereStr,
     );
@@ -623,4 +617,5 @@ SELECT label, value
       $this->_qill[$grouping][] = $label . ' - ' . implode(' ' . ts('and') . ' ', $qill);
     }
   }
+
 }