Change the upload name of a file in Editor 2.0.10

Change the upload name of a file in Editor 2.0.10

Mairie du PecqMairie du Pecq Posts: 14Questions: 4Answers: 0
edited July 24 in Editor

Hi,
I try to change the name of an uploaded file with the value of a field.
I was find this, but in Editor 2.0.10 that doesn't work

Field::inst( 'decisions.dec_titre' )
Field::inst( 'decisions.dec_file1_id' )
  ->upload(
    Upload::inst( function ($file, $id, $data)) {
      $titre = $data['decisions']['dec_titre'];
      $newName = $titre . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
      $filePath = '/mnt/' . $data['decisions']['dec_annee'] . '/' . $newName;
      move_uploaded_file($file['tmp_name'], $filePath);
      return $filePath;
  })
...

I have doing the update (2.2.2) to try if that work, but same problem.
Is there a way to do that ?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin
    Answer ✓

    No. Unfortunately that is not possible - the reason is that the file upload is async to the rest of the form. i.e. as soon as you select the file, it is uploaded. Even if that is because the user has entered anything else into the form. So your field might not have a value.

    You also need to consider what would happen if the user gave the same name for different files. It could just report an error, but then the user is left guessing at a file name that hasn't been used before.

    You could use the ajax option of upload to add data to send to the server (i.e. the field's value for use in your upload handler) and use preUpload to validate that there is a value before sending the file, but I feel it would be unintuitive.

    Allan

  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421
    Answer ✓

    Allan is of course right with his point. But you could update the file name either in a separate batch routine or you use "writeEdit" and "writeCreate, or "validatedEdit" and "validatedCreate". You can read the file names using your own SQL and then update the file names if they are not identical to your field contents.

    If you do it that way you would have the file names updated even before they are read back from the server.

    https://editor.datatables.net/manual/php/events

    On many occasions I do additional updates on "writeEdit" or "writeCreate" because I want to make sure that all updates have been made before the data is read back from the data base.

    This is a simple example. I call a function that makes additional database updates using Editor's db handler and Editor's "raw" and "insert" methods. Of course you can use your own db-handler and use "global" instead of using Editor's db handler.

    ->on('writeCreate', function ( $editor, $id, $values ) use ( $db ) {
        processCtrLinkedContracts ( $values, $id, $db );
    } )
    ->on( 'writeEdit', function ( $editor, $id, $values ) use ( $db ) {
        processCtrLinkedContracts ( $values, $id, $db );
    } )
    
    function processCtrLinkedContracts ( $values, $id, $db ) {
        //if comment or description: do nothing!
        if ( ! isset($values['ctr_has_ctr-many-count']) ) {
            return;
        }
        $db->raw()
           ->bind( ':id', $id )
           ->exec( 'DELETE FROM ctr_has_ctr
                     WHERE ctr_id        = :id
                        OR linked_ctr_id = :id' );
    
        for ($i = 0; $i < $values['ctr_has_ctr-many-count']; $i++) {
            $linkedId = $values["ctr_has_ctr"][$i]["linked_ctr_id"];
            $res = $db->insert( 'ctr_has_ctr', array (
                'ctr_id'        => $id,
                'linked_ctr_id' => $linkedId
            ) );
            $res = $db->insert( 'ctr_has_ctr', array (
                'ctr_id'        => $linkedId,
                'linked_ctr_id' => $id
            ) );
        }
    }
    
  • Mairie du PecqMairie du Pecq Posts: 14Questions: 4Answers: 0

    Thx @allan et @rf1234 for your feedback !

  • Mairie du PecqMairie du Pecq Posts: 14Questions: 4Answers: 0
    edited July 31

    But, in this example :

    $id = 4;
    
    Editor::inst( $db, 'decisions', 'dec_id' )
        ->fields(
            Field::inst( 'decisions.dec_id' ),
            Field::inst( 'decisions.dec_num' ),
            Field::inst( 'decisions.dec_annee' ),   
            Field::inst( 'decisions.dec_titre' )
                ->getFormatter('Format::strFirstLetterUpper'),
            Field::inst( 'decisions.dec_file1_id' )
                ->setFormatter( Format::ifEmpty( null ) )
                ->upload( 
                    Upload::inst( '/mnt/nas-pecq-01/ACTES/DECISIONS/'.$annee.'/'.$id.'.__EXTN__' )
    
    

    That work, my file become 4.docx

    In my case, that on an Edit bouton, with all the information already write before the upload action
    Do i need to push the information in the php script like with "annee" ?

    editor_edit = new $.fn.dataTable.Editor( {
                    ajax: "includes/inc_depot_decisions.php?annee="+annee,
    

    I only need the information in the field "decisions.dec_titre" to compose the file name that is already in DB.

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin
    Answer ✓

    The upload will happen before the rest of the form is submitted.

    If the end user has already filled in the contents of the field decisions.dec_titre you need to use the data option of theajax configuration for the upload field type to add the data to send to the server, so that you can do $_POST['dec_titre]` for example.

    Does that make sense?

    Allan

  • Mairie du PecqMairie du Pecq Posts: 14Questions: 4Answers: 0

    Thx Allan for your time and explanations.

    It works well !

    ajax: {
                        url: "includes/inc_depot_decisions.php",
                        type: 'POST',
                        data: function (d) {
                            d.annee = annee;
                            d.dec_num = editor.field("decisions.dec_num").val();
                            d.dec_titre = editor.field("decisions.dec_titre").val();
                        }
                    },
    
Sign In or Register to comment.