AnsweredAssumed Answered

create txt file and attach documents custom code

Question asked by Francisco Javier Garcia on Oct 21, 2019

Regards.

I am trying to create a csv or txt file that contains comma separated records, I still don't understand how to do it if the fopen functions for the cloud environment are blacklisted.
Anyone have any idea how I could do this?

Add these include/utils/file_utils.php and  include/utils/sugar_file_utils.php 

And check the following link  https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.1/Architecture/Module_Loader/Module_L…  

 

This is my code, if you generate the file and save it in upload but do not attach it to the document to download.

 public function buildFileCsv()
    {
        $GLOBALS['log']->fatal(" Construyendo archivos ");

        $filename = "PruebaTactos1.txt";
        $directorio='upload/'.$filename;
        //$cachefile = sugar_cached('upload/').$filename;
        $fp = sugar_fopen($directorio, 'w');
        fwrite($fp, "\n Agregamos otra linea mas");
        fclose($fp);
       // return($cachefile);
         $GLOBALS['log']->fatal(" CAche file ->  ".$fp);
         $nombre = $filename;
        $document = BeanFactory::newBean('Documents');
        $document->name = $nombre;
        $document->active_date = date("Y-m-d");
        $document->save();

        $document->load_relationship('tct5_periodo_comisiones_documents_1');

        $document->tct5_periodo_comisiones_documents_1->add($this->idperiodo);

        $docRevision = new DocumentRevision();
        $docRevision->revision = 1;
        $docRevision->document_id = $document->id;
        $docRevision->filename = $document->name;
        //$docRevision->file_ext = 'txt';
        //$docRevision->file_mime_type = 'text/plain';
        //$docRevision->assigned_user_id = $this->idUsr;
        $docRevision->save();

        $GLOBALS['log']->fatal("Documento " . $document->id . "  Revision  " . $docRevision->id . "  usuario  " . $this->idperiodo);
         //$uploadFile = new UploadFile();
         //get the file location
       // $uploadFile->temp_file_location = UploadFile::get_upload_path($docRevision->id);
       // $file_contents = $uploadFile->get_file_contents();
      
// $GLOBALS['log']->fatal(" Upload file content->  ".$file_contents);
         ob_end_clean();
      // $this->Output('upload/' . $docRevision->id, 'F');
}

Could you support me.

 

Thank you.

Outcomes