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