Merge pull request #22841 from braders/state-class-tidy
[civicrm-core.git] / CRM / Campaign / 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 Campaigns.
20 */
21 class CRM_Campaign_Page_DashBoard extends CRM_Core_Page {
22
23 /**
24 * The action links that we need to display for the browse screen.
25 *
26 * @var array
27 */
28 private static $_campaignActionLinks;
29 private static $_surveyActionLinks;
30 private static $_petitionActionLinks;
31
32 public $_tabs;
33
34 /**
35 * Get the action links for this page.
36 *
37 * @return array
38 */
39 public static function campaignActionLinks() {
40 // check if variable _actionsLinks is populated
41 if (!isset(self::$_campaignActionLinks)) {
42 self::$_campaignActionLinks = [
43 CRM_Core_Action::UPDATE => [
44 'name' => ts('Edit'),
45 'url' => 'civicrm/campaign/add',
46 'qs' => 'reset=1&action=update&id=%%id%%',
47 'title' => ts('Update Campaign'),
48 ],
49 CRM_Core_Action::DISABLE => [
50 'name' => ts('Disable'),
51 'title' => ts('Disable Campaign'),
52 'ref' => 'crm-enable-disable',
53 ],
54 CRM_Core_Action::ENABLE => [
55 'name' => ts('Enable'),
56 'title' => ts('Enable Campaign'),
57 'ref' => 'crm-enable-disable',
58 ],
59 CRM_Core_Action::DELETE => [
60 'name' => ts('Delete'),
61 'url' => 'civicrm/campaign/add',
62 'qs' => 'action=delete&reset=1&id=%%id%%',
63 'title' => ts('Delete Campaign'),
64 ],
65 ];
66 }
67
68 return self::$_campaignActionLinks;
69 }
70
71 /**
72 * @return array
73 */
74 public static function surveyActionLinks() {
75 // check if variable _actionsLinks is populated
76 if (!isset(self::$_surveyActionLinks)) {
77 self::$_surveyActionLinks = [
78 CRM_Core_Action::UPDATE => [
79 'name' => ts('Edit'),
80 'url' => 'civicrm/survey/configure/main',
81 'qs' => 'action=update&id=%%id%%&reset=1',
82 'title' => ts('Update Survey'),
83 ],
84 CRM_Core_Action::DISABLE => [
85 'name' => ts('Disable'),
86 'ref' => 'crm-enable-disable',
87 'title' => ts('Disable Survey'),
88 ],
89 CRM_Core_Action::ENABLE => [
90 'name' => ts('Enable'),
91 'ref' => 'crm-enable-disable',
92 'title' => ts('Enable Survey'),
93 ],
94 CRM_Core_Action::DELETE => [
95 'name' => ts('Delete'),
96 'url' => 'civicrm/survey/delete',
97 'qs' => 'id=%%id%%&reset=1',
98 'title' => ts('Delete Survey'),
99 ],
100 ];
101 }
102
103 return self::$_surveyActionLinks;
104 }
105
106 /**
107 * @return array
108 */
109 public static function petitionActionLinks() {
110 if (!isset(self::$_petitionActionLinks)) {
111 self::$_petitionActionLinks = self::surveyActionLinks();
112 self::$_petitionActionLinks[CRM_Core_Action::UPDATE] = [
113 'name' => ts('Edit'),
114 'url' => 'civicrm/petition/add',
115 'qs' => 'action=update&id=%%id%%&reset=1',
116 'title' => ts('Update Petition'),
117 ];
118 self::$_petitionActionLinks[CRM_Core_Action::DISABLE] = [
119 'name' => ts('Disable'),
120 'ref' => 'crm-enable-disable',
121 'title' => ts('Disable Petition'),
122 ];
123 self::$_petitionActionLinks[CRM_Core_Action::ENABLE] = [
124 'name' => ts('Enable'),
125 'ref' => 'crm-enable-disable',
126 'title' => ts('Enable Petition'),
127 ];
128 self::$_petitionActionLinks[CRM_Core_Action::DELETE] = [
129 'name' => ts('Delete'),
130 'url' => 'civicrm/petition/add',
131 'qs' => 'action=delete&id=%%id%%&reset=1',
132 'title' => ts('Delete Petition'),
133 ];
134 self::$_petitionActionLinks[CRM_Core_Action::PROFILE] = [
135 'name' => ts('Sign'),
136 'url' => 'civicrm/petition/sign',
137 'qs' => 'sid=%%id%%&reset=1',
138 'title' => ts('Sign Petition'),
139 'fe' => TRUE,
140 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
141 ];
142 self::$_petitionActionLinks[CRM_Core_Action::BROWSE] = [
143 'name' => ts('Signatures'),
144 'url' => 'civicrm/activity/search',
145 'qs' => 'survey=%%id%%&force=1',
146 'title' => ts('List the signatures'),
147 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
148 ];
149 }
150
151 return self::$_petitionActionLinks;
152 }
153
154 /**
155 * @return mixed
156 */
157 public function browseCampaign() {
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
180 /**
181 * @param array $params
182 *
183 * @return array
184 */
185 public static function getCampaignSummary($params = []) {
186 $campaignsData = [];
187
188 //get the campaigns.
189 $campaigns = CRM_Campaign_BAO_Campaign::getCampaignSummary($params);
190 if (!empty($campaigns)) {
191 $config = CRM_Core_Config::singleton();
192 $campaignType = CRM_Campaign_PseudoConstant::campaignType();
193 $campaignStatus = CRM_Campaign_PseudoConstant::campaignStatus();
194 $properties = [
195 'id',
196 'name',
197 'title',
198 'status_id',
199 'description',
200 'campaign_type_id',
201 'is_active',
202 'start_date',
203 'end_date',
204 ];
205 foreach ($campaigns as $cmpid => $campaign) {
206 foreach ($properties as $prop) {
207 $campaignsData[$cmpid][$prop] = $campaign[$prop] ?? NULL;
208 }
209 $statusId = $campaign['status_id'] ?? NULL;
210 $campaignsData[$cmpid]['status'] = $campaignStatus[$statusId] ?? NULL;
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
228 if (!empty($campaignsData[$cmpid]['start_date'])) {
229 $campaignsData[$cmpid]['start_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['start_date'],
230 $config->dateformatFull
231 );
232 }
233 if (!empty($campaignsData[$cmpid]['end_date'])) {
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,
240 ['id' => $campaign['id']],
241 ts('more'),
242 FALSE,
243 'campaign.dashboard.row',
244 'Campaign',
245 $campaign['id']
246 );
247 }
248 }
249
250 return $campaignsData;
251 }
252
253 /**
254 * @return mixed
255 */
256 public function browseSurvey() {
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
280 /**
281 * @param array $params
282 *
283 * @return array
284 */
285 public static function getSurveySummary($params = []) {
286 $surveysData = [];
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;
296 $campaignId = $survey['campaign_id'] ?? NULL;
297 $surveysData[$sid]['campaign'] = $campaigns[$campaignId] ?? NULL;
298 $surveysData[$sid]['activity_type'] = $surveyType[$survey['activity_type_id']];
299 if (!empty($survey['release_frequency'])) {
300 $surveysData[$sid]['release_frequency'] = ts('1 Day', ['plural' => '%count Days', 'count' => $survey['release_frequency']]);
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
317 // For some reason, 'is_default' is coming as a string.
318 $surveysData[$sid]['is_default'] = boolval($surveysData[$sid]['is_default']);
319
320 if ($surveysData[$sid]['result_id']) {
321 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
322 $surveysData[$sid]['result_id'] = $resultSet;
323 }
324 else {
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,
330 ['id' => $sid],
331 ts('more'),
332 FALSE,
333 'survey.dashboard.row',
334 'Survey',
335 $sid
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)) {
346 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
347 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
348 }
349 }
350 }
351
352 return $surveysData;
353 }
354
355 /**
356 * Browse petitions.
357 *
358 * @return mixed|null
359 */
360 public function browsePetition() {
361 // Ensure valid javascript - this must have a value set
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);
371 return NULL;
372 }
373 $this->assign('hasPetitions', TRUE);
374
375 // Build the ajax petition search and selector.
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
383 /**
384 * @param array $params
385 *
386 * @return array
387 */
388 public static function getPetitionSummary($params = []) {
389 $config = CRM_Core_Config::singleton();
390 $petitionsData = [];
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;
399 $camapignId = $petition['campaign_id'] ?? NULL;
400 $petitionsData[$pid]['campaign'] = $campaigns[$camapignId] ?? NULL;
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;
417
418 // For some reason, 'is_default' is coming as a string.
419 $petitionsData[$pid]['is_default'] = boolval($petitionsData[$pid]['is_default']);
420
421 $petitionsData[$pid]['action'] = CRM_Core_Action::formLink(self::petitionActionLinks(),
422 $action,
423 ['id' => $pid],
424 ts('more'),
425 FALSE,
426 'petition.dashboard.row',
427 'Petition',
428 $pid
429 );
430 }
431 }
432
433 return $petitionsData;
434 }
435
436 public function browse() {
437 $this->_tabs = [
438 'campaign' => ts('Campaigns'),
439 'survey' => ts('Surveys'),
440 'petition' => ts('Petitions'),
441 ];
442
443 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
444 // Load the data for a specific tab
445 if ($subPageType) {
446 if (!isset($this->_tabs[$subPageType])) {
447 CRM_Utils_System::permissionDenied();
448 }
449 //load the data in tabs.
450 $this->{'browse' . ucfirst($subPageType)}();
451 }
452 // Initialize tabs
453 else {
454 $this->buildTabs();
455 }
456 $this->assign('subPageType', ucfirst($subPageType));
457 }
458
459 /**
460 * @return string
461 */
462 public function run() {
463 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
464 CRM_Utils_System::permissionDenied();
465 }
466
467 $this->browse();
468
469 return parent::run();
470 }
471
472 public function buildTabs() {
473 $allTabs = [];
474 foreach ($this->_tabs as $name => $title) {
475 $allTabs[$name] = [
476 'title' => $title,
477 'valid' => TRUE,
478 'active' => TRUE,
479 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
480 'extra' => NULL,
481 'template' => NULL,
482 'count' => NULL,
483 'icon' => NULL,
484 'class' => NULL,
485 ];
486 }
487 $allTabs['campaign']['class'] = 'livePage';
488 $this->assign('tabHeader', $allTabs);
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 ]);
496 }
497
498 }