Recent Topics

BUG 6.9.X Dynamic plugin settings on several views AND fileselect breaks due to square brackets in fieldName

Started by on Jul 17, 2017 – Contents updated: Sep 29, 2017

Jul 17, 2017 17:23    

REFERENCE: https://github.com/b2evolution/b2evolution/commit/9a1fceb8dbc77f10ec25c382aa23bfb497a51b12

When including the item type 'type' => 'fileselect', the plugin breaks on file select.

[quote=@achillis#c109340]inc/_core/ui/forms/_form.class.php in function file_select_delete( event_object ) and function file_select_add( fieldName, root, path )

The solution is to wrap the square brackets with

fieldName = fieldName.replace(/(\[|\])/g, "\\\\$1");

function file_select_add( fieldName, root, path )

                    {
                        // check if value is already present

                        fieldName = fieldName.replace(/(\[|\])/g, "\\\\$1");
                        var inputField = jQuery( "input#" + fieldName );
                        var values = inputField.val().split( "'.$field_params['value_separator'].'" );

                        // Add new item
                        jQuery.ajax({
                            type: "GET",
                            url: "'.get_htsrv_url().'anon_async.php",
                            data: {
                                    "action": "get_file_select_item",
                                    "field_name": fieldName,
                                    "root": root,
                                    "path": path,
                                    "params": '.json_encode( $script_params ).'
                                },
                            success: function( result )
                                {
                                    result = jQuery.parseJSON( ajax_debug_clear( result) );
                                    var fieldName = result.fieldName;
                                    var fieldValue = result.fieldValue;
                                    var inputField = jQuery( "input#" + fieldName );
                                    var wrapper = jQuery( "div[name=" + fieldName + "].file_select_wrapper" );
                                    var maxLength = wrapper.data( "maxLength" );
                                    var overflowMode = wrapper.data( "overflowMode" );
                                    var addButton = jQuery( "button", wrapper );
                                    var items = jQuery( ".file_select_item:not(button)", wrapper );
                                    var lastItem = items.last();

                                    var newItem = jQuery( atob( result.item ) );

                                    if( fsel_replace )
                                    {
                                        var item = jQuery( fsel_obj ).closest( ".file_select_item" );
                                        newItem.insertAfter( item );
                                        file_select_delete( item );
                                    }
                                    else
                                    {
                                        // Attach new item
                                        // check if adding item will result to an overflow
                                        if( items.length >= maxLength )
                                        { // remove extra item first depending on overflow mode
                                            if( overflowMode == "queue" )
                                            {
                                                file_select_delete( items.first() );
                                            }
                                            else if( overflowMode == "stack" )
                                            {
                                                file_select_delete( items.last() );
                                            }

                                            items = jQuery( ".file_select_item:not(button)", wrapper );
                                            lastItem = items.last();
                                        }

                                        if( lastItem.length )
                                        { // attachment already exists, add to the last
                                            newItem.insertAfter( lastItem );
                                        }
                                        else
                                        { // no attachments yet
                                            wrapper.prepend( newItem );
                                        }
                                    }

                                    newItem.find( "span.remove_file_icon" ).replaceWith(\''.$remove_icon.'\'); // replace unlink icon with skin specific icon saved earlier
                                    newItem.find( "span.edit_file_icon" ).replaceWith(\''.$edit_icon.'\'); // replace unlink icon with skin specific icon saved earlier

                                    items = jQuery( ".file_select_item:not(button)", wrapper );
                                    lastItem = items.last();

                                    // Toggle add button
                                    addButton.html( items.length === 0 ? "'./* TRANS: verb */ T_('Select').'" : \''.get_icon( 'new' ).' '.T_('Add').'\' );
                                    if( maxLength > items.length )
                                    {
                                        addButton.show();
                                    }
                                    else
                                    {
                                        addButton.hide();
                                    }

                                    // append field value
                                    var values = inputField.val();
                                    values = values ? ( inputField.val().split( "'.$field_params['value_separator'].'" ) ) : [];
                                    values.push( fieldValue );
                                    inputField.val( values.join( "'.$field_params['value_separator'].'" ) );

                                    // Trigger change so bozo validator will pickup the change
                                    inputField.trigger( "change" );

                                    // close modal if single item select
                                    if( maxLength == 1 )
                                    {
                                        closeModalWindow();
                                    }
                                }
                        });

                        return false;
                    }

                    function file_select_delete( event_object )
                    {
                        var wrapper = jQuery( event_object ).closest( ".file_select_wrapper" );
                        var item = jQuery( event_object ).closest( ".file_select_item" );
                        var fieldName = wrapper.attr( "name" );
                        var fieldValue = item.data( "itemValue" ).toString(); // converted to string because it will later be compared to array of strings
                        var maxLength = wrapper.data( "maxLength" );
                        var addButton = jQuery( "button", wrapper );

                        // Remove file select item
                        item.remove();

                        var items = jQuery( ".file_select_item:not(button)", wrapper );
                        var lastItem = items.last();

                        // Toggle add button
                        addButton.html( items.length === 0 ? "'./* TRANS: verb */ T_('Select').'" : \''.get_icon( 'new' ).' '.T_('Add').'\' );
                        if( maxLength > items.length )
                        {
                            addButton.show();
                        }
                        else
                        {
                            addButton.hide();
                        }

                        // Change input value
                        fieldName = fieldName.replace(/(\[|\])/g, "\\\\$1");
                        var inputField = jQuery( "input#" + fieldName );
                        var values = inputField.val().split( "'.$field_params['value_separator'].'" );
                        var index =  values.indexOf( fieldValue );
                        if( index != -1 )
                        {
                            values.splice( index, 1 );
                        }
                        inputField.val( values.join( "'.$field_params['value_separator'].'" ) );
                        inputField.trigger( "change" );

                        return false;
                    }[/quote]

Jul 30, 2017 13:01

@achillis I confirm the issue and your solution. This information is already sent to the dev team in order to fix it.

Thank you!


Form is loading...

Multiblog engine – This forum is powered by b2evolution CMS, a complete engine for your website.