<?php

/**
 * Class TB_Shared_Ent_TeamData_TeamManagerModules
 *
 * @property int $id
 * @property int $team_id
 * @property bool $dashboard
 * @property bool $members
 * @property bool $appointments
 * @property bool $categories
 * @property bool $contracts
 * @property bool $reporting
 * @property bool $payments
 * @property bool $customization
 * @property DateTime $last_updated_dt
 * @property DateTime $created_dt
 */
class TB_Shared_Ent_TeamData_TeamManagerModules extends Francis_Db_Row
{
    const MODULE_DASHBOARD = 'dashboard';
    const MODULE_MEMBERS = 'members';
    const MODULE_APPOINTMENTS = 'appointments';
    const MODULE_CATEGORIES = 'categories';
    const MODULE_CONTRACTS = 'contracts';
    const MODULE_REPORTING = 'reporting';
    const MODULE_PAYMENTS = 'payments';
    const MODULE_CUSTOMIZATION = 'customization';

    /**
     * @param $teamId
     * @param $module
     * @return bool
     */
    public static function hasModuleAccess( $teamId, $module )
    {
        /** @var TB_Shared_Ent_TeamData_TeamManagerModules $mods */
        $mods = self::getByTeamId( $teamId );
        if ( is_null( $mods ) )
        {
            return false;
        }

        return ( 1 == $mods->{ $module } );
    }

    public static function getByTeamId( $teamId )
    {
        $sql = 'SELECT * FROM ' . self::getTable() . ' WHERE team_id = :team_id LIMIT 1';
        $params = array( ':team_id' => $teamId );
        return self::findOne( $sql, $params );
    }

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

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