From aa34db127e6c9263c52e3cea44c58effa355ddc6 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 18 May 2018 09:21:13 -0400 Subject: [PATCH] Add WireLog option for specifying which user should be recorded with the log entry (if different from current user). --- wire/core/Wire.php | 5 +++-- wire/core/WireLog.php | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/wire/core/Wire.php b/wire/core/Wire.php index b27896d1..7a57a9cc 100644 --- a/wire/core/Wire.php +++ b/wire/core/Wire.php @@ -1476,8 +1476,9 @@ abstract class Wire implements WireTranslatable, WireFuelable, WireTrackable { * * @param string $str Text to log, or omit to return the `$log` API variable. * @param array $options Optional extras to include: - * - `url` (string): URL to record the with the log entry (default=auto-detect) - * - `name` (string): Name of log to use (default=auto-detect) + * - `url` (string): URL to record the with the log entry (default=auto-detect) + * - `name` (string): Name of log to use (default=auto-detect) + * - `user` (User|string|null): User instance, user name, or null to log for current User. (default=null) * @return WireLog * */ diff --git a/wire/core/WireLog.php b/wire/core/WireLog.php index f7ac6909..1378f5e7 100644 --- a/wire/core/WireLog.php +++ b/wire/core/WireLog.php @@ -95,6 +95,7 @@ class WireLog extends Wire { * @param array $options Options to modify default behavior: * - `showUser` (bool): Include the username in the log entry? (default=true) * - `showURL` (bool): Include the current URL in the log entry? (default=true) + * - `user` (User|string|null): User instance, user name, or null to use current User. (default=null) * - `url` (bool): URL to record with the log entry (default=auto determine) * - `delimiter` (string): Log entry delimiter (default="\t" aka tab) * @return bool Whether it was written or not (generally always going to be true) @@ -106,6 +107,7 @@ class WireLog extends Wire { $defaults = array( 'showUser' => true, 'showURL' => true, + 'user' => null, 'url' => '', // URL to show (default=blank, auto-detect) 'delimiter' => "\t", ); @@ -141,8 +143,15 @@ class WireLog extends Wire { } if($options['showUser']) { - $user = $this->wire('user'); - $text = ($user && $user->id ? $user->name : "?") . "$options[delimiter]$text"; + $user = !empty($options['user']) ? $options['user'] : $this->wire('user'); + $userName = ''; + if($user instanceof Page) { + $userName = $user->id ? $user->name : '?'; + } else if(is_string($user)) { + $userName = $user; + } + if(empty($userName)) $userName = '?'; + $text = "$userName$options[delimiter]$text"; } return $log->save($text);