CRM-17310 Add my reports functionality
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
index 89dd47a3bb89ce30384ed84b6553d38914775cec..9fe13ebaf6cbd2dfca4ba4f58dd01774b702578a 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
  */
 class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
 
@@ -119,6 +119,7 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
 
   /**
    * Create instance.
+   *
    * takes an associative array and creates a instance object and does any related work like permissioning, adding to dashboard etc.
    *
    * This function is invoked from within the web form layer and also from the api layer
@@ -160,6 +161,12 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
       unset($params['is_navigation']);
     }
 
+    $viewMode = !empty($params['view_mode']) ? $params['view_mode'] : FALSE;
+    if ($viewMode) {
+      // Do not save to the DB - it's saved in the url.
+      unset($params['view_mode']);
+    }
+
     // add to dashboard
     $dashletParams = array();
     if (!empty($params['addToDashboard'])) {
@@ -185,7 +192,7 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
       if (empty($params['id']) && empty($params['instance_id']) && !empty($navigationParams['id'])) {
         unset($navigationParams['id']);
       }
-      $navigationParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1";
+      $navigationParams['url'] = "civicrm/report/instance/{$instance->id}" . ($viewMode == 'view' ? '?reset=1&force=1' : '?reset=1&output=criteria');
       $navigation = CRM_Core_BAO_Navigation::add($navigationParams);
 
       if (!empty($navigationParams['is_active'])) {
@@ -257,4 +264,56 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance {
     return NULL;
   }
 
+  /**
+   * Check if report is private.
+   *
+   * @param int $instance_id
+   *
+   * @return bool
+   */
+  public static function reportIsPrivate($instance_id) {
+    $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
+    if ($owner_id) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
+  /**
+   * Check if the logged in user is the owner.
+   *
+   * @param int $instance_id
+   *
+   * @return TRUE if contact owns the report, FALSE if not
+   */
+  public static function contactIsOwner($instance_id) {
+    $session = CRM_Core_Session::singleton();
+    $contact_id = $session->get('userID');
+    $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
+    if ($contact_id === $owner_id) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
+  /**
+   * Check if the logged in contact can administer the report.
+   *
+   * @param int $instance_id
+   *
+   * @return bool
+   *   True if contact can edit the private report, FALSE if not.
+   */
+  public static function contactCanAdministerReport($instance_id) {
+    if (self::reportIsPrivate($instance_id)) {
+      if (self::contactIsOwner($instance_id) || CRM_Core_Permission::check('access all private reports')) {
+        return TRUE;
+      }
+    }
+    elseif (CRM_Core_Permission::check('administer Reports')) {
+      return TRUE;
+    }
+    return FALSE;
+  }
+
 }