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