Merge pull request #17595 from eileenmcnaughton/settings
[civicrm-core.git] / CRM / Member / Page / DashBoard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Page for displaying list of Payment-Instrument
20 */
21 class CRM_Member_Page_DashBoard extends CRM_Core_Page {
22
23 /**
24 * Heart of the viewing process. The runner gets all the meta data for
25 * the contact and calls the appropriate type of page to view.
26 *
27 * @return void
28 */
29 public function preProcess() {
30
31 //CRM-13901 don't show dashboard to contacts with limited view writes & it does not relect
32 //what they have access to
33 //@todo implement acls on dashboard querys (preferably via api to enhance that at the same time)
34 if (!CRM_Core_Permission::check('view all contacts') && !CRM_Core_Permission::check('edit all contacts')) {
35 $this->showMembershipSummary = FALSE;
36 $this->assign('membershipSummary', FALSE);
37 return;
38 }
39 $this->assign('membershipSummary', TRUE);
40 CRM_Utils_System::setTitle(ts('CiviMember'));
41 $membershipSummary = [];
42 $preMonth = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
43 $preMonthEnd = date("Y-m-t", mktime(0, 0, 0, date("m") - 1, 01, date("Y")));
44
45 $preMonthYear = mktime(0, 0, 0, substr($preMonth, 4, 2), 1, substr($preMonth, 0, 4));
46
47 $today = getdate();
48 $date = CRM_Utils_Date::getToday();
49 $isCurrentMonth = 0;
50
51 // You can force the dashboard to display based upon a certain date
52 $ym = $_GET['date'] ?? NULL;
53
54 if ($ym) {
55 if (preg_match('/^\d{6}$/', $ym) == 0 ||
56 !checkdate(substr($ym, 4, 2), 1, substr($ym, 0, 4)) ||
57 substr($ym, 0, 1) == 0
58 ) {
59 CRM_Core_Error::statusBounce(ts('Invalid date query "%1" in URL (valid syntax is yyyymm).', array(1 => $ym)));
60 }
61
62 $isPreviousMonth = 0;
63 $isCurrentMonth = substr($ym, 0, 4) == $today['year'] && substr($ym, 4, 2) == $today['mon'];
64 $ymd = date('Y-m-d', mktime(0, 0, -1, substr($ym, 4, 2) + 1, 1, substr($ym, 0, 4)));
65 $monthStartTs = mktime(0, 0, 0, substr($ym, 4, 2), 1, substr($ym, 0, 4));
66 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
67 $ym = substr($ym, 0, 4) . '-' . substr($ym, 4, 2);
68 }
69 else {
70 $ym = sprintf("%04d-%02d", $today['year'], $today['mon']);
71 $ymd = sprintf("%04d-%02d-%02d", $today['year'], $today['mon'], $today['mday']);
72 $monthStartTs = mktime(0, 0, 0, $today['mon'], 1, $today['year']);
73 $current = CRM_Utils_Date::customFormat($date, '%Y-%m-%d');
74 $isCurrentMonth = 1;
75 $isPreviousMonth = 1;
76 }
77 $monthStart = $ym . '-01';
78 $yearStart = substr($ym, 0, 4) . '-01-01';
79
80 $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
81 // added
82 //$membership = new CRM_Member_BAO_Membership;
83
84 foreach ($membershipTypes as $key => $value) {
85
86 $membershipSummary[$key]['premonth']['new'] = array(
87 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $preMonth, $preMonthEnd),
88 'name' => $value,
89 );
90
91 $membershipSummary[$key]['premonth']['renew'] = array(
92 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $preMonth, $preMonthEnd),
93 'name' => $value,
94 );
95
96 $membershipSummary[$key]['premonth']['total'] = array(
97 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd),
98 'name' => $value,
99 );
100
101 $membershipSummary[$key]['month']['new'] = array(
102 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $monthStart, $ymd),
103 'name' => $value,
104 );
105
106 $membershipSummary[$key]['month']['renew'] = array(
107 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $monthStart, $ymd),
108 'name' => $value,
109 );
110
111 $membershipSummary[$key]['month']['total'] = array(
112 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd),
113 'name' => $value,
114 );
115
116 $membershipSummary[$key]['year']['new'] = array(
117 'count' => CRM_Member_BAO_Membership::getMembershipJoins($key, $yearStart, $ymd),
118 'name' => $value,
119 );
120
121 $membershipSummary[$key]['year']['renew'] = array(
122 'count' => CRM_Member_BAO_Membership::getMembershipRenewals($key, $yearStart, $ymd),
123 'name' => $value,
124 );
125
126 $membershipSummary[$key]['year']['total'] = array(
127 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd),
128 'name' => $value,
129 );
130
131 $membershipSummary[$key]['current']['total'] = array(
132 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current),
133 'name' => $value,
134 );
135
136 $membershipSummary[$key]['total']['total'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd));
137
138 //LCD also get summary stats for membership owners
139 $membershipSummary[$key]['premonth_owner']['premonth_owner'] = array(
140 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $preMonth, $preMonthEnd, 0, 1),
141 'name' => $value,
142 );
143
144 $membershipSummary[$key]['month_owner']['month_owner'] = array(
145 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $monthStart, $ymd, 0, 1),
146 'name' => $value,
147 );
148
149 $membershipSummary[$key]['year_owner']['year_owner'] = array(
150 'count' => CRM_Member_BAO_Membership::getMembershipStarts($key, $yearStart, $ymd, 0, 1),
151 'name' => $value,
152 );
153
154 $membershipSummary[$key]['current_owner']['current_owner'] = array(
155 'count' => CRM_Member_BAO_Membership::getMembershipCount($key, $current, 0, 1),
156 'name' => $value,
157 );
158
159 $membershipSummary[$key]['total_owner']['total_owner'] = array('count' => CRM_Member_BAO_Membership::getMembershipCount($key, $ymd, 0, 1));
160 //LCD end
161 }
162
163 $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent();
164 $status = implode(',', $status);
165
166 foreach ($membershipSummary as $typeID => $details) {
167 if (!$isCurrentMonth) {
168 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
169 "reset=1&force=1&start=&end=$ymd&membership_status_id=$status&membership_type_id=$typeID"
170 );
171 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&start=&end=$ymd&membership_status_id=$status&membership_type_id=$typeID&owner=1");
172 }
173 else {
174 $membershipSummary[$typeID]['total']['total']['url'] = CRM_Utils_System::url('civicrm/member/search',
175 "reset=1&force=1&membership_status_id=$status"
176 );
177 $membershipSummary[$typeID]['total_owner']['total_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1");
178 }
179 $membershipSummary[$typeID]['current']['total']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&membership_type_id=$typeID");
180 $membershipSummary[$typeID]['current_owner']['current_owner']['url'] = CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&membership_type_id=$typeID&owner=1");
181 }
182
183 $totalCount = [];
184
185 $newCountPreMonth = $newCountMonth = $newCountYear = 0;
186 $renewCountPreMonth = $renewCountMonth = $renewCountYear = 0;
187
188 $totalCountPreMonth = $totalCountMonth = $totalCountYear = $totalCountCurrent = $totalCountTotal = 0;
189 $totalCountPreMonth_owner = $totalCountMonth_owner = $totalCountYear_owner = $totalCountCurrent_owner = $totalCountTotal_owner = 0;
190 foreach ($membershipSummary as $key => $value) {
191 $newCountPreMonth = $newCountPreMonth + $value['premonth']['new']['count'];
192 $renewCountPreMonth = $renewCountPreMonth + $value['premonth']['renew']['count'];
193 $totalCountPreMonth = $totalCountPreMonth + $value['premonth']['total']['count'];
194 $newCountMonth = $newCountMonth + $value['month']['new']['count'];
195 $renewCountMonth = $renewCountMonth + $value['month']['renew']['count'];
196 $totalCountMonth = $totalCountMonth + $value['month']['total']['count'];
197 $newCountYear = $newCountYear + $value['year']['new']['count'];
198 $renewCountYear = $renewCountYear + $value['year']['renew']['count'];
199 $totalCountYear = $totalCountYear + $value['year']['total']['count'];
200 $totalCountCurrent = $totalCountCurrent + $value['current']['total']['count'];
201 $totalCountTotal = $totalCountTotal + $value['total']['total']['count'];
202
203 //LCD add owner values
204 $totalCountPreMonth_owner = $totalCountPreMonth_owner + $value['premonth_owner']['premonth_owner']['count'];
205 $totalCountMonth_owner = $totalCountMonth_owner + $value['month_owner']['month_owner']['count'];
206 $totalCountYear_owner = $totalCountYear_owner + $value['year_owner']['year_owner']['count'];
207 $totalCountCurrent_owner = $totalCountCurrent_owner + $value['current_owner']['current_owner']['count'];
208 $totalCountTotal_owner = $totalCountTotal_owner + $value['total_owner']['total_owner']['count'];
209 }
210
211 $totalCount['premonth']['new'] = array(
212 'count' => $newCountPreMonth,
213 );
214
215 $totalCount['premonth']['renew'] = array(
216 'count' => $renewCountPreMonth,
217 );
218
219 $totalCount['premonth']['total'] = array(
220 'count' => $totalCountPreMonth,
221 );
222
223 $totalCount['month']['new'] = array(
224 'count' => $newCountMonth,
225 );
226
227 $totalCount['month']['renew'] = array(
228 'count' => $renewCountMonth,
229 );
230
231 $totalCount['month']['total'] = array(
232 'count' => $totalCountMonth,
233 );
234
235 $totalCount['year']['new'] = array(
236 'count' => $newCountYear,
237 );
238
239 $totalCount['year']['renew'] = array(
240 'count' => $renewCountYear,
241 );
242
243 $totalCount['year']['total'] = array(
244 'count' => $totalCountYear,
245 );
246
247 $totalCount['current']['total'] = array(
248 'count' => $totalCountCurrent,
249 'url' => CRM_Utils_System::url('civicrm/member/search',
250 "reset=1&force=1&membership_status_id=$status"
251 ),
252 );
253
254 $totalCount['total']['total'] = array(
255 'count' => $totalCountTotal,
256 'url' => CRM_Utils_System::url('civicrm/member/search',
257 "reset=1&force=1&membership_status_id=$status"
258 ),
259 );
260
261 if (!$isCurrentMonth) {
262 $totalCount['total']['total'] = array(
263 'count' => $totalCountTotal,
264 'url' => CRM_Utils_System::url('civicrm/member/search',
265 "reset=1&force=1&membership_status_id=$status&start=&end=$ymd"
266 ),
267 );
268 }
269
270 // Activity search also unable to handle owner vs. inherited
271
272 //LCD add owner values
273 $totalCount['premonth_owner']['premonth_owner'] = array(
274 'count' => $totalCountPreMonth_owner,
275 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$preMonth&end=$preMonthEnd&owner=1"),
276 );
277
278 $totalCount['month_owner']['month_owner'] = array(
279 'count' => $totalCountMonth_owner,
280 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$monthStart&end=$ymd&owner=1"),
281 );
282
283 $totalCount['year_owner']['year_owner'] = array(
284 'count' => $totalCountYear_owner,
285 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=$yearStart&end=$ymd&owner=1"),
286 );
287
288 $totalCount['current_owner']['current_owner'] = array(
289 'count' => $totalCountCurrent_owner,
290 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1"),
291 );
292
293 $totalCount['total_owner']['total_owner'] = array(
294 'count' => $totalCountTotal_owner,
295 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&owner=1"),
296 );
297
298 if (!$isCurrentMonth) {
299 $totalCount['total_owner']['total_owner'] = array(
300 'count' => $totalCountTotal_owner,
301 // 'url' => CRM_Utils_System::url('civicrm/member/search', "reset=1&force=1&membership_status_id=$status&start=&end=$ymd&owner=1"),
302 );
303 }
304 //LCD end
305
306 $this->assign('membershipSummary', $membershipSummary);
307 $this->assign('totalCount', $totalCount);
308 $this->assign('month', CRM_Utils_Date::customFormatTs($monthStartTs, '%B'));
309 $this->assign('year', date('Y', $monthStartTs));
310 $this->assign('premonth', CRM_Utils_Date::customFormat($preMonth, '%B'));
311 $this->assign('currentMonth', date('F'));
312 $this->assign('currentYear', date('Y'));
313 $this->assign('isCurrent', $isCurrentMonth);
314 $this->assign('preMonth', $isPreviousMonth);
315 }
316
317 /**
318 * the main function that is called when the page loads,
319 * it decides the which action has to be taken for the page.
320 *
321 * @return null
322 */
323 public function run() {
324 $this->preProcess();
325
326 $controller = new CRM_Core_Controller_Simple('CRM_Member_Form_Search', ts('Member'), NULL);
327 $controller->setEmbedded(TRUE);
328 $controller->reset();
329 $controller->set('limit', 20);
330 $controller->set('force', 1);
331 $controller->set('context', 'dashboard');
332 $controller->process();
333 $controller->run();
334
335 return parent::run();
336 }
337
338 }