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


class TB_Server_Control_News {
    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     */
    public static function get( TB_Server_Core_RequestData $params ) {
        $resp = new TB_Server_Core_Response();

        $profile = TB_Server_Core_Session::get()->getProfile();
        $filter = $params->get( 'filter' );
        $news = TB_Shared_Ent_TeamData_News::getForTeamId( $profile->team_id, $filter, 100 );
        $resp->addData( 'news', $news );

        // Add all profiles
        $profiles = TB_Shared_Ent_TeamData_Profile::getProfilesByTeamId( $profile->team_id );
        $resp->addData( 'profiles', $profiles );

        // Update timestamp
        $profileTs = TB_Shared_Ent_TeamData_ProfileTimestamp::getByProfileId( $profile->id );
        $profileTs->last_news_update = time();
        $profileTs->save();

        return $resp;
    }

    /**
     * Called on intervals to get unread news
     *
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     */
    public static function getUnreadCount( TB_Server_Core_RequestData $params ) {
        $resp = new TB_Server_Core_Response();

        $profile = TB_Server_Core_Session::get()->getProfile();
        $profileTs = TB_Shared_Ent_TeamData_ProfileTimestamp::getByProfileId( $profile->id );
        $newsCount = TB_Shared_Ent_TeamData_News::getNewsCount( $profile, $profileTs );
        $resp->addData( 'newsCount', $newsCount );

        return $resp;
    }

    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     * @throws Exception
     */
    public static function create( TB_Server_Core_RequestData $params ) {
        $resp = new TB_Server_Core_Response();

        $profile = TB_Server_Core_Session::get()->getProfile();
        if ( !$profile->isTrainer() && !$profile->isCoTrainer() ) {
            throw new Exception( 'No rights' );
        }

        $content = $params->get( 'content' );
        if ( is_null( $content ) ) {
            throw new Exception( 'No content - no news' );
        }

        $news = new TB_Shared_Ent_TeamData_News();
        $news->team_id = $profile->team_id;
        $news->author_profile_id = $profile->id;
        $news->txt_key = 'txt_news_custom';
        $news->txt_key_params_sr= array( 'custom_content' => nl2br( _xss( $content ) ) );
        $news->ref_content_type = TB_Shared_Ent_TeamData_News::REF_CONTENT_TYPE_CUSTOM;
        $news->created = time();
        $news->save();

        if ( $params->get( 'notify_via_mail' ) ) {
            $mailN = new TB_Shared_Ent_Core_EmailNotification();
            $mailN->sender_profile_id = $profile->id;
            $mailN->receiver_type = TB_Shared_Ent_Core_EmailNotification::RECEIVER_TYPE_TEAM;
            $mailN->receiver_id = $profile->team_id;
            $mailN->subject = 'TBuddy -  Neues vom Coach';
            $mailN->content = $content;
            $mailN->save();
        }

        /** @var TB_Shared_Ent_TeamData_Team $team */
        $team = TB_Shared_Ent_TeamData_Team::get( $profile->team_id );
        Francis_Core_EventHandler::get()->trigger( 'news_custom_created', array(
            'team_name' => isset( $team ) ? $team->display_name : 'Unbekannt',
            'creator_name' => $profile->getName()
        ));

        $resp->addData( 'res', 'ok' );

        return $resp;
    }

    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     * @throws Exception
     */
    public static function createChat( TB_Server_Core_RequestData $params ) {
        $resp = new TB_Server_Core_Response();

        $profile = TB_Server_Core_Session::get()->getProfile();

        $content = $params->get( 'content' );
        if ( is_null( $content ) ) {
            throw new Exception( 'No content - no chat' );
        }

        $news = new TB_Shared_Ent_TeamData_News();
        $news->team_id = $profile->team_id;
        $news->author_profile_id = $profile->id;
        $news->txt_key = 'txt_news_chat';
        $news->txt_key_params_sr= array( 'chat_content' => nl2br( _xss( $content ) ) );
        $news->ref_content_type = TB_Shared_Ent_TeamData_News::REF_CONTENT_TYPE_CHAT;
        $news->created = time();
        $news->save();

        /** @var TB_Shared_Ent_TeamData_Team $team */
        $team = TB_Shared_Ent_TeamData_Team::get( $profile->team_id );
        Francis_Core_EventHandler::get()->trigger( 'chat_created', array(
            'team_name' => isset( $team ) ? $team->display_name : 'Unbekannt',
            'creator_name' => $profile->getName()
        ));

        $resp->addData( 'res', 'ok' );

        return $resp;
    }

    /**
     * @param TB_Server_Core_RequestData $params
     * @return TB_Server_Core_Response
     * @throws Exception
     */
    public static function delete( TB_Server_Core_RequestData $params ) {
        $resp = new TB_Server_Core_Response();

        /** @var TB_Shared_Ent_TeamData_Profile $profile */
        $profile = TB_Server_Core_Session::get()->getProfile();
        /** @var TB_Shared_Ent_TeamData_News $news */
        $news = TB_Shared_Ent_TeamData_News::get( $params->get( 'news_id' ) );

        if ( isset( $news ) &&
            ( $news->ref_content_type == TB_Shared_Ent_TeamData_News::REF_CONTENT_TYPE_CUSTOM || $news->ref_content_type == TB_Shared_Ent_TeamData_News::REF_CONTENT_TYPE_CHAT ) &&
             $news->team_id == $profile->team_id &&
            ( $profile->isTrainer() || $profile->isCoTrainer() ) ) {
            $news->delete();
            $resp->addData( 'res', 'ok' );
        } else {
            throw new Exception( 'No rights' );
        }

        return $resp;
    }
} 