Skip to content

Instantly share code, notes, and snippets.

@sdwru
Last active July 14, 2023 21:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdwru/8e3382f0be0fa0aa85c668ef4470c522 to your computer and use it in GitHub Desktop.
Save sdwru/8e3382f0be0fa0aa85c668ef4470c522 to your computer and use it in GitHub Desktop.

Combine multiple ajax permission calls into one.

Depending on how larger your tables are, and how large the latency between you and the server, the UI speed increases can be quite dramatic.

Add the following function to astpp/assets/js/module_js/generate_grid.js around line 360

function build_buttons_multi(buttons_arr) {
    var jsonObj = []; //declare object
    if (buttons_arr == "") {
        return jsonObj;
    }
    var btn_field = [];
    var button_property = new Array();

    var btn_arr = buttons_arr;
    for (var key in btn_arr) {
        if (btn_arr[key] != null)
            btn_field.push(btn_arr[key]);
    }

    var obj = {};
    var post_data = [];
    for (var i = 0; i < btn_field.length; i++) {
        if (btn_field[i] != "") {
            var btn_str = btn_field[i];
            button_property = btn_str.toString().split(',');
            //ASTPP 3.0  
            custom_url = base_url + button_property[4];
            result = custom_url.replace(/.*?:\/\//g, "");
            result = result.replace("//", "/");
            var newURL = window.location.protocol + "//" + result;
            var layout = 'small';

            if (typeof button_property[6] != 'undefined' && button_property[6] != '') {
                layout = button_property[6];
            }

            obj = {
                'button_name': button_property[7],
                'current_url': window.location.href,
                'button_property': button_property,
                'newURL': newURL,
                'layout': layout
            };
            post_data.push(obj)
            //console.log('post data ', obj);
        }
        //console.log('i got nothing ');
    }
    if (btn_field.length === 0) {
        post_data = {};
    }
    $.ajax({
        type: 'POST',
        async: false,
        url: base_url + "login/customer_permission_list_multi/",
        data: {post_data:post_data},
        success: function (response) {
            //console.log('result is ', response);
            result = JSON.parse(response);
            //console.log('result is ', result);
            // alert(button_property[0]+'---'+response_trim);
            $.each(result, function(idx, val) {
                //console.log('idx is ',post_data[idx].button_property);
                if (val == 0) {
                    if (post_data[idx].button_property[5] == 'popup') {
                        jsonObj.push({
                            //name: gettext_custom(button_property[0]), 
                            name: post_data[idx].button_property[0],
                            bclass: post_data[idx].button_property[1],
                            iclass: post_data[idx].button_property[2],
                            btn_url: post_data[idx].newURL,
                            clayout: post_data[idx].layout,
                            onpress: button_action_popup
                        });
                    } else {
                        jsonObj.push({
                            //name: gettext_custom(button_property[0]), 
                            name: post_data[idx].button_property[0],
                            bclass: post_data[idx].button_property[1],
                            iclass: post_data[idx].button_property[2],
                            btn_url: post_data[idx].button_property[4],
                            clayout: post_data[idx].layout,
                            onpress: button_action
                        });
                    }
                }
            });
        }
    });
    return jsonObj;
}

Add the following to astpp/application/modules/login/controllers/login.php around like 720

function customer_permission_list_multi()
{
    $postData = $this->input->post()['post_data'];
    if (!isset($postData)) {
        echo '0';
        return;
    }
    foreach ($postData as $k => $button_array) {
        //$button_array = $this->input->post();
        //var_dump($button_array['button_name']);
        $permissioninfo = $this->session->userdata('permissioninfo');
        $currnet_url = $button_array['current_url'];
        $url_explode = explode('/', $currnet_url);
        $module_name = $url_explode[3];
        $sub_module_name = $url_explode[4];
        //var_dump($button_array['button_name']);
        //var_dump($permissioninfo[$module_name][$sub_module_name][$button_array['button_name']]);
        if ((isset($permissioninfo[$module_name][$sub_module_name][$button_array['button_name']]) 
        && $permissioninfo[$module_name][$sub_module_name][$button_array['button_name']] == 0) or $permissioninfo['login_type'] == '-1' or $permissioninfo['login_type'] == '0' or $permissioninfo['login_type'] == '3') {
            //echo 0;
            $perm[$k] = 0;
        } else {
            //echo 1;
            $perm[$k] = 1;
        }
    }
    echo json_encode($perm);
}

Modify the following line in astpp/assets/js/module_js/generate_grid.js in function build_grid() around line 208.

Change

buttons: build_buttons(buttons),

To

buttons: build_buttons_multi(buttons),

Also, for additional speed improvements, remove language translation(?) by changing the following line around 272

Change

custom_display = gettext_custom(collumn_property[0]);

To

custom_display = collumn_property[0];

Add static file caching to nginx

Add the following to /etc/nginx/sites-available/astpp.conf around line 28, below the log entires in the server { section.

# Enable cache for static files
location ~* \.(js|css|png|jpg|jpeg|gif|ico|po)$ {
        expires 2d;
        add_header Cache-Control "public, no-transform";
}

and restart nginx

systemctl restart nginx
@tcreek
Copy link

tcreek commented Jul 14, 2023

I am seeing "astpp.conf" located in /etc/nginx/conf.d/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment