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