Replace all instances of check.gif appearing in listings
[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 /**
33 * Get the action links for this page.
34 *
35 * @return array
36 */
37 public static function campaignActionLinks() {
38 // check if variable _actionsLinks is populated
39 if (!isset(self::$_campaignActionLinks)) {
40 self::$_campaignActionLinks = [
41 CRM_Core_Action::UPDATE => [
42 'name' => ts('Edit'),
43 'url' => 'civicrm/campaign/add',
44 'qs' => 'reset=1&action=update&id=%%id%%',
45 'title' => ts('Update Campaign'),
46 ],
47 CRM_Core_Action::DISABLE => [
48 'name' => ts('Disable'),
49 'title' => ts('Disable Campaign'),
50 'ref' => 'crm-enable-disable',
51 ],
52 CRM_Core_Action::ENABLE => [
53 'name' => ts('Enable'),
54 'title' => ts('Enable Campaign'),
55 'ref' => 'crm-enable-disable',
56 ],
57 CRM_Core_Action::DELETE => [
58 'name' => ts('Delete'),
59 'url' => 'civicrm/campaign/add',
60 'qs' => 'action=delete&reset=1&id=%%id%%',
61 'title' => ts('Delete Campaign'),
62 ],
63 ];
64 }
65
66 return self::$_campaignActionLinks;
67 }
68
69 /**
70 * @return array
71 */
72 public static function surveyActionLinks() {
73 // check if variable _actionsLinks is populated
74 if (!isset(self::$_surveyActionLinks)) {
75 self::$_surveyActionLinks = [
76 CRM_Core_Action::UPDATE => [
77 'name' => ts('Edit'),
78 'url' => 'civicrm/survey/configure/main',
79 'qs' => 'action=update&id=%%id%%&reset=1',
80 'title' => ts('Update Survey'),
81 ],
82 CRM_Core_Action::DISABLE => [
83 'name' => ts('Disable'),
84 'ref' => 'crm-enable-disable',
85 'title' => ts('Disable Survey'),
86 ],
87 CRM_Core_Action::ENABLE => [
88 'name' => ts('Enable'),
89 'ref' => 'crm-enable-disable',
90 'title' => ts('Enable Survey'),
91 ],
92 CRM_Core_Action::DELETE => [
93 'name' => ts('Delete'),
94 'url' => 'civicrm/survey/delete',
95 'qs' => 'id=%%id%%&reset=1',
96 'title' => ts('Delete Survey'),
97 ],
98 ];
99 }
100
101 return self::$_surveyActionLinks;
102 }
103
104 /**
105 * @return array
106 */
107 public static function petitionActionLinks() {
108 if (!isset(self::$_petitionActionLinks)) {
109 self::$_petitionActionLinks = self::surveyActionLinks();
110 self::$_petitionActionLinks[CRM_Core_Action::UPDATE] = [
111 'name' => ts('Edit'),
112 'url' => 'civicrm/petition/add',
113 'qs' => 'action=update&id=%%id%%&reset=1',
114 'title' => ts('Update Petition'),
115 ];
116 self::$_petitionActionLinks[CRM_Core_Action::DISABLE] = [
117 'name' => ts('Disable'),
118 'ref' => 'crm-enable-disable',
119 'title' => ts('Disable Petition'),
120 ];
121 self::$_petitionActionLinks[CRM_Core_Action::ENABLE] = [
122 'name' => ts('Enable'),
123 'ref' => 'crm-enable-disable',
124 'title' => ts('Enable Petition'),
125 ];
126 self::$_petitionActionLinks[CRM_Core_Action::DELETE] = [
127 'name' => ts('Delete'),
128 'url' => 'civicrm/petition/add',
129 'qs' => 'action=delete&id=%%id%%&reset=1',
130 'title' => ts('Delete Petition'),
131 ];
132 self::$_petitionActionLinks[CRM_Core_Action::PROFILE] = [
133 'name' => ts('Sign'),
134 'url' => 'civicrm/petition/sign',
135 'qs' => 'sid=%%id%%&reset=1',
136 'title' => ts('Sign Petition'),
137 'fe' => TRUE,
138 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
139 ];
140 self::$_petitionActionLinks[CRM_Core_Action::BROWSE] = [
141 'name' => ts('Signatures'),
142 'url' => 'civicrm/activity/search',
143 'qs' => 'survey=%%id%%&force=1',
144 'title' => ts('List the signatures'),
145 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
146 ];
147 }
148
149 return self::$_petitionActionLinks;
150 }
151
152 /**
153 * @return mixed
154 */
155 public function browseCampaign() {
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
178 /**
179 * @param array $params
180 *
181 * @return array
182 */
183 public static function getCampaignSummary($params = []) {
184 $campaignsData = [];
185
186 //get the campaigns.
187 $campaigns = CRM_Campaign_BAO_Campaign::getCampaignSummary($params);
188 if (!empty($campaigns)) {
189 $config = CRM_Core_Config::singleton();
190 $campaignType = CRM_Campaign_PseudoConstant::campaignType();
191 $campaignStatus = CRM_Campaign_PseudoConstant::campaignStatus();
192 $properties = [
193 'id',
194 'name',
195 'title',
196 'status_id',
197 'description',
198 'campaign_type_id',
199 'is_active',
200 'start_date',
201 'end_date',
202 ];
203 foreach ($campaigns as $cmpid => $campaign) {
204 foreach ($properties as $prop) {
205 $campaignsData[$cmpid][$prop] = $campaign[$prop] ?? NULL;
206 }
207 $statusId = $campaign['status_id'] ?? NULL;
208 $campaignsData[$cmpid]['status'] = $campaignStatus[$statusId] ?? NULL;
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
226 if (!empty($campaignsData[$cmpid]['start_date'])) {
227 $campaignsData[$cmpid]['start_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['start_date'],
228 $config->dateformatFull
229 );
230 }
231 if (!empty($campaignsData[$cmpid]['end_date'])) {
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,
238 ['id' => $campaign['id']],
239 ts('more'),
240 FALSE,
241 'campaign.dashboard.row',
242 'Campaign',
243 $campaign['id']
244 );
245 }
246 }
247
248 return $campaignsData;
249 }
250
251 /**
252 * @return mixed
253 */
254 public function browseSurvey() {
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
278 /**
279 * @param array $params
280 *
281 * @return array
282 */
283 public static function getSurveySummary($params = []) {
284 $surveysData = [];
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;
294 $campaignId = $survey['campaign_id'] ?? NULL;
295 $surveysData[$sid]['campaign'] = $campaigns[$campaignId] ?? NULL;
296 $surveysData[$sid]['activity_type'] = $surveyType[$survey['activity_type_id']];
297 if (!empty($survey['release_frequency'])) {
298 $surveysData[$sid]['release_frequency'] = ts('1 Day', ['plural' => '%count Days', 'count' => $survey['release_frequency']]);
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
315 $surveysData[$sid]['is_default'] = self::crmIcon('fa-check', ts('Default'), $surveysData[$sid]['is_default']);
316
317 if ($surveysData[$sid]['result_id']) {
318 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
319 $surveysData[$sid]['result_id'] = $resultSet;
320 }
321 else {
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,
327 ['id' => $sid],
328 ts('more'),
329 FALSE,
330 'survey.dashboard.row',
331 'Survey',
332 $sid
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)) {
343 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
344 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
345 }
346 }
347 }
348
349 return $surveysData;
350 }
351
352 /**
353 * Browse petitions.
354 *
355 * @return mixed|null
356 */
357 public function browsePetition() {
358 // Ensure valid javascript - this must have a value set
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);
368 return NULL;
369 }
370 $this->assign('hasPetitions', TRUE);
371
372 // Build the ajax petition search and selector.
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
380 /**
381 * @param array $params
382 *
383 * @return array
384 */
385 public static function getPetitionSummary($params = []) {
386 $config = CRM_Core_Config::singleton();
387 $petitionsData = [];
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;
396 $camapignId = $petition['campaign_id'] ?? NULL;
397 $petitionsData[$pid]['campaign'] = $campaigns[$camapignId] ?? NULL;
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;
414 $petitionsData[$pid]['is_default'] = self::crmIcon('fa-check', ts('Default'), $petitionsData[$pid]['is_default']);
415
416 $petitionsData[$pid]['action'] = CRM_Core_Action::formLink(self::petitionActionLinks(),
417 $action,
418 ['id' => $pid],
419 ts('more'),
420 FALSE,
421 'petition.dashboard.row',
422 'Petition',
423 $pid
424 );
425 }
426 }
427
428 return $petitionsData;
429 }
430
431 public function browse() {
432 $this->_tabs = [
433 'campaign' => ts('Campaigns'),
434 'survey' => ts('Surveys'),
435 'petition' => ts('Petitions'),
436 ];
437
438 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
439 if ($subPageType) {
440 if (!isset($this->_tabs[$subPageType])) {
441 CRM_Utils_System::permissionDenied();
442 }
443 //load the data in tabs.
444 $this->{'browse' . ucfirst($subPageType)}();
445 $this->assign('subPageType', ucfirst($subPageType));
446 }
447 else {
448 //build the tabs.
449 $this->buildTabs();
450 }
451 CRM_Core_Resources::singleton()
452 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
453 ->addSetting([
454 'tabSettings' => [
455 'active' => strtolower(CRM_Utils_Array::value('subPage', $_GET, 'campaign')),
456 ],
457 ]);
458 }
459
460 /**
461 * @return string
462 */
463 public function run() {
464 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
465 CRM_Utils_System::permissionDenied();
466 }
467
468 $this->browse();
469
470 return parent::run();
471 }
472
473 public function buildTabs() {
474 $allTabs = [];
475 foreach ($this->_tabs as $name => $title) {
476 $allTabs[$name] = [
477 'title' => $title,
478 'valid' => TRUE,
479 'active' => TRUE,
480 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
481 ];
482 }
483 $allTabs['campaign']['class'] = 'livePage';
484 $this->assign('tabHeader', $allTabs);
485 }
486
487 }