Merge pull request #16733 from eileenmcnaughton/smarty
[civicrm-core.git] / CRM / Logging / ReportSummary.php
index 396c61f34f52f8536be0c2ae77a2beddc81a7633..f7e93eeb24b332de5a41ab31dfe3f897030f0f3e 100644 (file)
@@ -1,40 +1,24 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | 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.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | 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        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  * $Id$
  */
 class CRM_Logging_ReportSummary extends CRM_Report_Form {
   protected $cid;
 
-  protected $_logTables = array();
+  protected $_logTables = [];
 
   protected $loggingDB;
 
@@ -67,123 +51,123 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
     // used for redirect back to contact summary
     $this->cid = CRM_Utils_Request::retrieve('cid', 'Integer');
 
-    $this->_logTables = array(
-      'log_civicrm_contact' => array(
+    $this->_logTables = [
+      'log_civicrm_contact' => [
         'fk' => 'id',
-      ),
-      'log_civicrm_email' => array(
+      ],
+      'log_civicrm_email' => [
         'fk' => 'contact_id',
         'log_type' => 'Contact',
-      ),
-      'log_civicrm_phone' => array(
+      ],
+      'log_civicrm_phone' => [
         'fk' => 'contact_id',
         'log_type' => 'Contact',
-      ),
-      'log_civicrm_address' => array(
+      ],
+      'log_civicrm_address' => [
         'fk' => 'contact_id',
         'log_type' => 'Contact',
-      ),
-      'log_civicrm_note' => array(
+      ],
+      'log_civicrm_note' => [
         'fk' => 'entity_id',
         'entity_table' => TRUE,
-        'bracket_info' => array(
+        'bracket_info' => [
           'table' => 'log_civicrm_note',
           'column' => 'subject',
-        ),
-      ),
-      'log_civicrm_note_comment' => array(
+        ],
+      ],
+      'log_civicrm_note_comment' => [
         'fk' => 'entity_id',
         'table_name' => 'log_civicrm_note',
-        'joins' => array(
+        'joins' => [
           'table' => 'log_civicrm_note',
           'join' => "entity_log_civireport.entity_id = fk_table.id AND entity_log_civireport.entity_table = 'civicrm_note'",
-        ),
+        ],
         'entity_table' => TRUE,
-        'bracket_info' => array(
+        'bracket_info' => [
           'table' => 'log_civicrm_note',
           'column' => 'subject',
-        ),
-      ),
-      'log_civicrm_group_contact' => array(
+        ],
+      ],
+      'log_civicrm_group_contact' => [
         'fk' => 'contact_id',
-        'bracket_info' => array(
+        'bracket_info' => [
           'entity_column' => 'group_id',
           'table' => 'log_civicrm_group',
           'column' => 'title',
-        ),
+        ],
         'action_column' => 'status',
         'log_type' => 'Group',
-      ),
-      'log_civicrm_entity_tag' => array(
+      ],
+      'log_civicrm_entity_tag' => [
         'fk' => 'entity_id',
-        'bracket_info' => array(
+        'bracket_info' => [
           'entity_column' => 'tag_id',
           'table' => 'log_civicrm_tag',
           'column' => 'name',
-        ),
+        ],
         'entity_table' => TRUE,
-      ),
-      'log_civicrm_relationship' => array(
+      ],
+      'log_civicrm_relationship' => [
         'fk' => 'contact_id_a',
-        'bracket_info' => array(
+        'bracket_info' => [
           'entity_column' => 'relationship_type_id',
           'table' => 'log_civicrm_relationship_type',
           'column' => 'label_a_b',
-        ),
-      ),
-      'log_civicrm_activity_contact' => array(
+        ],
+      ],
+      'log_civicrm_activity_contact' => [
         'fk' => 'contact_id',
         'table_name' => 'log_civicrm_activity_contact',
-        'log_type' => 'Activity',
+        'log_type' => 'Activity Contact',
         'field' => 'activity_id',
-        'extra_joins' => array(
+        'extra_joins' => [
           'table' => 'log_civicrm_activity',
           'join' => 'extra_table.id = entity_log_civireport.activity_id',
-        ),
+        ],
 
-        'bracket_info' => array(
+        'bracket_info' => [
           'entity_column' => 'activity_type_id',
           'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
           'lookup_table' => 'log_civicrm_activity',
-        ),
-      ),
-      'log_civicrm_case' => array(
+        ],
+      ],
+      'log_civicrm_case' => [
         'fk' => 'contact_id',
-        'joins' => array(
+        'joins' => [
           'table' => 'log_civicrm_case_contact',
           'join' => 'entity_log_civireport.id = fk_table.case_id',
-        ),
-        'bracket_info' => array(
+        ],
+        'bracket_info' => [
           'entity_column' => 'case_type_id',
           'options' => CRM_Case_BAO_Case::buildOptions('case_type_id', 'search'),
-        ),
-      ),
-    );
+        ],
+      ],
+    ];
 
     $logging = new CRM_Logging_Schema();
 
     // build _logTables for contact custom tables
     $customTables = $logging->entityCustomDataLogTables('Contact');
     foreach ($customTables as $table) {
-      $this->_logTables[$table] = array(
+      $this->_logTables[$table] = [
         'fk' => 'entity_id',
         'log_type' => 'Contact',
-      );
+      ];
     }
 
     // build _logTables for address custom tables
     $customTables = $logging->entityCustomDataLogTables('Address');
     foreach ($customTables as $table) {
-      $this->_logTables[$table] = array(
+      $this->_logTables[$table] = [
         // For join of fk_table with contact table.
         'fk' => 'contact_id',
-        'joins' => array(
+        'joins' => [
           // fk_table
           'table' => 'log_civicrm_address',
           'join' => 'entity_log_civireport.entity_id = fk_table.id',
-        ),
+        ],
         'log_type' => 'Contact',
-      );
+      ];
     }
 
     // Allow log tables to be extended via report hooks.
@@ -226,7 +210,7 @@ class CRM_Logging_ReportSummary extends CRM_Report_Form {
 
   public function where() {
     // reset where clause as its called multiple times, every time insert sql is built.
-    $this->_whereClauses = array();
+    $this->_whereClauses = [];
 
     parent::where();
     $this->_where .= " AND (entity_log_civireport.log_action != 'Initialization')";
@@ -268,13 +252,13 @@ SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']}
   FROM `{$this->loggingDB}`.{$logTable}
  WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
 
-        $entityID = CRM_Core_DAO::singleValueQuery($sql, array(
-          1 => array(
+        $entityID = CRM_Core_DAO::singleValueQuery($sql, [
+          1 => [
             CRM_Utils_Date::isoToMysql($logDate),
             'Timestamp',
-          ),
-          2 => array($id, 'Integer'),
-        ));
+          ],
+          2 => [$id, 'Integer'],
+        ]);
       }
       else {
         $entityID = $id;
@@ -287,16 +271,16 @@ SELECT {$this->_logTables[$entity]['bracket_info']['entity_column']}
 SELECT {$this->_logTables[$entity]['bracket_info']['column']}
 FROM  `{$this->loggingDB}`.{$this->_logTables[$entity]['bracket_info']['table']}
 WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
-        return CRM_Core_DAO::singleValueQuery($sql, array(
-          1 => array(CRM_Utils_Date::isoToMysql($logDate), 'Timestamp'),
-          2 => array($entityID, 'Integer'),
-        ));
+        return CRM_Core_DAO::singleValueQuery($sql, [
+          1 => [CRM_Utils_Date::isoToMysql($logDate), 'Timestamp'],
+          2 => [$entityID, 'Integer'],
+        ]);
       }
       else {
         if (array_key_exists('options', $this->_logTables[$entity]['bracket_info']) &&
           $entityID
         ) {
-          return CRM_Utils_Array::value($entityID, $this->_logTables[$entity]['bracket_info']['options']);
+          return $this->_logTables[$entity]['bracket_info']['options'][$entityID] ?? NULL;
         }
       }
     }
@@ -316,10 +300,10 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
   public function getEntityAction($id, $connId, $entity, $oldAction) {
     if (!empty($this->_logTables[$entity]['action_column'])) {
       $sql = "select {$this->_logTables[$entity]['action_column']} from `{$this->loggingDB}`.{$entity} where id = %1 AND log_conn_id = %2";
-      $newAction = CRM_Core_DAO::singleValueQuery($sql, array(
-        1 => array($id, 'Integer'),
-        2 => array($connId, 'String'),
-      ));
+      $newAction = CRM_Core_DAO::singleValueQuery($sql, [
+        1 => [$id, 'Integer'],
+        2 => [$connId, 'String'],
+      ]);
 
       switch ($entity) {
         case 'log_civicrm_group_contact':
@@ -354,11 +338,11 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
     }
 
     // temp table to hold all altered contact-ids
-    $sql = "CREATE TEMPORARY TABLE civicrm_temp_civireport_logsummary ( {$tempColumns} ) ENGINE=HEAP";
-    CRM_Core_DAO::executeQuery($sql);
-    $this->addToDeveloperTab($sql);
+    $this->temporaryTable = CRM_Utils_SQL_TempTable::build()->setCategory('logsummary')->setMemory()->createwithColumns($tempColumns);
+    $this->addToDeveloperTab($this->temporaryTable->getCreateSql());
+    $this->temporaryTableName = $this->temporaryTable->getName();
 
-    $logTypes = CRM_Utils_Array::value('log_type_value', $this->_params);
+    $logTypes = $this->_params['log_type_value'] ?? NULL;
     unset($this->_params['log_type_value']);
     if (empty($logTypes)) {
       foreach (array_keys($this->_logTables) as $table) {
@@ -383,7 +367,7 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
         $this->currentLogTable = $entity;
         $sql = $this->buildQuery(FALSE);
         $sql = str_replace("entity_log_civireport.log_type as", "'{$entity}' as", $sql);
-        $sql = "INSERT IGNORE INTO civicrm_temp_civireport_logsummary {$sql}";
+        $sql = "INSERT IGNORE INTO {$this->temporaryTableName} {$sql}";
         CRM_Core_DAO::disableFullGroupByMode();
         CRM_Core_DAO::executeQuery($sql);
         CRM_Core_DAO::reenableFullGroupByMode();
@@ -395,14 +379,14 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
 
     // add computed log_type column so that we can do a group by after that, which will help
     // alterDisplay() counts sync with pager counts
-    $sql = "SELECT DISTINCT log_type FROM civicrm_temp_civireport_logsummary";
+    $sql = "SELECT DISTINCT log_type FROM {$this->temporaryTableName}";
     $dao = CRM_Core_DAO::executeQuery($sql);
     $this->addToDeveloperTab($sql);
-    $replaceWith = array();
+    $replaceWith = [];
     while ($dao->fetch()) {
       $type = $this->getLogType($dao->log_type);
       if (!array_key_exists($type, $replaceWith)) {
-        $replaceWith[$type] = array();
+        $replaceWith[$type] = [];
       }
       $replaceWith[$type][] = $dao->log_type;
     }
@@ -412,11 +396,11 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
       }
     }
 
-    $sql = "ALTER TABLE civicrm_temp_civireport_logsummary ADD COLUMN log_civicrm_entity_log_type_label varchar(64)";
+    $sql = "ALTER TABLE {$this->temporaryTableName} ADD COLUMN log_civicrm_entity_log_type_label varchar(64)";
     CRM_Core_DAO::executeQuery($sql);
     $this->addToDeveloperTab($sql);
     foreach ($replaceWith as $type => $in) {
-      $sql = "UPDATE civicrm_temp_civireport_logsummary SET log_civicrm_entity_log_type_label='{$type}', log_date=log_date WHERE log_type IN('$in')";
+      $sql = "UPDATE {$this->temporaryTableName} SET log_civicrm_entity_log_type_label='{$type}', log_date=log_date WHERE log_type IN('$in')";
       CRM_Core_DAO::executeQuery($sql);
       $this->addToDeveloperTab($sql);
     }
@@ -448,17 +432,17 @@ WHERE  log_date <= %1 AND id = %2 ORDER BY log_date DESC LIMIT 1";
     $this->limit();
     $this->orderBy();
     $sql = "{$this->_select}
-FROM civicrm_temp_civireport_logsummary entity_log_civireport
+FROM {$this->temporaryTableName} entity_log_civireport
 WHERE {$this->logTypeTableClause}
 GROUP BY log_civicrm_entity_log_date, log_civicrm_entity_log_type_label, log_civicrm_entity_log_conn_id, log_civicrm_entity_log_user_id, log_civicrm_entity_altered_contact_id, log_civicrm_entity_log_grouping
 {$this->_orderBy}
 {$this->_limit} ";
     $sql = str_replace('modified_contact_civireport.display_name', 'entity_log_civireport.altered_contact', $sql);
     $sql = str_replace('modified_contact_civireport.id', 'entity_log_civireport.altered_contact_id', $sql);
-    $sql = str_replace(array(
+    $sql = str_replace([
       'modified_contact_civireport.',
       'altered_by_contact_civireport.',
-    ), 'entity_log_civireport.', $sql);
+    ], 'entity_log_civireport.', $sql);
     return $sql;
   }
 
@@ -471,7 +455,7 @@ GROUP BY log_civicrm_entity_log_date, log_civicrm_entity_log_type_label, log_civ
   public function buildRows($sql, &$rows) {
     parent::buildRows($sql, $rows);
     // Clean up the temp table - mostly for the unit test.
-    CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civicrm_temp_civireport_logsummary');
+    $this->temporaryTable->drop();
   }
 
 }