<?php


class Permission {

    const USER_LEVEL_1 = "Admin";
    const USER_LEVEL_2 = "Technicien";
    const USER_LEVEL_3 = "Estimator";
    const USER_LEVEL_4 = "Gestionnaire Promutuel";

    const ACCUIEL = 'accuiel';
    const CLIENTS = 'clients';
    const RECLAMATION = 'reclamation';
    const CALENDER = 'calender';
    const CALENDER_HOUR = 'calender.hour';
    const CALENDER_ESTIMATION_REPAIR = 'calender.est.repair';
    const ESTIMATION = 'estimation';
    const FACTURE = 'facture';
    const REPORT = 'report';
    const REPORT_RECLAMATIONS = 'report.reclamation';
    const REPORT_ESTIMATION = 'report.estimation';
    const REPORT_ESTIMATION_RENDEZ = 'report.est.rendez';
    const REPORT_FACTURES = 'report.factures';
    const REPORT_PIECES = 'report.pieces';
    const USERS = 'users';

    public static $permissions = [
        1 => [
            self::ACCUIEL, self::CLIENTS, self::RECLAMATION, self::CALENDER, self::CALENDER_HOUR,
            self::CALENDER_ESTIMATION_REPAIR, self::ESTIMATION, self::FACTURE, self::REPORT,
            self::REPORT_ESTIMATION, self::REPORT_ESTIMATION_RENDEZ, self::REPORT_FACTURES,
            self::REPORT_PIECES, self::USERS
        ],
        2 => [
            self::ACCUIEL, self::REPORT, self::REPORT_ESTIMATION, self::REPORT_FACTURES
        ],
        3 => [
            self::ACCUIEL, self::CLIENTS, self::RECLAMATION, self::CALENDER, self::ESTIMATION, self::REPORT, self::REPORT_ESTIMATION, self::REPORT_ESTIMATION_RENDEZ, self::REPORT_FACTURES,
            self::REPORT_PIECES
        ],
        4 => [
            self::ACCUIEL, self::CLIENTS, self::RECLAMATION, self::CALENDER, self::ESTIMATION, self::REPORT, self::REPORT_ESTIMATION, self::REPORT_ESTIMATION_RENDEZ, self::REPORT_FACTURES,
            self::REPORT_PIECES, self::CALENDER_ESTIMATION_REPAIR
        ]
    ];

    public static function capable( $perm )
    {
        $currentUserId = !empty($_SESSION['jigowatt']['user_id']) ? $_SESSION['jigowatt']['user_id'] : null;
        if ( !$currentUserId ) {
            return false;
        }
        $user = DB::getInstance()->table('login_users')->where('user_id', $currentUserId)->get()->first();
        $user_level = unserialize( $user->user_level );

        $loginLevel = DB::getInstance()->table('login_levels')->where('id', $user_level[0])->get()->first();

        if ( $success = in_array( $perm, self::$permissions[$loginLevel->level] ) ) {
            return true;
        }

        protect('ME');
    }
}

if ( ! function_exists( 'can' ) ) {
    function can( $perm ) {
        return Permission::capable( $perm );
    }
}
