CRM-17310 Add my reports functionality
[civicrm-core.git] / CRM / Report / BAO / ReportInstance.php
index adb089d2ae70f1ab012e7fe046e9560fcef153c7..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
@@ -263,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;
+  }
+
 }