Merge pull request #4686 from lcdservices/CRM-15698-b
[civicrm-core.git] / CRM / Member / Page / DashBoard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * Page for displaying list of Payment-Instrument
38 */
39class CRM_Member_Page_DashBoard extends CRM_Core_Page {
40
41 /**
42 * Heart of the viewing process. The runner gets all the meta data for
43 * the contact and calls the appropriate type of page to view.
44 *
45 * @return void
46 * @access public
47 *
48 */
49 function preProcess() {
46c91013
E
50
51 //CRM-13901 don't show dashboard to contacts with limited view writes & it does not relect
52 //what they have access to
53 //@todo implement acls on dashboard querys (preferably via api to enhance that at the same time)
54 if(!CRM_Core_Permission::check(array('view all contacts', 'edit all contacts'))) {
55 $this->showMembershipSummary = FALSE;
56 $this->assign('membershipSummary', FALSE);
57 return;
58 }
59 $this->assign('membershipSummary', TRUE);
6a488035
TO
60 CRM_Utils_System::setTitle(ts('CiviMember'));
61 $membershipSummary = array();
4e636a74
AH
62 $preMonth = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
63 $preMonthEnd = date("Y-m-t", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
6a488035
TO
64
65 $preMonthYear = mktime(0, 0, 0, substr($preMonth, 4, 2), 1, substr($preMonth, 0, 4));
66
4e636a74
AH
67 $today = getdate();
68 $date = CRM_Utils_Date::getToday();
6a488035
TO
69 $isCurrentMonth = 0;
70
4e636a74 71 // You can force the dashboard to display based upon a certain date
6a488035 72 $ym = CRM_Utils_Array::value('date', $_GET);
8ef12e64 73
6a488035
TO
74 if ($ym) {
75 if (preg_match('/^\d{6}$/', $ym) == 0 ||
76 !checkdate(substr($ym, 4, 2), 1, substr($ym, 0, 4)) ||
77 substr($ym, 0, 1) == 0
78 ) {
79 CRM_Core_Error::fatal(ts('Invalid date query "%1" in URL (valid syntax is yyyymm).', array(1 => $ym)));
80 }
81
82 $isPreviousMonth = 0;
4e636a74
AH
83 $isCurrentMonth = substr($ym, 0, 4) == $today['year'] && substr($ym, 4, 2) == $today['mon'];
84 $ymd = date('Y-m-d', mktime(0, 0, -1, substr($ym, 4, 2) + 1, 1, substr($ym, 0, 4)));
85 $monthStartTs = mktime(0, 0, 0, substr($ym, 4, 2), 1, substr($ym, 0, 4));
86 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
87 $ym = substr($ym, 0, 4) . '-' . substr($ym, 4, 2);
6a488035
TO
88 }
89 else {
4e636a74
AH
90 $ym = sprintf("%04d-%02d", $today['year'], $today['mon']);
91 $ymd = sprintf("%04d-%02d-%02d", $today['year'], $today['mon'], $today['mday']);
92 $monthStartTs = mktime(0, 0, 0, $today['mon'], 1, $today['year']);
93 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
94 $isCurrentMonth = 1;
6a488035
TO
95 $isPreviousMonth = 1;
96 }
4e636a74
AH
97 $monthStart = $ym . '-01';
98 $yearStart = substr($ym, 0, 4) . '-01-01';
6a488035
TO
99
100 $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
101 // added
102 //$membership = new CRM_Member_BAO_Membership;
103
104 foreach ($membershipTypes as $key => $value) {
105
106 $membershipSummary[$key]['premonth']['new'] = array('count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $preMonth, $preMonthEnd),
107 'name' => $value,
108 );
109
110 $membershipSummary[$key]['premonth']['renew'] = array('count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $preMonth, $preMonthEnd),
111 'name' => $value,
112 );
113
114 $membershipSummary[$key]['premonth']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd),
115 'name' => $value,
116 );
117
118
119 $membershipSummary[$key]['month']['new'] = array('count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $monthStart, $ymd),
120 'name' => $value,
121 );
122
123 $membershipSummary[$key]['month']['renew'] = array('count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $monthStart, $ymd),
124 'name' => $value,
125 );
126
127 $membershipSummary[$key]['month']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd),
128 'name' => $value,
129 );
130
131
132 $membershipSummary[$key]['year']['new'] = array('count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $yearStart, $ymd),
133 'name' => $value,
134 );
135
136 $membershipSummary[$key]['year']['renew'] = array('count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $yearStart, $ymd),
137 'name' => $value,
138 );
139
140 $membershipSummary[$key]['year']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd),
141 'name' => $value,
142 );
143
144
145 $membershipSummary[$key]['current']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current),
146 'name' => $value,
147 );
148
149 $membershipSummary[$key]['total']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd));
150
151 //LCD also get summary stats for membership owners
152 $membershipSummary[$key]['premonth_owner']['premonth_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd, 0, 1),
153 'name' => $value,
154 );
155
156 $membershipSummary[$key]['month_owner']['month_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd, 0, 1),
157 'name' => $value,
158 );
159
160 $membershipSummary[$key]['year_owner']['year_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd, 0, 1),
161 'name' => $value,
162 );
163
164 $membershipSummary[$key]['current_owner']['current_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current, 0, 1),
165 'name' => $value,
166 );
167
168 $membershipSummary[$key]['total_owner']['total_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd, 0, 1));
169 //LCD end
170 }
171
6a488035
TO
172 $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
173 $status = implode(',', $status);
174
4e636a74 175 /* Disabled for lack of appropriate search
8ef12e64 176
4e636a74
AH
177 The Membership search isn't able to properly filter by join or renewal events.
178 Until that works properly, the subtotals shouldn't get links.
8ef12e64 179
6a488035
TO
180 foreach ($membershipSummary as $typeID => $details) {
181 foreach ($details as $key => $value) {
182 switch ($key) {
183 case 'premonth':
184 $membershipSummary[$typeID][$key]['new']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&join=$preMonth&joinEnd=$preMonthEnd&start=$preMonth&end=$preMonthEnd");
185 $membershipSummary[$typeID][$key]['renew']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&joinEnd=$prePreMonthEnd&start=$preMonth&end=$preMonthEnd");
186 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$preMonth&end=$preMonthEnd");
187 break;
188
189 case 'month':
190 $membershipSummary[$typeID][$key]['new']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&join=$monthStart&joinEnd=$ymd&start=$monthStart&end=$ymd");
191 $membershipSummary[$typeID][$key]['renew']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&joinEnd=$preMonthStart&start=$monthStart&end=$ymd");
192 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$monthStart&end=$ymd");
193 break;
194
195 case 'year':
196 $membershipSummary[$typeID][$key]['new']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&join=$yearStart&joinEnd=$ymd&start=$yearStart&end=$ymd");
197 $membershipSummary[$typeID][$key]['renew']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&joinEnd=$preYearStart&start=$yearStart&end=$ymd");
198 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$yearStart&end=$ymd");
199 break;
200
201 case 'current':
202 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID");
203 break;
204
205 case 'total':
206 if (!$isCurrentMonth) {
207 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
208 "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID"
209 );
210 }
211 else {
212 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
213 "reset=1&force=1&status=$status"
214 );
215 }
216 break;
217
218 //LCD add owner urls
219
220 case 'premonth_owner':
221 $membershipSummary[$typeID][$key]['premonth_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$preMonth&end=$preMonthEnd&owner=1");
222 break;
223
224 case 'month_owner':
225 $membershipSummary[$typeID][$key]['month_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$monthStart&end=$ymd&owner=1");
226 break;
227
228 case 'year_owner':
229 $membershipSummary[$typeID][$key]['year_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$yearStart&end=$ymd&owner=1");
230 break;
231
232 case 'current_owner':
233 $membershipSummary[$typeID][$key]['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&owner=1");
234 break;
235
236 case 'total_owner':
237 if (!$isCurrentMonth) {
238 $membershipSummary[$typeID][$key]['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID&owner=1");
239 }
240 else {
241 $membershipSummary[$typeID][$key]['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1");
242 }
243 break;
244 //LCD end
245 }
246 }
247 }
4e636a74 248 */
8ef12e64 249
4e636a74 250 // Temporary replacement for current totals column
8ef12e64 251
4e636a74
AH
252 foreach ($membershipSummary as $typeID => $details) {
253 if (!$isCurrentMonth) {
254 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
255 "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID"
256 );
257 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID&owner=1");
258 }
259 else {
260 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
261 "reset=1&force=1&status=$status"
262 );
263 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1");
264 }
265 $membershipSummary[$typeID]['current']['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID");
266 $membershipSummary[$typeID]['current_owner']['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&owner=1");
267 }
6a488035
TO
268
269 $totalCount = array();
270
271 $newCountPreMonth = $newCountMonth = $newCountYear = 0;
272 $renewCountPreMonth = $renewCountMonth = $renewCountYear = 0;
273
274 $totalCountPreMonth = $totalCountMonth = $totalCountYear = $totalCountCurrent = $totalCountTotal = 0;
275 $totalCountPreMonth_owner = $totalCountMonth_owner = $totalCountYear_owner = $totalCountCurrent_owner = $totalCountTotal_owner = 0;
276 foreach ($membershipSummary as $key => $value) {
277 $newCountPreMonth = $newCountPreMonth + $value['premonth']['new']['count'];
278 $renewCountPreMonth = $renewCountPreMonth + $value['premonth']['renew']['count'];
279 $totalCountPreMonth = $totalCountPreMonth + $value['premonth']['total']['count'];
280 $newCountMonth = $newCountMonth + $value['month']['new']['count'];
281 $renewCountMonth = $renewCountMonth + $value['month']['renew']['count'];
282 $totalCountMonth = $totalCountMonth + $value['month']['total']['count'];
283 $newCountYear = $newCountYear + $value['year']['new']['count'];
284 $renewCountYear = $renewCountYear + $value['year']['renew']['count'];
285 $totalCountYear = $totalCountYear + $value['year']['total']['count'];
286 $totalCountCurrent = $totalCountCurrent + $value['current']['total']['count'];
287 $totalCountTotal = $totalCountTotal + $value['total']['total']['count'];
288
289 //LCD add owner values
290 $totalCountPreMonth_owner = $totalCountPreMonth_owner + $value['premonth_owner']['premonth_owner']['count'];
291 $totalCountMonth_owner = $totalCountMonth_owner + $value['month_owner']['month_owner']['count'];
292 $totalCountYear_owner = $totalCountYear_owner + $value['year_owner']['year_owner']['count'];
293 $totalCountCurrent_owner = $totalCountCurrent_owner + $value['current_owner']['current_owner']['count'];
294 $totalCountTotal_owner = $totalCountTotal_owner + $value['total_owner']['total_owner']['count'];
295 }
296
297 $totalCount['premonth']['new'] = array(
298 'count' => $newCountPreMonth,
0370ad20
AH
299 //'url' => CRM_Utils_System::url('civicrm/activity/search',
300 // "reset=1&force=1&signupType=1&dateLow=$preMonth&dateHigh=$preMonthEnd"
301 //),
6a488035
TO
302 );
303
304 $totalCount['premonth']['renew'] = array(
305 'count' => $renewCountPreMonth,
0370ad20
AH
306 //'url' => CRM_Utils_System::url('civicrm/activity/search',
307 // "reset=1&force=1&signupType=2&dateLow=$preMonth&dateHigh=$preMonthEnd"
308 //),
6a488035
TO
309 );
310
311 $totalCount['premonth']['total'] = array(
312 'count' => $totalCountPreMonth,
0370ad20
AH
313 //'url' => CRM_Utils_System::url('civicrm/activity/search',
314 // "reset=1&force=1&signupType=3&dateLow=$preMonth&dateHigh=$preMonthEnd"
315 //),
6a488035
TO
316 );
317
318 $totalCount['month']['new'] = array(
319 'count' => $newCountMonth,
0370ad20
AH
320 //'url' => CRM_Utils_System::url('civicrm/activity/search',
321 // "reset=1&force=1&signupType=1&dateLow=$monthStart&dateHigh=$ymd"
322 //),
6a488035
TO
323 );
324
325 $totalCount['month']['renew'] = array(
326 'count' => $renewCountMonth,
0370ad20
AH
327 //'url' => CRM_Utils_System::url('civicrm/activity/search',
328 // "reset=1&force=1&signupType=2&dateLow=$monthStart&dateHigh=$ymd"
329 //),
6a488035
TO
330 );
331
332 $totalCount['month']['total'] = array(
333 'count' => $totalCountMonth,
0370ad20
AH
334 //'url' => CRM_Utils_System::url('civicrm/activity/search',
335 // "reset=1&force=1&signupType=3&dateLow=$monthStart&dateHigh=$ymd"
336 //),
6a488035
TO
337 );
338
339 $totalCount['year']['new'] = array(
340 'count' => $newCountYear,
0370ad20
AH
341 //'url' => CRM_Utils_System::url('civicrm/activity/search',
342 // "reset=1&force=1&signupType=1&dateLow=$yearStart&dateHigh=$ymd"
343 //),
6a488035
TO
344 );
345
346 $totalCount['year']['renew'] = array(
347 'count' => $renewCountYear,
0370ad20
AH
348 //'url' => CRM_Utils_System::url('civicrm/activity/search',
349 // "reset=1&force=1&signupType=2&dateLow=$yearStart&dateHigh=$ymd"
350 //),
6a488035
TO
351 );
352
353 $totalCount['year']['total'] = array(
354 'count' => $totalCountYear,
0370ad20
AH
355 //'url' => CRM_Utils_System::url('civicrm/activity/search',
356 // "reset=1&force=1&signupType=3&dateLow=$yearStart&dateHigh=$ymd"
357 //),
6a488035
TO
358 );
359
360 $totalCount['current']['total'] = array(
361 'count' => $totalCountCurrent,
362 'url' => CRM_Utils_System::url('civicrm/member/search',
363 "reset=1&force=1&status=$status"
364 ),
365 );
366
367 $totalCount['total']['total'] = array(
368 'count' => $totalCountTotal,
369 'url' => CRM_Utils_System::url('civicrm/member/search',
370 "reset=1&force=1&status=$status"
371 ),
372 );
373
374 if (!$isCurrentMonth) {
375 $totalCount['total']['total'] = array(
376 'count' => $totalCountTotal,
377 'url' => CRM_Utils_System::url('civicrm/member/search',
378 "reset=1&force=1&status=$status&start=&end=$ymd"
379 ),
380 );
381 }
382
4e636a74
AH
383 // Activity search also unable to handle owner vs. inherited
384
6a488035
TO
385 //LCD add owner values
386 $totalCount['premonth_owner']['premonth_owner'] = array(
387 'count' => $totalCountPreMonth_owner,
0370ad20 388 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$preMonth&end=$preMonthEnd&owner=1"),
6a488035
TO
389 );
390
391 $totalCount['month_owner']['month_owner'] = array(
392 'count' => $totalCountMonth_owner,
4e636a74 393 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$monthStart&end=$ymd&owner=1"),
6a488035
TO
394 );
395
396 $totalCount['year_owner']['year_owner'] = array(
397 'count' => $totalCountYear_owner,
4e636a74 398 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$yearStart&end=$ymd&owner=1"),
6a488035
TO
399 );
400
401 $totalCount['current_owner']['current_owner'] = array(
402 'count' => $totalCountCurrent_owner,
4e636a74 403 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
6a488035
TO
404 );
405
406 $totalCount['total_owner']['total_owner'] = array(
407 'count' => $totalCountTotal_owner,
4e636a74 408 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
6a488035
TO
409 );
410
411 if (!$isCurrentMonth) {
412 $totalCount['total_owner']['total_owner'] = array(
413 'count' => $totalCountTotal_owner,
4e636a74 414 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=&end=$ymd&owner=1"),
6a488035
TO
415 );
416 }
417 //LCD end
418
419 $this->assign('membershipSummary', $membershipSummary);
420 $this->assign('totalCount', $totalCount);
421 $this->assign('month', date('F', $monthStartTs));
422 $this->assign('year', date('Y', $monthStartTs));
0370ad20 423 $this->assign('premonth', date('F', strtotime($preMonth)));
6a488035
TO
424 $this->assign('currentMonth', date('F'));
425 $this->assign('currentYear', date('Y'));
426 $this->assign('isCurrent', $isCurrentMonth);
427 $this->assign('preMonth', $isPreviousMonth);
428 }
429
430 /**
431 * This function is the main function that is called when the page loads,
432 * it decides the which action has to be taken for the page.
433 *
434 * return null
435 * @access public
436 */
437 function run() {
438 $this->preProcess();
439
440 $controller = new CRM_Core_Controller_Simple('CRM_Member_Form_Search', ts('Member'), NULL);
441 $controller->setEmbedded(TRUE);
442 $controller->reset();
443 $controller->set('limit', 20);
444 $controller->set('force', 1);
445 $controller->set('context', 'dashboard');
446 $controller->process();
447 $controller->run();
448
449 return parent::run();
450 }
451}
452