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

/**
 * Class TB_Shared_Ent_TeamData_Attendee
 *
 * @property integer $id
 * @property integer|null $team_id
 * @property integer|null $author_profile_id
 * @property string $txt_key
 * @property string|null $txt_key_params_sr
 * @property string $ref_content_type
 * @property integer|null $ref_content_type_id
 * @property integer $created
 */
class TB_Shared_Ent_TeamData_News extends Francis_Db_Row {

    const REF_CONTENT_TYPE_APPOINTMENT = 'appointment';
    const REF_CONTENT_TYPE_TEAM = 'team';
    const REF_CONTENT_TYPE_PROFILE = 'profile';
    const REF_CONTENT_TYPE_ATTENDEE = 'attendee';
    const REF_CONTENT_TYPE_CUSTOM = 'custom';
    const REF_CONTENT_TYPE_CHAT = 'chat';
    const REF_CONTENT_TYPE_TBUDDY_NEWS = 'tbuddy_news';

    /**
     * Valid ref content type
     * @return array string[]
     */
    public static function getValidRefContentTypes() {
        return array(
            self::REF_CONTENT_TYPE_APPOINTMENT,
            self::REF_CONTENT_TYPE_PROFILE,
            self::REF_CONTENT_TYPE_TEAM,
            self::REF_CONTENT_TYPE_CUSTOM,
            self::REF_CONTENT_TYPE_ATTENDEE,
            self::REF_CONTENT_TYPE_CHAT,
            self::REF_CONTENT_TYPE_TBUDDY_NEWS
        );
    }

    /**
     * Get attendee status of given appointment
     *
     * @param integer $appointmentId
     * @return array TB_Shared_Ent_TeamData_Attendee[]
     */
    public static function getForTeamId( $teamId, $filter = 'all', $limit = self::DEFAULT_LIMIT, $offset = self::DEFAULT_OFFSET ) {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE ( team_id = :team_id OR ISNULL(team_id) ) ';
        if ( 'custom' == $filter ) {
            $sql .= "AND ( ref_content_type = 'custom' OR ref_content_type = 'chat' ) ";
        }
        $sql .= 'ORDER BY created DESC LIMIT ' . $limit . ' OFFSET ' . $offset;
        return self::findMany( $sql, array( ':team_id' => $teamId ) );
    }

    /**
     * @param $teamId
     * @param $tsLastNewsRead
     */
    public static function getNewsCount( TB_Shared_Ent_TeamData_Profile $profile, TB_Shared_Ent_TeamData_ProfileTimestamp $ts ) {
        $dbh = self::getDbh();
        $sql  = 'SELECT COUNT(*) AS cnt FROM ' . self::getTable() . ' WHERE ( team_id = :team_id OR ISNULL(team_id) ) ';
        $sql .= 'AND created > :tsLastNewsRead AND author_profile_id != :profile_id';
        $stmt = $dbh->prepare( $sql );
        $stmt->execute( array(
            ':team_id' => $profile->team_id,
            ':tsLastNewsRead' => $ts->last_news_update,
            ':profile_id' => $profile->id
        ));
        $res = $stmt->fetch( PDO::FETCH_ASSOC );
        if ( is_array ( $res ) && isset ( $res[ 'cnt' ] ) ) {
            return (int)$res[ 'cnt' ];
        } else {
            return 0;
        }
    }

    /**
     * Delete all profile related news entries
     *
     * @param int $profileId
     * @return bool
     */
    public static function deleteByAuthorProfileId( $profileId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE author_profile_id = :profile_id';
        return self::execute( $sql, array( ':profile_id' => $profileId ) );
    }

    /**
     * Delete all entries for given team id
     * @param int $teamId
     * @return bool
     */
    public static function deleteByTeamId( $teamId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE team_id  = :team_id';
        return self::execute( $sql, array( ':team_id' => $teamId) );
    }

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

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