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

/**
 * Class TB_Shared_Ent_Core_DeviceReg
 *
 * @property integer $id
 * @property string $platform
 * @property integer $account_id
 * @property integer $team_id
 * @property string $reg_id
 * @property integer $last_update
 */
class TB_Shared_Ent_Core_DeviceReg extends Francis_Db_Row {

    const PLATFORM_ANDROID = 'ANDROID';
    const PLATFORM_IOS = 'IOS';

    /**
     * @return array
     */
    public static function getPlatforms() {
        return array(
            self::PLATFORM_ANDROID,
            self::PLATFORM_IOS
        );
    }

    /**
     * @param integer $accountId

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


    /**
     * @param integer $accountId
     * @param string $regId
     * @return NULL|TB_Shared_Ent_Core_DeviceReg
     */
    public static function getByAccountIdAndRegId( $accountId, $regId ) {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE account_id = :account_id AND reg_id = :reg_id LIMIT 1';
        return self::findOne( $sql, array( ':account_id' => $accountId, ':reg_id' => $regId) );
    }

    /**
     * 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 'device_reg';
    }

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

}