Editor PHP Custom Upload question

Editor PHP Custom Upload question

gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

I have a custom upload function which takes an uploaded file and sends it to a cloud service.

At the moment the file uploads to a folder for which the name is hardcoded into the php script.

I'd like to set this folder name based upon another field set in the editor form but I can't work out how to get this value into the custom upload function.

I can set the name to be a variable and this works but it's how I get the variable to be set from another Editor field that's proving tricky for me. I'm wondering if a preUpload event might work somehow but I'm struggling.

Any ideas would be appreciated.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

    Use preCreate or preEdit or something else from this list: https://editor.datatables.net/manual/php/events
    and set a session variable with whatever you need from $values - (The values submitted by the client-side).
    Subsequently use the content of the session variable for your folder file etc name - and maybe unset it subsequently.

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Hi rf1234

    Thanks for you answer but I'm still struggling. preEdit and preCreate only fire on hitting the submit button. The file is uploaded before this happens.

    preUpload is on that list but it doesn't use $values.

    So I'm still kind of stuck.

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017

    Hmmm...

    "preEdit and preCreate only fire on hitting the submit button. The file is uploaded before this happens."

    Sounds like you are editing an existing record by uploading something. Then it is an easy one: Use a getFormatter to set the session variable!

    here is an example:

    Editor::inst( $db, 'table' )
        ->field(
            Field::inst( 'table.id' )->set( false )
                    ->getFormatter( function($val, $data, $opts) {
                        $_SESSION['thatId'] = $val;
                        return $val;
                    }),
    

    Roland

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Thanks anyway Roland. It's not an essential for my project but it would be great if I can get this working.

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    edited August 2017

    My recommendation doesn't make sense for you, I see. It only works under quite special circumstances and I could replace it myself by using $data[table.id] in the getFormatter of a different field in that same table.

    The only other thing I can think of is you set the session variable through ajax based on a suitable java script event. Here is an example:

    yourTable
    .on('select', function () {
        var selected = yourTable.row({selected: true});
        if (selected.any()) {
            $.ajax({
                type: "POST",
                url: 'actions.php?action=setSessionVariable',
                data: {
                    sessionVariable: selected.data().yourTable.id
                }
            }); ....
    
  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    I'd like to set this folder name based upon another field set in the editor form but I can't work out how to get this value into the custom upload function.

    There are two stages to this. The first is to send that extra information to the server - for that you need to use the ajaxData option of the upload field type. That will let you assign extra data to be submitted. You will need to make sure that the user enters that data before doing the upload though!

    Secondly - use a custom upload action. That will let you have complete control over where the file ends up and you can create your own path and move the file there using whatever data is required, including the information submitted.

    Regards,
    Allan

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Thanks Roland / Allan

    So if I add the following ajaxData line to my javascript for the upload field per the below this will feed the data from the "site" select field into the upload function in my PHP? Appreciating Allan your point that the user will have to have made the selection first.

    {
                                    'label': 'Site:',
                                    'name': 'drawings.site',
                                    'type': 'select'
                                },
    {
                            "label": "File ID:",
                            "name": "drawings.fId",
                            "type": "upload",
                            "display": function ( filenametoform ) {
                                return filenametoform ? //this
                                    filenametoform+' has been uploaded.' : //works
                                    'No file here pal'; // too
    
                            },
                            "clearText": "Remove file",
                            "ajaxData": function (d) {
                                    d.append ( 'sitename', 'table.row(ajaxRowId).data().drawings.site');
                                    }
                        },
    

    If this is correct how do I "read" the data in the PHP function. What would I write in here to get the $sitenamevariablefromajaxdata string?

    Field::inst( 'image' )
        ->upload(
            Upload::inst( function ( $file, $id ) {
                move_uploaded_file( $file['tmp_name'], '/uploads/'.$sitenamevariablefromajaxdata.'/'.$id );
                return $id;
            } )
                ->db( 'image', 'id', array(
                    'fileName' => Upload::DB_FILE_NAME,
                    'fileSize' => Upload::DB_FILE_SIZE
                ) )
        )
        ->setFormatter( 'Format::nullEmpty' );
    
  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0
    edited August 2017

    My javascript above is bunkem.

    Looking at the Network XHR Params in Firefox the data is sent in the request payload but the tablerow part simply returns that string of text. Removing the quotation marks doesn't work either. Apologies, I lifted that bit of code from another erroneous forum post. If I try the below the value it comes back undefined.

    "ajaxData": function (d) {
                                    d.append ( 'sitename', drawings.site);
                                    }
    

    Therefore is this where Roland's

    selected.data().yourTable.id
    

    bit comes in?

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    Answer ✓

    "I'd like to set this folder name based upon another field set in the editor form ..."

    Can't help you with Allan's recommendation but you could resolve the issue with this:

    editor
        .dependent('table.thatOtherField', function (val, data, callback) {
            //val: value of table.thatOtherField
            //other fields' values of the editor instance: data.values['table.anotherField'] 
            if (val > '') {  //set session variable when field is filled or changed
                $.ajax({
                    type: "POST",
                    url: 'actions.php?action=setSessionVariable',
                    data: { 
                        sessionVariable: val + data.values['table.anotherField'] 
                    }
                });
            }
        });
    

    In actions.php (or whatever you call it) you fill the $_SESSION variable and subsequently use it in the PHP upload instance.

    More on dependent: https://editor.datatables.net/reference/api/dependent()

  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Thanks Roland.

    I'll work on that. I think that's probably the final piece as I've now got the variable returning in the PHP from the XHR request. I can get the custom script to name a folder "this_is_a_test" on the cloud service using the $directory variable. All I need to do now (hopefully) is implement the dependent field part your have advised to replace "this_is_a_test" with the field value.

    "ajaxData": function (d) {
                                d.append ( 'foldername', 'this_is_a_test');
                                    }
    
    $directory = json_encode($_REQUEST['foldername']);
    
  • gordonc200gordonc200 Posts: 39Questions: 7Answers: 0

    Roland / John

    Many thanks for your help. Now all working. I went with Roland's answer and abandoned my code above.

    In the PHP script this line got the value and assigned it to the variable that I could use in the Custom Upload script.

    $sessionVariable = $_POST["sessionVariable"];
    
This discussion has been closed.