⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

upload of multiple pdf



marco

marco
  • profile picture
  • Member

Posted 11 April 2018 - 13:12 PM

Hi,

 

I did several tests to load several pdf but I still can't do it.

 

I was thinking of making a join table (file_id, notice_id) and a listing table (file_id, file_name). I would also have liked to have the list of loaded pdf (that I could click) in the form.

 

I'm not asking the community to make me all my code of course but I need leads.
 

someone can help me to start ?

 

Thanks


marco

marco
  • profile picture
  • Member

Posted 27 April 2018 - 09:12 AM

Here my solution

 

in Grocery_CRUD library, here the modifications in get_upload_file_input function:

/*
		$input = '<span class="fileinput-button qq-upload-button" id="upload-button-'.$unique.'" style="'.$uploader_display_none.'">
			<span>'.$this->l('form_upload_a_file').'</span>
			<input type="file" name="'.$this->_unique_field_name($field_info->name).'" class="gc-file-upload" rel="'.$this->getUploadUrl($field_info->name).'" id="'.$unique.'">
			<input class="hidden-upload-input" type="hidden" name="'.$field_info->name.'" value="'.$value.'" rel="'.$this->_unique_field_name($field_info->name).'" />
		</span>';
*/
		$input = '<span class="fileinput-button qq-upload-button" id="upload-button-'.$unique.'">
			<span>'.$this->l('form_upload_a_file').'</span> 
			<input type="file" name="'.$this->_unique_field_name($field_info->name).'" class="gc-file-upload" rel="'.$this->getUploadUrl($field_info->name).'" id="'.$unique.'">
			<input class="hidden-upload-input" type="hidden" name="'.$field_info->name.'" value="'.$value.'" rel="'.$this->_unique_field_name($field_info->name).'" />
		</span>';

before the return $input;

$input .= "<div id='pdfs'></div>"; //ajout

in my view, after js and css files

<script>
    /* fonction de suppression pour d'un pdf */
    function supprimerPdf(id) {
        var part = (window.location.pathname).match(/\d+$/);
        $.ajax({
            type: "get",
            url: "<?php echo base_url("index.php/participations/delete_pdfs/"); ?>" + id + "/" + part,
            dataType: 'JSON',
            success: function (data) {
                $('#pdf_' + id).css({'color':'#aaaaaa', 'text-decoration':'line-through'});
                $('#pdf_' + id + ' > .mm-delete').removeAttr('onclick').empty().next().next().removeAttr('onclick').empty();
            },
            error: function (a,b,c) {
                console.log(a);
                console.log(b);
                console.log(c);
            }
        });
    }
    /* fonction la lecture d'un pdf */
    function lirepdf(pdf) {
        window.open(pdf);
    }

</script>

at the end of view

<script>
    window.onload = function () {
        
        var id = (window.location.pathname).match(/\d+$/);
        
        /* Affichage des pdfs */
        function recherche_pdf(id) {
            
            $.ajax({
                type: "get",
                url: "<?php echo base_url("index.php/participations/get_pdfs/"); ?>" + id[0],
                dataType: 'JSON',
                success: function (data) {
                    $('#pdfs').empty();
                    if (data != 'nopdf') {
                        for (var pdf in data) {
                            var name_pdf = data[pdf].substr(6);

                            $('#pdfs').append('<br/><span id="pdf_' + pdf + '"><span class="mm-delete" onClick="supprimerPdf(' + pdf + ')"><span class="text-danger glyphicon glyphicon-trash"></span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span>' + name_pdf + '</span>&nbsp;&nbsp;&nbsp;&nbsp;<span onClick="lirepdf(\'<?php echo base_url('/assets/pdfs/'); ?>' + data[pdf] + '\')"><span class="text-info glyphicon glyphicon-eye-open"></span></span></span>');
                        };
                    }
                },
                error: function (a,b,c) {
                    console.log(a);
                    console.log(b);
                    console.log(c);
                }
            });
        }
        
        if (id != null) {
            recherche_pdf(id);
        }
        
        $('#file_url_display_as_box').click(function () {
            recherche_pdf(id);
        });

        
        
    }

</script>

The get_pdfs function returns an array $data[id_file] = $pdf_name.
this function calls a join table id_file / part_id (main table id)
and a table that lists the files.

 

in controller

$state = $crud->getState();

if ($state == 'delete_file')
{
   die; // prevents the deletion of the pdf after its insertion ;O))
}