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

/**
 * Class TB_Shared_Ent_Core_PasswordReset
 *
 * @property integer $id
 * @property integer $account_id
 * @property string $new_email
 * @property string $token
 * @property integer $valid_until_dt
 */
class TB_Shared_Ent_Core_EmailChange extends Francis_Db_Row {

    /**
     * @param $email
     * @return array
     */
    public static function getByEmail( $email ) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE new_email = :email LIMIT 1";
        return self::findOne( $sql, array( ':email' => $email ) );
    }

    /**
     * @return array
     */
    public static function getByAccountId( $accountId) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE account_id = :account_id LIMIT 1";
        return self::findOne( $sql, array( ':account_id' => $accountId) );
    }


    /**
     * @param string $token
     * @return NULL|TB_Shared_Ent_Core_PasswordReset
     */
    public static function getByToken( $token ) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE token = :token";
        return self::findOne( $sql, array( ':token' => $token ) );
    }

    /**
     * Delete by account id
     *
     * @param $accountId
     * @return bool
     */
    public static function deleteByAccountId( $accountId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE account_id = :account_id';
        return self::execute( $sql, array( ':account_id' => $accountId ) );
    }

    /**
     * Return table name
     *
     * @return null|string
     */
    public static function getTable() {
        return 'email_change';
    }

    /**
     * Return db handler
     *
     * @return Francis_Db_MySql|null
     */
    public static function getDbh() {
        return TB_Shared_Db_Core::get();
    }

}