app/control/communication/SystemDocumentForm.class.php
/**
     * Save form data
     * @param $param Request
     */
Save form data
@param $param Request
Save form data @param $param Request
public function onSave( $param )
    {
        try
        {
            TTransaction::open('communication'); 
            $this->form->validate(); 
            $object = new SystemDocument;  
            $data = $this->form->getData(); 
            $object->fromArray( (array) $data); 
            $object->system_user_id = TSession::getValue('userid');
            $object->filename = TSession::getValue('system_document_upload_file');
            $object->clearParts();
            $object->store(); 
            if ($data->user_ids)
            {
                foreach ($data->user_ids as $user_id)
                {
                    TTransaction::open('permission');
                    $system_user = SystemUser::find($user_id);
                    TTransaction::close();
                    $object->addSystemUser( $system_user );
                }
            }
            if ($data->group_ids)
            {
                foreach ($data->group_ids as $group_id)
                {
                    TTransaction::open('permission');
                    $system_group = SystemGroup::find($group_id);
                    TTransaction::close();
                    $object->addSystemGroup( $system_group );
                }
            }
            $source_file   = 'tmp/'.TSession::getValue('system_document_upload_file');
            $target_path   = 'files/documents/' . $object->id;
            $target_file   =  $target_path . '/'.$object->filename;
            if (file_exists($source_file))
            {
                if (!file_exists($target_path))
                {
                    if (!@mkdir($target_path, 0777, true))
                    {
                        throw new Exception(_t('Permission denied'). ': '. $target_path);
                    }
                }
                else
                {
                    foreach (glob("$target_path/*") as $file)
                    {
                        unlink($file);
                    }
                }
                if (file_exists($target_path))
                {
                    rename($source_file, $target_file);
                }
            }
            $data->id = $object->id;
            $this->form->setData($data); 
            TTransaction::close(); 
            new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
        }
        catch (Exception $e) 
        {
            new TMessage('error', $e->getMessage()); 
            $this->form->setData( $this->form->getData() ); 
            TTransaction::rollback(); 
        }
    }
public function onSave ( $ param ) { try { TTransaction :: open ( 'communication' ) ; $ this -> form -> validate ( ) ; $ object = new SystemDocument ; $ data = $ this -> form -> getData ( ) ; $ object -> fromArray ( ( array ) $ data ) ; $ object -> system_user_id = TSession :: getValue ( 'userid' ) ; $ object -> filename = TSession :: getValue ( 'system_document_upload_file' ) ; $ object -> clearParts ( ) ; $ object -> store ( ) ; if ( $ data -> user_ids ) { foreach ( $ data -> user_ids as $ user_id ) { TTransaction :: open ( 'permission' ) ; $ system_user = SystemUser :: find ( $ user_id ) ; TTransaction :: close ( ) ; $ object -> addSystemUser ( $ system_user ) ; } } if ( $ data -> group_ids ) { foreach ( $ data -> group_ids as $ group_id ) { TTransaction :: open ( 'permission' ) ; $ system_group = SystemGroup :: find ( $ group_id ) ; TTransaction :: close ( ) ; $ object -> addSystemGroup ( $ system_group ) ; } } $ source_file = 'tmp/' . TSession :: getValue ( 'system_document_upload_file' ) ; $ target_path = 'files/documents/' . $ object -> id ; $ target_file = $ target_path . '/' . $ object -> filename ; if ( file_exists ( $ source_file ) ) { if ( ! file_exists ( $ target_path ) ) { if ( ! @ mkdir ( $ target_path , 0777 , true ) ) { throw new Exception ( _t ( 'Permission denied' ) . ': ' . $ target_path ) ; } } else { foreach ( glob ( " $ target_path /* " ) as $ file ) { unlink ( $ file ) ; } } if ( file_exists ( $ target_path ) ) { rename ( $ source_file , $ target_file ) ; } } $ data -> id = $ object -> id ; $ this -> form -> setData ( $ data ) ; TTransaction :: close ( ) ; new TMessage ( 'info' , TAdiantiCoreTranslator :: translate ( 'Record saved' ) ) ; } catch ( Exception $ e ) { new TMessage ( 'error' , $ e -> getMessage ( ) ) ; $ this -> form -> setData ( $ this -> form -> getData ( ) ) ; TTransaction :: rollback ( ) ; } }
Save form data
 @param $param Request
Save form data @param $param Request
// open a transaction // validate form data // create an empty object // get form data as array // load the object with data // save the object // if the user uploaded a source file // move to the target directory // get the generated id // fill form data // close the transaction //$action = new TAction; // in case of exception // shows the exception error message // keep form data // undo all pending operations