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

require_once __DIR__ . '/../../config/boot_global.php';
require_once __DIR__ . '/../../config/boot_local.php';


class TB_Server_Cron_DailyCleanup extends TB_Server_Core_CronBase {

    /**
     * Delete outdated
     * @throws Exception
     */
    protected function run() {
        //
        // Remove old DB dumps
        //
        $dayInSeconds = 60 * 60 * 24;
        $pastDays = 7;
        $dumpFile = Francis_Utils_Config::get( 'path.backup_folder' ) . '/dbdump';
        while ( file_exists( $dumpFile . date( 'Y-m-d', ( time() - ( $pastDays * $dayInSeconds ) ) ) . '.zip' ) ) {
            unlink( $dumpFile . date( 'Y-m-d', ( time() - ( $pastDays * $dayInSeconds ) ) ) . '.zip' );
            $pastDays++;
        }

        //
        // Delete outdated INVITATION Mails
        //

        // Params
        $state = TB_Shared_Ent_Core_Invitation::STATE_ACCEPTED;
        $age = time() - 172800; // 2 days

        // SQL
        $sql = "DELETE FROM " . TB_Shared_Ent_Core_Invitation::getTable() . " ";
        $sql .= "WHERE state = :state AND UNIX_TIMESTAMP( last_update ) < :age";
        $params = array( ':state' => $state, ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_Invitation::execute( $sql, $params );


        // Params
        $state = TB_Shared_Ent_Core_Invitation::STATE_DELIVERED;
        $age = time() - 1209600; // 14 days

        // SQL
        $sql = "DELETE FROM " . TB_Shared_Ent_Core_Invitation::getTable() . " ";
        $sql .= "WHERE state = :state AND UNIX_TIMESTAMP( last_update ) < :age";
        $params = array( ':state' => $state, ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_Invitation::execute( $sql, $params );


        //
        // Delete outdated password reset mails
        //

        // Params
        $state = TB_Shared_Ent_Core_PasswordReset::STATE_DELIVERED;

        // SQL
        $sql = "DELETE FROM " . TB_Shared_Ent_Core_PasswordReset::getTable() . " ";
        $sql .= "WHERE state = :state";
        $params = array( ':state' => $state );

        // Delete
        TB_Shared_Ent_Core_Invitation::execute( $sql, $params );


        //
        // Delete outdated Validation Mails (expires after 2 days)
        //

        // Params
        $state = TB_Shared_Ent_Core_EmailValidation::STATE_DELIVERED;
        $age = time() - 172800; // 2 days

        // SQL
        $sql = "DELETE FROM " . TB_Shared_Ent_Core_EmailValidation::getTable() . " ";
        $sql .= "WHERE state = :state AND UNIX_TIMESTAMP( last_update ) < :age";
        $params = array( ':state' => $state, ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_Invitation::execute( $sql, $params );


        //
        // Delete outdated email change confirmations (expires after 2 days)
        //

        // Params
        $age = time();

        // SQL
        $sql = "DELETE FROM " . TB_Shared_Ent_Core_EmailChange::getTable() . " ";
        $sql .= "WHERE UNIX_TIMESTAMP( valid_until_dt ) < :age";
        $params = array( ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_EmailChange::execute( $sql, $params );

        //
        // Delete outdated sessions
        //

        // Params
        $age = time() - 60 * 60 * 24 * 90; // 90 days

        // SQL
        $sql  = "DELETE FROM " . TB_Shared_Ent_Core_Session::getTable() . " ";
        $sql .=  "WHERE UNIX_TIMESTAMP( last_update ) < :age";
        $params = array( ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_Session::execute( $sql, $params );


        //
        // Delete outdated news entry
        //

        // Params
        $age = time() - 60 * 60 * 24 * 180; // 180 days

        $sql  = "DELETE FROM " . TB_Shared_Ent_TeamData_News::getTable() . " ";
        $sql .=  "WHERE created < :age";
        $params = array( ':age' => $age );

        // Delete
        TB_Shared_Ent_TeamData_News::execute( $sql, $params );

        //
        // Delete outdated appointments incl. attendee entries
        //

        // Params
        $age = time() - 60 * 60 * 24 * 180; // 180 days

        $sql  = "SELECT id FROM " . TB_Shared_Ent_TeamData_Appointment::getTable() . " ";
        $sql .=  "WHERE starttime < :age";
        $params = array( ':age' => $age );

        $res = TB_Shared_Ent_TeamData_Appointment::findMany( $sql, $params );
        if ( $res && is_array( $res ) ) {
            /** @var TB_Shared_Ent_TeamData_Appointment $teamApp */
            foreach( $res as $teamApp ) {
                $teamApp->delete();
            }
        }


        //
        // Delete outdated device registrations (used for push notifications
        //
        // Params
        $age = time() - 60 * 60 * 24 * 180; // 180 days

        $sql  = "DELETE FROM " . TB_Shared_Ent_Core_DeviceReg::getTable() . " ";
        $sql .=  "WHERE last_update < :age";
        $params = array( ':age' => $age );

        // Delete
        TB_Shared_Ent_Core_DeviceReg::execute( $sql, $params );


    }
}

$cron = new TB_Server_Cron_DailyCleanup();
$cron->execute();