Merge pull request #2845 from elcapo/activity-contact-api
[civicrm-core.git] / CRM / Member / Page / DashBoard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @access public
47 *
48 */
49 function preProcess() {
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);
60 CRM_Utils_System::setTitle(ts('CiviMember'));
61 $membershipSummary = array();
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")));
64
65 $preMonthYear = mktime(0, 0, 0, substr($preMonth, 4, 2), 1, substr($preMonth, 0, 4));
66
67 $today = getdate();
68 $date = CRM_Utils_Date::getToday();
69 $isCurrentMonth = 0;
70
71 // You can force the dashboard to display based upon a certain date
72 $ym = CRM_Utils_Array::value('date', $_GET);
73
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;
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);
88 }
89 else {
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;
95 $isPreviousMonth = 1;
96 }
97 $monthStart = $ym . '-01';
98 $yearStart = substr($ym, 0, 4) . '-01-01';
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
172 // LCD debug
173 //CRM_Core_Error::debug($membershipSummary);
174 $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
175 $status = implode(',', $status);
176
177 /* Disabled for lack of appropriate search
178
179 The Membership search isn't able to properly filter by join or renewal events.
180 Until that works properly, the subtotals shouldn't get links.
181
182 foreach ($membershipSummary as $typeID => $details) {
183 foreach ($details as $key => $value) {
184 switch ($key) {
185 case 'premonth':
186 $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");
187 $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");
188 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$preMonth&end=$preMonthEnd");
189 break;
190
191 case 'month':
192 $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");
193 $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");
194 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$monthStart&end=$ymd");
195 break;
196
197 case 'year':
198 $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");
199 $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");
200 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&start=$yearStart&end=$ymd");
201 break;
202
203 case 'current':
204 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID");
205 break;
206
207 case 'total':
208 if (!$isCurrentMonth) {
209 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
210 "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID"
211 );
212 }
213 else {
214 $membershipSummary[$typeID][$key]['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
215 "reset=1&force=1&status=$status"
216 );
217 }
218 break;
219
220 //LCD add owner urls
221
222 case 'premonth_owner':
223 $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");
224 break;
225
226 case 'month_owner':
227 $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");
228 break;
229
230 case 'year_owner':
231 $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");
232 break;
233
234 case 'current_owner':
235 $membershipSummary[$typeID][$key]['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&owner=1");
236 break;
237
238 case 'total_owner':
239 if (!$isCurrentMonth) {
240 $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");
241 }
242 else {
243 $membershipSummary[$typeID][$key]['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1");
244 }
245 break;
246 //LCD end
247 }
248 }
249 }
250 */
251
252 // Temporary replacement for current totals column
253
254 foreach ($membershipSummary as $typeID => $details) {
255 if (!$isCurrentMonth) {
256 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
257 "reset=1&force=1&start=&end=$ymd&status=$status&type=$typeID"
258 );
259 $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");
260 }
261 else {
262 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
263 "reset=1&force=1&status=$status"
264 );
265 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1");
266 }
267 $membershipSummary[$typeID]['current']['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID");
268 $membershipSummary[$typeID]['current_owner']['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&owner=1");
269 }
270
271 $totalCount = array();
272
273 $newCountPreMonth = $newCountMonth = $newCountYear = 0;
274 $renewCountPreMonth = $renewCountMonth = $renewCountYear = 0;
275
276 $totalCountPreMonth = $totalCountMonth = $totalCountYear = $totalCountCurrent = $totalCountTotal = 0;
277 $totalCountPreMonth_owner = $totalCountMonth_owner = $totalCountYear_owner = $totalCountCurrent_owner = $totalCountTotal_owner = 0;
278 foreach ($membershipSummary as $key => $value) {
279 $newCountPreMonth = $newCountPreMonth + $value['premonth']['new']['count'];
280 $renewCountPreMonth = $renewCountPreMonth + $value['premonth']['renew']['count'];
281 $totalCountPreMonth = $totalCountPreMonth + $value['premonth']['total']['count'];
282 $newCountMonth = $newCountMonth + $value['month']['new']['count'];
283 $renewCountMonth = $renewCountMonth + $value['month']['renew']['count'];
284 $totalCountMonth = $totalCountMonth + $value['month']['total']['count'];
285 $newCountYear = $newCountYear + $value['year']['new']['count'];
286 $renewCountYear = $renewCountYear + $value['year']['renew']['count'];
287 $totalCountYear = $totalCountYear + $value['year']['total']['count'];
288 $totalCountCurrent = $totalCountCurrent + $value['current']['total']['count'];
289 $totalCountTotal = $totalCountTotal + $value['total']['total']['count'];
290
291 //LCD add owner values
292 $totalCountPreMonth_owner = $totalCountPreMonth_owner + $value['premonth_owner']['premonth_owner']['count'];
293 $totalCountMonth_owner = $totalCountMonth_owner + $value['month_owner']['month_owner']['count'];
294 $totalCountYear_owner = $totalCountYear_owner + $value['year_owner']['year_owner']['count'];
295 $totalCountCurrent_owner = $totalCountCurrent_owner + $value['current_owner']['current_owner']['count'];
296 $totalCountTotal_owner = $totalCountTotal_owner + $value['total_owner']['total_owner']['count'];
297 }
298
299 $totalCount['premonth']['new'] = array(
300 'count' => $newCountPreMonth,
301 //'url' => CRM_Utils_System::url('civicrm/activity/search',
302 // "reset=1&force=1&signupType=1&dateLow=$preMonth&dateHigh=$preMonthEnd"
303 //),
304 );
305
306 $totalCount['premonth']['renew'] = array(
307 'count' => $renewCountPreMonth,
308 //'url' => CRM_Utils_System::url('civicrm/activity/search',
309 // "reset=1&force=1&signupType=2&dateLow=$preMonth&dateHigh=$preMonthEnd"
310 //),
311 );
312
313 $totalCount['premonth']['total'] = array(
314 'count' => $totalCountPreMonth,
315 //'url' => CRM_Utils_System::url('civicrm/activity/search',
316 // "reset=1&force=1&signupType=3&dateLow=$preMonth&dateHigh=$preMonthEnd"
317 //),
318 );
319
320 $totalCount['month']['new'] = array(
321 'count' => $newCountMonth,
322 //'url' => CRM_Utils_System::url('civicrm/activity/search',
323 // "reset=1&force=1&signupType=1&dateLow=$monthStart&dateHigh=$ymd"
324 //),
325 );
326
327 $totalCount['month']['renew'] = array(
328 'count' => $renewCountMonth,
329 //'url' => CRM_Utils_System::url('civicrm/activity/search',
330 // "reset=1&force=1&signupType=2&dateLow=$monthStart&dateHigh=$ymd"
331 //),
332 );
333
334 $totalCount['month']['total'] = array(
335 'count' => $totalCountMonth,
336 //'url' => CRM_Utils_System::url('civicrm/activity/search',
337 // "reset=1&force=1&signupType=3&dateLow=$monthStart&dateHigh=$ymd"
338 //),
339 );
340
341 $totalCount['year']['new'] = array(
342 'count' => $newCountYear,
343 //'url' => CRM_Utils_System::url('civicrm/activity/search',
344 // "reset=1&force=1&signupType=1&dateLow=$yearStart&dateHigh=$ymd"
345 //),
346 );
347
348 $totalCount['year']['renew'] = array(
349 'count' => $renewCountYear,
350 //'url' => CRM_Utils_System::url('civicrm/activity/search',
351 // "reset=1&force=1&signupType=2&dateLow=$yearStart&dateHigh=$ymd"
352 //),
353 );
354
355 $totalCount['year']['total'] = array(
356 'count' => $totalCountYear,
357 //'url' => CRM_Utils_System::url('civicrm/activity/search',
358 // "reset=1&force=1&signupType=3&dateLow=$yearStart&dateHigh=$ymd"
359 //),
360 );
361
362 $totalCount['current']['total'] = array(
363 'count' => $totalCountCurrent,
364 'url' => CRM_Utils_System::url('civicrm/member/search',
365 "reset=1&force=1&status=$status"
366 ),
367 );
368
369 $totalCount['total']['total'] = array(
370 'count' => $totalCountTotal,
371 'url' => CRM_Utils_System::url('civicrm/member/search',
372 "reset=1&force=1&status=$status"
373 ),
374 );
375
376 if (!$isCurrentMonth) {
377 $totalCount['total']['total'] = array(
378 'count' => $totalCountTotal,
379 'url' => CRM_Utils_System::url('civicrm/member/search',
380 "reset=1&force=1&status=$status&start=&end=$ymd"
381 ),
382 );
383 }
384
385 // Activity search also unable to handle owner vs. inherited
386
387 //LCD add owner values
388 $totalCount['premonth_owner']['premonth_owner'] = array(
389 'count' => $totalCountPreMonth_owner,
390 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$preMonth&end=$preMonthEnd&owner=1"),
391 );
392
393 $totalCount['month_owner']['month_owner'] = array(
394 'count' => $totalCountMonth_owner,
395 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$monthStart&end=$ymd&owner=1"),
396 );
397
398 $totalCount['year_owner']['year_owner'] = array(
399 'count' => $totalCountYear_owner,
400 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$yearStart&end=$ymd&owner=1"),
401 );
402
403 $totalCount['current_owner']['current_owner'] = array(
404 'count' => $totalCountCurrent_owner,
405 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
406 );
407
408 $totalCount['total_owner']['total_owner'] = array(
409 'count' => $totalCountTotal_owner,
410 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
411 );
412
413 if (!$isCurrentMonth) {
414 $totalCount['total_owner']['total_owner'] = array(
415 'count' => $totalCountTotal_owner,
416 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=&end=$ymd&owner=1"),
417 );
418 }
419 //LCD end
420
421 $this->assign('membershipSummary', $membershipSummary);
422 $this->assign('totalCount', $totalCount);
423 $this->assign('month', date('F', $monthStartTs));
424 $this->assign('year', date('Y', $monthStartTs));
425 $this->assign('premonth', date('F', strtotime($preMonth)));
426 $this->assign('currentMonth', date('F'));
427 $this->assign('currentYear', date('Y'));
428 $this->assign('isCurrent', $isCurrentMonth);
429 $this->assign('preMonth', $isPreviousMonth);
430 }
431
432 /**
433 * This function is the main function that is called when the page loads,
434 * it decides the which action has to be taken for the page.
435 *
436 * return null
437 * @access public
438 */
439 function run() {
440 $this->preProcess();
441
442 $controller = new CRM_Core_Controller_Simple('CRM_Member_Form_Search', ts('Member'), NULL);
443 $controller->setEmbedded(TRUE);
444 $controller->reset();
445 $controller->set('limit', 20);
446 $controller->set('force', 1);
447 $controller->set('context', 'dashboard');
448 $controller->process();
449 $controller->run();
450
451 return parent::run();
452 }
453 }
454