<?php
class Projet{
	
	protected $ID;
	protected $name;
	protected $dateCreation;
	protected $owner;
	protected $secondaryOwners;
	
	protected $valid;
	
	public function __construct($ID){
		$this->ID = $ID;
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets WHERE ID=?');
		$req->execute(array($ID));
		if($rep = $req->fetch()){
			$this->valid = TRUE;
			$this->name = $rep['name'];
			$this->owner = new Membre($rep['ownerID']);
			$this->dateCreation = $rep['dateCreation'];
			$this->secondaryOwners = array();
			foreach (explode(';',$rep['secondaryOwners']) AS $secondaryOwner)
				$this->secondaryOwners[] = new Membre($secondaryOwner);
		}else{
			$this->valid = FALSE;
		}
	}
	
	public function exists(){
		return $this->valid;
	}
	
	public function __toString(){
		$out = 'Projet\n';
		$out .= '\tID:' . $this->ID . '\n';
		$out .= '\tName:' . $this->name . '\n';
		$out .= '\tOwner:{' . substr(str_replace('\n\t',';',$this->owner->__toString()),0,-1) . '}\n';
		foreach($this->secondaryOwners AS $secondaryOwner)
			$out .= '\tSecondary owner:{' . substr(str_replace('\n\t',';',$secondaryOwner->__toString()),0,-1) . '}\n';
		$out .= '\tDate of creation:' . $this->dateCreation . '\n';
		return out;
	}
	
	//Getters
	public function getID(){
		return $this->ID;
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function getOwner(){
		return $this->owner;
	}
	
	public function getSecondaryOwners(){
		return $this->secondaryOwners;
	}

	public function getDateCreation(){
		return $this->dateCreation;
	}
	

	//Useful getters
	public function haveRights($member){
		return $member->getID() == $owner->getID() or in_array($member,$this->secondaryOwners);
	}
	
	//Setters
	public function setSecondaryOwners($secondaryOwners){
		$this->secondaryOwners = $secondaryOwners;
		$secondaryOwnersID = array();
		foreach($secondaryOwners AS $secondaryOwner)$secondaryOwnersID[] = $secondaryOwner->getID();
		$req = $GLOBALS['bdd']->prepare('UPDATE projets SET secondaryOwners=? WHERE ID=?');
		$req->execute(array(implode(';',$secondaryOwnersID),$this->ID));
	}

	public function addSecondaryOwner($secondaryOwner){
		$this->secondaryOwners[] = $secondaryOwner;
		$this->setSecondaryOwners($this->secondaryOwners);
	}
	
	public function delSecondaryOwner($secondaryOwner){
		$this->secondaryOwners = array_diff($this->secondaryOwners,array($secondaryOwner));
		$this->setSecondaryOwners($this->secondaryOwners);
	}
	
	
	//Project getters
	private static function projectGetterOutput($req){
		$out = array();
		while($rep = $req->fetch())
			$out[] = new Project($rep['ID']);
			switch(count($out)){
				case 0:
					return NULL;
				case 1:
					return $out[0];
				default:
					return $out;
			}
	}
	
	public static function getFromName($name){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project WHERE name=?');
		$req->execute(array($name));
		return projectGetterOutput($req);
	}
	
	public static function getFromOwner($owner){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project WHERE owner=?');
		$req->execute(array($owner->getID()));
		return projectGetterOutput($req);
	}
	
	public static function getFromSecondaryOwner($secondaryOwner){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project');
		$req->execute();
		$out = array();
		while ($rep = $req->fetch()) {
			$projet = new Projet($rep['ID']);
			if ($projet->haveRights($secondaryOwner))
				$out[] = $projet;
		}
		switch(count($out)){
			case 0:
				return NULL;
			case 1:
				return $out[0];
			default:
				return $out;
		}
	}
	
	public static function getFromDateCreation($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project WHERE dateCreation=?');
		$req->execute(array($date));
		return projectGetterOutput($req);
	}
	
	public static function getCreatedLaterThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project WHERE dateCreation>?');
		$req->execute(array($date));
		return projectGetterOutput($req);
	}
	
	public static function getCreatedEarlierThan($date){
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project WHERE dateCreation<?');
		$req->execute(array($date));
		return projectGetterOutput($req);
	}
	
	public static function getWhichHeCanAccess($he){
		//TODO Faire la selection directement grâce à une requette SQL
		$req = $GLOBALS['bdd']->prepare('SELECT * FROM project');
		$req->execute(array());
		$out = array();
		while ( $rep = $req->fetch()) {
			$projet = new Projet($rep['ID']);
			if ($projet->haveRights($membre))
				$out[] = $projet;
		}
		switch(count($out)){
			case 0:
				return NULL;
			case 1:
				return $out[0];
			default:
				return $out;
		}
	}
	
	
	
}