Merge pull request #15813 from eileenmcnaughton/fee
[civicrm-core.git] / CRM / Contribute / Page / Tab.php
index e055d79e791365819e774434ab076bd0f617e60e..c84e1198b80806aa04ba44633426a39271494d7b 100644 (file)
@@ -1,46 +1,38 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Contribute_Page_Tab extends CRM_Core_Page {
 
   /**
-   * The action links that we need to display for the browse screen.
+   * The permission we have on this contact
    *
-   * @var array
+   * @var string
    */
-  static $_links = NULL;
-  static $_recurLinks = NULL;
   public $_permission = NULL;
+
+  /**
+   * The contact ID for the contributions we are acting on
+   * @var int
+   */
   public $_contactId = NULL;
+
+  /**
+   * The recurring contribution ID (if any)
+   * @var int
+   */
   public $_crid = NULL;
 
   /**
@@ -55,59 +47,56 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
    *
    * @return array
    */
-  public static function &recurLinks($recurID = FALSE, $context = 'contribution') {
-    if (!(self::$_links)) {
-      self::$_links = array(
-        CRM_Core_Action::VIEW => array(
-          'name' => ts('View'),
-          'title' => ts('View Recurring Payment'),
-          'url' => 'civicrm/contact/view/contributionrecur',
-          'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
-        ),
-        CRM_Core_Action::UPDATE => array(
-          'name' => ts('Edit'),
-          'title' => ts('Edit Recurring Payment'),
-          'url' => 'civicrm/contribute/updaterecur',
-          'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
-        ),
-        CRM_Core_Action::DISABLE => array(
-          'name' => ts('Cancel'),
-          'title' => ts('Cancel'),
-          'ref' => 'crm-enable-disable',
-        ),
-      );
-    }
+  public static function recurLinks($recurID = FALSE, $context = 'contribution') {
+    $links = [
+      CRM_Core_Action::VIEW => [
+        'name' => ts('View'),
+        'title' => ts('View Recurring Payment'),
+        'url' => 'civicrm/contact/view/contributionrecur',
+        'qs' => "reset=1&id=%%crid%%&cid=%%cid%%&context={$context}",
+      ],
+      CRM_Core_Action::UPDATE => [
+        'name' => ts('Edit'),
+        'title' => ts('Edit Recurring Payment'),
+        'url' => 'civicrm/contribute/updaterecur',
+        'qs' => "reset=1&action=update&crid=%%crid%%&cid=%%cid%%&context={$context}",
+      ],
+      CRM_Core_Action::DISABLE => [
+        'name' => ts('Cancel'),
+        'title' => ts('Cancel'),
+        'ref' => 'crm-enable-disable',
+      ],
+    ];
 
     if ($recurID) {
-      $links = self::$_links;
       $paymentProcessorObj = CRM_Contribute_BAO_ContributionRecur::getPaymentProcessorObject($recurID);
-      if (!$paymentProcessorObj) {
-        unset($links[CRM_Core_Action::DISABLE]);
-        unset($links[CRM_Core_Action::UPDATE]);
-        return $links;
-      }
-      if ($paymentProcessorObj->supports('cancelRecurring')) {
-        unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
-        $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
-        $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
-      }
+      if ($paymentProcessorObj) {
+        if ($paymentProcessorObj->supports('cancelRecurring')) {
+          unset($links[CRM_Core_Action::DISABLE]['extra'], $links[CRM_Core_Action::DISABLE]['ref']);
+          $links[CRM_Core_Action::DISABLE]['url'] = "civicrm/contribute/unsubscribe";
+          $links[CRM_Core_Action::DISABLE]['qs'] = "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}";
+        }
 
-      if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
-        $links[CRM_Core_Action::RENEW] = array(
-          'name' => ts('Change Billing Details'),
-          'title' => ts('Change Billing Details'),
-          'url' => 'civicrm/contribute/updatebilling',
-          'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
-        );
-      }
+        if ($paymentProcessorObj->supports('UpdateSubscriptionBillingInfo')) {
+          $links[CRM_Core_Action::RENEW] = [
+            'name' => ts('Change Billing Details'),
+            'title' => ts('Change Billing Details'),
+            'url' => 'civicrm/contribute/updatebilling',
+            'qs' => "reset=1&crid=%%crid%%&cid=%%cid%%&context={$context}",
+          ];
+        }
 
-      if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
+        if (!$paymentProcessorObj->supports('ChangeSubscriptionAmount') && !$paymentProcessorObj->supports('EditRecurringContribution')) {
+          unset($links[CRM_Core_Action::UPDATE]);
+        }
+      }
+      else {
+        unset($links[CRM_Core_Action::DISABLE]);
         unset($links[CRM_Core_Action::UPDATE]);
       }
-      return $links;
     }
 
-    return self::$_links;
+    return $links;
   }
 
   /**
@@ -116,7 +105,7 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
    */
   public function browse() {
     // add annual contribution
-    $annual = array();
+    $annual = [];
     list($annual['count'],
       $annual['amount'],
       $annual['avg']
@@ -151,13 +140,14 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     $softCreditList = CRM_Contribute_BAO_ContributionSoft::getSoftContributionList($this->_contactId, NULL, $isTest);
 
     if (!empty($softCreditList)) {
-      $softCreditTotals = array();
+      $softCreditTotals = [];
 
       list($softCreditTotals['count'],
         $softCreditTotals['cancel']['count'],
         $softCreditTotals['amount'],
         $softCreditTotals['avg'],
-        $softCreditTotals['cancel']['amount'] // to get cancel amount
+        // to get cancel amount
+        $softCreditTotals['cancel']['amount']
         ) = CRM_Contribute_BAO_ContributionSoft::getSoftContributionTotals($this->_contactId, $isTest);
 
       $this->assign('softCredit', TRUE);
@@ -195,19 +185,19 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
    * Loads active recurring contributions for the current contact and formats
    * them to be used on the form.
    *
-   * @return array;
+   * @return array
    */
   private function getActiveRecurringContributions() {
     try {
-      $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', array(
+      $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
         'contact_id' => $this->_contactId,
-        'contribution_status_id' => array('NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()),
+        'contribution_status_id' => ['NOT IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
         'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
-      ));
+      ]);
       $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
     }
     catch (Exception $e) {
-      $recurContributions = array();
+      $recurContributions = [];
     }
 
     return $this->buildRecurringContributionsArray($recurContributions);
@@ -217,15 +207,15 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
    * Loads inactive recurring contributions for the current contact and formats
    * them to be used on the form.
    *
-   * @return array;
+   * @return array
    */
   private function getInactiveRecurringContributions() {
     try {
-      $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', array(
+      $contributionRecurResult = civicrm_api3('ContributionRecur', 'get', [
         'contact_id' => $this->_contactId,
-        'contribution_status_id' => array('IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()),
+        'contribution_status_id' => ['IN' => CRM_Contribute_BAO_ContributionRecur::getInactiveStatuses()],
         'options' => ['limit' => 0, 'sort' => 'is_test, start_date DESC'],
-      ));
+      ]);
       $recurContributions = CRM_Utils_Array::value('values', $contributionRecurResult);
     }
     catch (Exception $e) {
@@ -238,7 +228,7 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
   /**
    * @param $recurContributions
    *
-   * @return mixed
+   * @return array
    */
   private function buildRecurringContributionsArray($recurContributions) {
     $liveRecurringContributionCount = 0;
@@ -266,11 +256,11 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
       }
 
       $recurContributions[$recurId]['action'] = CRM_Core_Action::formLink(self::recurLinks($recurId), $actionMask,
-        array(
+        [
           'cid' => $this->_contactId,
           'crid' => $recurId,
           'cxt' => 'contribution',
-        ),
+        ],
         ts('more'),
         FALSE,
         'contribution.selector.recurring',
@@ -326,6 +316,10 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     return $controller->run();
   }
 
+  /**
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
+   */
   public function preProcess() {
     $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
@@ -337,10 +331,10 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
     else {
       $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, empty($this->_id));
       if (empty($this->_contactId)) {
-        $this->_contactId = civicrm_api3('contribution', 'getvalue', array(
-            'id' => $this->_id,
-            'return' => 'contact_id',
-          ));
+        $this->_contactId = civicrm_api3('contribution', 'getvalue', [
+          'id' => $this->_id,
+          'return' => 'contact_id',
+        ]);
       }
       $this->assign('contactId', $this->_contactId);
 
@@ -360,7 +354,8 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
    * the main function that is called when the page
    * loads, it decides the which action has to be taken for the page.
    *
-   * @return null
+   * @throws \CRM_Core_Exception
+   * @throws \CiviCRM_API3_Exception
    */
   public function run() {
     $this->preProcess();
@@ -380,9 +375,12 @@ class CRM_Contribute_Page_Tab extends CRM_Core_Page {
       $this->browse();
     }
 
-    return parent::run();
+    parent::run();
   }
 
+  /**
+   * @throws \CRM_Core_Exception
+   */
   public function setContext() {
     $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
     $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric',