Merge pull request #22841 from braders/state-class-tidy
[civicrm-core.git] / CRM / Campaign / 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
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
df371444 19 * Page for displaying Campaigns.
6a488035
TO
20 */
21class CRM_Campaign_Page_DashBoard extends CRM_Core_Page {
22
23 /**
fe482240 24 * The action links that we need to display for the browse screen.
6a488035
TO
25 *
26 * @var array
27 */
28 private static $_campaignActionLinks;
29 private static $_surveyActionLinks;
30 private static $_petitionActionLinks;
31
d9774ef0
CW
32 public $_tabs;
33
6a488035
TO
34 /**
35 * Get the action links for this page.
36 *
a6c01b45 37 * @return array
6a488035 38 */
de18909a 39 public static function campaignActionLinks() {
6a488035
TO
40 // check if variable _actionsLinks is populated
41 if (!isset(self::$_campaignActionLinks)) {
be2fb01f
CW
42 self::$_campaignActionLinks = [
43 CRM_Core_Action::UPDATE => [
6a488035
TO
44 'name' => ts('Edit'),
45 'url' => 'civicrm/campaign/add',
46 'qs' => 'reset=1&action=update&id=%%id%%',
47 'title' => ts('Update Campaign'),
be2fb01f
CW
48 ],
49 CRM_Core_Action::DISABLE => [
6a488035
TO
50 'name' => ts('Disable'),
51 'title' => ts('Disable Campaign'),
4d17a233 52 'ref' => 'crm-enable-disable',
be2fb01f
CW
53 ],
54 CRM_Core_Action::ENABLE => [
6a488035
TO
55 'name' => ts('Enable'),
56 'title' => ts('Enable Campaign'),
4d17a233 57 'ref' => 'crm-enable-disable',
be2fb01f
CW
58 ],
59 CRM_Core_Action::DELETE => [
6a488035
TO
60 'name' => ts('Delete'),
61 'url' => 'civicrm/campaign/add',
62 'qs' => 'action=delete&reset=1&id=%%id%%',
63 'title' => ts('Delete Campaign'),
be2fb01f
CW
64 ],
65 ];
6a488035
TO
66 }
67
68 return self::$_campaignActionLinks;
69 }
70
30c4e065
EM
71 /**
72 * @return array
73 */
de18909a 74 public static function surveyActionLinks() {
6a488035
TO
75 // check if variable _actionsLinks is populated
76 if (!isset(self::$_surveyActionLinks)) {
be2fb01f
CW
77 self::$_surveyActionLinks = [
78 CRM_Core_Action::UPDATE => [
6a488035
TO
79 'name' => ts('Edit'),
80 'url' => 'civicrm/survey/configure/main',
81 'qs' => 'action=update&id=%%id%%&reset=1',
82 'title' => ts('Update Survey'),
be2fb01f
CW
83 ],
84 CRM_Core_Action::DISABLE => [
6a488035 85 'name' => ts('Disable'),
4d17a233 86 'ref' => 'crm-enable-disable',
6a488035 87 'title' => ts('Disable Survey'),
be2fb01f
CW
88 ],
89 CRM_Core_Action::ENABLE => [
6a488035 90 'name' => ts('Enable'),
4d17a233 91 'ref' => 'crm-enable-disable',
6a488035 92 'title' => ts('Enable Survey'),
be2fb01f
CW
93 ],
94 CRM_Core_Action::DELETE => [
6a488035
TO
95 'name' => ts('Delete'),
96 'url' => 'civicrm/survey/delete',
97 'qs' => 'id=%%id%%&reset=1',
98 'title' => ts('Delete Survey'),
be2fb01f
CW
99 ],
100 ];
6a488035
TO
101 }
102
103 return self::$_surveyActionLinks;
104 }
105
30c4e065
EM
106 /**
107 * @return array
108 */
de18909a 109 public static function petitionActionLinks() {
6a488035
TO
110 if (!isset(self::$_petitionActionLinks)) {
111 self::$_petitionActionLinks = self::surveyActionLinks();
be2fb01f 112 self::$_petitionActionLinks[CRM_Core_Action::UPDATE] = [
6a488035
TO
113 'name' => ts('Edit'),
114 'url' => 'civicrm/petition/add',
115 'qs' => 'action=update&id=%%id%%&reset=1',
116 'title' => ts('Update Petition'),
be2fb01f
CW
117 ];
118 self::$_petitionActionLinks[CRM_Core_Action::DISABLE] = [
6a488035 119 'name' => ts('Disable'),
4d17a233 120 'ref' => 'crm-enable-disable',
6a488035 121 'title' => ts('Disable Petition'),
be2fb01f
CW
122 ];
123 self::$_petitionActionLinks[CRM_Core_Action::ENABLE] = [
6a488035 124 'name' => ts('Enable'),
4d17a233 125 'ref' => 'crm-enable-disable',
6a488035 126 'title' => ts('Enable Petition'),
be2fb01f
CW
127 ];
128 self::$_petitionActionLinks[CRM_Core_Action::DELETE] = [
6a488035
TO
129 'name' => ts('Delete'),
130 'url' => 'civicrm/petition/add',
131 'qs' => 'action=delete&id=%%id%%&reset=1',
132 'title' => ts('Delete Petition'),
be2fb01f
CW
133 ];
134 self::$_petitionActionLinks[CRM_Core_Action::PROFILE] = [
6a488035
TO
135 'name' => ts('Sign'),
136 'url' => 'civicrm/petition/sign',
137 'qs' => 'sid=%%id%%&reset=1',
138 'title' => ts('Sign Petition'),
139 'fe' => TRUE,
353ffa53 140 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
be2fb01f
CW
141 ];
142 self::$_petitionActionLinks[CRM_Core_Action::BROWSE] = [
6a488035
TO
143 'name' => ts('Signatures'),
144 'url' => 'civicrm/activity/search',
145 'qs' => 'survey=%%id%%&force=1',
146 'title' => ts('List the signatures'),
353ffa53 147 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
be2fb01f 148 ];
6a488035
TO
149 }
150
151 return self::$_petitionActionLinks;
152 }
153
627456b5
EM
154 /**
155 * @return mixed
156 */
00be9182 157 public function browseCampaign() {
6a488035
TO
158 // ensure valid javascript (these must have a value set)
159 $this->assign('searchParams', json_encode(NULL));
160 $this->assign('campaignTypes', json_encode(NULL));
161 $this->assign('campaignStatus', json_encode(NULL));
162
163 $this->assign('addCampaignUrl', CRM_Utils_System::url('civicrm/campaign/add', 'reset=1&action=add'));
164 $campaignCount = CRM_Campaign_BAO_Campaign::getCampaignCount();
165 //don't load find interface when no campaigns in db.
166 if (!$campaignCount) {
167 $this->assign('hasCampaigns', FALSE);
168 return;
169 }
170 $this->assign('hasCampaigns', TRUE);
171
172 //build the ajaxify campaign search and selector.
173 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Campaign', ts('Search Campaigns'));
174 $controller->set('searchTab', 'campaign');
175 $controller->setEmbedded(TRUE);
176 $controller->process();
177 return $controller->run();
178 }
179
30c4e065
EM
180 /**
181 * @param array $params
182 *
183 * @return array
184 */
be2fb01f
CW
185 public static function getCampaignSummary($params = []) {
186 $campaignsData = [];
6a488035
TO
187
188 //get the campaigns.
189 $campaigns = CRM_Campaign_BAO_Campaign::getCampaignSummary($params);
190 if (!empty($campaigns)) {
353ffa53
TO
191 $config = CRM_Core_Config::singleton();
192 $campaignType = CRM_Campaign_PseudoConstant::campaignType();
6a488035 193 $campaignStatus = CRM_Campaign_PseudoConstant::campaignStatus();
be2fb01f 194 $properties = [
353ffa53
TO
195 'id',
196 'name',
197 'title',
198 'status_id',
199 'description',
200 'campaign_type_id',
201 'is_active',
202 'start_date',
203 'end_date',
be2fb01f 204 ];
6a488035
TO
205 foreach ($campaigns as $cmpid => $campaign) {
206 foreach ($properties as $prop) {
9c1bc317 207 $campaignsData[$cmpid][$prop] = $campaign[$prop] ?? NULL;
6a488035 208 }
9c1bc317
CW
209 $statusId = $campaign['status_id'] ?? NULL;
210 $campaignsData[$cmpid]['status'] = $campaignStatus[$statusId] ?? NULL;
6a488035
TO
211 $campaignsData[$cmpid]['campaign_id'] = $campaign['id'];
212 $campaignsData[$cmpid]['campaign_type'] = $campaignType[$campaign['campaign_type_id']];
213
214 $action = array_sum(array_keys(self::campaignActionLinks()));
215 if ($campaign['is_active']) {
216 $action -= CRM_Core_Action::ENABLE;
217 }
218 else {
219 $action -= CRM_Core_Action::DISABLE;
220 }
221
222 $isActive = ts('No');
223 if ($campaignsData[$cmpid]['is_active']) {
224 $isActive = ts('Yes');
225 }
226 $campaignsData[$cmpid]['isActive'] = $isActive;
227
a7488080 228 if (!empty($campaignsData[$cmpid]['start_date'])) {
6a488035
TO
229 $campaignsData[$cmpid]['start_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['start_date'],
230 $config->dateformatFull
231 );
232 }
a7488080 233 if (!empty($campaignsData[$cmpid]['end_date'])) {
6a488035
TO
234 $campaignsData[$cmpid]['end_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['end_date'],
235 $config->dateformatFull
236 );
237 }
238 $campaignsData[$cmpid]['action'] = CRM_Core_Action::formLink(self::campaignActionLinks(),
239 $action,
be2fb01f 240 ['id' => $campaign['id']],
87dab4a4
AH
241 ts('more'),
242 FALSE,
243 'campaign.dashboard.row',
244 'Campaign',
245 $campaign['id']
6a488035
TO
246 );
247 }
248 }
249
250 return $campaignsData;
251 }
252
bb271167
EM
253 /**
254 * @return mixed
255 */
00be9182 256 public function browseSurvey() {
6a488035
TO
257 // ensure valid javascript - this must have a value set
258 $this->assign('searchParams', json_encode(NULL));
259 $this->assign('surveyTypes', json_encode(NULL));
260 $this->assign('surveyCampaigns', json_encode(NULL));
261
262 $this->assign('addSurveyUrl', CRM_Utils_System::url('civicrm/survey/add', 'reset=1&action=add'));
263
264 $surveyCount = CRM_Campaign_BAO_Survey::getSurveyCount();
265 //don't load find interface when no survey in db.
266 if (!$surveyCount) {
267 $this->assign('hasSurveys', FALSE);
268 return;
269 }
270 $this->assign('hasSurveys', TRUE);
271
272 //build the ajaxify survey search and selector.
273 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Survey', ts('Search Survey'));
274 $controller->set('searchTab', 'survey');
275 $controller->setEmbedded(TRUE);
276 $controller->process();
277 return $controller->run();
278 }
279
30c4e065
EM
280 /**
281 * @param array $params
282 *
283 * @return array
284 */
be2fb01f
CW
285 public static function getSurveySummary($params = []) {
286 $surveysData = [];
6a488035
TO
287
288 //get the survey.
289 $config = CRM_Core_Config::singleton();
290 $surveys = CRM_Campaign_BAO_Survey::getSurveySummary($params);
291 if (!empty($surveys)) {
292 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
293 $surveyType = CRM_Campaign_BAO_Survey::getSurveyActivityType();
294 foreach ($surveys as $sid => $survey) {
295 $surveysData[$sid] = $survey;
9c1bc317
CW
296 $campaignId = $survey['campaign_id'] ?? NULL;
297 $surveysData[$sid]['campaign'] = $campaigns[$campaignId] ?? NULL;
6a488035 298 $surveysData[$sid]['activity_type'] = $surveyType[$survey['activity_type_id']];
a7488080 299 if (!empty($survey['release_frequency'])) {
be2fb01f 300 $surveysData[$sid]['release_frequency'] = ts('1 Day', ['plural' => '%count Days', 'count' => $survey['release_frequency']]);
6a488035
TO
301 }
302
303 $action = array_sum(array_keys(self::surveyActionLinks($surveysData[$sid]['activity_type'])));
304 if ($survey['is_active']) {
305 $action -= CRM_Core_Action::ENABLE;
306 }
307 else {
308 $action -= CRM_Core_Action::DISABLE;
309 }
310
311 $isActive = ts('No');
312 if ($surveysData[$sid]['is_active']) {
313 $isActive = ts('Yes');
314 }
315 $surveysData[$sid]['isActive'] = $isActive;
316
bc37053c
AH
317 // For some reason, 'is_default' is coming as a string.
318 $surveysData[$sid]['is_default'] = boolval($surveysData[$sid]['is_default']);
6a488035
TO
319
320 if ($surveysData[$sid]['result_id']) {
92fcb95f 321 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
6a488035 322 $surveysData[$sid]['result_id'] = $resultSet;
0db6c3e1
TO
323 }
324 else {
6a488035
TO
325 $resultUrl = CRM_Utils_System::url("civicrm/survey/configure/results", "action=update&id={$sid}&reset=1");
326 $surveysData[$sid]['result_id'] = "<a href='{$resultUrl}' class='status-warning'>(" . ts('Incomplete. Click to configure result set.') . ')</a>';
327 }
328 $surveysData[$sid]['action'] = CRM_Core_Action::formLink(self::surveyActionLinks($surveysData[$sid]['activity_type']),
329 $action,
be2fb01f 330 ['id' => $sid],
87dab4a4
AH
331 ts('more'),
332 FALSE,
333 'survey.dashboard.row',
334 'Survey',
335 $sid
6a488035
TO
336 );
337
338 if (CRM_Utils_Array::value('activity_type', $surveysData[$sid]) != 'Petition') {
339 $surveysData[$sid]['voterLinks'] = CRM_Campaign_BAO_Survey::buildPermissionLinks($sid,
340 TRUE,
341 ts('more')
342 );
343 }
344
345 if ($reportID = CRM_Campaign_BAO_Survey::getReportID($sid)) {
94880218 346 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
6a488035
TO
347 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
348 }
349 }
350 }
351
352 return $surveysData;
353 }
354
b896fa44
EM
355 /**
356 * Browse petitions.
357 *
358 * @return mixed|null
359 */
00be9182 360 public function browsePetition() {
211e2fca 361 // Ensure valid javascript - this must have a value set
6a488035
TO
362 $this->assign('searchParams', json_encode(NULL));
363 $this->assign('petitionCampaigns', json_encode(NULL));
364
365 $this->assign('addPetitionUrl', CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
366
367 $petitionCount = CRM_Campaign_BAO_Petition::getPetitionCount();
368 //don't load find interface when no petition in db.
369 if (!$petitionCount) {
370 $this->assign('hasPetitions', FALSE);
211e2fca 371 return NULL;
6a488035
TO
372 }
373 $this->assign('hasPetitions', TRUE);
374
211e2fca 375 // Build the ajax petition search and selector.
6a488035
TO
376 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Petition', ts('Search Petition'));
377 $controller->set('searchTab', 'petition');
378 $controller->setEmbedded(TRUE);
379 $controller->process();
380 return $controller->run();
381 }
382
30c4e065
EM
383 /**
384 * @param array $params
385 *
386 * @return array
387 */
be2fb01f 388 public static function getPetitionSummary($params = []) {
6a488035 389 $config = CRM_Core_Config::singleton();
be2fb01f 390 $petitionsData = [];
6a488035
TO
391
392 //get the petitions.
393 $petitions = CRM_Campaign_BAO_Petition::getPetitionSummary($params);
394 if (!empty($petitions)) {
395 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
396 $petitionType = CRM_Campaign_BAO_Survey::getSurveyActivityType('label', TRUE);
397 foreach ($petitions as $pid => $petition) {
398 $petitionsData[$pid] = $petition;
9c1bc317
CW
399 $camapignId = $petition['campaign_id'] ?? NULL;
400 $petitionsData[$pid]['campaign'] = $campaigns[$camapignId] ?? NULL;
6a488035
TO
401 $petitionsData[$pid]['activity_type'] = $petitionType[$petition['activity_type_id']];
402
403 $action = array_sum(array_keys(self::petitionActionLinks()));
404
405 if ($petition['is_active']) {
406 $action -= CRM_Core_Action::ENABLE;
407 }
408 else {
409 $action -= CRM_Core_Action::DISABLE;
410 }
411
412 $isActive = ts('No');
413 if ($petitionsData[$pid]['is_active']) {
414 $isActive = ts('Yes');
415 }
416 $petitionsData[$pid]['isActive'] = $isActive;
bc37053c
AH
417
418 // For some reason, 'is_default' is coming as a string.
419 $petitionsData[$pid]['is_default'] = boolval($petitionsData[$pid]['is_default']);
6a488035
TO
420
421 $petitionsData[$pid]['action'] = CRM_Core_Action::formLink(self::petitionActionLinks(),
422 $action,
be2fb01f 423 ['id' => $pid],
87dab4a4
AH
424 ts('more'),
425 FALSE,
426 'petition.dashboard.row',
427 'Petition',
428 $pid
6a488035
TO
429 );
430 }
431 }
432
433 return $petitionsData;
434 }
435
00be9182 436 public function browse() {
be2fb01f 437 $this->_tabs = [
349a45b7 438 'campaign' => ts('Campaigns'),
6a488035
TO
439 'survey' => ts('Surveys'),
440 'petition' => ts('Petitions'),
be2fb01f 441 ];
6a488035
TO
442
443 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
d9774ef0 444 // Load the data for a specific tab
6a488035 445 if ($subPageType) {
349a45b7
CW
446 if (!isset($this->_tabs[$subPageType])) {
447 CRM_Utils_System::permissionDenied();
448 }
6a488035
TO
449 //load the data in tabs.
450 $this->{'browse' . ucfirst($subPageType)}();
451 }
d9774ef0 452 // Initialize tabs
6a488035 453 else {
6a488035
TO
454 $this->buildTabs();
455 }
c4b395d5 456 $this->assign('subPageType', ucfirst($subPageType));
6a488035
TO
457 }
458
30c4e065
EM
459 /**
460 * @return string
461 */
00be9182 462 public function run() {
6a488035
TO
463 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
464 CRM_Utils_System::permissionDenied();
465 }
466
467 $this->browse();
468
469 return parent::run();
470 }
471
00be9182 472 public function buildTabs() {
be2fb01f 473 $allTabs = [];
6a488035 474 foreach ($this->_tabs as $name => $title) {
be2fb01f 475 $allTabs[$name] = [
6a488035 476 'title' => $title,
349a45b7
CW
477 'valid' => TRUE,
478 'active' => TRUE,
479 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
c4b395d5
EM
480 'extra' => NULL,
481 'template' => NULL,
482 'count' => NULL,
483 'icon' => NULL,
484 'class' => NULL,
be2fb01f 485 ];
6a488035 486 }
a2e0853f 487 $allTabs['campaign']['class'] = 'livePage';
349a45b7 488 $this->assign('tabHeader', $allTabs);
d9774ef0
CW
489 CRM_Core_Resources::singleton()
490 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
491 ->addSetting([
492 'tabSettings' => [
493 'active' => strtolower(CRM_Utils_Array::value('subPage', $_GET, 'campaign')),
494 ],
495 ]);
6a488035 496 }
96025800 497
6a488035 498}