| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 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 | * This file is for Pledge search |
| 38 | */ |
| 39 | class CRM_Pledge_Form_Search extends CRM_Core_Form { |
| 40 | |
| 41 | /** |
| 42 | * Are we forced to run a search |
| 43 | * |
| 44 | * @var int |
| 45 | * @access protected |
| 46 | */ |
| 47 | protected $_force; |
| 48 | |
| 49 | /** |
| 50 | * name of search button |
| 51 | * |
| 52 | * @var string |
| 53 | * @access protected |
| 54 | */ |
| 55 | protected $_searchButtonName; |
| 56 | |
| 57 | /** |
| 58 | * name of print button |
| 59 | * |
| 60 | * @var string |
| 61 | * @access protected |
| 62 | */ |
| 63 | protected $_printButtonName; |
| 64 | |
| 65 | /** |
| 66 | * name of action button |
| 67 | * |
| 68 | * @var string |
| 69 | * @access protected |
| 70 | */ |
| 71 | protected $_actionButtonName; |
| 72 | |
| 73 | /** |
| 74 | * form values that we will be using |
| 75 | * |
| 76 | * @var array |
| 77 | * @access protected |
| 78 | */ |
| 79 | protected $_formValues; |
| 80 | |
| 81 | /** |
| 82 | * the params that are sent to the query |
| 83 | * |
| 84 | * @var array |
| 85 | * @access protected |
| 86 | */ |
| 87 | protected $_queryParams; |
| 88 | |
| 89 | /** |
| 90 | * have we already done this search |
| 91 | * |
| 92 | * @access protected |
| 93 | * @var boolean |
| 94 | */ |
| 95 | protected $_done; |
| 96 | |
| 97 | /** |
| 98 | * are we restricting ourselves to a single contact |
| 99 | * |
| 100 | * @access protected |
| 101 | * @var boolean |
| 102 | */ |
| 103 | protected $_single = FALSE; |
| 104 | |
| 105 | /** |
| 106 | * are we restricting ourselves to a single contact |
| 107 | * |
| 108 | * @access protected |
| 109 | * @var boolean |
| 110 | */ |
| 111 | protected $_limit = NULL; |
| 112 | |
| 113 | /** |
| 114 | * what context are we being invoked from |
| 115 | * |
| 116 | * @access protected |
| 117 | * @var string |
| 118 | */ |
| 119 | protected $_context = NULL; |
| 120 | |
| 121 | /** |
| 122 | * prefix for the controller |
| 123 | * |
| 124 | */ |
| 125 | protected $_prefix = "pledge_"; |
| 126 | |
| 127 | protected $_defaults; |
| 128 | |
| 129 | /** |
| 130 | * processing needed for buildForm and later |
| 131 | * |
| 132 | * @return void |
| 133 | * @access public |
| 134 | */ |
| 135 | function preProcess() { |
| 136 | |
| 137 | /** |
| 138 | * set the button names |
| 139 | */ |
| 140 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 141 | $this->_printButtonName = $this->getButtonName('next', 'print'); |
| 142 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 143 | |
| 144 | $this->_done = FALSE; |
| 145 | $this->defaults = array(); |
| 146 | |
| 147 | /* |
| 148 | * we allow the controller to set force/reset externally, useful when we are being |
| 149 | * driven by the wizard framework |
| 150 | */ |
| 151 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 152 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
| 153 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
| 154 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
| 155 | |
| 156 | $this->assign("context", $this->_context); |
| 157 | |
| 158 | // get user submitted values |
| 159 | // get it from controller only if form has been submitted, else preProcess has set this |
| 160 | if (!empty($_POST) && !$this->controller->isModal()) { |
| 161 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 162 | } |
| 163 | else { |
| 164 | $this->_formValues = $this->get('formValues'); |
| 165 | } |
| 166 | |
| 167 | if (empty($this->_formValues)) { |
| 168 | if (isset($this->_ssID)) { |
| 169 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if ($this->_force) { |
| 174 | $this->postProcess(); |
| 175 | $this->set('force', 0); |
| 176 | } |
| 177 | |
| 178 | $sortID = NULL; |
| 179 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 180 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 181 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 187 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, |
| 188 | $this->_action, |
| 189 | NULL, |
| 190 | $this->_single, |
| 191 | $this->_limit, |
| 192 | $this->_context |
| 193 | ); |
| 194 | $prefix = NULL; |
| 195 | if ($this->_context == 'user') { |
| 196 | $prefix = $this->_prefix; |
| 197 | } |
| 198 | |
| 199 | $this->assign("{$prefix}limit", $this->_limit); |
| 200 | $this->assign("{$prefix}single", $this->_single); |
| 201 | |
| 202 | $controller = new CRM_Core_Selector_Controller($selector, |
| 203 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 204 | $sortID, |
| 205 | CRM_Core_Action::VIEW, |
| 206 | $this, |
| 207 | CRM_Core_Selector_Controller::TRANSFER, |
| 208 | $prefix |
| 209 | ); |
| 210 | $controller->setEmbedded(TRUE); |
| 211 | $controller->moveFromSessionToTemplate(); |
| 212 | |
| 213 | $this->assign('summary', $this->get('summary')); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Build the form |
| 218 | * |
| 219 | * @access public |
| 220 | * |
| 221 | * @return void |
| 222 | */ |
| 223 | function buildQuickForm() { |
| 224 | $this->addElement('text', 'sort_name', ts('Pledger Name or Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
| 225 | |
| 226 | CRM_Pledge_BAO_Query::buildSearchForm($this); |
| 227 | |
| 228 | /* |
| 229 | * add form checkboxes for each row. This is needed out here to conform to QF protocol |
| 230 | * of all elements being declared in builQuickForm |
| 231 | */ |
| 232 | $rows = $this->get('rows'); |
| 233 | if (is_array($rows)) { |
| 234 | |
| 235 | if (!$this->_single) { |
| 236 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true ); return toggleCheckboxVals('mark_x_',this);")); |
| 237 | |
| 238 | foreach ($rows as $row) { |
| 239 | $this->addElement('checkbox', $row['checkbox'], |
| 240 | NULL, NULL, |
| 241 | array('onclick' => "toggleTaskAction( true ); return checkSelectedBox('" . $row['checkbox'] . "');") |
| 242 | ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | $total = $cancel = 0; |
| 247 | |
| 248 | $permission = CRM_Core_Permission::getPermission(); |
| 249 | |
| 250 | $tasks = array('' => ts('- actions -')) + CRM_Pledge_Task::permissionedTaskTitles($permission); |
| 251 | |
| 252 | $this->add('select', 'task', ts('Actions:') . ' ', $tasks); |
| 253 | $this->add('submit', $this->_actionButtonName, ts('Go'), |
| 254 | array( |
| 255 | 'class' => 'form-submit', |
| 256 | 'id' => 'Go', |
| 257 | 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 0);", |
| 258 | ) |
| 259 | ); |
| 260 | |
| 261 | $this->add('submit', $this->_printButtonName, ts('Print'), |
| 262 | array( |
| 263 | 'class' => 'form-submit', |
| 264 | 'onclick' => "return checkPerformAction('mark_x', '" . $this->getName() . "', 1);", |
| 265 | ) |
| 266 | ); |
| 267 | |
| 268 | // need to perform tasks on all or selected items ? using radio_ts(task selection) for it |
| 269 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); |
| 270 | $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('onclick' => $this->getName() . ".toggleSelect.checked = false; toggleCheckboxVals('mark_x_',this); toggleTaskAction( true );")); |
| 271 | } |
| 272 | |
| 273 | // add buttons |
| 274 | $this->addButtons(array( |
| 275 | array( |
| 276 | 'type' => 'refresh', |
| 277 | 'name' => ts('Search'), |
| 278 | 'isDefault' => TRUE, |
| 279 | ), |
| 280 | )); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * The post processing of the form gets done here. |
| 285 | * |
| 286 | * Key things done during post processing are |
| 287 | * - check for reset or next request. if present, skip post procesing. |
| 288 | * - now check if user requested running a saved search, if so, then |
| 289 | * the form values associated with the saved search are used for searching. |
| 290 | * - if user has done a submit with new values the regular post submissing is |
| 291 | * done. |
| 292 | * The processing consists of using a Selector / Controller framework for getting the |
| 293 | * search results. |
| 294 | * |
| 295 | * @param |
| 296 | * |
| 297 | * @return void |
| 298 | * @access public |
| 299 | */ |
| 300 | function postProcess() { |
| 301 | if ($this->_done) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | $this->_done = TRUE; |
| 306 | |
| 307 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 308 | |
| 309 | $this->fixFormValues(); |
| 310 | |
| 311 | // we don't show test pledges in Contact Summary / User Dashboard |
| 312 | if (empty($this->_formValues['pledge_test']) && $this->_single) { |
| 313 | $this->_formValues["pledge_test"] = 0; |
| 314 | } |
| 315 | |
| 316 | if (isset($this->_ssID) && empty($_POST)) { |
| 317 | // if we are editing / running a saved search and the form has not been posted |
| 318 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 319 | } |
| 320 | |
| 321 | CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); |
| 322 | |
| 323 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 324 | |
| 325 | $this->set('formValues', $this->_formValues); |
| 326 | $this->set('queryParams', $this->_queryParams); |
| 327 | |
| 328 | $buttonName = $this->controller->getButtonName(); |
| 329 | if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) { |
| 330 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 331 | |
| 332 | // hack, make sure we reset the task values |
| 333 | $stateMachine = &$this->controller->getStateMachine(); |
| 334 | $formName = $stateMachine->getTaskFormName(); |
| 335 | $this->controller->resetPage($formName); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | $sortID = NULL; |
| 340 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 341 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 342 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 343 | ); |
| 344 | } |
| 345 | |
| 346 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 347 | |
| 348 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, |
| 349 | $this->_action, |
| 350 | NULL, |
| 351 | $this->_single, |
| 352 | $this->_limit, |
| 353 | $this->_context |
| 354 | ); |
| 355 | $selector->setKey($this->controller->_key); |
| 356 | |
| 357 | $prefix = NULL; |
| 358 | if ($this->_context == 'user') { |
| 359 | $prefix = $this->_prefix; |
| 360 | } |
| 361 | |
| 362 | $this->assign("{$prefix}limit", $this->_limit); |
| 363 | $this->assign("{$prefix}single", $this->_single); |
| 364 | |
| 365 | $controller = new CRM_Core_Selector_Controller($selector, |
| 366 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 367 | $sortID, |
| 368 | CRM_Core_Action::VIEW, |
| 369 | $this, |
| 370 | CRM_Core_Selector_Controller::SESSION, |
| 371 | $prefix |
| 372 | ); |
| 373 | $controller->setEmbedded(TRUE); |
| 374 | |
| 375 | $query = &$selector->getQuery(); |
| 376 | if ($this->_context == 'user') { |
| 377 | $query->setSkipPermission(TRUE); |
| 378 | } |
| 379 | $controller->run(); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * This function is used to add the rules (mainly global rules) for form. |
| 384 | * All local rules are added near the element |
| 385 | * |
| 386 | * @return None |
| 387 | * @access public |
| 388 | * @see valid_date |
| 389 | */ |
| 390 | function addRules() { |
| 391 | $this->addFormRule(array('CRM_Pledge_Form_Search', 'formRule')); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * global validation rules for the form |
| 396 | * |
| 397 | * @param array $fields posted values of the form |
| 398 | * @param array $errors list of errors to be posted back to the form |
| 399 | * |
| 400 | * @return void |
| 401 | * @static |
| 402 | * @access public |
| 403 | */ |
| 404 | static function formRule($fields) { |
| 405 | $errors = array(); |
| 406 | |
| 407 | if (!empty($errors)) { |
| 408 | return $errors; |
| 409 | } |
| 410 | |
| 411 | return TRUE; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Set the default form values |
| 416 | * |
| 417 | * @access protected |
| 418 | * |
| 419 | * @return array the default array reference |
| 420 | */ |
| 421 | function setDefaultValues() { |
| 422 | $defaults = array(); |
| 423 | $defaults = $this->_formValues; |
| 424 | return $defaults; |
| 425 | } |
| 426 | |
| 427 | function fixFormValues() { |
| 428 | if (!$this->_force) { |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | // set pledge payment related fields |
| 433 | $status = CRM_Utils_Request::retrieve('status', 'String', |
| 434 | CRM_Core_DAO::$_nullObject |
| 435 | ); |
| 436 | if ($status) { |
| 437 | $this->_formValues['pledge_payment_status_id'] = array($status => 1); |
| 438 | $this->_defaults['pledge_payment_status_id'] = array($status => 1); |
| 439 | } |
| 440 | |
| 441 | $fromDate = CRM_Utils_Request::retrieve('start', 'Date', |
| 442 | CRM_Core_DAO::$_nullObject |
| 443 | ); |
| 444 | if ($fromDate) { |
| 445 | list($date) = CRM_Utils_Date::setDateDefaults($fromDate); |
| 446 | $this->_formValues['pledge_payment_date_low'] = $date; |
| 447 | $this->_defaults['pledge_payment_date_low'] = $date; |
| 448 | } |
| 449 | |
| 450 | $toDate = CRM_Utils_Request::retrieve('end', 'Date', |
| 451 | CRM_Core_DAO::$_nullObject |
| 452 | ); |
| 453 | if ($toDate) { |
| 454 | list($date) = CRM_Utils_Date::setDateDefaults($toDate); |
| 455 | $this->_formValues['pledge_payment_date_high'] = $date; |
| 456 | $this->_defaults['pledge_payment_date_high'] = $date; |
| 457 | } |
| 458 | |
| 459 | // set pledge related fields |
| 460 | $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String', |
| 461 | CRM_Core_DAO::$_nullObject |
| 462 | ); |
| 463 | if ($pledgeStatus) { |
| 464 | $statusValues = CRM_Contribute_PseudoConstant::contributionStatus(); |
| 465 | |
| 466 | // Remove status values that are only used for recurring contributions for now (Failed). |
| 467 | unset($statusValues['4']); |
| 468 | |
| 469 | // we need set all statuses except Cancelled |
| 470 | unset($statusValues[$pledgeStatus]); |
| 471 | |
| 472 | $statuses = array(); |
| 473 | foreach ($statusValues as $statusId => $value) { |
| 474 | $statuses[$statusId] = 1; |
| 475 | } |
| 476 | |
| 477 | $this->_formValues['pledge_status_id'] = $statuses; |
| 478 | $this->_defaults['pledge_status_id'] = $statuses; |
| 479 | } |
| 480 | |
| 481 | $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date', |
| 482 | CRM_Core_DAO::$_nullObject |
| 483 | ); |
| 484 | if ($pledgeFromDate) { |
| 485 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate); |
| 486 | $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date; |
| 487 | } |
| 488 | |
| 489 | $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date', |
| 490 | CRM_Core_DAO::$_nullObject |
| 491 | ); |
| 492 | if ($pledgeToDate) { |
| 493 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate); |
| 494 | $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date; |
| 495 | } |
| 496 | |
| 497 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 498 | if ($cid) { |
| 499 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 500 | if ($cid > 0) { |
| 501 | $this->_formValues['contact_id'] = $cid; |
| 502 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); |
| 503 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, |
| 504 | 'sort_name' |
| 505 | ); |
| 506 | // also assign individual mode to the template |
| 507 | $this->_single = TRUE; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | function getFormValues() { |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Return a descriptive name for the page, used in wizard header |
| 518 | * |
| 519 | * @return string |
| 520 | * @access public |
| 521 | */ |
| 522 | public function getTitle() { |
| 523 | return ts('Find Pledges'); |
| 524 | } |
| 525 | } |
| 526 | |