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

/**
 * Class TB_Shared_Ent_Core_PasswordReset
 *
 * @property integer $id
 * @property integer $sender_profile_id
 * @property string $receiver_type
 * @property integer $receiver_id
 * @property string $subject
 * @property string $content
 * @property timestamp $last_update
 */
class TB_Shared_Ent_Core_EmailNotification extends Francis_Db_Row {

    /**
     * Get all entries
     * @return array [ TB_Shared_Ent_Core_EmailNotification, ... ]
     */
    public static function getAll() {
        $sql = "SELECT * FROM " . self::getTable();
        return self::findMany( $sql );
    }

    /**
     * Get all entries of given type
     * @param string $type
     * @return array [ TB_Shared_Ent_Core_EmailNotification, ... ]
     */
    public static function getAllByType( $type ) {
        $sql = "SELECT * FROM " . self::getTable() . " WHERE receiver_type = :type";
        return self::findMany( $sql, array( ':type' => $type ) );
    }

    /**
     * Delete by profile id
     *
     * @param $profileId
     * @return bool
     */
    public static function deleteByProfileId( $profileId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE receiver_type = "' . self::RECEIVER_TYPE_PROFILE . '" AND receiver_id = :receiver_id';
        return self::execute( $sql, array( ':receiver_id' => $profileId ) );
    }

    /**
     * Delete by team id
     *
     * @param int $teamId
     * @return bool
     */
    public static function deleteByTeamId( $teamId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE receiver_type = "' . self::RECEIVER_TYPE_TEAM . '" AND receiver_id = :receiver_id';
        return self::execute( $sql, array( ':receiver_id' => $teamId ) );
    }

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

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

    const RECEIVER_TYPE_PROFILE = 'profile';

}