create txt file and attach documents custom code

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.

  • I recently ran into a similar issue. We were generating a csv file within Sugar and wanted to attach that file as a Document under the Meetings module. Here is how we ended up solving the issue.

    $content = $this->getCSVContent();

    $documentBean = BeanFactory::newBean('Documents');
    $documentBean->name = 'Report.csv';
    $documentBean->status_id = 'Active';
    $documentBean->revision = '1';
    $documentBean->active_date = date('Y-m-d');
    $documentBean->save();

    $documentSoap = new DocumentSoap();
    $document = array(
        'id' => $documentBean->id,
        'file' => base64_encode($content),
        'filename' => 'report.csv',
        'revision' => '1',
    );

    $documentSoap->saveFile($document);

    $bean->load_relationship('meetings_documents_1');
    $bean->meetings_documents_1->add($documentBean);

    Note that this was tested in an on premise instance, we did not test the compatibility with Ondemand instances, but we're not using any forbidden functions, so it should be working fine either way.