Jump to content

Front end publish/unpublish/hide/trash/delete link


arturg
 Share

Recommended Posts

Sorry, it just occurred to me that that was a rather useless answer :)

You'll probably want to make use of the API and do something like this:

$p = $pages->get("page to alter");
$p->addStatus(Page::statusUnpublished);
$p->save();

You could put this inside a conditional that checks that the user has the correct role/permission. You may also want to do it based on the passing of a get or post variable so it can be part of your existing template file.

Hopefully that will get you started but let us know if you have any specific implementation questions.

  • Like 1
Link to comment
Share on other sites

thanks for answers, i would like to create dashboard for users , in dashboard users have a list of pages and small buttons for publish/unpublish , edit and delete. Can you help me how to implement that ? 

Link to comment
Share on other sites

First question - how are you going to handle the editing? The pub/unpub/trash are easy, but if editing you need to figure out if they will get a modal popup for editing there, or if they will be redirected to the admin for this. You might want to take a look at these modules: 

http://modules.processwire.com/modules/front-end-edit-lightbox/

http://modules.processwire.com/modules/fredi/

As for the pub/unpub/trash - there are a few ways to tackle this - I think the first thing you need to decide on is whether you want it done via ajax, or whether a page reload is acceptable. If the latter, then you can simply have a link like: http://mysite.com/change-status/?pid=1001&change=trash

The template file for the change-status page would contain something like this:

if($user->hasRole("required-role")) {
    $p = $pages->get((int) $input->get->pid);
    if($p->id) {
        if($input->get->change == 'trash') {
            $p->trash();
        }
        else {
            if($input->get->change == 'unpublish') {
                $p->addStatus(Page::statusUnpublished);
            }
            elseif($input->get->change == 'publish') {
                $p->removeStatus(Page::statusUnpublished);
            }
            $p->save();
        }
    }
}

Then you'd want to redirect back to the main dashboard page using $session->redirect()

Personally I'd prefer an AJAX approach. I would make use of jquery for this and call the above page via:

$('#trash').click(function(){
    $.get( '/change-status', function( data ) {
        //code to update status in dashboard

Of course there is more to it than that, but maybe you already know your way around jquery and ajax so I'll leave you to figure that bit out.

Link to comment
Share on other sites

Hi, if you define your buttons in a generic way from your dashboard template like:
 

<button class="pwbutton" data-pid="123" data-change="publish"...>
<button class="pwbutton" data-pid="123" data-change="unpublish"...>
<button class="pwbutton" data-pid="456" data-change="publish"...>   

Then you should be able to handle the ajax as follows (untested):

$('.pwbutton').click(function(){
    $.get( '/change-status', { pid: $(this).data('pid'), change: $(this).data('change') }, function() {
                        alert( "success" ); //e.g. update page status on dashboard
                }).fail(function() {
                        alert( "error" );  //e.g. error message
                });
});

Ideally the /change-status template should be aware of the ajax request and respone with infromation you need to update your dashboard accordingly.

Hope this helps to get you started. Maybe also take a look at the jQuery documentation to understand whats happening: jQuery.click()  jQuery.get()

Greetings

  • Like 3
Link to comment
Share on other sites

ok, i try your code, something goes wrong ,i have succes messege but nothing update in dashboard.

My change-status.php

  <?php

    $p = $pages->get((int) $input->get->pid);
    if($p->id) {
        if($input->get->change == 'trash') {
            $p->trash();
        }
        else {
            if($input->get->change == 'unpublish') {
                $p->addStatus(Page::statusUnpublished);
            }
            elseif($input->get->change == 'publish') {
                $p->removeStatus(Page::statusUnpublished);
            }
            $p->save();
        }
    }

?>

user-profile.php

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>

    <body>
            <button class="pwbutton" data-pid="1124" data-change="publish">pub</button>
            <button class="pwbutton" data-pid="1124" data-change="unpublish">unpub</button>

            <script src="https://code.jquery.com/jquery-2.2.3.min.js"   integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="   crossorigin="anonymous"></script>
            <script>
                $('.pwbutton').click(function () {
                    $.get('/change-status', {
                        pid: $(this).data('pid'),
                        change: $(this).data('change')
                    }, function () {
                        alert("success"); //e.g. update page status on dashboard
                    }).fail(function () {
                        alert("error"); //e.g. error message
                    });
                });
            </script>
    </body>

    </html>
Link to comment
Share on other sites

ok, i change:

$('.pwbutton').click(function () {
                    $.get('/change-status', {
                        pid: $(this).data('pid'),
                        change: $(this).data('change')
                    }, function () {
                        alert("success"); //e.g. update page status on dashboard
                    }).fail(function () {
                        alert("error"); //e.g. error message
                    });
                });

to: 

$('.pwbutton').click(function () {
                    $.get('<?php echo $pages->get('/change-status/')->url ?>', {
                        pid: $(this).data('pid'),
                        change: $(this).data('change')
                    }, function () {
                        alert("success"); //e.g. update page status on dashboard
                    }).fail(function () {
                        alert("error"); //e.g. error message
                    });
                });

It works ! :) 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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