| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Advanced search, extends basic search. |
| 20 | */ |
| 21 | class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { |
| 22 | |
| 23 | /** |
| 24 | * The params that are sent to the query. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $_queryParams; |
| 29 | |
| 30 | /** |
| 31 | * Are we restricting ourselves to a single contact. |
| 32 | * |
| 33 | * @var bool |
| 34 | */ |
| 35 | protected $_single = FALSE; |
| 36 | |
| 37 | /** |
| 38 | * Are we restricting ourselves to a single contact. |
| 39 | * |
| 40 | * @var bool |
| 41 | */ |
| 42 | protected $_limit = NULL; |
| 43 | |
| 44 | /** |
| 45 | * Prefix for the controller. |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $_prefix = "contribute_"; |
| 49 | |
| 50 | /** |
| 51 | * Explicitly declare the entity api name. |
| 52 | */ |
| 53 | public function getDefaultEntity() { |
| 54 | return 'Contribution'; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Processing needed for buildForm and later. |
| 59 | * |
| 60 | * @throws \CiviCRM_API3_Exception |
| 61 | * @throws \CRM_Core_Exception |
| 62 | */ |
| 63 | public function preProcess() { |
| 64 | $this->set('searchFormName', 'Search'); |
| 65 | |
| 66 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 67 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 68 | |
| 69 | $this->_done = FALSE; |
| 70 | |
| 71 | parent::preProcess(); |
| 72 | |
| 73 | //membership ID |
| 74 | $memberShipId = CRM_Utils_Request::retrieve('memberId', 'Positive', $this); |
| 75 | if (isset($memberShipId)) { |
| 76 | $this->_formValues['contribution_membership_id'] = $memberShipId; |
| 77 | } |
| 78 | $participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this); |
| 79 | if (isset($participantId)) { |
| 80 | $this->_formValues['contribution_participant_id'] = $participantId; |
| 81 | } |
| 82 | |
| 83 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 84 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, |
| 85 | $this->_action, |
| 86 | NULL, |
| 87 | $this->_single, |
| 88 | $this->_limit, |
| 89 | $this->_context |
| 90 | ); |
| 91 | $prefix = NULL; |
| 92 | if ($this->_context == 'user') { |
| 93 | $prefix = $this->_prefix; |
| 94 | } |
| 95 | |
| 96 | $this->assign("{$prefix}limit", $this->_limit); |
| 97 | $this->assign("{$prefix}single", $this->_single); |
| 98 | |
| 99 | $controller = new CRM_Core_Selector_Controller($selector, |
| 100 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 101 | $this->getSortID(), |
| 102 | CRM_Core_Action::VIEW, |
| 103 | $this, |
| 104 | CRM_Core_Selector_Controller::TRANSFER, |
| 105 | $prefix |
| 106 | ); |
| 107 | |
| 108 | $controller->setEmbedded(TRUE); |
| 109 | $controller->moveFromSessionToTemplate(); |
| 110 | |
| 111 | $this->assign('contributionSummary', $this->get('summary')); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Set defaults. |
| 116 | * |
| 117 | * @return array |
| 118 | * @throws \Exception |
| 119 | */ |
| 120 | public function setDefaultValues() { |
| 121 | $this->setDeprecatedDefaults(); |
| 122 | $this->_defaults = parent::setDefaultValues(); |
| 123 | |
| 124 | $this->_defaults = array_merge($this->getEntityDefaults('ContributionRecur'), $this->_defaults); |
| 125 | |
| 126 | if (empty($this->_defaults['contribution_status_id']) && !$this->_force) { |
| 127 | // In force mode only parameters from the url will be used. When visible/ explicit this is a useful default. |
| 128 | $this->_defaults['contribution_status_id'][1] = CRM_Core_PseudoConstant::getKey( |
| 129 | 'CRM_Contribute_BAO_Contribution', |
| 130 | 'contribution_status_id', |
| 131 | 'Completed' |
| 132 | ); |
| 133 | } |
| 134 | return $this->_defaults; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Build the form object. |
| 139 | * |
| 140 | * @throws \CRM_Core_Exception |
| 141 | * @throws \CiviCRM_API3_Exception |
| 142 | */ |
| 143 | public function buildQuickForm() { |
| 144 | if ($this->isFormInViewOrEditMode()) { |
| 145 | parent::buildQuickForm(); |
| 146 | $this->addContactSearchFields(); |
| 147 | |
| 148 | CRM_Contribute_BAO_Query::buildSearchForm($this); |
| 149 | } |
| 150 | |
| 151 | $rows = $this->get('rows'); |
| 152 | if (is_array($rows)) { |
| 153 | if (!$this->_single) { |
| 154 | $this->addRowSelectors($rows); |
| 155 | } |
| 156 | |
| 157 | $permission = CRM_Core_Permission::getPermission(); |
| 158 | |
| 159 | $queryParams = $this->get('queryParams'); |
| 160 | $taskParams['softCreditFiltering'] = FALSE; |
| 161 | if (!empty($queryParams)) { |
| 162 | $taskParams['softCreditFiltering'] = CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($queryParams); |
| 163 | } |
| 164 | $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $taskParams); |
| 165 | $this->addTaskMenu($tasks); |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get the label for the sortName field if email searching is on. |
| 172 | * |
| 173 | * (email searching is a setting under search preferences). |
| 174 | * |
| 175 | * @return string |
| 176 | */ |
| 177 | protected function getSortNameLabelWithEmail() { |
| 178 | return ts('Contributor Name or Email'); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Get the label for the sortName field if email searching is off. |
| 183 | * |
| 184 | * (email searching is a setting under search preferences). |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | protected function getSortNameLabelWithOutEmail() { |
| 189 | return ts('Contributor Name'); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get the label for the tag field. |
| 194 | * |
| 195 | * We do this in a function so the 'ts' wraps the whole string to allow |
| 196 | * better translation. |
| 197 | * |
| 198 | * @return string |
| 199 | */ |
| 200 | protected function getTagLabel() { |
| 201 | return ts('Contributor Tag(s)'); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the label for the group field. |
| 206 | * |
| 207 | * @return string |
| 208 | */ |
| 209 | protected function getGroupLabel() { |
| 210 | return ts('Contributor Group(s)'); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get the label for the group field. |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | protected function getContactTypeLabel() { |
| 219 | return ts('Contributor Contact Type'); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * The post processing of the form gets done here. |
| 224 | * |
| 225 | * Key things done during post processing are |
| 226 | * - check for reset or next request. if present, skip post processing. |
| 227 | * - now check if user requested running a saved search, if so, then |
| 228 | * the form values associated with the saved search are used for searching. |
| 229 | * - if user has done a submit with new values the regular post submission is |
| 230 | * done. |
| 231 | * The processing consists of using a Selector / Controller framework for getting the |
| 232 | * search results. |
| 233 | */ |
| 234 | public function postProcess() { |
| 235 | if ($this->_done) { |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | $this->_done = TRUE; |
| 240 | |
| 241 | $this->setFormValues(); |
| 242 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 243 | $this->fixFormValues(); |
| 244 | |
| 245 | // We don't show test records in summaries or dashboards |
| 246 | if (empty($this->_formValues['contribution_test']) && $this->_force && !empty($this->_context) && $this->_context == 'dashboard') { |
| 247 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 248 | $this->_formValues["contribution_test"] = 0; |
| 249 | } |
| 250 | |
| 251 | foreach ([ |
| 252 | 'contribution_amount_low', |
| 253 | 'contribution_amount_high', |
| 254 | ] as $f) { |
| 255 | if (isset($this->_formValues[$f])) { |
| 256 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 257 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (!empty($_POST)) { |
| 262 | $specialParams = [ |
| 263 | 'financial_type_id', |
| 264 | 'contribution_soft_credit_type_id', |
| 265 | 'contribution_status_id', |
| 266 | 'contribution_trxn_id', |
| 267 | 'contribution_product_id', |
| 268 | 'invoice_id', |
| 269 | 'payment_instrument_id', |
| 270 | 'contribution_batch_id', |
| 271 | ]; |
| 272 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 273 | CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, $specialParams); |
| 274 | |
| 275 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 276 | $tags = CRM_Utils_Array::value('contact_tags', $this->_formValues); |
| 277 | if ($tags && !is_array($tags)) { |
| 278 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 279 | unset($this->_formValues['contact_tags']); |
| 280 | $this->_formValues['contact_tags'][$tags] = 1; |
| 281 | } |
| 282 | |
| 283 | if ($tags && is_array($tags)) { |
| 284 | unset($this->_formValues['contact_tags']); |
| 285 | foreach ($tags as $notImportant => $tagID) { |
| 286 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 287 | $this->_formValues['contact_tags'][$tagID] = 1; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | $group = CRM_Utils_Array::value('group', $this->_formValues); |
| 292 | if ($group && !is_array($group)) { |
| 293 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 294 | unset($this->_formValues['group']); |
| 295 | $this->_formValues['group'][$group] = 1; |
| 296 | } |
| 297 | |
| 298 | if ($group && is_array($group)) { |
| 299 | unset($this->_formValues['group']); |
| 300 | foreach ($group as $groupID) { |
| 301 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 302 | $this->_formValues['group'][$groupID] = 1; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 308 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
| 309 | |
| 310 | // @todo - stop changing formValues - respect submitted form values, change a working array. |
| 311 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 312 | |
| 313 | $this->set('queryParams', $this->_queryParams); |
| 314 | |
| 315 | $buttonName = $this->controller->getButtonName(); |
| 316 | if ($buttonName == $this->_actionButtonName) { |
| 317 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 318 | |
| 319 | // hack, make sure we reset the task values |
| 320 | $stateMachine = $this->controller->getStateMachine(); |
| 321 | $formName = $stateMachine->getTaskFormName(); |
| 322 | $this->controller->resetPage($formName); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 327 | $selector = new CRM_Contribute_Selector_Search($this->_queryParams, |
| 328 | $this->_action, |
| 329 | NULL, |
| 330 | $this->_single, |
| 331 | $this->_limit, |
| 332 | $this->_context |
| 333 | ); |
| 334 | $selector->setKey($this->controller->_key); |
| 335 | |
| 336 | $prefix = NULL; |
| 337 | if ($this->_context == 'basic' || $this->_context == 'user') { |
| 338 | $prefix = $this->_prefix; |
| 339 | } |
| 340 | |
| 341 | $controller = new CRM_Core_Selector_Controller($selector, |
| 342 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 343 | $this->getSortID(), |
| 344 | CRM_Core_Action::VIEW, |
| 345 | $this, |
| 346 | CRM_Core_Selector_Controller::SESSION, |
| 347 | $prefix |
| 348 | ); |
| 349 | $controller->setEmbedded(TRUE); |
| 350 | |
| 351 | $query = &$selector->getQuery(); |
| 352 | if ($this->_context == 'user') { |
| 353 | $query->setSkipPermission(TRUE); |
| 354 | } |
| 355 | |
| 356 | $controller->run(); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Use values from $_GET if force is set to TRUE. |
| 361 | * |
| 362 | * Note that this means that GET over-rides POST. This was a historical decision & the reasoning is not explained. |
| 363 | */ |
| 364 | public function fixFormValues() { |
| 365 | if (!$this->_force) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
| 370 | if ($status) { |
| 371 | $this->_formValues['contribution_status_id'] = [$status => 1]; |
| 372 | $this->_defaults['contribution_status_id'] = [$status => 1]; |
| 373 | } |
| 374 | |
| 375 | $pcpid = (array) CRM_Utils_Request::retrieve('pcpid', 'String', $this); |
| 376 | if ($pcpid) { |
| 377 | // Add new pcpid to the tail of the array... |
| 378 | foreach ($pcpid as $pcpIdList) { |
| 379 | $this->_formValues['contribution_pcp_made_through_id'][] = $pcpIdList; |
| 380 | } |
| 381 | // and avoid any duplicate |
| 382 | $this->_formValues['contribution_pcp_made_through_id'] = array_unique($this->_formValues['contribution_pcp_made_through_id']); |
| 383 | } |
| 384 | |
| 385 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 386 | |
| 387 | if ($cid) { |
| 388 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 389 | if ($cid > 0) { |
| 390 | $this->_formValues['contact_id'] = $cid; |
| 391 | // @todo - why do we retrieve these when they are not used? |
| 392 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); |
| 393 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, |
| 394 | 'sort_name' |
| 395 | ); |
| 396 | // also assign individual mode to the template |
| 397 | $this->_single = TRUE; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | $this->_limit = CRM_Utils_Request::retrieve('limit', 'Positive', |
| 402 | $this |
| 403 | ); |
| 404 | |
| 405 | $test = CRM_Utils_Request::retrieve('test', 'Boolean'); |
| 406 | if (isset($test)) { |
| 407 | $test = CRM_Utils_Type::escape($test, 'Boolean'); |
| 408 | $this->_formValues['contribution_test'] = $test; |
| 409 | } |
| 410 | //Recurring id |
| 411 | $recur = CRM_Utils_Request::retrieve('recur', 'Positive', $this, FALSE); |
| 412 | if ($recur) { |
| 413 | $this->_formValues['contribution_recur_id'] = $recur; |
| 414 | $this->_formValues['contribution_recurring'] = 1; |
| 415 | } |
| 416 | |
| 417 | //check for contribution page id. |
| 418 | $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this); |
| 419 | if ($contribPageId) { |
| 420 | $this->_formValues['contribution_page_id'] = $contribPageId; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Return a descriptive name for the page, used in wizard header. |
| 426 | * |
| 427 | * @return string |
| 428 | */ |
| 429 | public function getTitle() { |
| 430 | return ts('Find Contributions'); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Set the metadata for the form. |
| 435 | * |
| 436 | * @throws \CiviCRM_API3_Exception |
| 437 | */ |
| 438 | protected function setSearchMetadata() { |
| 439 | $this->addSearchFieldMetadata(['Contribution' => CRM_Contribute_BAO_Query::getSearchFieldMetadata()]); |
| 440 | $this->addSearchFieldMetadata(['ContributionRecur' => CRM_Contribute_BAO_ContributionRecur::getContributionRecurSearchFieldMetadata()]); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Handling for url params that are deprecated. |
| 445 | * |
| 446 | * @throws \CRM_Core_Exception |
| 447 | */ |
| 448 | protected function setDeprecatedDefaults() { |
| 449 | $lowReceiveDate = CRM_Utils_Request::retrieve('start', 'Timestamp'); |
| 450 | if (!empty($lowReceiveDate)) { |
| 451 | $this->_formValues['receive_date_low'] = date('Y-m-d H:i:s', strtotime($lowReceiveDate)); |
| 452 | CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_low not start'); |
| 453 | } |
| 454 | $highReceiveDate = CRM_Utils_Request::retrieve('end', 'Timestamp'); |
| 455 | if (!empty($highReceiveDate)) { |
| 456 | $this->_formValues['receive_date_high'] = date('Y-m-d H:i:s', strtotime($highReceiveDate)); |
| 457 | CRM_Core_Error::deprecatedFunctionWarning('pass receive_date_high not end'); |
| 458 | } |
| 459 | //check for contribution page id. |
| 460 | $contribPageId = CRM_Utils_Request::retrieve('pid', 'Positive', $this); |
| 461 | if ($contribPageId) { |
| 462 | CRM_Core_Error::deprecatedFunctionWarning('pass contribution_page_id'); |
| 463 | $this->_formValues['contribution_page_id'] = $contribPageId; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | } |