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