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