<?php
namespace Apps\Immo\Helper;

use Apps\Immo\Entity\ImmoProject;
use Apps\Immo\Entity\ImmoProjectUser;

class ProjectHelper{
    
    static function AddUers($core, $projectId, $usersId){

        foreach(explode(",", $usersId) as $userId ){

            $userProject = new ImmoProjectUser($core);

            if(count($userProject->Find("ProjectId=" .$projectId . " and UserId=" . $userId )) == 0){
                $userProject = new ImmoProjectUser($core);
                $userProject->ProjectId->Value = $projectId;
                $userProject->UserId->Value = $userId;
                $userProject->Save();
            }
        }
    } 
    
    /****
     * Obtient les proejt de l'utilisateur
     */
    static function GetProjetByUser($core, $userId){
        
        $userProject = new ImmoProjectUser($core);
        
        $projet = new ImmoProject($core);
        $projet->Join("ImmoProjectUser", "ImmoProjectUser", "INNER", "ImmoProjectUser.ProjectId = ImmoProject.Id");
        
        $projets = $projet->Find("ImmoProjectUser.UserId=".$userId);
        
        return $projets;
    }
    
}
