diff --git a/include/class-DomainAPI.php b/include/class-DomainAPI.php index d4fcb0a..4b64ea6 100644 --- a/include/class-DomainAPI.php +++ b/include/class-DomainAPI.php @@ -1,160 +1,169 @@ . // load dependent traits class_exists( 'PlanAPI' ); /** * Methods related to a Domain class */ trait DomainAPITrait { use PlanAPITrait; /** * Where the domains are editable by me * * @return self */ public function whereDomainIsEditable() { - if( ! has_permission( 'edit-domain-all' ) ) { + if( !has_permission( 'edit-domain-all' ) ) { $this->whereDomainUser(); } return $this; } + /** + * Where the domains are visible by me + * + * @return self + */ + public function whereDomainIsVisible() { + return $this->whereDomainIsEditable(); + } + /** * 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->whereInt( '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() ); } /** * Order by the Domain name * * @param string $direction DESC|ASC * @return self */ public function orderByDomainName( $direction = null ) { return $this->orderBy( 'domain_name', $direction ); } /** * 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', 'domain', static::DOMAIN_ID, 'domain.domain_ID' ); } } /** * Domain API */ class DomainAPI extends Query { use DomainAPITrait; /** * Univoque Domain ID column name */ const DOMAIN_ID = 'domain.domain_ID'; /** * Univoque Plan ID column name */ protected $PLAN_ID = 'domain.plan_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 ); } } diff --git a/www/domain-plan.php b/www/domain-plan.php index 2baad96..447a9f2 100644 --- a/www/domain-plan.php +++ b/www/domain-plan.php @@ -1,104 +1,105 @@ . /* * This is the single e-mail forwarding edit page */ // load framework require '../load.php'; // wanted informations from the templates $domain = null; $plan = null; // URL paramenters (just domain) list( $domain_name ) = url_parts( 1 ); // eventually retrieve domain from database $domain = ( new DomainAPI() ) ->select( [ 'domain.domain_ID', 'domain_name', 'plan.plan_ID', 'plan_name', 'plan_mailboxes', 'plan_mailforwards', 'plan_databases', 'plan_ftpusers', 'plan_mailboxquota', 'plan_yearlyprice', ] ) ->whereDomainName( $domain_name ) + ->whereDomainIsVisible() ->joinPlan( 'LEFT' ) ->queryRow(); // no domain no party if( !$domain ) { PageNotFound::spawn(); } // save destination action if( is_action( 'domain-plan-save' ) ) { // check privileges require_permission( 'edit-domain-all' ); // check if the submitted request has sense $new_plan_uid = $_POST['plan_uid'] ?? null; if( !$new_plan_uid || !is_string( $new_plan_uid ) ) { BadRequest::spawn(); } // check if the new Plan exists $new_plan = ( new PlanAPI() ) ->wherePlanUID( $new_plan_uid ) ->queryRow(); // no Plan no party if( !$new_plan ) { BadRequest::spawn( __( "Missing Plan" ) ); } // finally update the Plan ( new DomainAPI() ) ->whereDomain( $domain ) ->update( [ 'plan_ID' => $new_plan->getPlanID(), ] ); // POST -> REDIRECT -> GET http_redirect( $domain->getDomainPlanPermalink() ); } // spawn header Header::spawn( [ 'uid' => false, 'title' => __( "Domain Plan" ), 'breadcrumb' => [ new MenuEntry( null, $domain->getDomainPermalink(), $domain->getDomainName() ), ], ] ); // spawn the page content template( 'domain-plan-page', [ 'domain' => $domain, 'plan' => $domain, ] ); // spawn the footer Footer::spawn();