diff --git a/include/class-APILog.php b/include/class-APILog.php index c99a562..6018e9e 100644 --- a/include/class-APILog.php +++ b/include/class-APILog.php @@ -1,158 +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' ); + // Allow to customize the timestamp. + $data['log_timestamp'] = $args['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; } }