151 lines
4.3 KiB
PHP
151 lines
4.3 KiB
PHP
<?php
|
||
class Discussion{
|
||
|
||
protected $ID;
|
||
protected $name;
|
||
protected $creatorID;
|
||
protected $dateCreation;
|
||
protected $accessibility;
|
||
|
||
protected $valid;
|
||
|
||
public function __construct($ID){
|
||
$this->ID = $ID;
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE ID=?');
|
||
$req->execute(array($ID));
|
||
if($rep = $req->fetch()){
|
||
$this->valid = TRUE;
|
||
$this->name = $rep['name'];
|
||
$this->creatorID = $rep['creatorID'];
|
||
$this->dateCreation = $rep['dateCreation'];
|
||
$this->accessibility = $rep['accessibility'];
|
||
}else{
|
||
$this->valid = FALSE;
|
||
}
|
||
}
|
||
public function exists(){
|
||
return $this->valid;
|
||
}
|
||
public function __toString(){
|
||
$out = 'Discussion\n';
|
||
$out .= '\tID:' . $this->ID . '\n';
|
||
$out .= '\tName:' . $this->name . '\n';
|
||
$out .= '\tCreator\'s ID:' . $this->creatorID . '\n';
|
||
$out .= '\tDate of creation:' . $this->dateCreation . '\n';
|
||
$out .= '\tAccessibility:' . $this->accessibility . '\n';
|
||
return out;
|
||
}
|
||
|
||
//Getters
|
||
public function getID(){
|
||
return $this->ID;
|
||
}
|
||
|
||
public function getName(){
|
||
return $this->name;
|
||
}
|
||
|
||
public function getCreatorID(){
|
||
return $creatorID;
|
||
}
|
||
|
||
public function getDateCreation(){
|
||
return $this->dateCreation;
|
||
}
|
||
|
||
public function getAccessibility(){
|
||
return $this->accessibility;
|
||
}
|
||
|
||
|
||
//Useful Getters
|
||
public function canAccess($membre){
|
||
$out = $this->accessibility === 'p';
|
||
if(preg_match ( "#^a[0-9]+$#", $this->accessibility ) == 1)
|
||
$out = $out or intval ( substr ( $this->accessibility, 1 ) ) <= $membre->getAdminLevel();
|
||
$out = $out or preg_match ( '#^x([0-9]+;)*' . $membre->getID() . '(;[0-9]+)*$#', $disc ['autorized'] ) == 1;
|
||
$out = $out or $membre->getAdminLevel() >= 14;
|
||
return $out;
|
||
}
|
||
|
||
//Setters
|
||
const NAME_ALREADY_USED = 'Nom déjà utilisé';
|
||
const ILLEGAL_NAME = 'Le nom de la discussion est incorrect : les caractères autorisés sont :'.
|
||
'<br/> abcdefghijklmnopqrstuvwxyz<wbr/>ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<wbr/>'.
|
||
'éèàμ_-\'()[\\]\\\\/<wbr/>,;:.§!ù%£$¤=+-*\\#~"|ç@';
|
||
const TOO_LONG_NAME = 'Le nom est trop long : maximum 256 caractères';
|
||
public function rename($newName){
|
||
if(!exists())
|
||
return NULL;
|
||
if(getFromName($newName))
|
||
return NAME_ALREADY_USED;
|
||
$regex = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.
|
||
'<27><><EFBFBD><EFBFBD>_\\-\'()\\[\\]\\\\/,;:.<2E>!<21>%<25>$<24>=+\\-*\\#~"|<7C>@';
|
||
$regex = '#^['.$regex.']+$#';
|
||
if(preg_match($regex,$newName) != 1)
|
||
return ILLEGAL_NAME;
|
||
}
|
||
|
||
//Discussions getters
|
||
private static function discussionGetterOutput($req){
|
||
$out = array();
|
||
while($rep = $req->fetch())
|
||
$out[] = new Discussion($rep['ID']);
|
||
if($out)
|
||
return $out;
|
||
else
|
||
return NULL;
|
||
}
|
||
|
||
public static function getFromName($name){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE name=?');
|
||
$req->execute(array($name));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getFromCreator($ID){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE creatorID=?');
|
||
$req->execute(array($ID));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getFromDateCreation($date){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation=?');
|
||
$req->execute(array($date));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getFromAccessibility($accessibility){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE creatorID=?');
|
||
$req->execute(array($accessibility));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getCreatedLaterThan($date){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation>?');
|
||
$req->execute(array($date));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getCreatedEarlierThan($date){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation<?');
|
||
$req->execute(array($date));
|
||
return discussionGetterOutput($req);
|
||
}
|
||
|
||
public static function getWhichHeCanAccess($he){
|
||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions');
|
||
$req->execute(array());
|
||
$out = array();
|
||
while ( $rep = $req->fetch()) {
|
||
$disc = new Discussion($rep['ID']);
|
||
if ($disc->canAccess($membre))
|
||
$out[] = $disc;
|
||
}
|
||
if($out)
|
||
return $out;
|
||
else
|
||
return NULL;
|
||
}
|
||
|
||
} |