bcom/clazz/Projet.class.php

252 lines
7.2 KiB
PHP

<?php
class Projet{
protected $ID;
protected $name;
protected $dateCreation;
protected $owner;
protected $secondaryOwners;
protected $publik;
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 (($rep['secondaryOwnersID']!="")?explode(';',$rep['secondaryOwnersID']):[] AS $secondaryOwner)
$this->secondaryOwners[] = new Membre($secondaryOwner);
$this->publik = $rep['public'];
}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';
$out .= '\tPublic:' . $this->publik == "1" . '\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;
}
public function isPublic(){
return $this->publik;
}
//Useful getters
public function haveRights($member){
return $member->getID() == $this->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 secondaryOwnersID=? WHERE ID=?');
$req->execute(array(implode(';',$secondaryOwnersID),$this->getID()));
}
public function setPublicy($publicy){
$this->publik = $publicy;
$req = $GLOBALS['bdd']->prepare('UPDATE projets SET public=? WHERE ID=?');
$req->execute(array($publicy?1:0,$this->getID()));
}
public function setOwner($nOwner){
$this->owner = $nOwner;
$req = $GLOBALS['bdd']->prepare('UPDATE projets SET ownerID=? WHERE ID=?');
$req->execute(array($nOwner->getID(),$this->getID()));
}
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 Projet($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 projets WHERE name=?');
$req->execute(array($name));
return projectGetterOutput($req);
}
public static function getFromOwner($owner){
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets WHERE owner=?');
$req->execute(array($owner->getID()));
return projectGetterOutput($req);
}
public static function getFromSecondaryOwner($secondaryOwner){
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets');
$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 projets WHERE dateCreation>?');
$req->execute(array($date));
return projectGetterOutput($req);
}
public static function getCreatedEarlierThan($date){
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets WHERE dateCreation<?');
$req->execute(array($date));
return projectGetterOutput($req);
}
public static function getWhichHeCanAccess($he){
//TODO Faire la selection directement gràce à une reqete SQL
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets');
$req->execute(array());
$out = array();
while ( $rep = $req->fetch()) {
$projet = new Projet($rep['ID']);
if ($projet->haveRights($he))
$out[] = $projet;
}
switch(count($out)){
case 0:
return NULL;
case 1:
return $out[0];
default:
return $out;
}
}
public static function getOthers($he){
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets');
$req->execute(array());
$out = array();
while ( $rep = $req->fetch()) {
$projet = new Projet($rep['ID']);
if ($he?$he->isAdminLevelGreaterThan(12):FALSE || $projet->isPublic() == '1')
$out[] = $projet;
}
switch(count($out)){
case 0:
return NULL;
case 1:
return $out[0];
default:
return $out;
}
}
public function delete(){
$req = $GLOBALS['bdd']->prepare('DELETE FROM projets WHERE ID=?');
$req->execute(array($this->getID()));
$this->valid = FALSE;
}
public function newVersion($name,$langage,$tags,$insertIndex){
if(Version::getFromProjectAndName($this, $name))
return USED_NAME;
if(!preg_match("#^[a-zA-Z0-9\\-_+ ]+$#", $name ))
return INVALID_NAME;
if(Version::getFromProjectLanguageAndVersionAbs($this,$langage, $insertIndex))
return USED_INDEX;
$req = $GLOBALS['bdd']->prepare('INSERT INTO versions (name,sendDate,publicFiles,tags,projectID,languageID,versionAbs) VALUES (?,NOW(),"",?,?,?,?)');
$req->execute(array($name,$tags,$this->getID(),$langage->getID(),$insertIndex));
return VERSION_CREATED;
}
public function editVersion($versionToEdit,$name,$langage,$tags,$insertIndex){
$namedVersion = Version::getFromProjectAndName($this, $name);
if(($namedVersion)?$namedVersion->getID() != $versionToEdit->getID():FALSE)
return USED_NAME;
if(!preg_match("#^[a-zA-Z0-9\\-_+ ]+$#", $name ))
return INVALID_NAME;
$versionedVersion = Version::getFromProjectLanguageAndVersionAbs($this,$langage, $insertIndex);
if(($versionedVersion)?$versionedVersion->getID() != $versionToEdit->getID():FALSE)
return USED_INDEX;
$req = $GLOBALS['bdd']->prepare('UPDATE versions SET name=?,tags=?,languageID=?,versionAbs=? WHERE ID=?');
$req->execute(array($name,$tags,$langage->getID(),$insertIndex,$versionToEdit->getID()));
return VERSION_EDITED;
}
const INVALID_NAME = "sfygmal";
const USED_NAME = "cflmfyqsdlm";
const USED_INDEX = "jhmvm";
const VERSION_CREATED = "jzbtdbgv";
const VERSION_EDITED = "jzbqgsderftdbgv";
}