Merge pull request #13972 from eileenmcnaughton/array_format_5
[civicrm-core.git] / CRM / Contribute / Page / UserDashboard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBoard {
34
35 /**
36 * called when action is browse.
37 */
38 public function listContribution() {
39 $rows = civicrm_api3('Contribution', 'get', [
40 'options' => [
41 'limit' => 12,
42 'sort' => 'receive_date DESC',
43 ],
44 'sequential' => 1,
45 'contact_id' => $this->_contactId,
46 'return' => [
47 'total_amount',
48 'contribution_recur_id',
49 'financial_type',
50 'receive_date',
51 'receipt_date',
52 'contribution_status',
53 'currency',
54 'amount_level',
55 'contact_id,',
56 'contribution_source',
57 ],
58 ])['values'];
59
60 // We want oldest first, just among the most recent contributions
61 $rows = array_reverse($rows);
62
63 foreach ($rows as $index => $row) {
64 // This is required for tpl logic. We should move away from hard-code this to adding an array of actions to the row
65 // which the tpl can iterate through - this should allow us to cope with competing attempts to add new buttons
66 // and allow extensions to assign new ones through the pageRun hook
67 $row[0]['contribution_status_name'] = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id']);;
68 }
69
70 $this->assign('contribute_rows', $rows);
71 $this->assign('contributionSummary', ['total_amount' => civicrm_api3('Contribution', 'getcount', ['contact_id' => $this->_contactId])]);
72
73 //add honor block
74 $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);
75
76 if (!empty($params)) {
77 // assign vars to templates
78 $this->assign('honorRows', $params);
79 $this->assign('honor', TRUE);
80 }
81
82 $recur = new CRM_Contribute_DAO_ContributionRecur();
83 $recur->contact_id = $this->_contactId;
84 $recur->is_test = 0;
85 $recur->find();
86
87 $recurStatus = CRM_Contribute_PseudoConstant::contributionStatus();
88
89 $recurRow = [];
90 $recurIDs = [];
91 while ($recur->fetch()) {
92 if (empty($recur->payment_processor_id)) {
93 // it's not clear why we continue here as any without a processor id would likely
94 // be imported from another system & still seem valid.
95 continue;
96 }
97
98 require_once 'api/v3/utils.php';
99 //@todo calling api functions directly is not supported
100 _civicrm_api3_object_to_array($recur, $values);
101
102 $values['recur_status'] = $recurStatus[$values['contribution_status_id']];
103 $recurRow[$values['id']] = $values;
104
105 $action = array_sum(array_keys(CRM_Contribute_Page_Tab::recurLinks($recur->id, 'dashboard')));
106
107 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
108 $hideUpdate = $details->membership_id & $details->auto_renew;
109
110 if ($hideUpdate) {
111 $action -= CRM_Core_Action::UPDATE;
112 }
113
114 $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::recurLinks($recur->id, 'dashboard'),
115 $action, [
116 'cid' => $this->_contactId,
117 'crid' => $values['id'],
118 'cxt' => 'contribution',
119 ],
120 ts('more'),
121 FALSE,
122 'contribution.dashboard.recurring',
123 'Contribution',
124 $values['id']
125 );
126
127 $recurIDs[] = $values['id'];
128 }
129 if (is_array($recurIDs) && !empty($recurIDs)) {
130 $getCount = CRM_Contribute_BAO_ContributionRecur::getCount($recurIDs);
131 foreach ($getCount as $key => $val) {
132 $recurRow[$key]['completed'] = $val;
133 $recurRow[$key]['link'] = CRM_Utils_System::url('civicrm/contribute/search',
134 "reset=1&force=1&recur=$key"
135 );
136 }
137 }
138
139 $this->assign('recurRows', $recurRow);
140 if (!empty($recurRow)) {
141 $this->assign('recur', TRUE);
142 }
143 else {
144 $this->assign('recur', FALSE);
145 }
146 }
147
148 /**
149 * the main function that is called when the page
150 * loads, it decides the which action has to be taken for the page.
151 */
152 public function run() {
153 $this->assign('invoicing', CRM_Invoicing_Utils::isInvoicingEnabled());
154 $this->assign('defaultInvoicePage', CRM_Invoicing_Utils::getDefaultPaymentPage());
155 parent::preProcess();
156 $this->listContribution();
157 }
158
159 }