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