Merge pull request #2326 from eileenmcnaughton/CRM-14069
[civicrm-core.git] / CRM / Report / Form / Activity.php
index c5648d9529f8f306007a3469e5deec3bfc5ae2c4..63b70e7df6ea900d722ec9a7239b7c438c489d98 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -41,6 +41,12 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
 
   protected $_nonDisplayFields = array();
 
+  /**
+   *
+   */
+  /**
+   *
+   */
   function __construct() {
     // There could be multiple contacts. We not clear on which contact id to display.
     // Lets hide it for now.
@@ -223,11 +229,12 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
           array('title' => ts('Duration'),
             'type' => CRM_Utils_Type::T_INT,
           ),
+          'details' => array(
+            'title' => ts('Activity Details'),
+          )
         ),
-        'filters' =>
-        array(
-          'activity_date_time' =>
-          array(
+        'filters' => array(
+          'activity_date_time' => array(
             'default' => 'this.month',
             'operatorType' => CRM_Report_Form::OP_DATE,
           ),
@@ -243,9 +250,12 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
             'operatorType' => CRM_Report_Form::OP_MULTISELECT,
             'options' => CRM_Core_PseudoConstant::activityStatus(),
           ),
+          'details' => array(
+            'title' => ts('Activity Details'),
+            'type' => CRM_Utils_Type::T_TEXT,
+          )
         ),
-        'order_bys' =>
-        array(
+        'order_bys' => array(
           'activity_date_time' =>
           array('title' => ts('Activity Date'), 'default_weight' => '1', 'dbAlias' => 'civicrm_activity_activity_date_time'),
           'activity_type_id' =>
@@ -301,10 +311,12 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     parent::__construct();
   }
 
+  /**
+   * @param null $recordType
+   */
   function select($recordType = NULL) {
     if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && $recordType != 'final') {
       $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}";
-      $this->_params['fields']["contact_{$recordType}"] = 1;
     }
     parent::select();
 
@@ -372,6 +384,9 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     }
   }
 
+  /**
+   * @param $recordType
+   */
   function from($recordType) {
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
@@ -437,6 +452,9 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     $this->addAddressFromClause();
   }
 
+  /**
+   * @param null $recordType
+   */
   function where($recordType = NULL) {
     $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND
                                 {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND
@@ -514,6 +532,9 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id";
   }
 
+  /**
+   * @param string $tableAlias
+   */
   function buildACLClause($tableAlias = 'contact_a') {
     //override for ACL( Since Contact may be source
     //contact/assignee or target also it may be null )
@@ -540,6 +561,11 @@ class CRM_Report_Form_Activity extends CRM_Report_Form {
     $this->_aclWhere = NULL;
   }
 
+  /**
+   * @param $groupID
+   *
+   * @throws Exception
+   */
   function add2group($groupID) {
     if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') {
       CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group'));
@@ -573,22 +599,21 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}";
   }
 
   function postProcess() {
-    $this->buildACLClause(array('civicrm_contact_source', 'civicrm_contact_target', 'civicrm_contact_assignee'));
     $this->beginPostProcess();
 
     //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty'
     $nullFilters = array();
     foreach (array('target', 'source', 'assignee') as $type) {
-      if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll' ||
-        CRM_Utils_Array::value("contact_{$type}_value", $this->_params)) {
-        $nullFilters[] = " civicrm_contact_contact_{$type} IS NOT NULL ";
+      if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll' || !empty($this->_params["contact_{$type}_value"])) {
+        $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NOT NULL ";
       }
       else if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nll') {
-        $nullFilters[] = " civicrm_contact_contact_{$type} IS NULL ";
+        $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NULL ";
       }
     }
 
       // 1. fill temp table with target results
+    $this->buildACLClause(array('civicrm_contact_target'));
     $this->select('target');
     $this->from('target');
     $this->customDataFrom();
@@ -611,6 +636,7 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}";
     CRM_Core_DAO::executeQuery($tempQuery);
 
     // 3. fill temp table with assignee results
+    $this->buildACLClause(array('civicrm_contact_assignee'));
     $this->select('assignee');
     $this->from('assignee');
     $this->customDataFrom();
@@ -622,6 +648,7 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}";
     CRM_Core_DAO::executeQuery($tempQuery);
 
     // 4. fill temp table with source results
+    $this->buildACLClause(array('civicrm_contact_source'));
     $this->select('source');
     $this->from('source');
     $this->customDataFrom();
@@ -656,6 +683,9 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"
     $this->endPostProcess($rows);
   }
 
+  /**
+   * @param $rows
+   */
   function alterDisplay(&$rows) {
     // custom code to alter rows
 
@@ -665,6 +695,7 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"
     $viewLinks      = FALSE;
     $seperator      = CRM_Core_DAO::VALUE_SEPARATOR;
     $context        = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report');
+    $actUrl         = '';
 
     if (CRM_Core_Permission::check('access CiviCRM')) {
       $viewLinks  = TRUE;
@@ -673,6 +704,34 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"
     }
 
     foreach ($rows as $rowNum => $row) {
+      // if we have an activity type, format the View Activity link for use in various columns
+      if ($viewLinks && array_key_exists('civicrm_activity_activity_type_id', $row)) {
+        // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
+        // else use source contact id
+        if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
+          $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
+          $cid = $targets[0];
+        }
+        else {
+          $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
+        }
+
+        $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
+          CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
+          FALSE,
+          $rows[$rowNum]['civicrm_activity_id']
+        );
+
+        $actLinkValues = array(
+          'id' => $rows[$rowNum]['civicrm_activity_id'],
+          'cid' => $cid,
+          'cxt' => $context,
+        );
+        $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'],
+          CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE
+        );
+      }
+
       if (array_key_exists('civicrm_contact_contact_source', $row)) {
         if ($value = $row['civicrm_contact_contact_source_id']) {
           if ($viewLinks) {
@@ -733,31 +792,7 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"
         if ($value = $row['civicrm_activity_activity_type_id']) {
           $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value];
           if ($viewLinks) {
-            // Check for target contact id(s) and use the first contact id in that list for view activity link if found,
-            // else use source contact id
-            if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) {
-              $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']);
-              $cid = $targets[0];
-            }
-            else {
-              $cid = $rows[$rowNum]['civicrm_contact_contact_source_id'];
-            }
-
-            $actionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'],
-              CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]),
-              FALSE,
-              $rows[$rowNum]['civicrm_activity_id']
-            );
-
-            $linkValues = array(
-              'id' => $rows[$rowNum]['civicrm_activity_id'],
-              'cid' => $cid,
-              'cxt' => $context,
-            );
-            $url = CRM_Utils_System::url($actionLinks[CRM_Core_Action::VIEW]['url'],
-              CRM_Core_Action::replace($actionLinks[CRM_Core_Action::VIEW]['qs'], $linkValues), TRUE
-            );
-            $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $url;
+            $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $actUrl;
             $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct;
           }
           $entryFound = TRUE;
@@ -771,6 +806,17 @@ GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"
         }
       }
 
+      if (array_key_exists('civicrm_activity_details', $row)) {
+        if ($value = $row['civicrm_activity_details']) {
+          $fullDetails = $rows[$rowNum]['civicrm_activity_details'];
+          $rows[$rowNum]['civicrm_activity_details'] = substr($fullDetails, 0, strrpos(substr($fullDetails, 0, 80), ' '));
+          if ($actUrl) {
+            $rows[$rowNum]['civicrm_activity_details'] .= " <a href='{$actUrl}' title='{$onHoverAct}'>(more)</a>";
+          }
+          $entryFound = TRUE;
+        }
+      }
+
       if (array_key_exists('civicrm_activity_campaign_id', $row)) {
         if ($value = $row['civicrm_activity_campaign_id']) {
           $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value];