Merge pull request #17588 from artfulrobot/artfulrobot-property-bag-support-empty
[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 // For some reason, 'is_default' is coming as a string.
316 $surveysData[$sid]['is_default'] = boolval($surveysData[$sid]['is_default']);
317
318 if ($surveysData[$sid]['result_id']) {
319 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
320 $surveysData[$sid]['result_id'] = $resultSet;
321 }
322 else {
323 $resultUrl = CRM_Utils_System::url("civicrm/survey/configure/results", "action=update&id={$sid}&reset=1");
324 $surveysData[$sid]['result_id'] = "<a href='{$resultUrl}' class='status-warning'>(" . ts('Incomplete. Click to configure result set.') . ')</a>';
325 }
326 $surveysData[$sid]['action'] = CRM_Core_Action::formLink(self::surveyActionLinks($surveysData[$sid]['activity_type']),
327 $action,
328 ['id' => $sid],
329 ts('more'),
330 FALSE,
331 'survey.dashboard.row',
332 'Survey',
333 $sid
334 );
335
336 if (CRM_Utils_Array::value('activity_type', $surveysData[$sid]) != 'Petition') {
337 $surveysData[$sid]['voterLinks'] = CRM_Campaign_BAO_Survey::buildPermissionLinks($sid,
338 TRUE,
339 ts('more')
340 );
341 }
342
343 if ($reportID = CRM_Campaign_BAO_Survey::getReportID($sid)) {
344 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
345 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
346 }
347 }
348 }
349
350 return $surveysData;
351 }
352
353 /**
354 * Browse petitions.
355 *
356 * @return mixed|null
357 */
358 public function browsePetition() {
359 // Ensure valid javascript - this must have a value set
360 $this->assign('searchParams', json_encode(NULL));
361 $this->assign('petitionCampaigns', json_encode(NULL));
362
363 $this->assign('addPetitionUrl', CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
364
365 $petitionCount = CRM_Campaign_BAO_Petition::getPetitionCount();
366 //don't load find interface when no petition in db.
367 if (!$petitionCount) {
368 $this->assign('hasPetitions', FALSE);
369 return NULL;
370 }
371 $this->assign('hasPetitions', TRUE);
372
373 // Build the ajax petition search and selector.
374 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Petition', ts('Search Petition'));
375 $controller->set('searchTab', 'petition');
376 $controller->setEmbedded(TRUE);
377 $controller->process();
378 return $controller->run();
379 }
380
381 /**
382 * @param array $params
383 *
384 * @return array
385 */
386 public static function getPetitionSummary($params = []) {
387 $config = CRM_Core_Config::singleton();
388 $petitionsData = [];
389
390 //get the petitions.
391 $petitions = CRM_Campaign_BAO_Petition::getPetitionSummary($params);
392 if (!empty($petitions)) {
393 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
394 $petitionType = CRM_Campaign_BAO_Survey::getSurveyActivityType('label', TRUE);
395 foreach ($petitions as $pid => $petition) {
396 $petitionsData[$pid] = $petition;
397 $camapignId = $petition['campaign_id'] ?? NULL;
398 $petitionsData[$pid]['campaign'] = $campaigns[$camapignId] ?? NULL;
399 $petitionsData[$pid]['activity_type'] = $petitionType[$petition['activity_type_id']];
400
401 $action = array_sum(array_keys(self::petitionActionLinks()));
402
403 if ($petition['is_active']) {
404 $action -= CRM_Core_Action::ENABLE;
405 }
406 else {
407 $action -= CRM_Core_Action::DISABLE;
408 }
409
410 $isActive = ts('No');
411 if ($petitionsData[$pid]['is_active']) {
412 $isActive = ts('Yes');
413 }
414 $petitionsData[$pid]['isActive'] = $isActive;
415
416 // For some reason, 'is_default' is coming as a string.
417 $petitionsData[$pid]['is_default'] = boolval($petitionsData[$pid]['is_default']);
418
419 $petitionsData[$pid]['action'] = CRM_Core_Action::formLink(self::petitionActionLinks(),
420 $action,
421 ['id' => $pid],
422 ts('more'),
423 FALSE,
424 'petition.dashboard.row',
425 'Petition',
426 $pid
427 );
428 }
429 }
430
431 return $petitionsData;
432 }
433
434 public function browse() {
435 $this->_tabs = [
436 'campaign' => ts('Campaigns'),
437 'survey' => ts('Surveys'),
438 'petition' => ts('Petitions'),
439 ];
440
441 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
442 if ($subPageType) {
443 if (!isset($this->_tabs[$subPageType])) {
444 CRM_Utils_System::permissionDenied();
445 }
446 //load the data in tabs.
447 $this->{'browse' . ucfirst($subPageType)}();
448 $this->assign('subPageType', ucfirst($subPageType));
449 }
450 else {
451 //build the tabs.
452 $this->buildTabs();
453 }
454 CRM_Core_Resources::singleton()
455 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
456 ->addSetting([
457 'tabSettings' => [
458 'active' => strtolower(CRM_Utils_Array::value('subPage', $_GET, 'campaign')),
459 ],
460 ]);
461 }
462
463 /**
464 * @return string
465 */
466 public function run() {
467 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
468 CRM_Utils_System::permissionDenied();
469 }
470
471 $this->browse();
472
473 return parent::run();
474 }
475
476 public function buildTabs() {
477 $allTabs = [];
478 foreach ($this->_tabs as $name => $title) {
479 $allTabs[$name] = [
480 'title' => $title,
481 'valid' => TRUE,
482 'active' => TRUE,
483 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
484 ];
485 }
486 $allTabs['campaign']['class'] = 'livePage';
487 $this->assign('tabHeader', $allTabs);
488 }
489
490 }