Merge pull request #15168 from MegaphoneJon/class-fixes
[civicrm-core.git] / CRM / Campaign / Page / DashBoard.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 --------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Page for displaying Campaigns.
36 */
37 class CRM_Campaign_Page_DashBoard extends CRM_Core_Page {
38
39 /**
40 * The action links that we need to display for the browse screen.
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 *
51 * @return array
52 */
53 public static function campaignActionLinks() {
54 // check if variable _actionsLinks is populated
55 if (!isset(self::$_campaignActionLinks)) {
56 self::$_campaignActionLinks = [
57 CRM_Core_Action::UPDATE => [
58 'name' => ts('Edit'),
59 'url' => 'civicrm/campaign/add',
60 'qs' => 'reset=1&action=update&id=%%id%%',
61 'title' => ts('Update Campaign'),
62 ],
63 CRM_Core_Action::DISABLE => [
64 'name' => ts('Disable'),
65 'title' => ts('Disable Campaign'),
66 'ref' => 'crm-enable-disable',
67 ],
68 CRM_Core_Action::ENABLE => [
69 'name' => ts('Enable'),
70 'title' => ts('Enable Campaign'),
71 'ref' => 'crm-enable-disable',
72 ],
73 CRM_Core_Action::DELETE => [
74 'name' => ts('Delete'),
75 'url' => 'civicrm/campaign/add',
76 'qs' => 'action=delete&reset=1&id=%%id%%',
77 'title' => ts('Delete Campaign'),
78 ],
79 ];
80 }
81
82 return self::$_campaignActionLinks;
83 }
84
85 /**
86 * @return array
87 */
88 public static function surveyActionLinks() {
89 // check if variable _actionsLinks is populated
90 if (!isset(self::$_surveyActionLinks)) {
91 self::$_surveyActionLinks = [
92 CRM_Core_Action::UPDATE => [
93 'name' => ts('Edit'),
94 'url' => 'civicrm/survey/configure/main',
95 'qs' => 'action=update&id=%%id%%&reset=1',
96 'title' => ts('Update Survey'),
97 ],
98 CRM_Core_Action::DISABLE => [
99 'name' => ts('Disable'),
100 'ref' => 'crm-enable-disable',
101 'title' => ts('Disable Survey'),
102 ],
103 CRM_Core_Action::ENABLE => [
104 'name' => ts('Enable'),
105 'ref' => 'crm-enable-disable',
106 'title' => ts('Enable Survey'),
107 ],
108 CRM_Core_Action::DELETE => [
109 'name' => ts('Delete'),
110 'url' => 'civicrm/survey/delete',
111 'qs' => 'id=%%id%%&reset=1',
112 'title' => ts('Delete Survey'),
113 ],
114 ];
115 }
116
117 return self::$_surveyActionLinks;
118 }
119
120 /**
121 * @return array
122 */
123 public static function petitionActionLinks() {
124 if (!isset(self::$_petitionActionLinks)) {
125 self::$_petitionActionLinks = self::surveyActionLinks();
126 self::$_petitionActionLinks[CRM_Core_Action::UPDATE] = [
127 'name' => ts('Edit'),
128 'url' => 'civicrm/petition/add',
129 'qs' => 'action=update&id=%%id%%&reset=1',
130 'title' => ts('Update Petition'),
131 ];
132 self::$_petitionActionLinks[CRM_Core_Action::DISABLE] = [
133 'name' => ts('Disable'),
134 'ref' => 'crm-enable-disable',
135 'title' => ts('Disable Petition'),
136 ];
137 self::$_petitionActionLinks[CRM_Core_Action::ENABLE] = [
138 'name' => ts('Enable'),
139 'ref' => 'crm-enable-disable',
140 'title' => ts('Enable Petition'),
141 ];
142 self::$_petitionActionLinks[CRM_Core_Action::DELETE] = [
143 'name' => ts('Delete'),
144 'url' => 'civicrm/petition/add',
145 'qs' => 'action=delete&id=%%id%%&reset=1',
146 'title' => ts('Delete Petition'),
147 ];
148 self::$_petitionActionLinks[CRM_Core_Action::PROFILE] = [
149 'name' => ts('Sign'),
150 'url' => 'civicrm/petition/sign',
151 'qs' => 'sid=%%id%%&reset=1',
152 'title' => ts('Sign Petition'),
153 'fe' => TRUE,
154 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
155 ];
156 self::$_petitionActionLinks[CRM_Core_Action::BROWSE] = [
157 'name' => ts('Signatures'),
158 'url' => 'civicrm/activity/search',
159 'qs' => 'survey=%%id%%&force=1',
160 'title' => ts('List the signatures'),
161 //CRM_Core_Action::PROFILE is used because there isn't a specific action for sign
162 ];
163 }
164
165 return self::$_petitionActionLinks;
166 }
167
168 /**
169 * @return mixed
170 */
171 public function browseCampaign() {
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
194 /**
195 * @param array $params
196 *
197 * @return array
198 */
199 public static function getCampaignSummary($params = []) {
200 $campaignsData = [];
201
202 //get the campaigns.
203 $campaigns = CRM_Campaign_BAO_Campaign::getCampaignSummary($params);
204 if (!empty($campaigns)) {
205 $config = CRM_Core_Config::singleton();
206 $campaignType = CRM_Campaign_PseudoConstant::campaignType();
207 $campaignStatus = CRM_Campaign_PseudoConstant::campaignStatus();
208 $properties = [
209 'id',
210 'name',
211 'title',
212 'status_id',
213 'description',
214 'campaign_type_id',
215 'is_active',
216 'start_date',
217 'end_date',
218 ];
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
242 if (!empty($campaignsData[$cmpid]['start_date'])) {
243 $campaignsData[$cmpid]['start_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['start_date'],
244 $config->dateformatFull
245 );
246 }
247 if (!empty($campaignsData[$cmpid]['end_date'])) {
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,
254 ['id' => $campaign['id']],
255 ts('more'),
256 FALSE,
257 'campaign.dashboard.row',
258 'Campaign',
259 $campaign['id']
260 );
261 }
262 }
263
264 return $campaignsData;
265 }
266
267 /**
268 * @return mixed
269 */
270 public function browseSurvey() {
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
294 /**
295 * @param array $params
296 *
297 * @return array
298 */
299 public static function getSurveySummary($params = []) {
300 $surveysData = [];
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']];
313 if (!empty($survey['release_frequency'])) {
314 $surveysData[$sid]['release_frequency'] = ts('1 Day', ['plural' => '%count Days', 'count' => $survey['release_frequency']]);
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']) {
338 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
339 $surveysData[$sid]['result_id'] = $resultSet;
340 }
341 else {
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,
347 ['id' => $sid],
348 ts('more'),
349 FALSE,
350 'survey.dashboard.row',
351 'Survey',
352 $sid
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)) {
363 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
364 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
365 }
366 }
367 }
368
369 return $surveysData;
370 }
371
372 /**
373 * Browse petitions.
374 *
375 * @return mixed|null
376 */
377 public function browsePetition() {
378 // Ensure valid javascript - this must have a value set
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);
388 return NULL;
389 }
390 $this->assign('hasPetitions', TRUE);
391
392 // Build the ajax petition search and selector.
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
400 /**
401 * @param array $params
402 *
403 * @return array
404 */
405 public static function getPetitionSummary($params = []) {
406 $config = CRM_Core_Config::singleton();
407 $petitionsData = [];
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,
442 ['id' => $pid],
443 ts('more'),
444 FALSE,
445 'petition.dashboard.row',
446 'Petition',
447 $pid
448 );
449 }
450 }
451
452 return $petitionsData;
453 }
454
455 public function browse() {
456 $this->_tabs = [
457 'campaign' => ts('Campaigns'),
458 'survey' => ts('Surveys'),
459 'petition' => ts('Petitions'),
460 ];
461
462 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
463 if ($subPageType) {
464 if (!isset($this->_tabs[$subPageType])) {
465 CRM_Utils_System::permissionDenied();
466 }
467 //load the data in tabs.
468 $this->{'browse' . ucfirst($subPageType)}();
469 $this->assign('subPageType', ucfirst($subPageType));
470 }
471 else {
472 //build the tabs.
473 $this->buildTabs();
474 }
475 CRM_Core_Resources::singleton()
476 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
477 ->addSetting([
478 'tabSettings' => [
479 'active' => strtolower(CRM_Utils_Array::value('subPage', $_GET, 'campaign')),
480 ],
481 ]);
482 }
483
484 /**
485 * @return string
486 */
487 public function run() {
488 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
489 CRM_Utils_System::permissionDenied();
490 }
491
492 $this->browse();
493
494 return parent::run();
495 }
496
497 public function buildTabs() {
498 $allTabs = [];
499 foreach ($this->_tabs as $name => $title) {
500 $allTabs[$name] = [
501 'title' => $title,
502 'valid' => TRUE,
503 'active' => TRUE,
504 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
505 ];
506 }
507 $allTabs['campaign']['class'] = 'livePage';
508 $this->assign('tabHeader', $allTabs);
509 }
510
511 }