Vtiger 7.1. Is there any way to modify the position of a Field or a Block using VTlib?

67 Views Asked by At

In vtiger theres a table in the database called vtiger_field that stores everything related to a field. that table has a column called sequence which represents what position those fields are in. Though it is possible to use php code to modify the positions of the field, I would like to know if there is a way that we can move the field positions using Vtlib instead.

this question also applies to modifying the position of a block in the table vtiger_blocks

        $blockData =  array();
        // Get tabid
        $sql = mysql_query("SELECT tabid FROM vtigercrm71.vtiger_tab where name = 'ServiceContracts';",$DBconn); 
        $row = mysql_fetch_assoc($sql);
        $moduleId = $row['tabid'];        

        // get BlockId of the block intended to be moved
        $sql = mysql_query("SELECT blockid FROM vtigercrm71.vtiger_blocks where blocklabel = 'Renewal Details';",$DBconn); 
        $row = mysql_fetch_assoc($sql);
        $blockId = $row['blockid'];     

        // get sequence of the block to be moved under
        $sql = mysql_query("SELECT sequence FROM vtigercrm71.vtiger_blocks where blocklabel = 'LBL_SERVICE_CONTRACT_INFORMATION';",$DBconn); 
        $row = mysql_fetch_assoc($sql);
        $blockSequence = $row['sequence']; 

        // Get blocks in that module
        $sql = mysql_query("SELECT blockid,sequence FROM vtigercrm71.vtiger_blocks where tabid = ".$moduleId.";",$DBconn); 
        while($row = mysql_fetch_assoc($sql))
        {
            $blockData[] = array(
                'blockid' => $row['blockid'],
                'sequence' => $row['sequence']
            ); 
        }

        $moveToSequence = $blockSequence + 1; //No of sequence need to be moved to
        $targetSequence = null;

        // set the sequence of the block needed to be moved
        foreach($blockData as $block)
        {
            if ($blockId == $block['blockid'])
            {
                $targetSequence = $block['sequence'];
                break;
            }
        }

        // Update the block sequences from a lower sequence to a higher sequence
        foreach($blockData as $block)
        {
            $sequence = $block['sequence'];
            $blockId = $block['blockid'];
            if($block['sequence'] == $targetSequence)
            {
                $sequence = $moveToSequence;
            }
            elseif ($block['sequence'] >= $moveToSequence && $block['sequence'] < $targetSequence)
            {
                $sequence = $block['sequence'];
                $sequence + 1;
            }
            mysql_query("UPDATE vtigercrm71.vtiger_blocks SET sequence = '".$sequence."' WHERE (blockid = '".$blockId."');",$DBconn);
        }

    echo'<br>Block Sequence Changed<br>';

above is the code that i cooked up myself to change the sequence/position of a block. all of this is too long and messy just to move a single block sequence that is why im trying to find if there is a way to change the sequence using vtlib instead

Edited 4/12/2023: after trying to find the popup that appears when I edit a block position using the CRM, I think I managed to find the codes that are used to modify the block. However, I cant quite understand how it works, plus there are 2 files with the same functions yet different repositories.

the one in 'VtigerCRM/layouts/vlayout/modules/Settings/LayoutEditor/resources/LayoutEditor.js':

    /**
     * Function to regiser the event to make the blocks sortable
     */
    makeBlocksListSortable : function() {
        var thisInstance = this;
        var contents = jQuery('#layoutEditorContainer').find('.contents');
        var table = contents.find('.blockSortable');
        contents.sortable({
            'containment' : contents,
            'items' : table,
            'revert' : true,
            'tolerance':'pointer',
            'cursor' : 'move',
            'update' : function(e, ui) {
                thisInstance.updateBlockSequence();
            }
        });
    },

    /**
     * Function which will update block sequence
     */
    updateBlockSequence : function() {
        var thisInstance = this;
        var progressIndicatorElement = jQuery.progressIndicator({
            'position' : 'html',
            'blockInfo' : {
                'enabled' : true
            }
        });

        var sequence = JSON.stringify(thisInstance.updateBlocksListByOrder());
        var params = {};
        params['module'] = app.getModuleName();
        params['parent'] = app.getParentModuleName();
        params['action'] = 'Block';
        params['mode'] = 'updateSequenceNumber';
        params['sequence'] = sequence;

        AppConnector.request(params).then(
            function(data) {
                progressIndicatorElement.progressIndicator({'mode' : 'hide'});
                var params = {};
                params['text'] = app.vtranslate('JS_BLOCK_SEQUENCE_UPDATED');
                Settings_Vtiger_Index_Js.showMessage(params);
            },
            function(error) {
                progressIndicatorElement.progressIndicator({'mode' : 'hide'});
            }
        );
    },

    /**
     * Function which will arrange the sequence number of blocks
     */
    updateBlocksListByOrder : function() {
        var thisInstance = this;
        var contents = jQuery('#layoutEditorContainer').find('.contents');
        contents.find('.editFieldsTable').each(function(index,domElement){
            var blockTable = jQuery(domElement);
            var blockId = blockTable.data('blockId');
            var actualBlockSequence = blockTable.data('sequence');
            var expectedBlockSequence = (index+1);

            if(expectedBlockSequence != actualBlockSequence) {
                blockTable.data('sequence', expectedBlockSequence);
            }
            thisInstance.updatedBlockSequence[blockId] = expectedBlockSequence;
        });
        return thisInstance.updatedBlockSequence;
    },

the other one which is in 'VtigerCRM/layouts/v7/modules/Settings/LayoutEditor/resources/LayoutEditor.js':

    /**
     * Function to regiser the event to make the blocks sortable
     */
    makeBlocksListSortable: function () {
        var thisInstance = this;
        var contents = jQuery('#layoutEditorContainer').find('.contents');
        var table = contents.find('.blockSortable');
        contents.sortable({
            'containment': contents,
            'items': table,
            'revert': true,
            'tolerance': 'pointer',
            'cursor': 'move',
            'update': function (e, ui) {
                thisInstance.updateBlockSequence();
            }
        });
    },
    /**
     * Function which will update block sequence
     */
    updateBlockSequence: function () {
        var thisInstance = this;
        app.helper.showProgress();

        var sequence = JSON.stringify(thisInstance.updateBlocksListByOrder());
        var params = {};
        params['module'] = thisInstance.getModuleName();
        params['parent'] = app.getParentModuleName();
        params['action'] = 'Block';
        params['mode'] = 'updateSequenceNumber';
        params['sequence'] = sequence;
        params['selectedModule'] = jQuery('#selectedModuleName').attr('value');

        app.request.post({'data': params}).then(
            function (err, data) {
                app.helper.hideProgress();
                if (err === null) {
                    app.helper.showSuccessNotification({'message': app.vtranslate('JS_BLOCK_SEQUENCE_UPDATED')});
                } else {
                    app.helper.showErrorNotification({'message': err.message});
                }
            });
    },
    /**
     * Function which will arrange the sequence number of blocks
     */
    updateBlocksListByOrder: function () {
        var thisInstance = this;
        var contents = jQuery('#layoutEditorContainer').find('.contents');
        contents.find('.blockSortable:visible').each(function (index, domElement) {
            var blockTable = jQuery(domElement);
            var blockId = blockTable.data('blockId');
            var actualBlockSequence = blockTable.data('sequence');
            var expectedBlockSequence = (index+1);

            if (expectedBlockSequence != actualBlockSequence) {
                blockTable.data('sequence', expectedBlockSequence);
            }
            thisInstance.updatedBlockSequence[blockId] = expectedBlockSequence;
        });
        return thisInstance.updatedBlockSequence;
    },

Now if I were to follow Ruben's suggestion, I would have no idea on how to use those functions to modify the blocks

1

There are 1 best solutions below

0
On

Vtiger vtlib does not support field & block sequence updates. You have to prepare a query to update all blocks or fields impacted by change.

  1. Vtiger Layout Editor

In this screen, Vtiger used a jQuery library to get the sequence of blocks or fields. So when you click save on the Layout Editor page, it posts the sequence from UI.

  1. Update Sequence by Vtiger

When a request is posted to the server, it goes to the "Settings_LayoutEditor_Block_Action" class, "updateSequenceNumber" function which eventually calls to the "Vtiger_Block_Model" class, "updateSequenceNumber" function, and prepares a single update query with when case and executes.

File paths for class mentioned

  • Settings_LayoutEditor_Block_Action - Vtiger/modules/Settings/LayoutEditor/actions/Blocks.php
  • Vtiger_Block_Model - Vtiger/modules/Vtiger/models/Blocks.php