| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Advanced search, extends basic search. |
| 36 | */ |
| 37 | class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { |
| 38 | |
| 39 | /** |
| 40 | * The params that are sent to the query. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $_queryParams; |
| 45 | |
| 46 | /** |
| 47 | * Are we restricting ourselves to a single contact. |
| 48 | * |
| 49 | * @var boolean |
| 50 | */ |
| 51 | protected $_single = FALSE; |
| 52 | |
| 53 | /** |
| 54 | * Are we restricting ourselves to a single contact. |
| 55 | * |
| 56 | * @var boolean |
| 57 | */ |
| 58 | protected $_limit = NULL; |
| 59 | |
| 60 | /** |
| 61 | * Prefix for the controller. |
| 62 | */ |
| 63 | protected $_prefix = "contribute_"; |
| 64 | |
| 65 | /** |
| 66 | * Processing needed for buildForm and later. |
| 67 | */ |
| 68 | public function preProcess() { |
| 69 | $this->set('searchFormName', 'Search'); |
| 70 | |
| 71 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 72 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 73 | |
| 74 | $this->_done = FALSE; |
| 75 | // @todo - is this an error - $this->_defaults is used. |
| 76 | $this->defaults = array(); |
| 77 | |
| 78 | /* |
| 79 | * we allow the controller to set force/reset externally, useful when we are being |
| 80 | * driven by the wizard framework |
| 81 | */ |
| 82 | |
| 83 | $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); |
| 84 | $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', $this, FALSE); |
| 85 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', $this); |
| 86 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); |
| 87 | |
| 88 | $this->assign("context", $this->_context); |
| 89 | |
| 90 | // get user submitted values |
| 91 | // get it from controller only if form has been submitted, else preProcess has set this |
| 92 | if (!empty($_POST)) { |
| 93 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 94 | } |
| 95 | else { |
| 96 | $this->_formValues = $this->get('formValues'); |
| 97 | } |
| 98 | |
| 99 | //membership ID |
| 100 | $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this); |
| 101 | if (isset($memberShipId)) { |
| 102 | $this->_formValues['contribution_membership_id'] = $memberShipId; |
| 103 | } |
| 104 | $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); |
| 105 | if (isset($participantId)) { |
| 106 | $this->_formValues['contribution_participant_id'] = $participantId; |
| 107 | } |
| 108 | |
| 109 | if ($this->_force) { |
| 110 | $this->postProcess(); |
| 111 | $this->set('force', 0); |
| 112 | } |
| 113 | |
| 114 | $sortID = NULL; |
| 115 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 116 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 117 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 122 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, |
| 123 | $this->_action, |
| 124 | NULL, |
| 125 | $this->_single, |
| 126 | $this->_limit, |
| 127 | $this->_context |
| 128 | ); |
| 129 | $prefix = NULL; |
| 130 | if ($this->_context == 'user') { |
| 131 | $prefix = $this->_prefix; |
| 132 | } |
| 133 | |
| 134 | $this->assign("{$prefix}limit", $this->_limit); |
| 135 | $this->assign("{$prefix}single", $this->_single); |
| 136 | |
| 137 | $controller = new CRM_Core_Selector_Controller($selector, |
| 138 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 139 | $sortID, |
| 140 | CRM_Core_Action::VIEW, |
| 141 | $this, |
| 142 | CRM_Core_Selector_Controller::TRANSFER, |
| 143 | $prefix |
| 144 | ); |
| 145 | |
| 146 | $controller->setEmbedded(TRUE); |
| 147 | $controller->moveFromSessionToTemplate(); |
| 148 | |
| 149 | $this->assign('contributionSummary', $this->get('summary')); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set defaults. |
| 154 | * |
| 155 | * @return array |
| 156 | */ |
| 157 | public function setDefaultValues() { |
| 158 | if (empty($this->_defaults['contribution_status'])) { |
| 159 | $this->_defaults['contribution_status'][1] = 1; |
| 160 | } |
| 161 | return $this->_defaults; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Build the form object. |
| 166 | */ |
| 167 | public function buildQuickForm() { |
| 168 | parent::buildQuickForm(); |
| 169 | // text for sort_name |
| 170 | $this->addElement('text', |
| 171 | 'sort_name', |
| 172 | ts('Contributor Name or Email'), |
| 173 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', |
| 174 | 'sort_name' |
| 175 | ) |
| 176 | ); |
| 177 | |
| 178 | $this->_group = CRM_Core_PseudoConstant::nestedGroup(); |
| 179 | |
| 180 | // multiselect for groups |
| 181 | if ($this->_group) { |
| 182 | $this->add('select', 'group', ts('Groups'), $this->_group, FALSE, |
| 183 | array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2') |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | // multiselect for tags |
| 188 | $contactTags = CRM_Core_BAO_Tag::getTags(); |
| 189 | |
| 190 | if ($contactTags) { |
| 191 | $this->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE, |
| 192 | array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2') |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | CRM_Contribute_BAO_Query::buildSearchForm($this); |
| 197 | |
| 198 | $rows = $this->get('rows'); |
| 199 | if (is_array($rows)) { |
| 200 | if (!$this->_single) { |
| 201 | $this->addRowSelectors($rows); |
| 202 | } |
| 203 | |
| 204 | $permission = CRM_Core_Permission::getPermission(); |
| 205 | |
| 206 | $queryParams = $this->get('queryParams'); |
| 207 | $softCreditFiltering = FALSE; |
| 208 | if (!empty($queryParams)) { |
| 209 | $softCreditFiltering = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams); |
| 210 | } |
| 211 | $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering); |
| 212 | $this->addTaskMenu($tasks); |
| 213 | } |
| 214 | |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * The post processing of the form gets done here. |
| 219 | * |
| 220 | * Key things done during post processing are |
| 221 | * - check for reset or next request. if present, skip post processing. |
| 222 | * - now check if user requested running a saved search, if so, then |
| 223 | * the form values associated with the saved search are used for searching. |
| 224 | * - if user has done a submit with new values the regular post submission is |
| 225 | * done. |
| 226 | * The processing consists of using a Selector / Controller framework for getting the |
| 227 | * search results. |
| 228 | */ |
| 229 | public function postProcess() { |
| 230 | if ($this->_done) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | $this->_done = TRUE; |
| 235 | |
| 236 | if (!empty($_POST)) { |
| 237 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 238 | } |
| 239 | |
| 240 | $this->fixFormValues(); |
| 241 | |
| 242 | // We don't show test records in summaries or dashboards |
| 243 | if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') { |
| 244 | $this->_formValues["contribution_test"] = 0; |
| 245 | } |
| 246 | |
| 247 | foreach (array( |
| 248 | 'contribution_amount_low', |
| 249 | 'contribution_amount_high', |
| 250 | ) as $f) { |
| 251 | if (isset($this->_formValues[$f])) { |
| 252 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | $config = CRM_Core_Config::singleton(); |
| 257 | if (!empty($_POST)) { |
| 258 | $specialParams = array( |
| 259 | 'financial_type_id', |
| 260 | 'contribution_soft_credit_type_id', |
| 261 | 'contribution_status_id', |
| 262 | 'contribution_source', |
| 263 | 'contribution_trxn_id', |
| 264 | 'contribution_page_id', |
| 265 | 'contribution_product_id', |
| 266 | 'invoice_id', |
| 267 | 'payment_instrument_id', |
| 268 | ); |
| 269 | foreach ($specialParams as $element) { |
| 270 | $value = CRM_Utils_Array::value($element, $this->_formValues); |
| 271 | if ($value) { |
| 272 | if (is_array($value)) { |
| 273 | $this->_formValues[$element] = array('IN' => $value); |
| 274 | } |
| 275 | else { |
| 276 | $this->_formValues[$element] = array('LIKE' => "%$value%"); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues); |
| 282 | if ($tags && !is_array($tags)) { |
| 283 | unset($this->_formValues['contact_tags']); |
| 284 | $this->_formValues['contact_tags'][$tags] = 1; |
| 285 | } |
| 286 | |
| 287 | if ($tags && is_array($tags)) { |
| 288 | unset($this->_formValues['contact_tags']); |
| 289 | foreach ($tags as $notImportant => $tagID) { |
| 290 | $this->_formValues['contact_tags'][$tagID] = 1; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (!$config->groupTree) { |
| 295 | $group = CRM_Utils_Array::value('group', $this->_formValues); |
| 296 | if ($group && !is_array($group)) { |
| 297 | unset($this->_formValues['group']); |
| 298 | $this->_formValues['group'][$group] = 1; |
| 299 | } |
| 300 | |
| 301 | if ($group && is_array($group)) { |
| 302 | unset($this->_formValues['group']); |
| 303 | foreach ($group as $notImportant => $groupID) { |
| 304 | $this->_formValues['group'][$groupID] = 1; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
| 311 | |
| 312 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 313 | |
| 314 | $this->set('formValues', $this->_formValues); |
| 315 | $this->set('queryParams', $this->_queryParams); |
| 316 | |
| 317 | $buttonName = $this->controller->getButtonName(); |
| 318 | if ($buttonName == $this->_actionButtonName) { |
| 319 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 320 | |
| 321 | // hack, make sure we reset the task values |
| 322 | $stateMachine = $this->controller->getStateMachine(); |
| 323 | $formName = $stateMachine->getTaskFormName(); |
| 324 | $this->controller->resetPage($formName); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | $sortID = NULL; |
| 329 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 330 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 331 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 336 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, |
| 337 | $this->_action, |
| 338 | NULL, |
| 339 | $this->_single, |
| 340 | $this->_limit, |
| 341 | $this->_context |
| 342 | ); |
| 343 | $selector->setKey($this->controller->_key); |
| 344 | |
| 345 | $prefix = NULL; |
| 346 | if ($this->_context == 'basic' || $this->_context == 'user') { |
| 347 | $prefix = $this->_prefix; |
| 348 | } |
| 349 | |
| 350 | $controller = new CRM_Core_Selector_Controller($selector, |
| 351 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 352 | $sortID, |
| 353 | CRM_Core_Action::VIEW, |
| 354 | $this, |
| 355 | CRM_Core_Selector_Controller::SESSION, |
| 356 | $prefix |
| 357 | ); |
| 358 | $controller->setEmbedded(TRUE); |
| 359 | |
| 360 | $query = &$selector->getQuery(); |
| 361 | if ($this->_context == 'user') { |
| 362 | $query->setSkipPermission(TRUE); |
| 363 | } |
| 364 | $summary = &$query->summaryContribution($this->_context); |
| 365 | $this->set('summary', $summary); |
| 366 | $this->assign('contributionSummary', $summary); |
| 367 | $controller->run(); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Use values from $_GET if force is set to TRUE. |
| 372 | * |
| 373 | * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained. |
| 374 | */ |
| 375 | public function fixFormValues() { |
| 376 | if (!$this->_force) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | $status = CRM_Utils_Request::retrieve('status', 'String', |
| 381 | CRM_Core_DAO::$_nullObject |
| 382 | ); |
| 383 | if ($status) { |
| 384 | $this->_formValues['contribution_status_id'] = array($status => 1); |
| 385 | $this->_defaults['contribution_status_id'] = array($status => 1); |
| 386 | } |
| 387 | |
| 388 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 389 | |
| 390 | if ($cid) { |
| 391 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 392 | if ($cid > 0) { |
| 393 | $this->_formValues['contact_id'] = $cid; |
| 394 | // @todo - why do we retrieve these when they are not used? |
| 395 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); |
| 396 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, |
| 397 | 'sort_name' |
| 398 | ); |
| 399 | // also assign individual mode to the template |
| 400 | $this->_single = TRUE; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | $lowDate = CRM_Utils_Request::retrieve('start', 'Timestamp', |
| 405 | CRM_Core_DAO::$_nullObject |
| 406 | ); |
| 407 | if ($lowDate) { |
| 408 | $lowDate = CRM_Utils_Type::escape($lowDate, 'Timestamp'); |
| 409 | $date = CRM_Utils_Date::setDateDefaults($lowDate); |
| 410 | $this->_formValues['contribution_date_low'] = $this->_defaults['contribution_date_low'] = $date[0]; |
| 411 | } |
| 412 | |
| 413 | $highDate = CRM_Utils_Request::retrieve('end', 'Timestamp', |
| 414 | CRM_Core_DAO::$_nullObject |
| 415 | ); |
| 416 | if ($highDate) { |
| 417 | $highDate = CRM_Utils_Type::escape($highDate, 'Timestamp'); |
| 418 | $date = CRM_Utils_Date::setDateDefaults($highDate); |
| 419 | $this->_formValues['contribution_date_high'] = $this->_defaults['contribution_date_high'] = $date[0]; |
| 420 | } |
| 421 | |
| 422 | if ($highDate || $lowDate) { |
| 423 | //set the Choose Date Range value |
| 424 | $this->_formValues['contribution_date_relative'] = 0; |
| 425 | } |
| 426 | |
| 427 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', |
| 428 | $this |
| 429 | ); |
| 430 | |
| 431 | $test = CRM_Utils_Request::retrieve('test', 'Boolean', |
| 432 | CRM_Core_DAO::$_nullObject |
| 433 | ); |
| 434 | if (isset($test)) { |
| 435 | $test = CRM_Utils_Type::escape($test, 'Boolean'); |
| 436 | $this->_formValues['contribution_test'] = $test; |
| 437 | } |
| 438 | //Recurring id |
| 439 | $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE); |
| 440 | if ($recur) { |
| 441 | $this->_formValues['contribution_recur_id'] = $recur; |
| 442 | $this->_formValues['contribution_recurring'] = 1; |
| 443 | } |
| 444 | |
| 445 | //check for contribution page id. |
| 446 | $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this); |
| 447 | if ($contribPageId) { |
| 448 | $this->_formValues['contribution_page_id'] = $contribPageId; |
| 449 | } |
| 450 | |
| 451 | //give values to default. |
| 452 | $this->_defaults = $this->_formValues; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Return a descriptive name for the page, used in wizard header. |
| 457 | * |
| 458 | * @return string |
| 459 | */ |
| 460 | public function getTitle() { |
| 461 | return ts('Find Contributions'); |
| 462 | } |
| 463 | |
| 464 | } |