Jump to content

Recommended Posts

Posted

Hello @ all,

I have created a simple module which uses a cronjob to trash pages of certain templates after the end date. I think my code should be correct, but nothing happens.

Here is my code:

<?php
class HookForEvents extends WireData implements Module
{
    public static function getModuleInfo()
    {
        return array(
            'title' => 'Cronjob to trash events',
            'summary' => 'Add a cronjob to delete events',
            'version' => 1,
            'autoload' => true
        );
    }
    public function init()
    {
        $this->addHook("LazyCron::everyMinute", $this, "trashevent");
    }
    public function trashevent(HookEvent $e)
    {
        //put event pages to trash after the end date
        $eventpages = wire("pages")->find("template=single-event|events");
        foreach ($eventpages as $eventpage) {
            $enddate     = $eventpage->publish_until;
            $currenttime = time();
            if ($enddate < $currenttime) {
                $pages->trash($eventpage);
            }
        }
    }
}
?>

The cronjob should only delete pages with the templates "events" or "single-event". The condition ist that the current time must be greater than the enddate (comparison).

If the condition is true, then the page should be trashed.

Maybe someone find a reason why this code doesnt work.

Posted

A really cursory look suggests it's because of $pages->trash - remember $pages is not available inside a function. Try wire('pages') like you have above.

Posted

No luck with wire('pages') at the moment. I have triggered the page in front- and backend but the pages are still there. 

Maybe it has something to do that the pages are unpublished at the moment?

Posted

No luck with wire('pages') at the moment. I have triggered the page in front- and backend but the pages are still there. 

Maybe it has something to do that the pages are unpublished at the moment?

Quite possibly - have you checked that the $eventpages array is containing the pages you are wanting to delete? What about if you add "include=all" to the selector?

  • Like 1
Posted

What about if you add "include=all" to the selector?

What a silly mistake. This was the reason - now it works!!! Thanks Adrian

  • Like 1

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
  • Recently Browsing   0 members

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