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

/**
 * Class TB_Shared_Ent_TeamData_Attendee
 *
 * @property integer $id
 * @property integer $appointment_id
 * @property integer $profile_id
 * @property string $status
 * @property string|null $comment
 * @property integer $last_update
 * @property string $last_accepted
 *
 */
class TB_Shared_Ent_TeamData_Attendee extends Francis_Db_Row {

    // Invitation status
    const STATUS_ACCEPTED = 'accepted';
    const STATUS_DECLINED = 'declined';

    /**
     * @return array
     */
    public static function getValidStatus() {
        return array(
            self::STATUS_ACCEPTED,
            self::STATUS_DECLINED
        );
    }

    /**
     * Get attendee status of given appointment
     *
     * @param integer $appointmentId
     * @return array TB_Shared_Ent_TeamData_Attendee[]
     */
    public static function getByAppointmentId( $appointmentId ) {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE appointment_id = :appointment_id ';
        $sql .= 'ORDER BY (last_accepted IS NULL), last_accepted ASC';
        return self::findMany( $sql, array( ':appointment_id' => $appointmentId ) );
    }

    /**
     * Find attendee entry for appointement+profile ids
     *
     * @param integer $appointmentId
     * @param integer $profileId
     */
    public static function getByAppointmentIdAndProfileId( $appointmentId, $profileId ) {
        $sql  = 'SELECT * FROM ' . self::getTable() . ' WHERE appointment_id = :appointment_id ';
        $sql .= 'AND profile_id = :profile_id LIMIT 1';
        return self::findOne( $sql, array( ':appointment_id' => $appointmentId, ':profile_id' => $profileId ) );
    }

    /**
     * Deletes all attendees for given appointment id
     *
     * @param int $appointmentId
     */
    public static function deleteAllForAppointmentId( $appointmentId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE appointment_id = :appointment_id';
        return self::execute( $sql, array( ':appointment_id' => $appointmentId ) );
    }

    /**
     * Deletes all statuses of given profile id
     *
     * @param $profileId
     * @return bool
     */
    public static function deleteAllForProfileId( $profileId ) {
        $sql = 'DELETE FROM ' . self::getTable() . ' WHERE profile_id = :profile_id';
        return self::execute( $sql, array( ':profile_id' => $profileId ) );
    }

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

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