Suitecrm: When deleting attachments of custom type "File", empty records appear in Listview

Created on 3 May 2017  路  1Comment  路  Source: salesagility/SuiteCRM

I have created a File upload field using this tutorial:
http://www.bhea.com/blog/creating-file-upload-field-core-modulecustom-module-sugar-7-x/
I have reproduced steps 1,2, and 3 in the tutorial.

  1. I created "custom/Extension/modules/{YOUR_MODULE}/Ext/Vardefs/sugarfield_{your_field_name}.php"
  2. I created "custom/Extension/modules/{YOUR_MODULE}/Ext/Language/{your_field_name}.en_us.lang.php"
  3. I performed Admin > Repair > Quick Repair and Rebuild
  4. I added the new field to Admin> Studio > {MY_MODULE} > Layouts > Editview

Now I have a functional File Upload button in Editview.
1

Issue

When I go to Editview and i remove the file, the file is unlinked from the entity. But an empty record is displayed in Listview.
It is a bug because the database gets filled with empty records.
2

Expected Behavior

Empty lines should not appear in Listview. The uploaded File should be simply removed and deleted from the database.

Actual Behavior

Each time I delete a file uploaded in the File field, an empty record appears.

Steps to Reproduce

  1. Reproduce the first 3 steps from this tutorial http://www.bhea.com/blog/creating-file-upload-field-core-modulecustom-module-sugar-7-x/
  2. Go to Admin > Studio > {YOUR_MODULE} > Layouts > EditView and place the new field in the Edit view panel (for me, the remove button only worked once I placed the File field next to Image upload fields in editview!)

Your Environment

SuiteCRM 7.8.1

Most helpful comment

I found the solution, I want to share it with you:

I also proceeded to the next steps in the tutorial to remove the uploaded file.
http://www.bhea.com/blog/creating-file-upload-field-core-modulecustom-module-sugar-7-x/

  1. I modified the file custom/Extension/application/Ext/Include/{YOUR_MODULE}.php
  2. and the file custom/modules/{YOUR_MODULE}/{YOUR_MODULE}.php

At step 2, instead of the code for the deleteAttachment method provided by the tutorial, i added the following code:

public function deleteAttachment($isDuplicate = 'false')
{
    if ($this->ACLAccess('edit')) {
        if ($isDuplicate === 'true') {
            return true;
        }
        $removeFile = "upload://{$this->id}";
    }
    if (file_exists($removeFile)) {
        if (!unlink($removeFile)) {
            $GLOBALS['log']->error("*** Could not unlink() file: [ {$removeFile} ]");
        } else {
            $this->uploadfile = '';
            $this->filename = '';
            $this->file_mime_type = '';
            $this->file_ext = '';
            $this->save();

            return true;
        }
    } else {
        $this->uploadfile = '';
        $this->filename = '';
        $this->file_mime_type = '';
        $this->file_ext = '';
        $this->save();

        return true;
    }

    return false;
}

Now everything works fine!

>All comments

I found the solution, I want to share it with you:

I also proceeded to the next steps in the tutorial to remove the uploaded file.
http://www.bhea.com/blog/creating-file-upload-field-core-modulecustom-module-sugar-7-x/

  1. I modified the file custom/Extension/application/Ext/Include/{YOUR_MODULE}.php
  2. and the file custom/modules/{YOUR_MODULE}/{YOUR_MODULE}.php

At step 2, instead of the code for the deleteAttachment method provided by the tutorial, i added the following code:

public function deleteAttachment($isDuplicate = 'false')
{
    if ($this->ACLAccess('edit')) {
        if ($isDuplicate === 'true') {
            return true;
        }
        $removeFile = "upload://{$this->id}";
    }
    if (file_exists($removeFile)) {
        if (!unlink($removeFile)) {
            $GLOBALS['log']->error("*** Could not unlink() file: [ {$removeFile} ]");
        } else {
            $this->uploadfile = '';
            $this->filename = '';
            $this->file_mime_type = '';
            $this->file_ext = '';
            $this->save();

            return true;
        }
    } else {
        $this->uploadfile = '';
        $this->filename = '';
        $this->file_mime_type = '';
        $this->file_ext = '';
        $this->save();

        return true;
    }

    return false;
}

Now everything works fine!

Was this page helpful?
0 / 5 - 0 ratings