Merge pull request #17580 from samuelsov/dev/core#1670
[civicrm-core.git] / CRM / Financial / Form / Search.php
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 CRM_Core_Resources::singleton()
45 ->addScriptFile('civicrm', 'packages/jquery/plugins/jquery.redirect.min.js', 0, 'html-header');
46 $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch');
47 $attributes['total']['class'] = $attributes['item_count']['class'] = 'number';
48 $this->add('text', 'title', ts('Batch Name'), $attributes['title']);
49
50 $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', ['labelColumn' => 'name']);
51 $this->add(
52 'select',
53 'status_id',
54 ts('Batch Status'),
55 [
56 '' => ts('- any -'),
57 array_search('Open', $batchStatus) => ts('Open'),
58 array_search('Closed', $batchStatus) => ts('Closed'),
59 array_search('Exported', $batchStatus) => ts('Exported'),
60 ],
61 FALSE
62 );
63
64 $this->add(
65 'select',
66 'payment_instrument_id',
67 ts('Payment Method'),
68 ['' => ts('- any -')] + CRM_Contribute_PseudoConstant::paymentInstrument(),
69 FALSE
70 );
71
72 $this->add('text', 'total', ts('Total Amount'), $attributes['total']);
73
74 $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count']);
75 $this->add('text', 'sort_name', ts('Created By'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
76
77 $this->assign('elements', ['status_id', 'title', 'sort_name', 'payment_instrument_id', 'item_count', 'total']);
78 $this->addElement('checkbox', 'toggleSelect', NULL, NULL, ['class' => 'select-rows']);
79 $batchAction = [
80 'reopen' => ts('Re-open'),
81 'close' => ts('Close'),
82 'export' => ts('Export'),
83 'delete' => ts('Delete'),
84 ];
85
86 foreach ($batchAction as $action => $ignore) {
87 if (!CRM_Batch_BAO_Batch::checkBatchPermission($action)) {
88 unset($batchAction[$action]);
89 }
90 }
91 $this->add('select',
92 'batch_update',
93 ts('Task'),
94 ['' => ts('- actions -')] + $batchAction);
95
96 $this->add('submit', 'submit', ts('Go'),
97 [
98 'class' => 'crm-form-submit',
99 'id' => 'Go',
100 ]);
101
102 $this->addButtons(
103 [
104 [
105 'type' => 'refresh',
106 'name' => ts('Search'),
107 'isDefault' => TRUE,
108 ],
109 ]
110 );
111 parent::buildQuickForm();
112 }
113
114 public function postProcess() {
115 $batchIds = [];
116 foreach ($_POST as $key => $value) {
117 if (substr($key, 0, 6) == "check_") {
118 $batch = explode("_", $key);
119 $batchIds[] = $batch[1];
120 }
121 }
122 if (!empty($_POST['batch_update'])) {
123 CRM_Batch_BAO_Batch::closeReOpen($batchIds, $_POST['batch_update']);
124 }
125 }
126
127 }