Merge pull request #8780 from eileenmcnaughton/on_hold_data_fix
[civicrm-core.git] / CRM / Report / Page / InstanceList.php
index 3051a4b1561054a75c6e8fb8f4a43a3a22c6187c..143c48ef772e11f5a164240bb586ae08b018ef9e 100644 (file)
@@ -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, v.name as class_name,
+        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,14 +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&output=criteria");
-        $rows[$dao->compName][$dao->id]['viewUrl'] = CRM_Utils_System::url("{$url}/{$dao->id}", 'force=1&reset=1');
-        $rows[$dao->compName][$dao->id]['actions'] = $this->getActionLinks($dao->id, $dao->class_name);
+
+        $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;
   }
 
@@ -175,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);
 
@@ -205,6 +227,7 @@ 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();
   }