diff --git a/include/class-APILog.php b/include/class-APILog.php index fab0e3d..c99a562 100644 --- a/include/class-APILog.php +++ b/include/class-APILog.php @@ -1,150 +1,158 @@ . /** * Class to interact with the `log` database table */ class APILog { /** * @param array $args Arguments */ public static function insert( $args ) { // data to be saved $data = []; // no family no party if( !isset( $args['family'] ) ) { throw new Exception( "missing family" ); } // no action no party if( !isset( $args['action'] ) ) { throw new Exception( "missing action" ); } // set the default Actor ID if( isset( $args['actor'] ) ) { $data['actor_ID'] = User::getID( $args['actor'] ); } else { // otherwise please take the currently logged-in user as default $data['actor_ID'] = get_user()->getUserID(); } // you cannot change the timestamp $data['log_timestamp'] = date( 'Y-m-d H:i:s' ); // set the family $data['log_family'] = $args['family']; // set the action name $data['log_action'] = $args['action']; // eventually set the Domain ID if( isset( $args['domain'] ) ) { $data['domain_ID'] = Domain::getID( $args['domain'] ); } // eventually set the Mailbox ID if( isset( $args['mailbox'] ) ) { $data['mailbox_ID'] = Mailbox::getID( $args['mailbox'] ); } // eventually set the Mailforwardfrom ID if( isset( $args['mailforwardfrom'] ) ) { $data['mailforwardfrom_ID'] = Mailforwardfrom::getID( $args['mailforwardfrom'] ); } // eventually set the Plan ID if( isset( $args['plan'] ) ) { $data['plan_ID'] = Plan::getID( $args['plan'] ); } // eventually set the marionette ID (the touched User's ID) if( isset( $args['marionette'] ) ) { $data['marionette_ID'] = User::getID( $args['marionette'] ); } // finally insert the row ( new QueryLog() ) ->insertRow( $data ); } /** * Help in querying stuff * * @param array $args Arguments */ public static function query( $args ) { // expected arguments defaults to NULL $actor = $args['actor'] ?? null; $marionette = $args['marionette'] ?? null; $mailbox = $args['mailbox'] ?? null; $domain = $args['domain'] ?? null; + $mailforwardfrom + = $args['mailforwardfrom'] ?? null; // create a fresh query builder $query = new QueryLog(); // select the most important columns $query->select( [ 'log_timestamp', 'log_family', 'log_action', ] ); // eventually filter by Actor (the user who was doing the action) if( $actor ) { $query->whereLogActor( $actor ); } // eventually filter by Marionette (the user who was receiving an edit) if( $marionette ) { $query->whereLogMarionette( $marionette ); } // eventually filter by Mailbox if( $mailbox ) { $query->whereMailbox( $mailbox ); } // eventually filter by Domain if( $domain ) { $query->whereDomain( $domain ); } + // eventually filter by Mailforward From + if( $mailforwardfrom ) { + $query->whereMailforwardfrom( $mailforwardfrom ); + } + // eventually skip to join something $query->joinLogMessageTables( [ 'actor' => is_object( $actor ), 'marionette' => is_object( $marionette ), 'mailbox' => is_object( $mailbox ), 'domain' => is_object( $domain ), + 'mailforwardfrom' => is_object( $mailforwardfrom ), ] ); // as default sort descending the timeline $query->orderByLogTimestamp( 'DESC' ); // allow to change the limit $query->limit( $args['limit'] ?? 15 ); return $query; } } diff --git a/include/class-Log.php b/include/class-Log.php index 61d1d3c..f9b8b60 100644 --- a/include/class-Log.php +++ b/include/class-Log.php @@ -1,360 +1,366 @@ . // make sure that this class is loaded at startup class_exists( User::class ); class_exists( Domain::class ); class_exists( Mailbox::class ); class_exists( Mailforwardfrom::class ); trait LogTrait { /** * Get the log actor name * * @return string */ public function getLogActorFirm() { return User::firm( $this->get( 'actor_uid' ) ); } /** * Get the action family * * @return string */ public function getLogFamily() { return $this->get( 'log_family' ); } /** * Get the action */ public function getLogAction() { return $this->get( 'log_action' ); } /** * Get the action */ public function getLogDate() { return $this->get( 'log_timestamp' ); } /** * Get the UID of the User Marionette (user passively involved) * * @return string */ public function getLogMarionetteUID() { return $this->get( 'marionette_uid' ); } /** * Get the log message * * @param array $args Arguments * @return self */ public function getLogMessage( $args ) { $family = $this->getLogFamily(); $action = $this->getLogAction(); // trigger the right message family switch( $family ) { case 'domain': return self::domainMessage( $action, $this, $args ); case 'mailbox': return self::mailboxMessage( $action, $this, $args ); case 'mailforward': return self::mailforwardMessage( $action, $this, $args ); case 'user': return self::userMessage( $action, $this, $args ); } return self::unknownAction( $family, $action ); } /** * Get the log message alongside the date and the actor name * * @param array $args Arguments * @return self */ public function getLogMessageWithDateAndUser( $args ) { $actor = $args['actor'] ?? $this; // create the Actor firm from the passed User object or from the Log $actor_firm = $actor instanceof User ? $actor->getUserFirm() : $actor->getLogActorFirm(); return sprintf( "%s - %s %s", $this->getLogDate()->format( __( "Y-m-d H:i" ) ), $actor_firm, $this->getLogMessage( $args ) ); } protected function normalizeLog() { $this->datetimes( 'log_timestamp' ); } } /** * A generic log of an action * * Something happened. Dunno what. */ class Log extends Queried { use LogTrait; use UserTrait; use DomainTrait; use MailboxTrait; use MailforwardfromTrait; public function __construct() { $this->normalizeLog(); } /** * Database table name */ const T = 'log'; /** * Generate a Domain-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function domainMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'actor' User object * A complete 'domain' Domain object */ $domain = $args['domain'] ?? $log; $plan = $args['plan'] ?? $log; switch( $action ) { // an administrator created the Domain case 'create': return sprintf( __( "created the domain %s" ), $domain->getDomainFirm() ); // an administrator has changed the Plan for a Domain case 'plan.change': return sprintf( __( "changed the Plan for %s to %s" ), $domain->getDomainFirm(), esc_html( $plan->getPlanName() ) ); } // default dummy message return self::unknownAction( 'domain', $action ); } /** * Generate a Mailbox-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function mailboxMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'actor' User object * A complete 'domain' Domain object * A complete 'mailbox' Mailbox object */ $actor = $args['actor'] ?? $log; $domain = $args['domain'] ?? $log; $mailbox = $args['mailbox'] ?? $log; $mailbox_firm = Mailbox::firm( $domain->getDomainName(), $mailbox->getMailboxUsername() ); // trigger the right action message switch( $action ) { // the mailbox was created case 'create': return sprintf( __( "created the mailbox %s" ), $mailbox_firm ); // the description was changed case 'description.change': return sprintf( __( "edited description of %s" ), $mailbox_firm ); case 'newpassword': return sprintf( __( "reset password of %s" ), $mailbox_firm ); } // default dummy message return self::unknownAction( 'mailbox', $action ); } /** * Generate a Mailforward-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function mailforwardMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * - * A complete 'domain' Domain object - * A complete 'mailbox' Mailbox object + * A complete 'domain' Domain object + * A complete 'mailforwardfrom' Mailforwardfrom object */ - $domain = $args['domain'] ?? $log; - $mailforward = $args['mailforward'] ?? $log; - - if( $mailforward ) { - $firm = Mailforwardfrom::firm( - $domain->getDomainName(), - $mailforward->getMailforwardfromUsername() - ); - } else { - $firm = $domain->getDomainFirm(); + $domain = $args['domain'] ?? $log; + $mailforwardfrom = $args['mailforwardfrom'] ?? $log; + + $domain_name = '?'; + if( $domain ) { + $domain_name = $domain->getDomainName(); } + $mailforwardfrom_username = '?'; + if( $mailforwardfrom ) { + $mailforwardfrom_username = $mailforwardfrom->getMailforwardfromUsername(); + } + + $firm = Mailforwardfrom::firm( + $domain_name, + $mailforwardfrom_username + ); + // trigger the right action message switch( $action ) { // a Mailforward destination was added case 'add.destination': return sprintf( __( "added a destination for %s" ), $firm ); // a Mailforward destination was removed case 'remove.destination': return sprintf( __( "removed a destination for %s" ), $firm ); // a Mailforward mailbox was created case 'create': return sprintf( __( "created %s" ), $firm ); case 'delete': return sprintf( __( "deleted an %s mail forwarding" ), $firm ); } // default dummy message return self::unknownAction( 'mailforward', $action ); } /** * Generate a User-related message * * @param string $action The related action name * @param object $log * @param array $args Arguments * @return string Message */ public static function userMessage( $action, $log, $args ) { /** * You can pass some objects to build the message: * * A complete 'marionette' User object */ $marionette = $args['marionette'] ?? null; $marionette_uid = '?'; if( $args['marionette'] ) { $marionette_uid = $marionette->getUserUID(); } else { $marionette_uid = $log->getLogMarionetteUID(); } $firm = '?'; if( $marionette_uid ) { $firm = User::firm( $marionette_uid ); } // trigger the right action message switch( $action ) { // an User creation case 'create': return sprintf( __( "gave birth to %s" ), $firm ); // a password reset case 'password.reset': return sprintf( __( "reset password for %s" ), $firm ); } // default dummy message return self::unknownAction( 'user', $action ); } private static function unknownAction( $family, $action ) { return esc_html( sprintf( __( "misterious action about %s (%s)" ), $family, $action ) ); } } diff --git a/include/class-MailforwardfromQuery.php b/include/class-MailforwardfromQuery.php index b9db34e..72d7e05 100644 --- a/include/class-MailforwardfromQuery.php +++ b/include/class-MailforwardfromQuery.php @@ -1,71 +1,76 @@ . // load DomainAPITrait; class_exists( 'DomainAPI', true ); trait MailforwardfromQueryTrait { /** * Filter to a specific mailforward username * * @param $username string * @return self */ public function whereMailforwardfromUsername( $username ) { return $this->whereStr( 'mailforwardfrom_username', $username ); } public function whereMailforwardfromID( $id ) { return $this->whereInt( $this->MAILFORWARDFROM_ID, $id ); } public function whereMailforwardfrom( $mailforwardfrom ) { return $this->whereMailforwardfromID( $mailforwardfrom->getMailforwardfromID() ); } } /** * Execute query againsts a Mailforwardfrom */ class MailforwardfromQuery extends Query { use MailforwardfromQueryTrait; use DomainAPITrait; /** * Univoque Domain ID column name */ const DOMAIN_ID = 'mailforwardfrom.domain_ID'; + /** + * Column name of the Plan ID + */ + protected $PLAN_ID = 'domain.plan_ID'; + /** * Column name of the Mailforwardfrom ID */ protected $MAILFORWARDFROM_ID = 'mailforwardfrom.mailforwardfrom_ID'; /** * Construct */ public function __construct() { parent::__construct(); $this->from( 'mailforwardfrom' ); $this->defaultClass( 'Mailforwardfrom' ); } } diff --git a/template/mailforward.php b/template/mailforward.php index 864e911..4b7967f 100644 --- a/template/mailforward.php +++ b/template/mailforward.php @@ -1,73 +1,87 @@ . /* * This is the template for a single e-mail forwarding * * Called from: * mailforward.php * * Available variables: * $domain Domain object * $mailforwardfrom Mailforward object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; ?>
= __( "Forward the incoming e-mails to these destinations:" ) ?>
$domain, 'mailforwardfrom' => $mailforwardfrom, ] ) ?> + +