Merge pull request #4925 from KarinG/patch-1
[civicrm-core.git] / CRM / Report / Form / Contribute / Repeat.php
index e10a830dc4290c19748d11d9e90ae2ee4d6c7c97..7d07560621757db94cc3e8b70a436d6eab47160a 100644 (file)
@@ -1,8 +1,7 @@
 <?php
-
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -39,55 +38,43 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
   public $_drilldownReport = array('contribute/detail' => 'Link to Detail Report');
 
   /**
-   *
    */
   /**
-   *
    */
-  function __construct() {
+  public function __construct() {
     $this->_columns = array(
-      'civicrm_contact' =>
-      array(
+      'civicrm_contact' => array(
         'dao' => 'CRM_Contact_DAO_Contact',
         'grouping' => 'contact-fields',
-        'fields' =>
-        array(
-          'sort_name' =>
-          array(
+        'fields' => array(
+          'sort_name' => array(
             'title' => ts('Contact Name'),
             'no_repeat' => TRUE,
             'default' => TRUE,
           ),
-          'display_name' =>
-          array(
+          'display_name' => array(
             'title' => ts('Display Name'),
             'no_repeat' => TRUE,
           ),
-          'addressee_display' =>
-          array(
+          'addressee_display' => array(
             'title' => ts('Addressee Name'),
             'no_repeat' => TRUE,
           ),
-          'id' =>
-          array(
+          'id' => array(
             'no_display' => TRUE,
             'required' => TRUE,
           ),
-          'contact_type' =>
-          array(
+          'contact_type' => array(
             'title' => ts('Contact Type'),
             'no_repeat' => TRUE,
           ),
-          'contact_sub_type' =>
-          array(
+          'contact_sub_type' => array(
             'title' => ts('Contact Subtype'),
             'no_repeat' => TRUE,
           ),
         ),
-        'filters' =>
-        array(
-          'percentage_change' =>
-          array(
+        'filters' => array(
+          'percentage_change' => array(
             'title' => ts('Percentage Change'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_INT,
@@ -95,47 +82,37 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
             'dbAlias' => '( ( contribution_civireport2.total_amount_sum - contribution_civireport1.total_amount_sum ) * 100 / contribution_civireport1.total_amount_sum )',
           ),
         ),
-        'group_bys' =>
-        array(
-          'id' =>
-          array(
+        'group_bys' => array(
+          'id' => array(
             'title' => ts('Contact'),
             'default' => TRUE,
           ),
         ),
       ),
-      'civicrm_email' =>
-      array(
+      'civicrm_email' => array(
         'dao' => 'CRM_Core_DAO_Email',
-        'fields' =>
-        array(
-          'email' =>
-          array(
+        'fields' => array(
+          'email' => array(
             'title' => ts('Email'),
             'no_repeat' => TRUE,
           ),
         ),
         'grouping' => 'contact-fields',
       ),
-      'civicrm_phone' =>
-      array(
+      'civicrm_phone' => array(
         'dao' => 'CRM_Core_DAO_Phone',
-        'fields' =>
-        array(
-          'phone' =>
-          array(
+        'fields' => array(
+          'phone' => array(
             'title' => ts('Phone'),
             'no_repeat' => TRUE,
           ),
         ),
         'grouping' => 'contact-fields',
       ),
-      'civicrm_address' =>
-      array(
+      'civicrm_address' => array(
         'dao' => 'CRM_Core_DAO_Address',
         'grouping' => 'contact-fields',
-        'fields' =>
-        array(
+        'fields' => array(
           'street_address' => array('title' => ts('Street Address')),
           'supplemental_address_1' => array('title' => ts('Supplemental Address 1')),
           'city' => array('title' => ts('City')),
@@ -143,98 +120,81 @@ class CRM_Report_Form_Contribute_Repeat extends CRM_Report_Form {
           'state_province_id' => array('title' => ts('State/Province')),
           'postal_code' => array('title' => ts('Postal Code')),
         ),
-        'group_bys' =>
-        array(
+        'group_bys' => array(
           'country_id' => array('title' => ts('Country')),
-          'state_province_id' => array('title' => ts('State/Province'),
+          'state_province_id' => array(
+            'title' => ts('State/Province'),
           ),
         ),
       ),
-      'civicrm_financial_type' =>
-      array(
+      'civicrm_financial_type' => array(
         'dao' => 'CRM_Financial_DAO_FinancialType',
         'fields' => array('financial_type' => array('title' => ts('Financial Type'))),
-        'grouping'  => 'contri-fields',
-        'group_bys' =>
-        array('financial_type' =>
-        array(
-          'name' => 'id',
-          'title' => ts('Financial Type'),
-        ),
+        'grouping' => 'contri-fields',
+        'group_bys' => array(
+          'financial_type' => array(
+            'name' => 'id',
+            'title' => ts('Financial Type'),
+          ),
         ),
       ),
-      'civicrm_contribution' =>
-      array(
+      'civicrm_contribution' => array(
         'dao' => 'CRM_Contribute_DAO_Contribution',
-        'fields' =>
-        array(
+        'fields' => array(
           'contribution_source' => NULL,
-          'total_amount1' =>
-          array(
+          'total_amount1' => array(
             'name' => 'total_amount',
             'alias' => 'contribution1',
             'title' => ts('Range One Stat'),
             'type' => CRM_Utils_Type::T_MONEY,
             'default' => TRUE,
             'required' => TRUE,
-            'clause' => '
-contribution_civireport1.total_amount_count as contribution1_total_amount_count,
-contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
+            'clause' => 'contribution_civireport1.total_amount_count as contribution1_total_amount_count, contribution_civireport1.total_amount_sum as contribution1_total_amount_sum',
           ),
-          'total_amount2' =>
-          array(
+          'total_amount2' => array(
             'name' => 'total_amount',
             'alias' => 'contribution2',
             'title' => ts('Range Two Stat'),
             'type' => CRM_Utils_Type::T_MONEY,
             'default' => TRUE,
             'required' => TRUE,
-            'clause' => '
-contribution_civireport2.total_amount_count as contribution2_total_amount_count,
-contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
+            'clause' => 'contribution_civireport2.total_amount_count as contribution2_total_amount_count, contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
           ),
         ),
         'grouping' => 'contri-fields',
-        'filters' =>
-        array(
-          'receive_date1' =>
-          array(
+        'filters' => array(
+          'receive_date1' => array(
             'title' => ts('Initial Date Range'),
             'default' => 'previous.year',
             'type' => CRM_Utils_Type::T_DATE,
             'operatorType' => CRM_Report_Form::OP_DATE,
             'name' => 'receive_date',
           ),
-          'receive_date2' =>
-          array(
+          'receive_date2' => array(
             'title' => ts('Second Date Range'),
             'default' => 'this.year',
             'type' => CRM_Utils_Type::T_DATE,
             'operatorType' => CRM_Report_Form::OP_DATE,
             'name' => 'receive_date',
           ),
-          'total_amount1' =>
-          array(
+          'total_amount1' => array(
             'title' => ts('Range One Amount'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_INT,
             'name' => 'total_amount',
           ),
-          'total_amount2' =>
-          array(
+          'total_amount2' => array(
             'title' => ts('Range Two Amount'),
             'type' => CRM_Utils_Type::T_INT,
             'operatorType' => CRM_Report_Form::OP_INT,
             'name' => 'total_amount',
           ),
-          'financial_type_id' =>
-          array(
+          'financial_type_id' => array(
             'title' => ts('Financial Type'),
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
             'options' => CRM_Contribute_PseudoConstant::financialType(),
           ),
-          'contribution_status_id' =>
-          array(
+          'contribution_status_id' => array(
             'title' => ts('Contribution Status'),
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
             'options' => CRM_Contribute_PseudoConstant::contributionStatus(),
@@ -251,7 +211,7 @@ contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
     parent::__construct();
   }
 
-  function preProcess() {
+  public function preProcess() {
     parent::preProcess();
   }
 
@@ -260,11 +220,11 @@ contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
    *
    * @return array
    */
-  function setDefaultValues($freeze = TRUE) {
+  public function setDefaultValues($freeze = TRUE) {
     return parent::setDefaultValues($freeze);
   }
 
-  function select() {
+  public function select() {
     $select = array();
     $append = NULL;
     // since contact fields not related to financial type
@@ -277,7 +237,9 @@ contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
     foreach ($this->_columns as $tableName => $table) {
       if (array_key_exists('fields', $table)) {
         foreach ($table['fields'] as $fieldName => $field) {
-          if (!empty($field['required']) || !empty($this->_params['fields'][$fieldName])) {
+          if (!empty($field['required']) ||
+            !empty($this->_params['fields'][$fieldName])
+          ) {
             if (isset($field['clause'])) {
               $select[] = $field['clause'];
 
@@ -307,9 +269,11 @@ contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
   /**
    * @param bool $tableCol
    */
-  function groupBy($tableCol = FALSE) {
+  public function groupBy($tableCol = FALSE) {
     $this->_groupBy = "";
-    if (!empty($this->_params['group_bys']) && is_array($this->_params['group_bys'])) {
+    if (!empty($this->_params['group_bys']) &&
+      is_array($this->_params['group_bys'])
+    ) {
       foreach ($this->_columns as $tableName => $table) {
         if (array_key_exists('group_bys', $table)) {
           foreach ($table['group_bys'] as $fieldName => $field) {
@@ -329,7 +293,7 @@ contribution_civireport2.total_amount_sum as contribution2_total_amount_sum',
     }
   }
 
-  function from() {
+  public function from() {
     list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
     $from = "$fromTable $fromAlias";
 
@@ -343,7 +307,7 @@ LEFT JOIN civicrm_phone   {$this->_aliases['civicrm_phone']}
        ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND {$this->_aliases['civicrm_phone']}.is_primary = 1";
 
     }
-    else if ($fromTable == 'civicrm_financial_type') {
+    elseif ($fromTable == 'civicrm_financial_type') {
       $contriCol = "financial_type_id";
     }
     elseif ($fromTable == 'civicrm_contribution') {
@@ -353,7 +317,7 @@ LEFT JOIN civicrm_phone   {$this->_aliases['civicrm_phone']}
       $from .= "
 INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']} ON {$this->_aliases['civicrm_address']}.contact_id = {$this->_aliases['civicrm_contact']}.id";
       $fromAlias = $this->_aliases['civicrm_contact'];
-      $fromCol   = "id";
+      $fromCol = "id";
       $contriCol = "contact_id";
     }
 
@@ -370,15 +334,15 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
    *
    * @return mixed|string
    */
-  function whereContribution($replaceAliasWith = 'contribution1') {
+  public function whereContribution($replaceAliasWith = 'contribution1') {
     $clauses = array("is_test" => "{$this->_aliases['civicrm_contribution']}.is_test = 0");
 
     foreach ($this->_columns['civicrm_contribution']['filters'] as $fieldName => $field) {
       $clause = NULL;
       if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) {
         $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
-        $from     = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
-        $to       = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
+        $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
+        $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
 
         $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to, $field['type']);
       }
@@ -427,15 +391,17 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
     return $whereClause;
   }
 
-  function where() {
+  public function where() {
     if (!$this->_amountClauseWithAND) {
-      $this->_amountClauseWithAND =
-        "!({$this->_aliases['civicrm_contribution']}1.total_amount_count IS NULL AND {$this->_aliases['civicrm_contribution']}2.total_amount_count IS NULL)";
+      $this->_amountClauseWithAND
+        "!({$this->_aliases['civicrm_contribution']}1.total_amount_count IS NULL AND {$this->_aliases['civicrm_contribution']}2.total_amount_count IS NULL)";
     }
     $clauses = array("atleast_one_amount" => $this->_amountClauseWithAND);
 
     foreach ($this->_columns as $tableName => $table) {
-      if (array_key_exists('filters', $table) && $tableName != 'civicrm_contribution') {
+      if (array_key_exists('filters', $table) &&
+        $tableName != 'civicrm_contribution'
+      ) {
         foreach ($table['filters'] as $fieldName => $field) {
           $clause = NULL;
           $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
@@ -464,7 +430,7 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
    *
    * @return array
    */
-  static function formRule($fields, $files, $self) {
+  public static function formRule($fields, $files, $self) {
 
     $errors = $checkDate = $errorCount = array();
 
@@ -506,8 +472,8 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
       $errors['fields'] = ts('Please select at least one Group by field.');
     }
     elseif ((array_key_exists('contribution_source', $fields['group_bys']) ||
-      array_key_exists('contribution_type', $fields['group_bys'])
-    ) &&
+        array_key_exists('contribution_type', $fields['group_bys'])
+      ) &&
       (count($fields['group_bys']) > 1)
     ) {
       $errors['fields'] = ts('You can not use other Group by with Financial type or Contribution source.');
@@ -528,12 +494,15 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
           }
           if (!$found) {
             $erorrGrps = implode(',', $invlidGroups);
-            $tempErrors[] = ts("Do not select field %1 with Group by %2.", array(1 => $idMapping[$fld_id], 2 => $erorrGrps));
+            $tempErrors[] = ts("Do not select field %1 with Group by %2.", array(
+              1 => $idMapping[$fld_id],
+              2 => $erorrGrps,
+            ));
           }
         }
       }
       if (!empty($tempErrors)) {
-        $errors['fields'] = implode("<br>", $tempErrors);
+        $errors['fields'] = implode("<br/>", $tempErrors);
       }
     }
 
@@ -621,7 +590,7 @@ LEFT JOIN civicrm_temp_civireport_repeat2 {$this->_aliases['civicrm_contribution
    *
    * @return array
    */
-  function statistics(&$rows) {
+  public function statistics(&$rows) {
     $statistics = parent::statistics($rows);
 
     //fetch contributions for both date ranges from pre-existing temp tables
@@ -644,10 +613,10 @@ LEFT JOIN civicrm_temp_civireport_repeat2
     //store contributions in array 'contact_sums' for comparison
     $contact_sums = array();
     while ($dao->fetch()) {
-      $contact_sums[$dao->contact_id] =
-        array(
+      $contact_sums[$dao->contact_id]
+        array(
           'contribution1_total_amount_sum' => $dao->contribution1_total_amount_sum,
-          'contribution2_total_amount_sum' => $dao->contribution2_total_amount_sum
+          'contribution2_total_amount_sum' => $dao->contribution2_total_amount_sum,
         );
     }
 
@@ -659,20 +628,26 @@ LEFT JOIN civicrm_temp_civireport_repeat2
     $number_lapsed = 0;
 
     foreach ($contact_sums as $uid => $row) {
-      if ($row['contribution1_total_amount_sum'] && $row['contribution2_total_amount_sum']) {
-        $change = ($row['contribution1_total_amount_sum'] - $row['contribution2_total_amount_sum']);
-        if($change == 0)
+      if ($row['contribution1_total_amount_sum'] &&
+        $row['contribution2_total_amount_sum']
+      ) {
+        $change = ($row['contribution1_total_amount_sum'] -
+          $row['contribution2_total_amount_sum']);
+        if ($change == 0) {
           $number_maintained += 1;
-        elseif($change > 0)
+        }
+        elseif ($change > 0) {
           $number_upgraded += 1;
-        elseif($change < 0)
+        }
+        elseif ($change < 0) {
           $number_downgraded += 1;
+        }
       }
       elseif ($row['contribution1_total_amount_sum']) {
-        $number_new +=1;
+        $number_new += 1;
       }
       elseif ($row['contribution2_total_amount_sum']) {
-        $number_lapsed +=1;
+        $number_lapsed += 1;
       }
     }
 
@@ -722,14 +697,18 @@ GROUP BY    currency
     $count = $count2 = 0;
     while ($dao->fetch()) {
       if ($dao->amount) {
-        $amount[] = CRM_Utils_Money::format($dao->amount, $dao->currency)."(".$dao->count.")";
-        $average[] =   CRM_Utils_Money::format($dao->avg, $dao->currency);
+        $amount[]
+          = CRM_Utils_Money::format($dao->amount, $dao->currency) . "(" .
+          $dao->count . ")";
+        $average[] = CRM_Utils_Money::format($dao->avg, $dao->currency);
       }
 
       $count += $dao->count;
       if ($dao->amount2) {
-        $amount2[] = CRM_Utils_Money::format($dao->amount2, $dao->currency)."(".$dao->count.")";
-        $average2[] =   CRM_Utils_Money::format($dao->avg2, $dao->currency);
+        $amount2[]
+          = CRM_Utils_Money::format($dao->amount2, $dao->currency) . "(" .
+          $dao->count . ")";
+        $average2[] = CRM_Utils_Money::format($dao->avg2, $dao->currency);
       }
       $count2 += $dao->count2;
     }
@@ -770,7 +749,7 @@ GROUP BY    currency
     return $statistics;
   }
 
-  function postProcess() {
+  public function postProcess() {
     $this->beginPostProcess();
     $create = $subSelect1 = $subSelect2 = NULL;
     list($fromTable, $fromAlias, $fromCol) = $this->groupBy(TRUE);
@@ -842,9 +821,9 @@ currency varchar(3)
     $this->limit();
 
     $count = 0;
-    $sql   = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_limit}";
-    $dao   = CRM_Core_DAO::executeQuery($sql);
-    $rows  = array();
+    $sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_limit}";
+    $dao = CRM_Core_DAO::executeQuery($sql);
+    $rows = array();
     while ($dao->fetch()) {
       foreach ($this->_columnHeaders as $key => $value) {
         $rows[$count][$key] = $dao->$key;
@@ -854,11 +833,13 @@ currency varchar(3)
 
     // FIXME: calculate % using query
     foreach ($rows as $uid => $row) {
-      if ($row['contribution1_total_amount_sum'] && $row['contribution2_total_amount_sum']) {
+      if ($row['contribution1_total_amount_sum'] &&
+        $row['contribution2_total_amount_sum']
+      ) {
         $rows[$uid]['change'] = number_format((($row['contribution2_total_amount_sum'] -
-            $row['contribution1_total_amount_sum']
-          ) * 100) /
-            ($row['contribution1_total_amount_sum']), 2
+              $row['contribution1_total_amount_sum']
+            ) * 100) /
+          ($row['contribution1_total_amount_sum']), 2
         );
       }
       elseif ($row['contribution1_total_amount_sum']) {
@@ -868,10 +849,14 @@ currency varchar(3)
         $rows[$uid]['change'] = ts('New Donor');
       }
       if ($row['contribution1_total_amount_count']) {
-        $rows[$uid]['contribution1_total_amount_sum'] = $row['contribution1_total_amount_sum'] . " ({$row['contribution1_total_amount_count']})";
+        $rows[$uid]['contribution1_total_amount_sum']
+          = $row['contribution1_total_amount_sum'] .
+          " ({$row['contribution1_total_amount_count']})";
       }
       if ($row['contribution2_total_amount_count']) {
-        $rows[$uid]['contribution2_total_amount_sum'] = $row['contribution2_total_amount_sum'] . " ({$row['contribution2_total_amount_count']})";
+        $rows[$uid]['contribution2_total_amount_sum']
+          = $row['contribution2_total_amount_sum'] .
+          " ({$row['contribution2_total_amount_count']})";
       }
     }
     $this->_columnHeaders['change'] = array(
@@ -897,7 +882,7 @@ currency varchar(3)
     $this->_columnHeaders['contribution1_total_amount_sum']['title'] = "$from1 -<br/> $to1";
     $this->_columnHeaders['contribution2_total_amount_sum']['title'] = "$from2 -<br/> $to2";
     unset($this->_columnHeaders['contribution1_total_amount_count'],
-    $this->_columnHeaders['contribution2_total_amount_count']
+      $this->_columnHeaders['contribution2_total_amount_count']
     );
 
     $this->formatDisplay($rows);
@@ -911,7 +896,7 @@ currency varchar(3)
   /**
    * @param $rows
    */
-  function alterDisplay(&$rows) {
+  public function alterDisplay(&$rows) {
     // custom code to alter rows
     list($from1, $to1) = $this->getFromTo(CRM_Utils_Array::value("receive_date1_relative", $this->_params),
       CRM_Utils_Array::value("receive_date1_from", $this->_params),
@@ -944,8 +929,8 @@ currency varchar(3)
 
           $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
             "reset=1&force=1&" .
-              "country_id_op=in&country_id_value={$value}&" .
-              "$dateUrl",
+            "country_id_op=in&country_id_value={$value}&" .
+            "$dateUrl",
             $this->_absoluteUrl, $this->_id, $this->_drilldownReport
           );
 
@@ -961,8 +946,8 @@ currency varchar(3)
 
           $url = CRM_Report_Utils_Report::getNextUrl('contribute/detail',
             "reset=1&force=1&" .
-              "state_province_id_op=in&state_province_id_value={$value}&" .
-              "$dateUrl",
+            "state_province_id_op=in&state_province_id_value={$value}&" .
+            "$dateUrl",
             $this->_absoluteUrl, $this->_id, $this->_drilldownReport
           );
 
@@ -987,4 +972,3 @@ currency varchar(3)
     // foreach ends
   }
 }
-