| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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 | * This class generates form components for batch entry. |
| 30 | */ |
| 31 | class CRM_Batch_Form_Batch extends CRM_Admin_Form { |
| 32 | |
| 33 | /** |
| 34 | * PreProcess function. |
| 35 | */ |
| 36 | public function preProcess() { |
| 37 | parent::preProcess(); |
| 38 | // Set the user context. |
| 39 | $session = CRM_Core_Session::singleton(); |
| 40 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch', "reset=1")); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Build the form object. |
| 45 | */ |
| 46 | public function buildQuickForm() { |
| 47 | parent::buildQuickForm(); |
| 48 | |
| 49 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $this->applyFilter('__ALL__', 'trim'); |
| 54 | $attributes = CRM_Core_DAO::getAttribute('CRM_Batch_DAO_Batch'); |
| 55 | $this->add('text', 'title', ts('Batch Name'), $attributes['name'], TRUE); |
| 56 | |
| 57 | $batchTypes = CRM_Batch_BAO_Batch::buildOptions('type_id'); |
| 58 | |
| 59 | $type = $this->add('select', 'type_id', ts('Type'), $batchTypes); |
| 60 | |
| 61 | if ($this->_action & CRM_Core_Action::UPDATE) { |
| 62 | $type->freeze(); |
| 63 | } |
| 64 | |
| 65 | $this->add('textarea', 'description', ts('Description'), $attributes['description']); |
| 66 | $this->add('text', 'item_count', ts('Number of Items'), $attributes['item_count'], TRUE); |
| 67 | $this->add('text', 'total', ts('Total Amount'), $attributes['total'], TRUE); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set default values for the form. |
| 72 | */ |
| 73 | public function setDefaultValues() { |
| 74 | $defaults = array(); |
| 75 | |
| 76 | if ($this->_action & CRM_Core_Action::ADD) { |
| 77 | // Set batch name default. |
| 78 | $defaults['title'] = CRM_Batch_BAO_Batch::generateBatchName(); |
| 79 | } |
| 80 | else { |
| 81 | $defaults = $this->_values; |
| 82 | } |
| 83 | return $defaults; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Process the form submission. |
| 88 | */ |
| 89 | public function postProcess() { |
| 90 | $params = $this->controller->exportValues($this->_name); |
| 91 | if ($this->_action & CRM_Core_Action::DELETE) { |
| 92 | CRM_Core_Session::setStatus("", ts("Batch Deleted"), "success"); |
| 93 | CRM_Batch_BAO_Batch::deleteBatch($this->_id); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | if ($this->_id) { |
| 98 | $params['id'] = $this->_id; |
| 99 | } |
| 100 | else { |
| 101 | $session = CRM_Core_Session::singleton(); |
| 102 | $params['created_id'] = $session->get('userID'); |
| 103 | $params['created_date'] = CRM_Utils_Date::processDate(date("Y-m-d"), date("H:i:s")); |
| 104 | } |
| 105 | |
| 106 | // always create with data entry status |
| 107 | $params['status_id'] = CRM_Core_OptionGroup::getValue('batch_status', 'Data Entry', 'name'); |
| 108 | $batch = CRM_Batch_BAO_Batch::create($params); |
| 109 | |
| 110 | // redirect to batch entry page. |
| 111 | $session = CRM_Core_Session::singleton(); |
| 112 | if ($this->_action & CRM_Core_Action::ADD) { |
| 113 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1&action=add")); |
| 114 | } |
| 115 | else { |
| 116 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/batch/entry', "id={$batch->id}&reset=1")); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |