CRM-17335 - Remove more uses of CRM_Core_DAO::$_nullObject
[civicrm-core.git] / CRM / Contribute / Form / Task / Delete.php
index 222eba1a96a556e58aa457fbde994f9a44e1eafa..842d04f0d1094f605fcd4a7cd63e3ee10d5b9f42 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
  */
 
 /**
@@ -61,7 +61,43 @@ class CRM_Contribute_Form_Task_Delete extends CRM_Contribute_Form_Task {
    * Build the form object.
    */
   public function buildQuickForm() {
-    $this->addDefaultButtons(ts('Delete Contributions'), 'done');
+    $count = 0;
+    if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus()) {
+      foreach ($this->_contributionIds as $key => $id) {
+        $finTypeID = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $id, 'financial_type_id');
+        if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($finTypeID))) {
+          unset($this->_contributionIds[$key]);
+          $count++;
+        }
+        // Now check for lineItems
+        if ($lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($id)) {
+          foreach ($lineItems as $items) {
+            if (!CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($items['financial_type_id']))) {
+              unset($this->_contributionIds[$key]);
+              $count++;
+              break;
+            }
+          }
+        }
+      }
+    }
+    if ($count && empty($this->_contributionIds)) {
+      CRM_Core_Session::setStatus(ts('1 contribution could not be deleted.', array('plural' => '%count contributions could not be deleted.', 'count' => $count)), ts('Error'), 'error');
+      $this->addButtons(array(
+        array(
+          'type' => 'back',
+          'name' => ts('Cancel'),
+        ),
+       )
+      );
+    }
+    elseif ($count && !empty($this->_contributionIds)) {
+      CRM_Core_Session::setStatus(ts('1 contribution will not be deleted.', array('plural' => '%count contributions will not be deleted.', 'count' => $count)), ts('Warning'), 'warning');
+      $this->addDefaultButtons(ts('Delete Contributions'), 'done');
+    }
+    else {
+      $this->addDefaultButtons(ts('Delete Contributions'), 'done');
+    }
   }
 
   /**