Merge pull request #5101 from agh1/context-abbrev
[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',
213 'name',
214 'title',
215 'status_id',
216 'description',
217 'campaign_type_id',
218 'is_active',
219 'start_date',
220 'end_date',
221 );
222 foreach ($campaigns as $cmpid => $campaign) {
223 foreach ($properties as $prop) {
224 $campaignsData[$cmpid][$prop] = CRM_Utils_Array::value($prop, $campaign);
225 }
226 $statusId = CRM_Utils_Array::value('status_id', $campaign);
227 $campaignsData[$cmpid]['status'] = CRM_Utils_Array::value($statusId, $campaignStatus);
228 $campaignsData[$cmpid]['campaign_id'] = $campaign['id'];
229 $campaignsData[$cmpid]['campaign_type'] = $campaignType[$campaign['campaign_type_id']];
230
231 $action = array_sum(array_keys(self::campaignActionLinks()));
232 if ($campaign['is_active']) {
233 $action -= CRM_Core_Action::ENABLE;
234 }
235 else {
236 $action -= CRM_Core_Action::DISABLE;
237 }
238
239 $isActive = ts('No');
240 if ($campaignsData[$cmpid]['is_active']) {
241 $isActive = ts('Yes');
242 }
243 $campaignsData[$cmpid]['isActive'] = $isActive;
244
245 if (!empty($campaignsData[$cmpid]['start_date'])) {
246 $campaignsData[$cmpid]['start_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['start_date'],
247 $config->dateformatFull
248 );
249 }
250 if (!empty($campaignsData[$cmpid]['end_date'])) {
251 $campaignsData[$cmpid]['end_date'] = CRM_Utils_Date::customFormat($campaignsData[$cmpid]['end_date'],
252 $config->dateformatFull
253 );
254 }
255 $campaignsData[$cmpid]['action'] = CRM_Core_Action::formLink(self::campaignActionLinks(),
256 $action,
257 array('id' => $campaign['id']),
258 ts('more'),
259 FALSE,
260 'campaign.dashboard.row',
261 'Campaign',
262 $campaign['id']
263 );
264 }
265 }
266
267 return $campaignsData;
268 }
269
270 /**
271 * @return mixed
272 */
273 public function browseSurvey() {
274 // ensure valid javascript - this must have a value set
275 $this->assign('searchParams', json_encode(NULL));
276 $this->assign('surveyTypes', json_encode(NULL));
277 $this->assign('surveyCampaigns', json_encode(NULL));
278
279 $this->assign('addSurveyUrl', CRM_Utils_System::url('civicrm/survey/add', 'reset=1&action=add'));
280
281 $surveyCount = CRM_Campaign_BAO_Survey::getSurveyCount();
282 //don't load find interface when no survey in db.
283 if (!$surveyCount) {
284 $this->assign('hasSurveys', FALSE);
285 return;
286 }
287 $this->assign('hasSurveys', TRUE);
288
289 //build the ajaxify survey search and selector.
290 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Survey', ts('Search Survey'));
291 $controller->set('searchTab', 'survey');
292 $controller->setEmbedded(TRUE);
293 $controller->process();
294 return $controller->run();
295 }
296
297 /**
298 * @param array $params
299 *
300 * @return array
301 */
302 public function getSurveySummary($params = array()) {
303 $surveysData = array();
304
305 //get the survey.
306 $config = CRM_Core_Config::singleton();
307 $surveys = CRM_Campaign_BAO_Survey::getSurveySummary($params);
308 if (!empty($surveys)) {
309 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
310 $surveyType = CRM_Campaign_BAO_Survey::getSurveyActivityType();
311 foreach ($surveys as $sid => $survey) {
312 $surveysData[$sid] = $survey;
313 $campaignId = CRM_Utils_Array::value('campaign_id', $survey);
314 $surveysData[$sid]['campaign'] = CRM_Utils_Array::value($campaignId, $campaigns);
315 $surveysData[$sid]['activity_type'] = $surveyType[$survey['activity_type_id']];
316 if (!empty($survey['release_frequency'])) {
317 $surveysData[$sid]['release_frequency'] = $survey['release_frequency'] . ' Day(s)';
318 }
319
320 $action = array_sum(array_keys(self::surveyActionLinks($surveysData[$sid]['activity_type'])));
321 if ($survey['is_active']) {
322 $action -= CRM_Core_Action::ENABLE;
323 }
324 else {
325 $action -= CRM_Core_Action::DISABLE;
326 }
327
328 $isActive = ts('No');
329 if ($surveysData[$sid]['is_active']) {
330 $isActive = ts('Yes');
331 }
332 $surveysData[$sid]['isActive'] = $isActive;
333
334 $isDefault = NULL;
335 if ($surveysData[$sid]['is_default']) {
336 $isDefault = '<img src="' . $config->resourceBase . 'i/check.gif" alt="' . ts('Default') . '" />';
337 }
338 $surveysData[$sid]['is_default'] = $isDefault;
339
340 if ($surveysData[$sid]['result_id']) {
341 $resultSet = '<a href= "javascript:displayResultSet( ' . $sid . ',' . "'" . $surveysData[$sid]['title'] . "'" . ', ' . $surveysData[$sid]['result_id'] . ' )" title="' . ts('view result set') . '">' . ts('Result Set') . '</a>';
342 $surveysData[$sid]['result_id'] = $resultSet;
343 }
344 else {
345 $resultUrl = CRM_Utils_System::url("civicrm/survey/configure/results", "action=update&id={$sid}&reset=1");
346 $surveysData[$sid]['result_id'] = "<a href='{$resultUrl}' class='status-warning'>(" . ts('Incomplete. Click to configure result set.') . ')</a>';
347 }
348 $surveysData[$sid]['action'] = CRM_Core_Action::formLink(self::surveyActionLinks($surveysData[$sid]['activity_type']),
349 $action,
350 array('id' => $sid),
351 ts('more'),
352 FALSE,
353 'survey.dashboard.row',
354 'Survey',
355 $sid
356 );
357
358 if (CRM_Utils_Array::value('activity_type', $surveysData[$sid]) != 'Petition') {
359 $surveysData[$sid]['voterLinks'] = CRM_Campaign_BAO_Survey::buildPermissionLinks($sid,
360 TRUE,
361 ts('more')
362 );
363 }
364
365 if ($reportID = CRM_Campaign_BAO_Survey::getReportID($sid)) {
366 $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1');
367 $surveysData[$sid]['title'] = "<a href='{$url}' title='View Survey Report'>{$surveysData[$sid]['title']}</a>";
368 }
369 }
370 }
371
372 return $surveysData;
373 }
374
375 /**
376 * Browse petitions.
377 *
378 * @return mixed|null
379 */
380 public function browsePetition() {
381 // Ensure valid javascript - this must have a value set
382 $this->assign('searchParams', json_encode(NULL));
383 $this->assign('petitionCampaigns', json_encode(NULL));
384
385 $this->assign('addPetitionUrl', CRM_Utils_System::url('civicrm/petition/add', 'reset=1&action=add'));
386
387 $petitionCount = CRM_Campaign_BAO_Petition::getPetitionCount();
388 //don't load find interface when no petition in db.
389 if (!$petitionCount) {
390 $this->assign('hasPetitions', FALSE);
391 return NULL;
392 }
393 $this->assign('hasPetitions', TRUE);
394
395 // Build the ajax petition search and selector.
396 $controller = new CRM_Core_Controller_Simple('CRM_Campaign_Form_Search_Petition', ts('Search Petition'));
397 $controller->set('searchTab', 'petition');
398 $controller->setEmbedded(TRUE);
399 $controller->process();
400 return $controller->run();
401 }
402
403 /**
404 * @param array $params
405 *
406 * @return array
407 */
408 public function getPetitionSummary($params = array()) {
409 $config = CRM_Core_Config::singleton();
410 $petitionsData = array();
411
412 //get the petitions.
413 $petitions = CRM_Campaign_BAO_Petition::getPetitionSummary($params);
414 if (!empty($petitions)) {
415 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
416 $petitionType = CRM_Campaign_BAO_Survey::getSurveyActivityType('label', TRUE);
417 foreach ($petitions as $pid => $petition) {
418 $petitionsData[$pid] = $petition;
419 $camapignId = CRM_Utils_Array::value('campaign_id', $petition);
420 $petitionsData[$pid]['campaign'] = CRM_Utils_Array::value($camapignId, $campaigns);
421 $petitionsData[$pid]['activity_type'] = $petitionType[$petition['activity_type_id']];
422
423 $action = array_sum(array_keys(self::petitionActionLinks()));
424
425 if ($petition['is_active']) {
426 $action -= CRM_Core_Action::ENABLE;
427 }
428 else {
429 $action -= CRM_Core_Action::DISABLE;
430 }
431
432 $isActive = ts('No');
433 if ($petitionsData[$pid]['is_active']) {
434 $isActive = ts('Yes');
435 }
436 $petitionsData[$pid]['isActive'] = $isActive;
437 $isDefault = NULL;
438 if ($petitionsData[$pid]['is_default']) {
439 $isDefault = '<img src="' . $config->resourceBase . 'i/check.gif" alt="' . ts('Default') . '" />';
440 }
441 $petitionsData[$pid]['is_default'] = $isDefault;
442
443 $petitionsData[$pid]['action'] = CRM_Core_Action::formLink(self::petitionActionLinks(),
444 $action,
445 array('id' => $pid),
446 ts('more'),
447 FALSE,
448 'petition.dashboard.row',
449 'Petition',
450 $pid
451 );
452 }
453 }
454
455 return $petitionsData;
456 }
457
458 public function browse() {
459 $this->_tabs = array(
460 'campaign' => ts('Campaigns'),
461 'survey' => ts('Surveys'),
462 'petition' => ts('Petitions'),
463 );
464
465 $subPageType = CRM_Utils_Request::retrieve('type', 'String', $this);
466 if ($subPageType) {
467 if (!isset($this->_tabs[$subPageType])) {
468 CRM_Utils_System::permissionDenied();
469 }
470 //load the data in tabs.
471 $this->{'browse' . ucfirst($subPageType)}();
472 $this->assign('subPageType', ucfirst($subPageType));
473 }
474 else {
475 //build the tabs.
476 $this->buildTabs();
477 }
478 CRM_Core_Resources::singleton()
479 ->addScriptFile('civicrm', 'templates/CRM/common/TabHeader.js', 1, 'html-header')
480 ->addSetting(array(
481 'tabSettings' => array(
482 'active' => strtolower(CRM_Utils_Array::value('subPage', $_GET, 'campaign')),
483 ),
484 ));
485 }
486
487 /**
488 * @return string
489 */
490 public function run() {
491 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
492 CRM_Utils_System::permissionDenied();
493 }
494
495 $this->browse();
496
497 return parent::run();
498 }
499
500 public function buildTabs() {
501 $allTabs = array();
502 foreach ($this->_tabs as $name => $title) {
503 $allTabs[$name] = array(
504 'title' => $title,
505 'valid' => TRUE,
506 'active' => TRUE,
507 'link' => CRM_Utils_System::url('civicrm/campaign', "reset=1&type=$name"),
508 );
509 }
510 $allTabs['campaign']['class'] = 'livePage';
511 $this->assign('tabHeader', $allTabs);
512 }
513
514 }