| 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 | * @todo Add comments if possible. |
| 20 | */ |
| 21 | class CRM_Financial_Form_Search extends CRM_Core_Form { |
| 22 | |
| 23 | public $_batchStatus; |
| 24 | |
| 25 | public function preProcess() { |
| 26 | $this->_batchStatus = CRM_Utils_Request::retrieve('batchStatus', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, NULL); |
| 27 | $this->assign('batchStatus', $this->_batchStatus); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @return array |
| 32 | */ |
| 33 | public function setDefaultValues() { |
| 34 | $defaults = []; |
| 35 | $status = CRM_Utils_Request::retrieve('status', 'Positive', CRM_Core_DAO::$_nullObject, FALSE, 1); |
| 36 | $defaults['batch_update'] = $status; |
| 37 | if ($this->_batchStatus) { |
| 38 | $defaults['status_id'] = $this->_batchStatus; |
| 39 | } |
| 40 | return $defaults; |
| 41 | } |
| 42 | |
| 43 | public function buildQuickForm() { |
| 44 | $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch'); |
| 45 | $attributes['total']['class'] = $attributes['item_count']['class'] = 'number'; |
| 46 | $this->add('text', 'title', ts('Batch Name'), $attributes['title']); |
| 47 | |
| 48 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', ['labelColumn' => 'name']); |
| 49 | $this->add( |
| 50 | 'select', |
| 51 | 'status_id', |
| 52 | ts('Batch Status'), |
| 53 | [ |
| 54 | '' => ts('- any -'), |
| 55 | array_search('Open', $batchStatus) => ts('Open'), |
| 56 | array_search('Closed', $batchStatus) => ts('Closed'), |
| 57 | array_search('Exported', $batchStatus) => ts('Exported'), |
| 58 | ], |
| 59 | FALSE |
| 60 | ); |
| 61 | |
| 62 | $this->add( |
| 63 | 'select', |
| 64 | 'payment_instrument_id', |
| 65 | ts('Payment Method'), |
| 66 | ['' => ts('- any -')] + CRM_Contribute_PseudoConstant::paymentInstrument(), |
| 67 | FALSE |
| 68 | ); |
| 69 | |
| 70 | $this->add('text', 'total', ts('Total Amount'), $attributes['total']); |
| 71 | |
| 72 | $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count']); |
| 73 | $this->add('text', 'sort_name', ts('Created By'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); |
| 74 | |
| 75 | $this->assign('elements', ['status_id', 'title', 'sort_name', 'payment_instrument_id', 'item_count', 'total']); |
| 76 | $this->addElement('checkbox', 'toggleSelect', NULL, NULL, ['class' => 'select-rows']); |
| 77 | $batchAction = [ |
| 78 | 'reopen' => ts('Re-open'), |
| 79 | 'close' => ts('Close'), |
| 80 | 'export' => ts('Export'), |
| 81 | 'delete' => ts('Delete'), |
| 82 | ]; |
| 83 | |
| 84 | foreach ($batchAction as $action => $ignore) { |
| 85 | if (!CRM_Batch_BAO_Batch::checkBatchPermission($action)) { |
| 86 | unset($batchAction[$action]); |
| 87 | } |
| 88 | } |
| 89 | $this->add('select', |
| 90 | 'batch_update', |
| 91 | ts('Task'), |
| 92 | ['' => ts('- actions -')] + $batchAction); |
| 93 | |
| 94 | $this->add('xbutton', 'submit', ts('Go'), |
| 95 | [ |
| 96 | 'type' => 'submit', |
| 97 | 'class' => 'crm-form-submit', |
| 98 | 'id' => 'Go', |
| 99 | ]); |
| 100 | |
| 101 | $this->addButtons( |
| 102 | [ |
| 103 | [ |
| 104 | 'type' => 'refresh', |
| 105 | 'name' => ts('Search'), |
| 106 | 'isDefault' => TRUE, |
| 107 | ], |
| 108 | ] |
| 109 | ); |
| 110 | parent::buildQuickForm(); |
| 111 | } |
| 112 | |
| 113 | public function postProcess() { |
| 114 | $batchIds = []; |
| 115 | foreach ($_POST as $key => $value) { |
| 116 | if (substr($key, 0, 6) == "check_") { |
| 117 | $batch = explode("_", $key); |
| 118 | $batchIds[] = $batch[1]; |
| 119 | } |
| 120 | } |
| 121 | if (!empty($_POST['batch_update'])) { |
| 122 | CRM_Batch_BAO_Batch::closeReOpen($batchIds, $_POST['batch_update']); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } |