Merge pull request #17857 from demeritcowboy/membershiptest-5.28
[civicrm-core.git] / CRM / Batch / Form / Batch.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
37b8953e 13 * This class generates form components for batch entry.
6a488035
TO
14 */
15class CRM_Batch_Form_Batch extends CRM_Admin_Form {
16
37b8953e
EM
17 /**
18 * PreProcess function.
19 */
6a488035
TO
20 public function preProcess() {
21 parent::preProcess();
37b8953e 22 // Set the user context.
6a488035
TO
23 $session = CRM_Core_Session::singleton();
24 $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch', "reset=1"));
25 }
26
27 /**
eceb18cc 28 * Build the form object.
6a488035
TO
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');
fc4aa398 38 $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch');
6a488035
TO
39 $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE);
40
fc4aa398 41 $batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id');
8ef12e64 42
6a488035
TO
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']);
73708df6 50 $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count'], TRUE);
6a488035
TO
51 $this->add('text', 'total', ts('Total Amount'), $attributes['total'], TRUE);
52 }
53
54 /**
c490a46a 55 * Set default values for the form.
6a488035 56 */
00be9182 57 public function setDefaultValues() {
be2fb01f 58 $defaults = [];
6a488035
TO
59
60 if ($this->_action & CRM_Core_Action::ADD) {
37b8953e 61 // Set batch name default.
6a488035
TO
62 $defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName();
63 }
64 else {
65 $defaults = $this->_values;
66 }
6a488035
TO
67 return $defaults;
68 }
69
70 /**
eceb18cc 71 * Process the form submission.
6a488035
TO
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');
481a74f4 87 $params['created_date'] = CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s"));
6a488035 88 }
8ef12e64 89
6a488035 90 // always create with data entry status
7f0ddece 91 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Batch_BAO_Batch', 'status_id', 'Data Entry');
6a488035
TO
92 $batch = CRM_Batch_BAO_Batch::create($params);
93
94 // redirect to batch entry page.
95 $session = CRM_Core_Session::singleton();
481a74f4 96 if ($this->_action & CRM_Core_Action::ADD) {
6a488035
TO
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 }
96025800 103
6a488035 104}