Convert remaining Authorize.net test to use guzzle
[civicrm-core.git] / CRM / Member / Page / DashBoard.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 * $Id$
17 *
18 */
19
20 /**
21 * Page for displaying list of Payment-Instrument
22 */
23 class CRM_Member_Page_DashBoard extends CRM_Core_Page {
24
25 /**
26 * Heart of the viewing process. The runner gets all the meta data for
27 * the contact and calls the appropriate type of page to view.
28 *
29 * @return void
30 */
31 public function preProcess() {
32
33 //CRM-13901 don't show dashboard to contacts with limited view writes & it does not relect
34 //what they have access to
35 //@todo implement acls on dashboard querys (preferably via api to enhance that at the same time)
36 if (!CRM_Core_Permission::check('view all contacts') && !CRM_Core_Permission::check('edit all contacts')) {
37 $this->showMembershipSummary = FALSE;
38 $this->assign('membershipSummary', FALSE);
39 return;
40 }
41 $this->assign('membershipSummary', TRUE);
42 CRM_Utils_System::setTitle(ts('CiviMember'));
43 $membershipSummary = array();
44 $preMonth = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
45 $preMonthEnd = date("Y-m-t", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
46
47 $preMonthYear = mktime(0, 0, 0, substr($preMonth, 4, 2), 1, substr($preMonth, 0, 4));
48
49 $today = getdate();
50 $date = CRM_Utils_Date::getToday();
51 $isCurrentMonth = 0;
52
53 // You can force the dashboard to display based upon a certain date
54 $ym = $_GET['date'] ?? NULL;
55
56 if ($ym) {
57 if (preg_match('/^\d{6}$/', $ym) == 0 ||
58 !checkdate(substr($ym, 4, 2), 1, substr($ym, 0, 4)) ||
59 substr($ym, 0, 1) == 0
60 ) {
61 CRM_Core_Error::fatal(ts('Invalid date query "%1" in URL (valid syntax is yyyymm).', array(1 => $ym)));
62 }
63
64 $isPreviousMonth = 0;
65 $isCurrentMonth = substr($ym, 0, 4) == $today['year'] && substr($ym, 4, 2) == $today['mon'];
66 $ymd = date('Y-m-d', mktime(0, 0, -1, substr($ym, 4, 2) + 1, 1, substr($ym, 0, 4)));
67 $monthStartTs = mktime(0, 0, 0, substr($ym, 4, 2), 1, substr($ym, 0, 4));
68 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
69 $ym = substr($ym, 0, 4) . '-' . substr($ym, 4, 2);
70 }
71 else {
72 $ym = sprintf("%04d-%02d", $today['year'], $today['mon']);
73 $ymd = sprintf("%04d-%02d-%02d", $today['year'], $today['mon'], $today['mday']);
74 $monthStartTs = mktime(0, 0, 0, $today['mon'], 1, $today['year']);
75 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
76 $isCurrentMonth = 1;
77 $isPreviousMonth = 1;
78 }
79 $monthStart = $ym . '-01';
80 $yearStart = substr($ym, 0, 4) . '-01-01';
81
82 $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
83 // added
84 //$membership = new CRM_Member_BAO_Membership;
85
86 foreach ($membershipTypes as $key => $value) {
87
88 $membershipSummary[$key]['premonth']['new'] = array(
89 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $preMonth, $preMonthEnd),
90 'name' => $value,
91 );
92
93 $membershipSummary[$key]['premonth']['renew'] = array(
94 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $preMonth, $preMonthEnd),
95 'name' => $value,
96 );
97
98 $membershipSummary[$key]['premonth']['total'] = array(
99 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd),
100 'name' => $value,
101 );
102
103 $membershipSummary[$key]['month']['new'] = array(
104 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $monthStart, $ymd),
105 'name' => $value,
106 );
107
108 $membershipSummary[$key]['month']['renew'] = array(
109 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $monthStart, $ymd),
110 'name' => $value,
111 );
112
113 $membershipSummary[$key]['month']['total'] = array(
114 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd),
115 'name' => $value,
116 );
117
118 $membershipSummary[$key]['year']['new'] = array(
119 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $yearStart, $ymd),
120 'name' => $value,
121 );
122
123 $membershipSummary[$key]['year']['renew'] = array(
124 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $yearStart, $ymd),
125 'name' => $value,
126 );
127
128 $membershipSummary[$key]['year']['total'] = array(
129 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd),
130 'name' => $value,
131 );
132
133 $membershipSummary[$key]['current']['total'] = array(
134 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current),
135 'name' => $value,
136 );
137
138 $membershipSummary[$key]['total']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd));
139
140 //LCD also get summary stats for membership owners
141 $membershipSummary[$key]['premonth_owner']['premonth_owner'] = array(
142 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd, 0, 1),
143 'name' => $value,
144 );
145
146 $membershipSummary[$key]['month_owner']['month_owner'] = array(
147 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd, 0, 1),
148 'name' => $value,
149 );
150
151 $membershipSummary[$key]['year_owner']['year_owner'] = array(
152 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd, 0, 1),
153 'name' => $value,
154 );
155
156 $membershipSummary[$key]['current_owner']['current_owner'] = array(
157 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current, 0, 1),
158 'name' => $value,
159 );
160
161 $membershipSummary[$key]['total_owner']['total_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd, 0, 1));
162 //LCD end
163 }
164
165 $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
166 $status = implode(',', $status);
167
168 foreach ($membershipSummary as $typeID => $details) {
169 if (!$isCurrentMonth) {
170 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
171 "reset=1&force=1&start=&end=$ymd&membership_status_id=$status&membership_type_id=$typeID"
172 );
173 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&start=&end=$ymd&membership_status_id=$status&membership_type_id=$typeID&owner=1");
174 }
175 else {
176 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
177 "reset=1&force=1&membership_status_id=$status"
178 );
179 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1");
180 }
181 $membershipSummary[$typeID]['current']['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&membership_type_id=$typeID");
182 $membershipSummary[$typeID]['current_owner']['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&membership_type_id=$typeID&owner=1");
183 }
184
185 $totalCount = [];
186
187 $newCountPreMonth = $newCountMonth = $newCountYear = 0;
188 $renewCountPreMonth = $renewCountMonth = $renewCountYear = 0;
189
190 $totalCountPreMonth = $totalCountMonth = $totalCountYear = $totalCountCurrent = $totalCountTotal = 0;
191 $totalCountPreMonth_owner = $totalCountMonth_owner = $totalCountYear_owner = $totalCountCurrent_owner = $totalCountTotal_owner = 0;
192 foreach ($membershipSummary as $key => $value) {
193 $newCountPreMonth = $newCountPreMonth + $value['premonth']['new']['count'];
194 $renewCountPreMonth = $renewCountPreMonth + $value['premonth']['renew']['count'];
195 $totalCountPreMonth = $totalCountPreMonth + $value['premonth']['total']['count'];
196 $newCountMonth = $newCountMonth + $value['month']['new']['count'];
197 $renewCountMonth = $renewCountMonth + $value['month']['renew']['count'];
198 $totalCountMonth = $totalCountMonth + $value['month']['total']['count'];
199 $newCountYear = $newCountYear + $value['year']['new']['count'];
200 $renewCountYear = $renewCountYear + $value['year']['renew']['count'];
201 $totalCountYear = $totalCountYear + $value['year']['total']['count'];
202 $totalCountCurrent = $totalCountCurrent + $value['current']['total']['count'];
203 $totalCountTotal = $totalCountTotal + $value['total']['total']['count'];
204
205 //LCD add owner values
206 $totalCountPreMonth_owner = $totalCountPreMonth_owner + $value['premonth_owner']['premonth_owner']['count'];
207 $totalCountMonth_owner = $totalCountMonth_owner + $value['month_owner']['month_owner']['count'];
208 $totalCountYear_owner = $totalCountYear_owner + $value['year_owner']['year_owner']['count'];
209 $totalCountCurrent_owner = $totalCountCurrent_owner + $value['current_owner']['current_owner']['count'];
210 $totalCountTotal_owner = $totalCountTotal_owner + $value['total_owner']['total_owner']['count'];
211 }
212
213 $totalCount['premonth']['new'] = array(
214 'count' => $newCountPreMonth,
215 );
216
217 $totalCount['premonth']['renew'] = array(
218 'count' => $renewCountPreMonth,
219 );
220
221 $totalCount['premonth']['total'] = array(
222 'count' => $totalCountPreMonth,
223 );
224
225 $totalCount['month']['new'] = array(
226 'count' => $newCountMonth,
227 );
228
229 $totalCount['month']['renew'] = array(
230 'count' => $renewCountMonth,
231 );
232
233 $totalCount['month']['total'] = array(
234 'count' => $totalCountMonth,
235 );
236
237 $totalCount['year']['new'] = array(
238 'count' => $newCountYear,
239 );
240
241 $totalCount['year']['renew'] = array(
242 'count' => $renewCountYear,
243 );
244
245 $totalCount['year']['total'] = array(
246 'count' => $totalCountYear,
247 );
248
249 $totalCount['current']['total'] = array(
250 'count' => $totalCountCurrent,
251 'url' => CRM_Utils_System::url('civicrm/member/search',
252 "reset=1&force=1&membership_status_id=$status"
253 ),
254 );
255
256 $totalCount['total']['total'] = array(
257 'count' => $totalCountTotal,
258 'url' => CRM_Utils_System::url('civicrm/member/search',
259 "reset=1&force=1&membership_status_id=$status"
260 ),
261 );
262
263 if (!$isCurrentMonth) {
264 $totalCount['total']['total'] = array(
265 'count' => $totalCountTotal,
266 'url' => CRM_Utils_System::url('civicrm/member/search',
267 "reset=1&force=1&membership_status_id=$status&start=&end=$ymd"
268 ),
269 );
270 }
271
272 // Activity search also unable to handle owner vs. inherited
273
274 //LCD add owner values
275 $totalCount['premonth_owner']['premonth_owner'] = array(
276 'count' => $totalCountPreMonth_owner,
277 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$preMonth&end=$preMonthEnd&owner=1"),
278 );
279
280 $totalCount['month_owner']['month_owner'] = array(
281 'count' => $totalCountMonth_owner,
282 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$monthStart&end=$ymd&owner=1"),
283 );
284
285 $totalCount['year_owner']['year_owner'] = array(
286 'count' => $totalCountYear_owner,
287 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$yearStart&end=$ymd&owner=1"),
288 );
289
290 $totalCount['current_owner']['current_owner'] = array(
291 'count' => $totalCountCurrent_owner,
292 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1"),
293 );
294
295 $totalCount['total_owner']['total_owner'] = array(
296 'count' => $totalCountTotal_owner,
297 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1"),
298 );
299
300 if (!$isCurrentMonth) {
301 $totalCount['total_owner']['total_owner'] = array(
302 'count' => $totalCountTotal_owner,
303 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=&end=$ymd&owner=1"),
304 );
305 }
306 //LCD end
307
308 $this->assign('membershipSummary', $membershipSummary);
309 $this->assign('totalCount', $totalCount);
310 $this->assign('month', CRM_Utils_Date::customFormatTs($monthStartTs, '%B'));
311 $this->assign('year', date('Y', $monthStartTs));
312 $this->assign('premonth', CRM_Utils_Date::customFormat($preMonth, '%B'));
313 $this->assign('currentMonth', date('F'));
314 $this->assign('currentYear', date('Y'));
315 $this->assign('isCurrent', $isCurrentMonth);
316 $this->assign('preMonth', $isPreviousMonth);
317 }
318
319 /**
320 * the main function that is called when the page loads,
321 * it decides the which action has to be taken for the page.
322 *
323 * @return null
324 */
325 public function run() {
326 $this->preProcess();
327
328 $controller = new CRM_Core_Controller_Simple('CRM_Member_Form_Search', ts('Member'), NULL);
329 $controller->setEmbedded(TRUE);
330 $controller->reset();
331 $controller->set('limit', 20);
332 $controller->set('force', 1);
333 $controller->set('context', 'dashboard');
334 $controller->process();
335 $controller->run();
336
337 return parent::run();
338 }
339
340 }