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