Merge pull request #13948 from civicrm/5.12
[civicrm-core.git] / CRM / Report / Form.php
index 900a35d4936c87866107e410e3156cfedad3a0d9..56e056fb3f4c85e8a9541d22368b44312e38cc2c 100644 (file)
@@ -1146,18 +1146,31 @@ class CRM_Report_Form extends CRM_Core_Form {
    * This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
    *
    * @param string $identifier
-   * @param $sql
+   *   This is the key that will be used for the table in the temporaryTables property.
+   * @param string $sql
+   *   Sql select statement or column description (the latter requires the columns flag)
+   * @param bool $isColumns
+   *   Is the sql describing columns to create (rather than using a select query).
+   * @param bool $isMemory
+   *   Create a memory table rather than a normal INNODB table.
    *
    * @return string
    */
-  public function createTemporaryTable($identifier, $sql) {
-    $tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8(TRUE)->createWithQuery($sql);
+  public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
+    $tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8();
+    if ($isMemory) {
+      $tempTable->setMemory();
+    }
+    if ($isColumns) {
+      $tempTable->createWithColumns($sql);
+    }
+    else {
+      $tempTable->createWithQuery($sql);
+    }
     $name = $tempTable->getName();
     // Developers may force tables to be durable to assist in debugging so lets check.
     $isNotTrueTemporary = $tempTable->isDurable();
-    // The TempTable build routine adds the next line - we output it to help developers see what has happened.
-    $sql = 'CREATE ' . ($isNotTrueTemporary ? '' : 'TEMPORARY ') . "TABLE $name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci " . $sql;
-    $this->addToDeveloperTab($sql);
+    $this->addToDeveloperTab($tempTable->getCreateSql());
     $this->temporaryTables[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
     return $name;
   }
@@ -2057,6 +2070,10 @@ class CRM_Report_Form extends CRM_Core_Form {
         break;
     }
 
+    //dev/core/544 Add report support for multiple contact subTypes
+    if ($field['name'] == 'contact_sub_type' && $clause) {
+      $clause = $this->whereSubtypeClause($field, $value, $op);
+    }
     if (!empty($field['group']) && $clause) {
       $clause = $this->whereGroupClause($field, $value, $op);
     }
@@ -2075,6 +2092,27 @@ class CRM_Report_Form extends CRM_Core_Form {
     return $clause;
   }
 
+  /**
+   * Get SQL where clause for contact subtypes
+   * @param string $field
+   * @param mixed $value
+   * @param string $op SQL Operator
+   *
+   * @return string
+   */
+  public function whereSubtypeClause($field, $value, $op) {
+    $clause = '( ';
+    $subtypeFilters = count($value);
+    for ($i = 0; $i < $subtypeFilters; $i++) {
+      $clause .= "{$field['dbAlias']} LIKE '%$value[$i]%'";
+      if ($i !== ($subtypeFilters - 1)) {
+        $clause .= " OR ";
+      }
+    }
+    $clause .= ' )';
+    return $clause;
+  }
+
   /**
    * Get SQL where clause for a date field.
    *
@@ -2101,12 +2139,10 @@ class CRM_Report_Form extends CRM_Core_Form {
     list($from, $to) = $this->getFromTo($relative, $from, $to, $fromTime, $toTime);
 
     if ($from) {
-      $from = ($type == CRM_Utils_Type::T_DATE) ? substr($from, 0, 8) : $from;
       $clauses[] = "( {$fieldName} >= $from )";
     }
 
     if ($to) {
-      $to = ($type == CRM_Utils_Type::T_DATE) ? substr($to, 0, 8) : $to;
       $clauses[] = "( {$fieldName} <= {$to} )";
     }