_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); // this mean it's a batch action if (!$this->_id ) { if (!empty($_GET['batch_id'])) { //validate batch ids $batchIds = explode(',', $_GET['batch_id']); foreach($batchIds as $batchId) { CRM_Utils_Type::validate($batchId,'Positive'); } $this->_batchIds = $_GET['batch_id']; $this->set('batchIds', $this->_batchIds); } else { $this->_batchIds = $this->get('batchIds'); } if (!empty($_GET['export_format']) && in_array($_GET['export_format'], array('IIF', 'CSV'))) { $this->_exportFormat = $_GET['export_format']; } } else { $this->_batchIds = $this->_id; } $allBatchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); $this->_exportStatusId = CRM_Utils_Array::key('Exported', $allBatchStatus); //check if batch status is valid, do not allow exported batches to export again $batchStatus = CRM_Batch_BAO_Batch::getBatchStatuses($this->_batchIds); foreach( $batchStatus as $batchStatusId ) { if ($batchStatusId == $this->_exportStatusId) { CRM_Core_Error::fatal(ts('You cannot exported the batches which were exported earlier.')); } } $session = CRM_Core_Session::singleton(); $session->replaceUserContext(CRM_Utils_System::url('civicrm/financial/financialbatches', "reset=1&batchStatus={$this->_exportStatusId}")); } /** * Build the form * * @access public * @return void */ function buildQuickForm() { // this mean it's a batch action if (!empty($this->_batchIds)) { $batchNames = CRM_Batch_BAO_Batch::getBatchNames($this->_batchIds); $this->assign('batchNames', $batchNames); // Skip building the form if we already have batches and an export format if ($this->_exportFormat) { $this->postProcess(); } } $optionTypes = array( 'IIF' => ts('Export to IIF'), 'CSV' => ts('Export to CSV'), ); $this->addRadio('export_format', NULL, $optionTypes, NULL, '
', TRUE); $this->addButtons( array( array( 'type' => 'next', 'name' => ts('Export Batch'), 'spacing' => '         ', 'isDefault' => TRUE, ), array( 'type' => 'cancel', 'name' => ts('Cancel'), ), ) ); } /** * process the form after the input has been submitted and validated * * @access public * @return void */ public function postProcess( ) { if (!$this->_exportFormat) { $params = $this->exportValues(); $this->_exportFormat = $params['export_format']; } if ($this->_id) { $batchIds = array($this->_id); } else if (!empty($this->_batchIds)) { $batchIds = explode(',', $this->_batchIds); } // Recalculate totals $totals = CRM_Batch_BAO_Batch::batchTotals($batchIds); // build batch params $session = CRM_Core_Session::singleton(); $batchParams['modified_date'] = date('YmdHis'); $batchParams['modified_id'] = $session->get('userID'); $batchParams['status_id'] = $this->_exportStatusId; $ids = array(); foreach($batchIds as $batchId) { $batchParams['id'] = $ids['batchID'] = $batchId; // Update totals $batchParams = array_merge($batchParams, $totals[$batchId]); CRM_Batch_BAO_Batch::create($batchParams, $ids, 'financialBatch'); } CRM_Batch_BAO_Batch::exportFinancialBatch($batchIds, $this->_exportFormat); } }