Jump to content

Admin Panel's Client List. Close account from list


Recommended Posts

Hello. We need to changing client status from cliens.php in admin by checkboxes. for example check users which we want to change status and on clicking button(change) change selected users status (or more every client has additional link/button when click change status on closed. please tell us right way for your cms.
p.s. and please sql request tell too maybe from mysql changing will need.

please check what i did. and tell me is it good way?


 

add_hook('AdminAreaFooterOutput', 1, function ($vars) {

    if ($vars['filename'] != 'clients') {
        return;
    }

    $token = generate_token('plain');

    return <<<HTML
<script>
jQuery(function($){

    window.closeClientFromList = function(userid) {

        if (!confirm('დარწმუნებული ხართ, რომ გსურთ არჩეული კლიენტების დახურვა? ეს პაკეტებსა და გადაუხდელ ინვოისებს გაუქმებულზე დააყენებს.')) {
            return false;
        }

        var url = 'clientssummary.php?userid=' + userid +
                  '&action=closeclient' +
                  '&token={$token}';

        var iframe = $('<iframe>', {
            src: url,
            style: 'display:none;',
            load: function() {
                setTimeout(function(){
                    location.reload();
                }, 1000);
            }
        });

        $('body').append(iframe);

        return false;
    };


    $('table tbody tr').each(function(){

        var row = $(this);
        var checkbox = row.find('input[name="selectedclients[]"]');

        if (!checkbox.length) {
            return;
        }

        var userid = checkbox.val();

        var link = '<a class="btn btn-xs btn-danger ml-1" '
            + 'href="#" '
            + 'onclick="return closeClientFromList(' + userid + ');">'
            + 'კლიენტის დახურვა</a>';

        row.find('td:last').append('&nbsp;' + link);

    });

});
</script>
HTML;

});

















add_hook('AdminAreaFooterOutput', 1, function ($vars) {

    if ($vars['filename'] != 'clients') {
        return;
    }

    $token = generate_token('plain');

    return <<<HTML
<script>
jQuery(function($){


    var btn = $('<button type="button" class="btn btn-danger" id="closeSelectedClients" style="margin-left:-160px;margin-top:20px;padding:10px;">მონიშნული კლიენტების დახურვა</button>');

   $('#btnSearchClients').after(btn);


    $('#closeSelectedClients').click(function(){

        var clients = [];

        $('input[name="selectedclients[]"]:checked').each(function(){
            clients.push($(this).val());
        });


        if (clients.length == 0) {
            alert('გთხოვთ მონიშნეტ მინიმუმ 1 კლიენტი');
            return;
        }


        if (!confirm('დარწმუნებული ხართ, რომ გსურთ არჩეული კლიენტების დახურვა? ეს პაკეტებსა და გადაუხდელ ინვოისებს გაუქმებულზე დააყენებს.')) {
            return;
        }


        var completed = 0;


        clients.forEach(function(userid){

            var url = 'clientssummary.php?userid=' + userid +
                      '&action=closeclient' +
                      '&token={$token}';


            var iframe = $('<iframe>', {
                src: url,
                style: 'display:none;',
                load: function(){

                    completed++;

                    if (completed == clients.length) {
                        location.reload();
                    }
                }
            });


            $('body').append(iframe);

        });

    });

});
</script>
HTML;

});

 

Link to comment
Share on other sites

  • 4 months later...

Your hook will work, but I wouldn't run it in production as is. It fires a hidden iframe GET at the admin "close client" URL for each row, so you get no error back if one fails, several requests can run in parallel if you add bulk selection, and you depend on an internal admin URL and token handling that can change between versions.

 

The cleaner way is to call the official API instead of replaying the admin page:

 

1. Build it as a small addon module (or at least a dedicated admin-only endpoint). An addon gives you an admin page plus access control by admin role, so not every staff member gets a "close clients" button.

2. Your list button only collects the selected client IDs and POSTs them, with the CSRF token, to that addon page.

3. On the server side, loop over the IDs and call localAPI('CloseClient', ['clientid' => $id]) for each one, collecting the result per client so you can show "closed 14, failed 1 (reason)" instead of reloading blindly.

 

Per the API docs, CloseClient does the same thing as the Close button: it closes the client, cancels their invoices and sets all their products to Cancelled or Terminated. It also fires the ClientClose hook, so anything else you have tied to closing still runs. One thing to test on a single client first: the docs only talk about statuses, so check whether the account is actually removed from the server (cPanel/Plesk etc.) or whether you still need to run Terminate on those services separately.

 

About SQL: please don't do it by setting the status to Closed in tblclients. That only changes the label. Services stay Active, unpaid invoices stay open, the cron keeps generating renewals and sending reminders, nothing is written to the activity log and no hooks run. Going through CloseClient avoids all of that.

 

If you just need this occasionally rather than daily, the same loop works as a one-off script that reads a list of client IDs. Take a database backup before the first big batch either way.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated