 <?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_SendEmailValidation extends TB_Server_Core_CronBase {
    /**
     * @throws Exception
     * @throws phpmailerException
     */
    protected function run() {
        $cnt = 0;
        $emvs = TB_Shared_Ent_Core_EmailValidation::getForSending();
        /** @var TB_Shared_Ent_Core_EmailValidation $emv */
        foreach( $emvs as $emv ) {
            try {
                /** @var TB_Shared_Ent_TeamData_Profile $profile */
                $profile = TB_Shared_Ent_TeamData_Profile::getByAccountId( $emv->account_id );
                if ( is_array( $profile ) && count( $profile ) == 1 ) {
                    $profile = $profile[0];
                }
                /** @var TB_Shared_Ent_TeamData_Team $team */
                $team = TB_Shared_Ent_TeamData_Team::get( $profile->team_id );
                if ( isset( $profile ) && isset( $team ) ) {
                    $data = array(
                        'email' => $emv->email,
                        'coach_display_name' => $profile->getName(),
                        'team_display_name' => $team->display_name,
                        'emv_link' => TB_Server_Core_Request::createClientUrl( 'Auth', 'validate_email', array( 't' => $emv->token ) ),
                        'help_link' => Francis_Utils_Config::get( 'url.client' ) . '/dl/TBuddy-NeuesTeamAnlegen.pdf'
                    );
                    TB_Server_Utils_Mail::sendEmailValidation( $data );
                    $emv->state = TB_Shared_Ent_Core_PasswordReset::STATE_DELIVERED;
                    $emv->save();
                }
            } catch( Exception $e ) {
                $emv->delete();
                TB_Server_Utils_Log::get()->log( $e );
            }
        }

    }
}

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