Merge pull request #20308 from masetto/master
[civicrm-core.git] / CRM / Contribute / Page / UserDashboard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contribute_Page_UserDashboard extends CRM_Contact_Page_View_UserDashBoard {
18
19 /**
20 * called when action is browse.
21 */
22 public function listContribution() {
23 $rows = civicrm_api3('Contribution', 'get', [
24 'options' => [
25 'limit' => 12,
26 'sort' => 'receive_date DESC',
27 ],
28 'sequential' => 1,
29 'contact_id' => $this->_contactId,
30 'return' => [
31 'total_amount',
32 'contribution_recur_id',
33 'financial_type',
34 'receive_date',
35 'receipt_date',
36 'contribution_status',
37 'currency',
38 'amount_level',
39 'contact_id,',
40 'contribution_source',
41 ],
42 ])['values'];
43
44 // We want oldest first, just among the most recent contributions
45 $rows = array_reverse($rows);
46
47 foreach ($rows as $index => &$row) {
48 // This is required for tpl logic. We should move away from hard-code this to adding an array of actions to the row
49 // which the tpl can iterate through - this should allow us to cope with competing attempts to add new buttons
50 // and allow extensions to assign new ones through the pageRun hook
51 $row['balance_amount'] = CRM_Contribute_BAO_Contribution::getContributionBalance($row['contribution_id']);
52 $contributionStatus = CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $row['contribution_status_id']);
53
54 if (in_array($contributionStatus, ['Pending', 'Partially paid'])) {
55 $row['buttons']['pay'] = [
56 'class' => 'button',
57 'label' => ts('Pay Now'),
58 'url' => CRM_Utils_System::url('civicrm/contribute/transact', [
59 'reset' => 1,
60 'id' => Civi::settings()->get('default_invoice_page'),
61 'ccid' => $row['contribution_id'],
62 'cs' => $this->getUserChecksum(),
63 'cid' => $row['contact_id'],
64 ]),
65 ];
66 }
67 }
68
69 $this->assign('contribute_rows', $rows);
70 $this->assign('contributionSummary', ['total_amount' => civicrm_api3('Contribution', 'getcount', ['contact_id' => $this->_contactId])]);
71
72 //add honor block
73 $params = CRM_Contribute_BAO_Contribution::getHonorContacts($this->_contactId);
74
75 if (!empty($params)) {
76 // assign vars to templates
77 $this->assign('honorRows', $params);
78 $this->assign('honor', TRUE);
79 }
80
81 $recur = new CRM_Contribute_DAO_ContributionRecur();
82 $recur->contact_id = $this->_contactId;
83 $recur->is_test = 0;
84 $recur->find();
85
86 $recurStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label');
87
88 $recurRow = [];
89 $recurIDs = [];
90 while ($recur->fetch()) {
91 if (empty($recur->payment_processor_id)) {
92 // it's not clear why we continue here as any without a processor id would likely
93 // be imported from another system & still seem valid.
94 continue;
95 }
96
97 require_once 'api/v3/utils.php';
98 //@todo calling api functions directly is not supported
99 _civicrm_api3_object_to_array($recur, $values);
100
101 $values['recur_status'] = $recurStatus[$values['contribution_status_id']];
102 $recurRow[$values['id']] = $values;
103
104 $action = array_sum(array_keys(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $recur->contact_id)));
105
106 $details = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recur->id, 'recur');
107 $hideUpdate = $details->membership_id & $details->auto_renew;
108
109 if ($hideUpdate) {
110 $action -= CRM_Core_Action::UPDATE;
111 }
112
113 $recurRow[$values['id']]['action'] = CRM_Core_Action::formLink(CRM_Contribute_Page_Tab::dashboardRecurLinks((int) $recur->id, (int) $this->_contactId),
114 $action, [
115 'cid' => $this->_contactId,
116 'crid' => $values['id'],
117 'cxt' => 'contribution',
118 ],
119 ts('more'),
120 FALSE,
121 'contribution.dashboard.recurring',
122 'Contribution',
123 $values['id']
124 );
125
126 $recurIDs[] = $values['id'];
127 }
128 if (is_array($recurIDs) && !empty($recurIDs)) {
129 $getCount = CRM_Contribute_BAO_ContributionRecur::getCount($recurIDs);
130 foreach ($getCount as $key => $val) {
131 $recurRow[$key]['completed'] = $val;
132 $recurRow[$key]['link'] = CRM_Utils_System::url('civicrm/contribute/search',
133 "reset=1&force=1&recur=$key"
134 );
135 }
136 }
137
138 $this->assign('recurRows', $recurRow);
139 if (!empty($recurRow)) {
140 $this->assign('recur', TRUE);
141 }
142 else {
143 $this->assign('recur', FALSE);
144 }
145 }
146
147 /**
148 * Should invoice links be displayed on the template.
149 *
150 * @todo This should be moved to a hook-like structure on the invoicing class
151 * (currently CRM_Utils_Invoicing) with a view to possible removal from core.
152 */
153 public function isIncludeInvoiceLinks() {
154 if (!CRM_Invoicing_Utils::isInvoicingEnabled()) {
155 return FALSE;
156 }
157 $dashboardOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
158 'user_dashboard_options'
159 );
160 return $dashboardOptions['Invoices / Credit Notes'];
161 }
162
163 /**
164 * the main function that is called when the page
165 * loads, it decides the which action has to be taken for the page.
166 */
167 public function run() {
168 $this->assign('isIncludeInvoiceLinks', $this->isIncludeInvoiceLinks());
169 parent::preProcess();
170 $this->listContribution();
171 }
172
173 }