Merge pull request #15820 from seamuslee001/dev_core_183_custom_contribsybnt
[civicrm-core.git] / CRM / Member / Page / DashBoard.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * Page for displaying list of Payment-Instrument
22 */
23class 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
6a488035 30 */
00be9182 31 public function preProcess() {
46c91013
E
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)
feead5e7 36 if (!CRM_Core_Permission::check('view all contacts') && !CRM_Core_Permission::check('edit all contacts')) {
46c91013
E
37 $this->showMembershipSummary = FALSE;
38 $this->assign('membershipSummary', FALSE);
39 return;
40 }
41 $this->assign('membershipSummary', TRUE);
6a488035
TO
42 CRM_Utils_System::setTitle(ts('CiviMember'));
43 $membershipSummary = array();
4e636a74
AH
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")));
6a488035
TO
46
47 $preMonthYear = mktime(0, 0, 0, substr($preMonth, 4, 2), 1, substr($preMonth, 0, 4));
48
4e636a74
AH
49 $today = getdate();
50 $date = CRM_Utils_Date::getToday();
6a488035
TO
51 $isCurrentMonth = 0;
52
4e636a74 53 // You can force the dashboard to display based upon a certain date
6a488035 54 $ym = CRM_Utils_Array::value('date', $_GET);
8ef12e64 55
6a488035
TO
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;
4e636a74
AH
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);
6a488035
TO
70 }
71 else {
4e636a74
AH
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;
6a488035
TO
77 $isPreviousMonth = 1;
78 }
4e636a74
AH
79 $monthStart = $ym . '-01';
80 $yearStart = substr($ym, 0, 4) . '-01-01';
6a488035
TO
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
b09fe5ed 88 $membershipSummary[$key]['premonth']['new'] = array(
353ffa53 89 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $preMonth, $preMonthEnd),
6a488035
TO
90 'name' => $value,
91 );
92
b09fe5ed 93 $membershipSummary[$key]['premonth']['renew'] = array(
353ffa53 94 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $preMonth, $preMonthEnd),
6a488035
TO
95 'name' => $value,
96 );
97
b09fe5ed 98 $membershipSummary[$key]['premonth']['total'] = array(
353ffa53 99 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd),
6a488035
TO
100 'name' => $value,
101 );
102
b09fe5ed 103 $membershipSummary[$key]['month']['new'] = array(
353ffa53 104 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $monthStart, $ymd),
6a488035
TO
105 'name' => $value,
106 );
107
b09fe5ed 108 $membershipSummary[$key]['month']['renew'] = array(
353ffa53 109 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $monthStart, $ymd),
6a488035
TO
110 'name' => $value,
111 );
112
b09fe5ed 113 $membershipSummary[$key]['month']['total'] = array(
353ffa53 114 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd),
6a488035
TO
115 'name' => $value,
116 );
117
b09fe5ed 118 $membershipSummary[$key]['year']['new'] = array(
353ffa53 119 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $yearStart, $ymd),
6a488035
TO
120 'name' => $value,
121 );
122
b09fe5ed 123 $membershipSummary[$key]['year']['renew'] = array(
353ffa53 124 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $yearStart, $ymd),
6a488035
TO
125 'name' => $value,
126 );
127
b09fe5ed 128 $membershipSummary[$key]['year']['total'] = array(
353ffa53 129 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd),
6a488035
TO
130 'name' => $value,
131 );
132
b09fe5ed 133 $membershipSummary[$key]['current']['total'] = array(
353ffa53 134 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current),
6a488035
TO
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
b09fe5ed 141 $membershipSummary[$key]['premonth_owner']['premonth_owner'] = array(
353ffa53 142 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd, 0, 1),
6a488035
TO
143 'name' => $value,
144 );
145
b09fe5ed 146 $membershipSummary[$key]['month_owner']['month_owner'] = array(
353ffa53 147 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd, 0, 1),
6a488035
TO
148 'name' => $value,
149 );
150
b09fe5ed 151 $membershipSummary[$key]['year_owner']['year_owner'] = array(
353ffa53 152 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd, 0, 1),
6a488035
TO
153 'name' => $value,
154 );
155
b09fe5ed 156 $membershipSummary[$key]['current_owner']['current_owner'] = array(
353ffa53 157 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current, 0, 1),
6a488035
TO
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
6a488035
TO
165 $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
166 $status = implode(',', $status);
167
4e636a74
AH
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&status=$status&type=$typeID"
172 );
173 $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");
174 }
175 else {
176 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
177 "reset=1&force=1&status=$status"
178 );
179 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1");
180 }
181 $membershipSummary[$typeID]['current']['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID");
182 $membershipSummary[$typeID]['current_owner']['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&type=$typeID&owner=1");
183 }
6a488035
TO
184
185 $totalCount = array();
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) {
353ffa53 193 $newCountPreMonth = $newCountPreMonth + $value['premonth']['new']['count'];
6a488035
TO
194 $renewCountPreMonth = $renewCountPreMonth + $value['premonth']['renew']['count'];
195 $totalCountPreMonth = $totalCountPreMonth + $value['premonth']['total']['count'];
353ffa53
TO
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'];
6a488035
TO
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,
6a488035
TO
215 );
216
217 $totalCount['premonth']['renew'] = array(
218 'count' => $renewCountPreMonth,
6a488035
TO
219 );
220
221 $totalCount['premonth']['total'] = array(
222 'count' => $totalCountPreMonth,
6a488035
TO
223 );
224
225 $totalCount['month']['new'] = array(
226 'count' => $newCountMonth,
6a488035
TO
227 );
228
229 $totalCount['month']['renew'] = array(
230 'count' => $renewCountMonth,
6a488035
TO
231 );
232
233 $totalCount['month']['total'] = array(
234 'count' => $totalCountMonth,
6a488035
TO
235 );
236
237 $totalCount['year']['new'] = array(
238 'count' => $newCountYear,
6a488035
TO
239 );
240
241 $totalCount['year']['renew'] = array(
242 'count' => $renewCountYear,
6a488035
TO
243 );
244
245 $totalCount['year']['total'] = array(
246 'count' => $totalCountYear,
6a488035
TO
247 );
248
249 $totalCount['current']['total'] = array(
250 'count' => $totalCountCurrent,
251 'url' => CRM_Utils_System::url('civicrm/member/search',
252 "reset=1&force=1&status=$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&status=$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&status=$status&start=&end=$ymd"
268 ),
269 );
270 }
271
4e636a74
AH
272 // Activity search also unable to handle owner vs. inherited
273
6a488035
TO
274 //LCD add owner values
275 $totalCount['premonth_owner']['premonth_owner'] = array(
276 'count' => $totalCountPreMonth_owner,
353ffa53 277 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$preMonth&end=$preMonthEnd&owner=1"),
6a488035
TO
278 );
279
280 $totalCount['month_owner']['month_owner'] = array(
281 'count' => $totalCountMonth_owner,
353ffa53 282 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$monthStart&end=$ymd&owner=1"),
6a488035
TO
283 );
284
285 $totalCount['year_owner']['year_owner'] = array(
286 'count' => $totalCountYear_owner,
353ffa53 287 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=$yearStart&end=$ymd&owner=1"),
6a488035
TO
288 );
289
290 $totalCount['current_owner']['current_owner'] = array(
291 'count' => $totalCountCurrent_owner,
353ffa53 292 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
6a488035
TO
293 );
294
295 $totalCount['total_owner']['total_owner'] = array(
296 'count' => $totalCountTotal_owner,
353ffa53 297 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&owner=1"),
6a488035
TO
298 );
299
300 if (!$isCurrentMonth) {
301 $totalCount['total_owner']['total_owner'] = array(
302 'count' => $totalCountTotal_owner,
353ffa53 303 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&status=$status&start=&end=$ymd&owner=1"),
6a488035
TO
304 );
305 }
306 //LCD end
307
308 $this->assign('membershipSummary', $membershipSummary);
309 $this->assign('totalCount', $totalCount);
d2f262da 310 $this->assign('month', CRM_Utils_Date::customFormatTs($monthStartTs, '%B'));
6a488035 311 $this->assign('year', date('Y', $monthStartTs));
74d3ce5e 312 $this->assign('premonth', CRM_Utils_Date::customFormat($preMonth, '%B'));
6a488035
TO
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 /**
dc195289 320 * the main function that is called when the page loads,
6a488035
TO
321 * it decides the which action has to be taken for the page.
322 *
76e7a76c 323 * @return null
6a488035 324 */
00be9182 325 public function run() {
6a488035
TO
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 }
96025800 339
6a488035 340}