diff --git a/include/class-DomainAPI.php b/include/class-DomainAPI.php index 4a4f2ac..63a7395 100644 --- a/include/class-DomainAPI.php +++ b/include/class-DomainAPI.php @@ -1,106 +1,145 @@ . /** - * Domain API + * Methods related to a Domain class */ -class DomainAPI extends Query { - - const UID = 'name'; - - /** - * Domain ID column name - */ - const DOMAIN_ID = 'domain.domain_ID'; - - public function __construct() { - parent::__construct(); - $this->from( Domain::T ); - $this->defaultClass( 'Domain' ); - } +trait DomainAPITrait { /** * Where the domains are editable by me * * @return self */ public function whereDomainIsEditable() { if( ! has_permission( 'edit-domain-all' ) ) { $this->whereDomainUser(); } return $this; } + /** + * Where the Domain is Active (or not) + * + * @param boolean $active If you want the active, or the inactive + * @return self + */ + public function whereDomainIsActive( $active = true ) { + return $this->wheerInt( 'domain_active', $active ); + } + /** * Limit to a certain user (or yourself) * * @param $user_ID int * @return self */ public function whereDomainUser( $user_ID = false ) { if( $user_ID === false ) { $user_ID = get_user( 'user_ID' ); } $this->joinDomainUser(); return $this->whereInt( 'domain_user.user_ID', $user_ID ); } /** * Limit to a certian domain name * * @param $domain_name string * @return self */ public function whereDomainName( $domain_name ) { return $this->whereStr( 'domain_name', $domain_name ); } /** * Constructor from a domain ID * * @param $domain_ID int * @return self */ public function whereDomainID( $domain_ID ) { return $this->whereInt( static::DOMAIN_ID, $domain_ID ); } /** * Constructor from a Domain object * * @param $domain object * @return self */ public function whereDomain( $domain ) { return $this->whereDomainID( $domain->getDomainID() ); } /** * Join domain and users (once) * * @return self */ public function joinDomainUser() { if( empty( $this->joinedDomainUser ) ) { $this->from( 'domain_user' ); $this->equals( 'domain_user.domain_ID', 'domain.domain_ID' ); $this->joinedDomainUser = true; } return $this; } + + /** + * Join whatever table with the domain table + * + * @return self + */ + public function joinDomain() { + return $this->joinOn( 'INNER', static::DOMAIN_ID, 'domain.domain_ID' ); + } + +} + +/** + * Domain API + */ +class DomainAPI extends Query { + + use DomainAPITrait; + + /** + * @TODO: what is this shit? Is this used? + */ + const UID = 'name'; + + /** + * Domain ID column name + */ + const DOMAIN_ID = 'domain.domain_ID'; + + /** + * Constructor + * + * @param object $db Database (or NULL for the current one) + */ + public function __construct( $db = null ) { + // set database and class name + parent::__construct( $db, 'Domain' ); + + // set database table + $this->from( Domain::T ); + } + }