diff --git a/include/class-Domain.php b/include/class-Domain.php index 18c5062..b3859ab 100644 --- a/include/class-Domain.php +++ b/include/class-Domain.php @@ -1,131 +1,185 @@ . // load Plan trait class_exists( 'Plan' ); /** * Methods for a Domain class */ trait DomainTrait { use PlanTrait; + /** + * Count of the Domain's Mailboxes + * + * This is a kind of cache + * + * @var int + */ + private $domainMailboxCount = null; + /** * Get domain ID * * @return int */ public function getDomainID() { return $this->get( 'domain_ID' ); } /* * Get domain name * * @return string */ public function getDomainName() { return $this->get( 'domain_name' ); } /** * Get the domain edit URl * * @param boolean $absolute True for an absolute URL * @return string */ public function getDomainPermalink( $absolute = false ) { return Domain::permalink( $this->get( 'domain_name' ), $absolute ); } /** * Factory mailbox from this domain * * @return MailboxFullAPI */ public function factoryMailbox() { return ( new MailboxFullAPI() )->whereDomain( $this ); } /** * Factory e-mail forward from this domain * * @return MailforwardFullAPI */ public function factoryMailforwardfrom() { return ( new MailforwardfromAPI() )->whereDomain( $this ); } + /** + * Set a count of Domain's Mailboxes + * + * @param $count int + * @return self + */ + public function setDomainMailboxCount( $count ) { + $this->domainMailboxCount = $count; + } + + /** + * Get the number of Mailboxes of this Domain + * + * This method has a layer of cache. + * + * @return int + */ + public function getDomainMailboxCount() { + + // check if we already know the count + if( !isset( $this->domainMailboxCount ) ) { + + // count the number of mailboxes associated to this Domain + $count = $this->factoryMailbox() + ->select( 'COUNT(*) count' ) + ->queryValue( 'count' ); + + // save in cache + $this->domainMailboxCount = (int) $count; + } + + return $this->domainMailboxCount; + } + + /** + * Check if you can create a new mailbox for this Domain + * + * The Domain must have plan informations. + * + * @return boolean + */ + public function canCreateMailboxInDomain() { + return $this->getPlanMailboxes() > $this->getDomainMailboxCount() || has_permission( 'edit-email-all' ); + } + /** * Factory FTP users from this domain * * @return FTPAPI */ public function factoryFTP() { return ( new FTPAPI() )->whereDomain( $this ); } /** * Normalize a Domain object after being retrieved from database */ protected function normalizeDomain() { $this->integers( 'domain_ID' ); $this->booleans( 'domain_active' ); $this->dates( 'domain_born', 'domain_expiration' ); $this->normalizePlan(); } } /** * Describe the 'domain' table */ class Domain extends Queried { use DomainTrait; /** * Table name */ const T = 'domain'; const UID = 'domain_name'; /** * Constructor */ public function __construct() { $this->normalizeDomain(); } /** * Get the domain permalink * * @param string $domain_name Domain name * @param boolean $absolute True for an absolute URL */ public static function permalink( $domain_name = null, $absolute = false ) { $url = 'domain.php'; if( $domain_name ) { $url .= _ . $domain_name; } return site_page( $url, $absolute ); } } diff --git a/include/class-MailboxAPI.php b/include/class-MailboxAPI.php index 10a4035..bad385a 100644 --- a/include/class-MailboxAPI.php +++ b/include/class-MailboxAPI.php @@ -1,118 +1,118 @@ . // load dependend traits class_exists( 'DomainAPI' ); /** * Methods for a MailboxAPI class */ trait MailboxAPITrait { use DomainAPITrait; /** * Limit to a specific mailbox * * @param object $mailbox Mailbox * @return self */ public function whereMailbox( $mailbox ) { return $this->whereDomain( $mailbox ) - ->whereMaiboxUsername( $mailbox->getMailboxUsername() ); + ->whereMailboxUsername( $mailbox->getMailboxUsername() ); } /** * Filter a specific Mailbox username * * @param string $username Mailbox username (without domain name) * @return self */ public function whereMailboxUsername( $username ) { return $this->whereStr( 'mailbox_username', $username ); } /** * Where the Mailbox is Active (or not) * * @param boolean $active If you want the active, or the inactive * @return self */ public function whereMailboxIsActive( $active = true ) { return $this->whereInt( 'mailbox_active', $active ); } /** * Join mailboxes and domain (once) * * @return self */ public function joinMailboxDomain() { if( empty( $this->joinedMailboxDomain ) ) { $this->from( 'domain' ); $this->equals( 'domain.domain_ID', 'mailbox.domain_ID' ); $this->joinedMailboxDomain = true; } return $this; } /** * Check if I can edit this mailbox * * Actually it just checks if you can edit the whole domain. * * @return boolean */ public function whereMailboxIsEditable() { return $this->whereDomainIsEditable(); } } /** * Mailbox API */ class MailboxAPI extends Query { use MailboxAPITrait; /** * Univoque Domain ID column name * * Used by DomainAPITrait */ const DOMAIN_ID = 'mailbox.domain_ID'; /** * Univoque Plan ID column name */ const PLAN_ID = 'domain.plan_ID'; /** * Constructor */ public function __construct( $db = null ) { // set database and class name parent::__construct( $db, Mailbox::class ); // set database table $this->from( Mailbox::T ); } } diff --git a/template/mailboxes.php b/template/mailboxes.php index 15d8684..1e53bc5 100644 --- a/template/mailboxes.php +++ b/template/mailboxes.php @@ -1,76 +1,79 @@ . /* * This is the template for the mailboxes list * * Called from: * template/domain.php */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // domain mailboxes $mailboxes = $domain->factoryMailbox() ->select( [ 'domain_name', 'mailbox_username', 'mailbox_receive', ] ) ->queryGenerator(); // total number of mailboxes $count = DB::instance()->affectedRows(); + +// cache the value that can be useful +$domain->setDomainMailboxCount( $count ); ?>

null, ] ) ?> valid() ): ?>

getPlanName(), $plan->getPlanMailboxes(), __( "Mailboxes" ) ) ) ?>

-getPlanMailboxes() > $count || has_permission( 'edit-email-all' ) ): ?> +canCreateMailboxInDomain() ): ?>

getDomainName() ), __( "Create" ) ) ?>