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