Merge pull request #17857 from demeritcowboy/membershiptest-5.28
[civicrm-core.git] / CRM / Batch / Form / Batch.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 * This class generates form components for batch entry.
14 */
15 class CRM_Batch_Form_Batch extends CRM_Admin_Form {
16
17 /**
18 * PreProcess function.
19 */
20 public function preProcess() {
21 parent::preProcess();
22 // Set the user context.
23 $session = CRM_Core_Session::singleton();
24 $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch', "reset=1"));
25 }
26
27 /**
28 * Build the form object.
29 */
30 public function buildQuickForm() {
31 parent::buildQuickForm();
32
33 if ($this->_action & CRM_Core_Action::DELETE) {
34 return;
35 }
36
37 $this->applyFilter('__ALL__', 'trim');
38 $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch');
39 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
40
41 $batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id');
42
43 $type = $this->add('select', 'type_id', ts('Type'), $batchTypes);
44
45 if ($this->_action & CRM_Core_Action::UPDATE) {
46 $type->freeze();
47 }
48
49 $this->add('textarea', 'description', ts('Description'), $attributes['description']);
50 $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count'], TRUE);
51 $this->add('text', 'total', ts('Total Amount'), $attributes['total'], TRUE);
52 }
53
54 /**
55 * Set default values for the form.
56 */
57 public function setDefaultValues() {
58 $defaults = [];
59
60 if ($this->_action & CRM_Core_Action::ADD) {
61 // Set batch name default.
62 $defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName();
63 }
64 else {
65 $defaults = $this->_values;
66 }
67 return $defaults;
68 }
69
70 /**
71 * Process the form submission.
72 */
73 public function postProcess() {
74 $params = $this->controller->exportValues($this->_name);
75 if ($this->_action & CRM_Core_Action::DELETE) {
76 CRM_Core_Session::setStatus("", ts("Batch Deleted"), "success");
77 CRM_Batch_BAO_Batch::deleteBatch($this->_id);
78 return;
79 }
80
81 if ($this->_id) {
82 $params['id'] = $this->_id;
83 }
84 else {
85 $session = CRM_Core_Session::singleton();
86 $params['created_id'] = $session->get('userID');
87 $params['created_date'] = CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s"));
88 }
89
90 // always create with data entry status
91 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry');
92 $batch = CRM_Batch_BAO_Batch::create($params);
93
94 // redirect to batch entry page.
95 $session = CRM_Core_Session::singleton();
96 if ($this->_action & CRM_Core_Action::ADD) {
97 $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1&action=add"));
98 }
99 else {
100 $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1"));
101 }
102 }
103
104 }