diff --git a/lib/composer/autoload_classmap.php b/lib/composer/autoload_classmap.php index a9dd5e4e4..fe3b211c3 100644 --- a/lib/composer/autoload_classmap.php +++ b/lib/composer/autoload_classmap.php @@ -294,6 +294,7 @@ return array( 'Combodo\\iTop\\Composer\\iTopComposer' => $baseDir . '/sources/Composer/iTopComposer.php', 'Combodo\\iTop\\Controller\\AjaxRenderController' => $baseDir . '/sources/Controller/AjaxRenderController.php', 'Combodo\\iTop\\Controller\\Base\\Layout\\ActivityPanelController' => $baseDir . '/sources/Controller/Base/Layout/ActivityPanelController.php', + 'Combodo\\iTop\\Controller\\PreferencesController' => $baseDir . '/sources/Controller/PreferencesController.php', 'Combodo\\iTop\\DesignDocument' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => $baseDir . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\Form\\Field\\BlobField' => $baseDir . '/sources/Form/Field/BlobField.php', diff --git a/lib/composer/autoload_static.php b/lib/composer/autoload_static.php index 444e095c0..9fdd49196 100644 --- a/lib/composer/autoload_static.php +++ b/lib/composer/autoload_static.php @@ -524,6 +524,7 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b 'Combodo\\iTop\\Composer\\iTopComposer' => __DIR__ . '/../..' . '/sources/Composer/iTopComposer.php', 'Combodo\\iTop\\Controller\\AjaxRenderController' => __DIR__ . '/../..' . '/sources/Controller/AjaxRenderController.php', 'Combodo\\iTop\\Controller\\Base\\Layout\\ActivityPanelController' => __DIR__ . '/../..' . '/sources/Controller/Base/Layout/ActivityPanelController.php', + 'Combodo\\iTop\\Controller\\PreferencesController' => __DIR__ . '/../..' . '/sources/Controller/PreferencesController.php', 'Combodo\\iTop\\DesignDocument' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\DesignElement' => __DIR__ . '/../..' . '/core/designdocument.class.inc.php', 'Combodo\\iTop\\Form\\Field\\BlobField' => __DIR__ . '/../..' . '/sources/Form/Field/BlobField.php', diff --git a/pages/ajax.render.php b/pages/ajax.render.php index 1cf1f2be1..659c425bd 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -6,6 +6,7 @@ use Combodo\iTop\Controller\AjaxRenderController; use Combodo\iTop\Controller\Base\Layout\ActivityPanelController; +use Combodo\iTop\Controller\PreferencesController; use Combodo\iTop\Renderer\Console\ConsoleBlockRenderer; use Combodo\iTop\Renderer\Console\ConsoleFormRenderer; @@ -2736,6 +2737,24 @@ EOF $oPage->add(json_encode($aResult)); break; + //-------------------------------- + // Preferences + //-------------------------------- + case 'preferences_set_user_picture': + $oPage = new JsonPage(); + try { + $aResult = PreferencesController::SetUserPicture(); + $aResult['success'] = true; + } + catch (Exception $oException) { + $aResult = [ + 'success' => false, + 'error_message' => $oException->getMessage(), + ]; + } + $oPage->SetData($aResult); + break; + //-------------------------------- // Activity panel //-------------------------------- diff --git a/pages/preferences.php b/pages/preferences.php index fe662dd58..b5807727d 100644 --- a/pages/preferences.php +++ b/pages/preferences.php @@ -402,9 +402,28 @@ JS $oP->add_ready_script( << + * @since 3.0.0 + * @package Combodo\iTop\Controller + */ +class PreferencesController +{ + /** + * @return string[] + * @throws \CoreException + * @throws \CoreUnexpectedValue + * @throws \MySQLException + * @throws \Exception + */ + public static function SetUserPicture(): array + { + $sImageFilename = utils::ReadPostedParam('image_filename', null, utils::ENUM_SANITIZATION_FILTER_RAW_DATA); + + // Set preference for the user + appUserPreferences::SetPref('user_picture_placeholder', $sImageFilename); + + $sUserPicturesFolder = 'images/user-pictures/'; + $sImageAbsPath = APPROOT.$sUserPicturesFolder.$sImageFilename; + $sImageAbsUrl = utils::GetAbsoluteUrlAppRoot().$sUserPicturesFolder.$sImageFilename; + + // Check file can be read + $sImageData = file_get_contents($sImageAbsPath); + if (false === $sImageData) { + throw new Exception('Error while updating user image, could not open file "'.$sImageAbsPath.'"'); + } + + // Check if user has a contact with an image attribute, so we put the image in it also + $sPersonClass = 'Person'; + if (true === MetaModel::HasImageAttributeCode($sPersonClass)) { + $oCurContact = UserRights::GetContactObject(); + if (null !== $oCurContact) { + // Update contact + $sImageMimeType = mime_content_type($sImageAbsPath); + $oOrmImage = new ormDocument($sImageData, $sImageMimeType, $sImageFilename); + + $sImageAttCode = MetaModel::GetImageAttributeCode($sPersonClass); + $oCurContact->Set($sImageAttCode, $oOrmImage); + $oCurContact->DBUpdate(); + } + } + + return ['image_url' => $sImageAbsUrl]; + } +} diff --git a/templates/base/layouts/navigation-menu/layout.html.twig b/templates/base/layouts/navigation-menu/layout.html.twig index 7f7e96991..cdf237c2d 100644 --- a/templates/base/layouts/navigation-menu/layout.html.twig +++ b/templates/base/layouts/navigation-menu/layout.html.twig @@ -42,11 +42,12 @@ {% endif %} -
-