<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Server_Job_DailyCleanup extends TB_Server_Job_Base
{
    public function __construct( TB_Shared_Ent_TeamData_Job $jobEntity )
    {
        $this->entity = $jobEntity;
    }

    public function execute()
    {
        $notificationOffsetDt = new DateTime( 'now',  new \DateTimeZone( "UTC" )  );
        $notificationOffsetDt->modify( '-14 days' );
        TB_Shared_Ent_TeamData_Notification::cleanUp( $notificationOffsetDt );
        TB_Shared_Ent_TeamData_Job::cleanup();
    }

    public function onDone()
    {
        // Reset event to start the next day
        $nowDt = new DateTime( 'now',  new \DateTimeZone( "UTC" )  );
        $nowDt->setTime( 1, 0, 0 );
        $nowDt->modify( '+1 day' );
        $this->entity->trigger_dt = $nowDt;
        $this->entity->job_status = TB_Shared_Ent_TeamData_Job::JOB_STATUS_READY;
        $this->entity->touch( 'job_status' );
        $this->entity->save();
    }

    public static function getRefId()
    {
        return 'system-daily-cleanup';
    }
}