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