| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This file is for Pledge search |
| 36 | */ |
| 37 | class CRM_Pledge_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 | * @return string |
| 48 | */ |
| 49 | public function getDefaultEntity() { |
| 50 | return 'Pledge'; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Are we restricting ourselves to a single contact. |
| 55 | * |
| 56 | * @var bool |
| 57 | */ |
| 58 | protected $_single = FALSE; |
| 59 | |
| 60 | /** |
| 61 | * Are we restricting ourselves to a single contact. |
| 62 | * |
| 63 | * @var bool |
| 64 | */ |
| 65 | protected $_limit = NULL; |
| 66 | |
| 67 | /** |
| 68 | * Prefix for the controller. |
| 69 | * @var string |
| 70 | */ |
| 71 | protected $_prefix = "pledge_"; |
| 72 | |
| 73 | /** |
| 74 | * Processing needed for buildForm and later. |
| 75 | */ |
| 76 | public function preProcess() { |
| 77 | |
| 78 | $this->_searchButtonName = $this->getButtonName('refresh'); |
| 79 | $this->_actionButtonName = $this->getButtonName('next', 'action'); |
| 80 | |
| 81 | $this->_done = FALSE; |
| 82 | |
| 83 | $this->loadStandardSearchOptionsFromUrl(); |
| 84 | |
| 85 | // get user submitted values |
| 86 | // get it from controller only if form has been submitted, else preProcess has set this |
| 87 | if (!empty($_POST) && !$this->controller->isModal()) { |
| 88 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 89 | } |
| 90 | else { |
| 91 | $this->_formValues = $this->get('formValues'); |
| 92 | } |
| 93 | |
| 94 | if (empty($this->_formValues)) { |
| 95 | if (isset($this->_ssID)) { |
| 96 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if ($this->_force) { |
| 101 | $this->postProcess(); |
| 102 | $this->set('force', 0); |
| 103 | } |
| 104 | |
| 105 | $sortID = NULL; |
| 106 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 107 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 108 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 113 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, |
| 114 | $this->_action, |
| 115 | NULL, |
| 116 | $this->_single, |
| 117 | $this->_limit, |
| 118 | $this->_context |
| 119 | ); |
| 120 | $prefix = NULL; |
| 121 | if ($this->_context == 'user') { |
| 122 | $prefix = $this->_prefix; |
| 123 | } |
| 124 | |
| 125 | $this->assign("{$prefix}limit", $this->_limit); |
| 126 | $this->assign("{$prefix}single", $this->_single); |
| 127 | |
| 128 | $controller = new CRM_Core_Selector_Controller($selector, |
| 129 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 130 | $sortID, |
| 131 | CRM_Core_Action::VIEW, |
| 132 | $this, |
| 133 | CRM_Core_Selector_Controller::TRANSFER, |
| 134 | $prefix |
| 135 | ); |
| 136 | $controller->setEmbedded(TRUE); |
| 137 | $controller->moveFromSessionToTemplate(); |
| 138 | |
| 139 | $this->assign('summary', $this->get('summary')); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Build the form object. |
| 144 | */ |
| 145 | public function buildQuickForm() { |
| 146 | parent::buildQuickForm(); |
| 147 | $this->addContactSearchFields(); |
| 148 | |
| 149 | CRM_Pledge_BAO_Query::buildSearchForm($this); |
| 150 | |
| 151 | $rows = $this->get('rows'); |
| 152 | if (is_array($rows)) { |
| 153 | if (!$this->_single) { |
| 154 | $this->addRowSelectors($rows); |
| 155 | } |
| 156 | |
| 157 | $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission())); |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the label for the sortName field if email searching is on. |
| 164 | * |
| 165 | * (email searching is a setting under search preferences). |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | protected function getSortNameLabelWithEmail() { |
| 170 | return ts('Pledger Name or Email'); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the label for the sortName field if email searching is off. |
| 175 | * |
| 176 | * (email searching is a setting under search preferences). |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | protected function getSortNameLabelWithOutEmail() { |
| 181 | return ts('Pledger Name'); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get the label for the tag field. |
| 186 | * |
| 187 | * We do this in a function so the 'ts' wraps the whole string to allow |
| 188 | * better translation. |
| 189 | * |
| 190 | * @return string |
| 191 | */ |
| 192 | protected function getTagLabel() { |
| 193 | return ts('Pledger Tag(s)'); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the label for the group field. |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | protected function getGroupLabel() { |
| 202 | return ts('Pledger Group(s)'); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get the label for the group field. |
| 207 | * |
| 208 | * @return string |
| 209 | */ |
| 210 | protected function getContactTypeLabel() { |
| 211 | return ts('Pledger Contact Type'); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * The post processing of the form gets done here. |
| 216 | * |
| 217 | * Key things done during post processing are |
| 218 | * - check for reset or next request. if present, skip post procesing. |
| 219 | * - now check if user requested running a saved search, if so, then |
| 220 | * the form values associated with the saved search are used for searching. |
| 221 | * - if user has done a submit with new values the regular post submissing is |
| 222 | * done. |
| 223 | * The processing consists of using a Selector / Controller framework for getting the |
| 224 | * search results. |
| 225 | */ |
| 226 | public function postProcess() { |
| 227 | if ($this->_done) { |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | $this->_done = TRUE; |
| 232 | |
| 233 | $this->_formValues = $this->controller->exportValues($this->_name); |
| 234 | |
| 235 | $this->fixFormValues(); |
| 236 | |
| 237 | // We don't show test records in summaries or dashboards |
| 238 | if (empty($this->_formValues['pledge_test']) && $this->_force) { |
| 239 | $this->_formValues["pledge_test"] = 0; |
| 240 | } |
| 241 | |
| 242 | foreach (['pledge_amount_low', 'pledge_amount_high'] as $f) { |
| 243 | if (isset($this->_formValues[$f])) { |
| 244 | $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (isset($this->_ssID) && empty($_POST)) { |
| 249 | // if we are editing / running a saved search and the form has not been posted |
| 250 | $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); |
| 251 | } |
| 252 | |
| 253 | CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); |
| 254 | |
| 255 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 256 | |
| 257 | $this->set('formValues', $this->_formValues); |
| 258 | $this->set('queryParams', $this->_queryParams); |
| 259 | |
| 260 | $buttonName = $this->controller->getButtonName(); |
| 261 | if ($buttonName == $this->_actionButtonName) { |
| 262 | // check actionName and if next, then do not repeat a search, since we are going to the next page |
| 263 | |
| 264 | // hack, make sure we reset the task values |
| 265 | $stateMachine = $this->controller->getStateMachine(); |
| 266 | $formName = $stateMachine->getTaskFormName(); |
| 267 | $this->controller->resetPage($formName); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | $sortID = NULL; |
| 272 | if ($this->get(CRM_Utils_Sort::SORT_ID)) { |
| 273 | $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), |
| 274 | $this->get(CRM_Utils_Sort::SORT_DIRECTION) |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); |
| 279 | |
| 280 | $selector = new CRM_Pledge_Selector_Search($this->_queryParams, |
| 281 | $this->_action, |
| 282 | NULL, |
| 283 | $this->_single, |
| 284 | $this->_limit, |
| 285 | $this->_context |
| 286 | ); |
| 287 | $selector->setKey($this->controller->_key); |
| 288 | |
| 289 | $prefix = NULL; |
| 290 | if ($this->_context == 'user') { |
| 291 | $prefix = $this->_prefix; |
| 292 | } |
| 293 | |
| 294 | $this->assign("{$prefix}limit", $this->_limit); |
| 295 | $this->assign("{$prefix}single", $this->_single); |
| 296 | |
| 297 | $controller = new CRM_Core_Selector_Controller($selector, |
| 298 | $this->get(CRM_Utils_Pager::PAGE_ID), |
| 299 | $sortID, |
| 300 | CRM_Core_Action::VIEW, |
| 301 | $this, |
| 302 | CRM_Core_Selector_Controller::SESSION, |
| 303 | $prefix |
| 304 | ); |
| 305 | $controller->setEmbedded(TRUE); |
| 306 | |
| 307 | $query = &$selector->getQuery(); |
| 308 | if ($this->_context == 'user') { |
| 309 | $query->setSkipPermission(TRUE); |
| 310 | } |
| 311 | $controller->run(); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * add the rules (mainly global rules) for form. |
| 316 | * All local rules are added near the element |
| 317 | * |
| 318 | * @see valid_date |
| 319 | */ |
| 320 | public function addRules() { |
| 321 | $this->addFormRule(['CRM_Pledge_Form_Search', 'formRule']); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Set the default form values. |
| 326 | * |
| 327 | * |
| 328 | * @return array |
| 329 | * the default array reference |
| 330 | */ |
| 331 | public function setDefaultValues() { |
| 332 | $defaults = []; |
| 333 | $defaults = $this->_formValues; |
| 334 | return $defaults; |
| 335 | } |
| 336 | |
| 337 | public function fixFormValues() { |
| 338 | if (!$this->_force) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // set pledge payment related fields |
| 343 | $status = CRM_Utils_Request::retrieve('status', 'String'); |
| 344 | if ($status) { |
| 345 | $this->_formValues['pledge_payment_status_id'] = [$status => 1]; |
| 346 | $this->_defaults['pledge_payment_status_id'] = [$status => 1]; |
| 347 | } |
| 348 | |
| 349 | $fromDate = CRM_Utils_Request::retrieve('start', 'Date'); |
| 350 | if ($fromDate) { |
| 351 | list($date) = CRM_Utils_Date::setDateDefaults($fromDate); |
| 352 | $this->_formValues['pledge_payment_date_low'] = $date; |
| 353 | $this->_defaults['pledge_payment_date_low'] = $date; |
| 354 | } |
| 355 | |
| 356 | $toDate = CRM_Utils_Request::retrieve('end', 'Date'); |
| 357 | if ($toDate) { |
| 358 | list($date) = CRM_Utils_Date::setDateDefaults($toDate); |
| 359 | $this->_formValues['pledge_payment_date_high'] = $date; |
| 360 | $this->_defaults['pledge_payment_date_high'] = $date; |
| 361 | } |
| 362 | |
| 363 | // set pledge related fields |
| 364 | $pledgeStatus = CRM_Utils_Request::retrieve('pstatus', 'String'); |
| 365 | |
| 366 | if ($pledgeStatus) { |
| 367 | $statusValues = CRM_Pledge_BAO_Pledge::buildOptions('status_id'); |
| 368 | |
| 369 | // we need set all statuses except Cancelled |
| 370 | unset($statusValues[$pledgeStatus]); |
| 371 | |
| 372 | $statuses = []; |
| 373 | foreach ($statusValues as $statusId => $value) { |
| 374 | $statuses[$statusId] = 1; |
| 375 | } |
| 376 | |
| 377 | $this->_formValues['pledge_status_id'] = $statuses; |
| 378 | $this->_defaults['pledge_status_id'] = $statuses; |
| 379 | } |
| 380 | |
| 381 | $pledgeFromDate = CRM_Utils_Request::retrieve('pstart', 'Date'); |
| 382 | if ($pledgeFromDate) { |
| 383 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeFromDate); |
| 384 | $this->_formValues['pledge_create_date_low'] = $this->_defaults['pledge_create_date_low'] = $date; |
| 385 | } |
| 386 | |
| 387 | $pledgeToDate = CRM_Utils_Request::retrieve('pend', 'Date'); |
| 388 | if ($pledgeToDate) { |
| 389 | list($date) = CRM_Utils_Date::setDateDefaults($pledgeToDate); |
| 390 | $this->_formValues['pledge_create_date_high'] = $this->_defaults['pledge_create_date_high'] = $date; |
| 391 | } |
| 392 | |
| 393 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this); |
| 394 | if ($cid) { |
| 395 | $cid = CRM_Utils_Type::escape($cid, 'Integer'); |
| 396 | if ($cid > 0) { |
| 397 | $this->_formValues['contact_id'] = $cid; |
| 398 | list($display, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($cid); |
| 399 | $this->_defaults['sort_name'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, |
| 400 | 'sort_name' |
| 401 | ); |
| 402 | // also assign individual mode to the template |
| 403 | $this->_single = TRUE; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @return null |
| 410 | */ |
| 411 | public function getFormValues() { |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Return a descriptive name for the page, used in wizard header |
| 417 | * |
| 418 | * @return string |
| 419 | */ |
| 420 | public function getTitle() { |
| 421 | return ts('Find Pledges'); |
| 422 | } |
| 423 | |
| 424 | } |