/** * method onSave() * Executed whenever the user clicks at the save button */ function onSave() { try { // open a transaction with database 'library' TTransaction::open('library'); // get the form data into an active record Loan $data = $this->form->getData('Loan'); $msg = ''; $persisted_objects = TSession::getValue('checkout_objects'); if ($persisted_objects) { // iterate the collection of active records foreach ($persisted_objects as $object) { // add the object inside the datagrid $this->datagrid->addItem($object); $item = Item::newFromBarcode($object->barcode); if ($item->status_id == '1') { $loan = new Loan(); $loan->member_id = $data->member_id; $loan->barcode = $item->barcode; $loan->loan_date = date('Y-m-d'); $loan->due_date = date('Y-m-d', strtotime('+7 day', strtotime(date('Y-m-d')))); // store the item $item->status_id = '2'; $item->store(); // stores the loan $loan->store(); $msg .= "{$item->barcode} - {$loan->due_date} - " . _t('Success') . "<br>"; } else { $msg .= "{$item->barcode} - " . _t('Not available') . "<br>"; } } TSession::setValue('checkout_objects', NULL); // set the data back to the form $this->form->setData($object); // shows the success message new TMessage('info', $msg); } // close the transaction TTransaction::close(); // reload the listing $this->onReload(); } catch (Exception $e) { // shows the exception error message new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // undo all pending operations TTransaction::rollback(); } }