diff --git a/com_dpattachments/admin/src/Controller/AttachmentController.php b/com_dpattachments/admin/src/Controller/AttachmentController.php index 88ac096..41afb41 100644 --- a/com_dpattachments/admin/src/Controller/AttachmentController.php +++ b/com_dpattachments/admin/src/Controller/AttachmentController.php @@ -40,9 +40,14 @@ public function upload(): void try { $model->upload($data); + $item = $model->getItem($model->getState($model->getName() . '.id')); + // if no "created_by_alias" is stored in DB, we need to load creator user info (name and email) to avoid showing user id + if( $item->created_by_alias == "" ){ + $item = (object) \array_merge((array)$item, $model->getAuthor($item->created_by)); + } $content = $this->app->bootComponent('dpattachments')->renderLayout( 'attachment.render', - ['attachment' => $model->getItem($model->getState($model->getName() . '.id'))] + ['attachment' => $item] ); $returnData['html'] = '
' . $content . '
'; diff --git a/com_dpattachments/admin/src/Model/AttachmentModel.php b/com_dpattachments/admin/src/Model/AttachmentModel.php index 507710d..8149cc4 100644 --- a/com_dpattachments/admin/src/Model/AttachmentModel.php +++ b/com_dpattachments/admin/src/Model/AttachmentModel.php @@ -17,11 +17,15 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Model\AdminModel; use Joomla\CMS\Plugin\PluginHelper; +use Joomla\CMS\User\UserFactoryAwareInterface; +use Joomla\CMS\User\UserFactoryAwareTrait; use Joomla\Filesystem\File; use Joomla\Utilities\ArrayHelper; -class AttachmentModel extends AdminModel +class AttachmentModel extends AdminModel implements UserFactoryAwareInterface { + use UserFactoryAwareTrait; + protected $text_prefix = 'COM_DPATTACHMENTS'; /** @@ -227,4 +231,15 @@ public function delete(&$pks) return $success; } + + public function getAuthor(int $userId): array { + $userInfo = array(); + $user = $this->getUserFactory()->loadUserById($userId); + // "loadUserById" always returns a "User" object, even if no user found, no need to check for null + $userInfo['author_id'] = $user->id; + $userInfo['author_name'] = $user->name; + $userInfo['author_email'] = $user->email; + + return $userInfo; + } }