Merge pull request #8780 from eileenmcnaughton/on_hold_data_fix
[civicrm-core.git] / CRM / Report / Page / InstanceList.php
index e3b6455fdec5b280504fd864a81913856612c323..143c48ef772e11f5a164240bb586ae08b018ef9e 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2015
+ * @copyright CiviCRM LLC (c) 2004-2016
  */
 
 /**
@@ -82,7 +82,7 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
    *
    * @return array
    */
-  public function &info() {
+  public function info() {
 
     $report = '';
     if ($this->ovID) {
@@ -108,9 +108,12 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
     elseif ($this->grouping) {
       $report .= " AND v.grouping = '{$this->grouping}' ";
     }
+    elseif ($this->myReports) {
+      $report .= " AND inst.owner_id = " . CRM_Core_Session::getLoggedInContactID();
+    }
 
     $sql = "
-        SELECT inst.id, inst.title, inst.report_id, inst.description, v.label, v.grouping,
+        SELECT inst.id, inst.title, inst.report_id, inst.description,  inst.owner_id, v.label, v.grouping, v.name as class_name,
         CASE
           WHEN comp.name IS NOT NULL THEN SUBSTRING(comp.name, 5)
           WHEN v.grouping IS NOT NULL THEN v.grouping
@@ -136,6 +139,7 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
     $config = CRM_Core_Config::singleton();
     $rows = array();
     $url = 'civicrm/report/instance';
+    $my_reports_grouping = 'My';
     while ($dao->fetch()) {
       if (in_array($dao->report_id, self::$_exceptions)) {
         continue;
@@ -145,6 +149,12 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
       if ($dao->compName == 'Contact' || $dao->compName == $dao->grouping) {
         $enabled = TRUE;
       }
+
+      // filter report listings for private reports
+      if (!empty($dao->owner_id) && CRM_Core_Session::getLoggedInContactID() != $dao->owner_id) {
+        continue;
+      }
+
       //filter report listings by permissions
       if (!($enabled && CRM_Report_Utils_Report::isInstancePermissioned($dao->id))) {
         continue;
@@ -158,15 +168,25 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
         if ($this->ovID) {
           $this->title = ts("Report(s) created from the template: %1", array(1 => $dao->label));
         }
-        $rows[$dao->compName][$dao->id]['title'] = $dao->title;
-        $rows[$dao->compName][$dao->id]['label'] = $dao->label;
-        $rows[$dao->compName][$dao->id]['description'] = $dao->description;
-        $rows[$dao->compName][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1");
-        if (CRM_Core_Permission::check('administer Reports')) {
-          $rows[$dao->compName][$dao->id]['deleteUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'action=delete&reset=1');
+
+        $report_grouping = $dao->compName;
+        if ($dao->owner_id != NULL) {
+          $report_grouping = $my_reports_grouping;
         }
+        $rows[$report_grouping][$dao->id]['title'] = $dao->title;
+        $rows[$report_grouping][$dao->id]['label'] = $dao->label;
+        $rows[$report_grouping][$dao->id]['description'] = $dao->description;
+        $rows[$report_grouping][$dao->id]['url'] = CRM_Utils_System::url("{$url}/{$dao->id}", "reset=1&output=criteria");
+        $rows[$report_grouping][$dao->id]['viewUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'force=1&reset=1');
+        $rows[$report_grouping][$dao->id]['actions'] = $this->getActionLinks($dao->id, $dao->class_name);
       }
     }
+    // Move My Reports to the beginning of the reports list
+    if (isset($rows[$my_reports_grouping])) {
+      $my_reports = $rows[$my_reports_grouping];
+      unset($rows[$my_reports_grouping]);
+      $rows = array($my_reports_grouping => $my_reports) + $rows;
+    }
     return $rows;
   }
 
@@ -176,6 +196,7 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
   public function run() {
     //Filters by source report template or by component
     $this->ovID = CRM_Utils_Request::retrieve('ovid', 'Positive', $this);
+    $this->myReports = CRM_Utils_Request::retrieve('myreports', 'String', $this);
     $this->compID = CRM_Utils_Request::retrieve('compid', 'Positive', $this);
     $this->grouping = CRM_Utils_Request::retrieve('grp', 'String', $this);
 
@@ -206,8 +227,58 @@ class CRM_Report_Page_InstanceList extends CRM_Core_Page {
       $this->assign('newButton', $newButton);
       $this->assign('templateUrl', $templateUrl);
       $this->assign('compName', $this->_compName);
+      $this->assign('myReports', $this->myReports);
     }
     return parent::run();
   }
 
+  /**
+   * Get action links.
+   *
+   * @param int $instanceID
+   * @param string $className
+   *
+   * @return array
+   */
+  protected function getActionLinks($instanceID, $className) {
+    $urlCommon = 'civicrm/report/instance/' . $instanceID;
+    $actions = array(
+      'copy' => array(
+        'url' => CRM_Utils_System::url($urlCommon, 'reset=1&output=copy'),
+        'label' => ts('Save a Copy'),
+      ),
+      'pdf' => array(
+        'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=pdf'),
+        'label' => ts('View as pdf'),
+      ),
+      'print' => array(
+        'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=print'),
+        'label' => ts('Print report'),
+      ),
+    );
+    // Hackery, Hackera, Hacker ahahahahahaha a super nasty hack.
+    // Almost all report classes support csv & loading each class to call the method seems too
+    // expensive. We also have on our later list 'do they support charts' which is instance specific
+    // e.g use of group by might affect it. So, lets just skip for the few that don't for now.
+    $csvBlackList = array(
+      'CRM_Report_Form_Contact_Detail',
+      'CRM_Report_Form_Event_Income',
+    );
+    if (!in_array($className, $csvBlackList)) {
+      $actions['csv'] = array(
+        'url' => CRM_Utils_System::url($urlCommon, 'reset=1&force=1&output=csv'),
+        'label' => ts('Export to csv'),
+      );
+    }
+    if (CRM_Core_Permission::check('administer Reports')) {
+      $actions['delete'] = array(
+        'url' => CRM_Utils_System::url($urlCommon, 'reset=1&action=delete'),
+        'label' => ts('Delete report'),
+        'confirm_message' => ts('Are you sure you want delete this report? This action cannot be undone.'),
+      );
+    }
+
+    return $actions;
+  }
+
 }