CaseWithActivityPivot.php 4.09 KB
Newer Older
bgm's avatar
bgm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

/**
 * Class CRM_Extendedreport_Form_Report_Case_CaseWithActivityPivot
 */
class CRM_Extendedreport_Form_Report_Case_CaseWithActivityPivot extends CRM_Extendedreport_Form_Report_ExtendedReport {
  protected $_baseTable = 'civicrm_case';
  protected $skipACL = FALSE;
  protected $_customGroupAggregates = TRUE;
  protected $_aggregatesIncludeNULL = TRUE;
  protected $_aggregatesAddTotal = TRUE;
  protected $_rollup = 'WITH ROLLUP';
  protected $_temporary = ' TEMPORARY ';
  protected $_aggregatesAddPercentage = TRUE;
  public $_drilldownReport = array();
  protected $isPivot = TRUE;
17
  protected $_noFields = TRUE;
bgm's avatar
bgm committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  /**
   * PreConstrain means the query gets run twice - the first time for generating temp tables
   * which go in the from the second time around
   * @var unknown
   */
  protected $_preConstrain = TRUE;
  /**
   * Name of table that links activities to cases. The 'real' table is replaced with the name of a filtered
   * temp table during processing
   *
   * @var unknown
   */
  protected $_caseActivityTable = 'civicrm_case_activity';
  protected $_potentialCriteria = array();

  /**
   *
   */
  public function __construct() {
    $this->_customGroupExtended['civicrm_case'] = array(
      'extends' => array('Case'),
      'filters' => TRUE,
      'title' => ts('Case'),
    );
    $this->_customGroupExtended['civicrm_activity'] = array(
      'extends' => array('Activity'),
      'filters' => TRUE,
      'title' => ts('Activity'),
    );

    $this->_columns = $this->getColumns('Case', array(
          'fields' => FALSE,
        )
      )
      + $this->getColumns('Contact', array())
      + $this->_columns = $this->getColumns('Activity', array(
          'fields' => FALSE,
        )
      );
    $this->_columns['civicrm_case']['fields']['id']['required'] = TRUE;
    $this->_columns['civicrm_contact']['fields']['id']['required'] = TRUE;
    //  $this->_columns['civicrm_case']['fields']['id']['alter_display'] = 'alterCaseID';
    $this->_columns['civicrm_case']['fields']['id']['title'] = 'Case';
    $this->_columns['civicrm_contact']['fields']['gender_id']['no_display'] = TRUE;
    $this->_columns['civicrm_contact']['fields']['gender_id']['title'] = 'Gender';

    $this->_tagFilter = TRUE;
    $this->_groupFilter = TRUE;
    parent::__construct();
  }

  /**
   * Generate a temp table to reflect the pre-constrained report group
   * This could be a group of contacts on whom we are going to do a series of contribution
   * comparisons.
   *
   * We apply where criteria from the form to generate this
   *
   * We create a temp table of their ids in the first instance
   * and use this as the base
   */
  function generateTempTable() {
    $tempTable = 'civicrm_report_temp_activities' . date('d_H_I') . rand(1, 10000);
    $sql = "CREATE {$this->_temporary} TABLE $tempTable
    (`case_id` INT(10) UNSIGNED NULL DEFAULT '0',
    `activity_id` INT(10) UNSIGNED NULL DEFAULT '0',
    INDEX `case_id` (`case_id`),
    INDEX `activity_id` (`activity_id`)
    )
    COLLATE='utf8_unicode_ci'
    ENGINE=HEAP;";
    CRM_Core_DAO::executeQuery($sql);
    $sql = "
      INSERT INTO $tempTable
      SELECT ccase.id, activity_id
      FROM civicrm_case ccase
      LEFT JOIN
        (SELECT
          {$this->_aliases['civicrm_case']}.id as case_id, activity_id
          {$this->_from} {$this->_where}
        ) as case_activities ON case_activities.case_id = ccase.id";
    CRM_Core_DAO::executeQuery($sql);
    $this->_caseActivityTable = $tempTable;
  }

  /**
   * @return array
   */
  function fromClauses() {
    return array(
      'contact_from_case',
      'activity_from_case',
    );
  }

  /**
   * constrainedWhere applies to Where clauses applied AFTER the
   * 'pre-constrained' report temp tables are created. Here we only keep the clauses relating to
   * civicrm_case
   */
  function constrainedWhere() {
    $this->_where = " WHERE 1";
    foreach ($this->whereClauses as $table => $clauses) {
      if (substr($table, 0, 12) == 'civicrm_case') {
        foreach ($clauses as $clause) {
          if (!empty($clause)) {
            $this->_where .= " AND " . $clause;
          }
        }
      }
    }
  }
}