diff --git a/include/class-MailboxQuotaAPI.php b/include/class-MailboxQuotaAPI.php index e04e7f9..3d5634e 100644 --- a/include/class-MailboxQuotaAPI.php +++ b/include/class-MailboxQuotaAPI.php @@ -1,54 +1,89 @@ . // load dependend traits class_exists( 'MailboxAPI' ); /** * Methods for a MailboxQuotaAPI class */ trait MailboxQuotaAPITrait { use MailboxAPITrait; + /** + * Select the MAX Mailbox quota date + * + * @return self + */ + public function selectMaxMailboxQuotaDate() { + return $this->select( 'MAX( mailboxquota_date ) AS max_mailboxquota_date' ) + ->groupBy( 'mailboxquota_date' ); + } + + /** + * Assure that this is only the more updated Mailbox quota + * + * @return self + */ + public function whereMailboxQuotaIsLast() { + + // subquery with a maximum constraint + $max = ( new MailboxQuotaAPI( null, false ) ) + ->fromCustom( DB::instance()->getTable( 'mailboxquota', 'mailboxquota_sub' ) ) + ->equals( 'mailboxquota.mailbox_ID', 'mailboxquota_sub.mailbox_ID' ) + ->selectMaxMailboxQuotaDate() + ->getQuery(); + + return $this->where( sprintf( 'mailboxquota_date = (%s)', $max ) ); + } } /** * MailboxQuota API */ class MailboxQuotaAPI extends Query { use MailboxQuotaAPITrait; /** * Univoque column name to the Mailbox ID */ protected $MAILBOX_ID = 'mailboxquota.mailbox_ID'; /** * Constructor + * + * @param object $db Database + * @param mixed $from Set to false to avoid to use the default FROM */ - public function __construct( $db = null ) { + public function __construct( $db = null, $from = true ) { // set database and class name parent::__construct( $db, MailboxQuota::class ); - // set database table - $this->from( MailboxQuota::T ); + /** + * Set database table (sometime the standard alias it's not useful) + * + * See MailboxQuotaAPI class. + */ + if( $from ) { + $this->from( MailboxQuota::T ); + } } } diff --git a/template/mailbox-stats.php b/template/mailbox-stats.php new file mode 100644 index 0000000..2177183 --- /dev/null +++ b/template/mailbox-stats.php @@ -0,0 +1,57 @@ +. + +/* + * This is the template for some mailbox stats + * + * Called from: + * template/mailbox.php + * + * Available variables: + * $mailbox object + * $plan object + */ + +// avoid to be load directly +defined( 'BOZ_PHP' ) or die; + +// calculate the quota +$quota = ( new MailboxQuotaAPI() ) + ->select( 'mailboxquota_bytes' ) + ->whereMailbox( $mailbox ) + ->whereMailboxQuotaIsLast() + ->queryRow(); +?> + +

+ + + + + + + + +
getMailboxQuotaHumanSize() ?>
+ + + + + + + + diff --git a/template/mailbox.php b/template/mailbox.php index 91c8973..86ab756 100644 --- a/template/mailbox.php +++ b/template/mailbox.php @@ -1,71 +1,81 @@ . /* * This is the template for a mailbox * * Called from: - * mailbox.php + * www/mailbox.php * * Available variables: * $mailbox object * $domain object + * $plan object * $mailbox_password string|null */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; ?> + $mailbox, ] ) ?> + + + + $mailbox, + 'plan' => $plan, + ] ) ?> +