Uploads Many & RowReorder

Uploads Many & RowReorder

Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

What is the best method to allow a user to reorder multiple uploads? I think it cannot be done in the 'create' or 'edit' form, which would be the best solution I think?

If that is the case would it be best to create a separate datatable view of just the uploads linked to that item and in that table enable the RowReorder plugin?

I am using a link table for my one-to-many join which currently just has two columns, the file identifier and the identifier of the item the file is linked too. Is the appropriate place for a position column in this link table too? So my link table structure would be; position, file_id, linked_item_id

Any thoughts gratefully received.

Thanks

Chris

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    would it be best to create a separate datatable view of just the uploads linked to that item and in that table enable the RowReorder plugin

    Exactly that.

    Ideally yes, the order should be in the link table, but the Editor libraries don't currently play particularly well with other meta data in the link table - they expect just the two pieces of link information. It will delete and create new rows for an edit which causes any meta information to be lost.

    The other option is to create or modify the upload plug-in to allow items to rearranged, although to be honest I think that might be fairly tricky to do.

    I fear that this one is on the boundary, or just past it, of what the libraries can currently do without a fair amount of alteration.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    So, effectively using a link table does not allow for reordering.

    If I remove the link table and do a direct link one-to-many join to a 'files' table and introduce the 'position' column into the directly linked 'files' table will that work better?

    I think the user will still be able to do an 'uploads many' from the main 'create / edit' form but if the user wants to reorder the photos I will need to show them a separate datatable which has just the linked photos and RowReorder enabled. In fact if the user wanted to add any additional image specific information, i.e. alt tag or caption, then the data would have to be editable in the image specific child datatable only?

    Would that work?

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Yes, that would work - the downside is that it wouldn't be possible to reuse the same photo in multiple parent rows (which is the benefit of the link table).

    This is without question a limitation in the software at the moment I'm afraid. Meta data just isn't well handled in the link tables.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Thanks for your reply Allan.

    Maybe another option could be to introduce a third table which holds the Meta data on a per file relationship level, i.e. the same file could have different Meta data for each relationship it has in a link table. However, not sure how I would do this right now or if it is even possible, would need to give the structure some thought.

    Thanks

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2

    Hi Allan. Seems this does not work or I do not have the correct code. When I use a link table the value in the column iChaletId is inserted correctly. However, when I use a direct link I thought I could use the same column in the 't_file' table. However, it doesn't get updated. Is that normal behaviour?

                    ->join(
                        DataTables\Editor\Mjoin::inst( 't_file' )
                            ->link( 't_chalet.iChaletId', 't_file.iChaletId' )
                            ->fields(
                                DataTables\Editor\Field::inst( 'iChaletId' ),
                                DataTables\Editor\Field::inst( 'iFileId' )
                                    ->upload( DataTables\Editor\Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/images/common/chalet/__ID__.__EXTN__' )
                                        ->db( 't_file', 'iFileId', array(
                                            'filename'    => DataTables\Editor\Upload::DB_FILE_NAME,
                                            'filesize'    => DataTables\Editor\Upload::DB_FILE_SIZE,
                                            'web_path'    => DataTables\Editor\Upload::DB_WEB_PATH,
                                            'system_path' => DataTables\Editor\Upload::DB_SYSTEM_PATH
                                        ) )
                                        ->validator( DataTables\Editor\Validate::fileSize( 2000000, 'Files must be smaller that 2mb' ) )
                                        ->validator( DataTables\Editor\Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                                    )
                            )
                    )    
    

    Thanks

    Chris

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin

    The table used for the files in the Upload->db() shouldn't be used as the Editor table as well. The reason for that is that the file upload is async to the rest of the form, so the file information will already have been written to the database. If you then try to create another row on that same table, the database structure is going to go badly wrong.

    Have a table just for the file meta information only.

    Allan

  • Restful Web ServicesRestful Web Services Posts: 202Questions: 49Answers: 2
    edited April 2018

    I have implemented the following solution, for anyone interested or in a similar situation. I didn't have a requirement to be able to reuse the same image multiple times so I could attach position (i.e. the order) directly to the file in the files table.

    I noticed that the rowReorder plugin does not require the position value to be an exact count from X to Y. I.e. if you have 3 items to reorder you do not need to have positions 1, 2 and 3. If the position values are unique and incremental it does not seem to matter what the gap is between them. So, for example with 3 items to reorder you could have position values 45, 109 and 1005.

    So I decided the easiest way to implement rowReorder without the need for any additional postUpload, postRemove, etc... code was to simply use a timestamp as the position value like so:

                    ->join(
                        DataTables\Editor\Mjoin::inst( 't_file' )
                            ->link( 't_chalet.iChaletId', 't_chalet_file.iChaletId' )
                            ->link( 't_file.iFileId', 't_chalet_file.iFileId' )
                            ->fields(
                                DataTables\Editor\Field::inst( 'iFileId' )
                                    ->upload( DataTables\Editor\Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/images/common/chalet/__ID__.__EXTN__' )
                                    ->db( 't_file', 'iFileId', array(
                                        'filename'    => DataTables\Editor\Upload::DB_FILE_NAME,
                                        'filesize'    => DataTables\Editor\Upload::DB_FILE_SIZE,
                                        'extension'   => DataTables\Editor\Upload::DB_EXTN,
                                        'web_path'    => DataTables\Editor\Upload::DB_WEB_PATH,
                                        'system_path' => DataTables\Editor\Upload::DB_SYSTEM_PATH,
                                        'position'    => time(),
                                    ))
                                    ->validator( DataTables\Editor\Validate::fileSize( 4000000, 'Files must be smaller that 4mb' ) )
                                    ->validator( DataTables\Editor\Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ))
                                )
                            )                      
                    )  
    

    That way a unique, incremental value for position automatically gets generated when the file is uploaded using the multi-upload method without any post processing.

    If the user wants to reorder the files then they click a custom button within the table which loads a second datatable view showing just the files with the rowReorder plugin enabled allowing the user to reorder to the files.

    I have a very small number of users who have access to add files so for me it is extremely unlikely two users would be uploading images for the same parent item at exactly the same second so time() should provide enough uniqueness for me.

This discussion has been closed.