diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 5c9b42023800bcc90605f4ac64fb5d23427280f2..48c20283e169b4a3333125726fa65e980aa25cf4 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -224,6 +224,8 @@ class APIController extends GoGoController public function getManifestAction(Request $request, DocumentManager $dm) { $config = $dm->get('Configuration')->findConfiguration(); + if (!$config) return new Response(json_encode(['error' => "No configuration found"])); + $img = $config->getFavicon() ? $config->getFavicon() : $config->getLogo(); $imageData = null; diff --git a/src/Controller/DirectoryController.php b/src/Controller/DirectoryController.php index 0a669b4c8122d6859ef0e3fa58206a502e4d6a28..104174ee34b35aefa34931078ece3beee0f2c932 100755 --- a/src/Controller/DirectoryController.php +++ b/src/Controller/DirectoryController.php @@ -18,9 +18,9 @@ class DirectoryController extends GoGoController public function appShell(Request $request, DocumentManager $dm) { $config = $dm->get('Configuration')->findConfiguration(); - + $params = ['gogoConfigUrl' => $this->generateUrl('gogo_api_gogocartojs_configuration')]; - if( $config->getHideHeaderInPwa() ) $params['hideHeader'] = true; + $params['hideHeader'] = $config->getHideHeaderInPwa() ?? false; return $this->render('directory/directory.html.twig', $params); } diff --git a/src/Services/ConfigurationService.php b/src/Services/ConfigurationService.php index 081abb5ea5b5b044579cc1030c3d1da9a4d26c83..d05a6cf360b99296abf4050a1df1084949f69152 100755 --- a/src/Services/ConfigurationService.php +++ b/src/Services/ConfigurationService.php @@ -18,7 +18,6 @@ class ConfigurationService { $this->dm = $dm; $this->securityContext = $securityContext; - $this->config = $this->dm->get('Configuration')->findConfiguration(); } public function isUserAllowed($featureName, $request = null) @@ -37,19 +36,21 @@ class ConfigurationService public function getConfig() { - return $this->config; + return $this->dm->get('Configuration')->findConfiguration(); } public function getFeatureConfig($featureName) { + if (!$this->getConfig()) return null; + switch ($featureName) { - case 'report': $feature = $this->config->getReportFeature(); break; - case 'add': $feature = $this->config->getAddFeature(); break; - case 'edit': $feature = $this->config->getEditFeature(); break; - case 'directModeration': $feature = $this->config->getDirectModerationFeature(); break; - case 'delete': $feature = $this->config->getDeleteFeature(); break; - case 'vote': $feature = $this->config->getCollaborativeModerationFeature(); break; - case 'pending': $feature = $this->config->getPendingFeature(); break; + case 'report': $feature = $this->getConfig()->getReportFeature(); break; + case 'add': $feature = $this->getConfig()->getAddFeature(); break; + case 'edit': $feature = $this->getConfig()->getEditFeature(); break; + case 'directModeration': $feature = $this->getConfig()->getDirectModerationFeature(); break; + case 'delete': $feature = $this->getConfig()->getDeleteFeature(); break; + case 'vote': $feature = $this->getConfig()->getCollaborativeModerationFeature(); break; + case 'pending': $feature = $this->getConfig()->getPendingFeature(); break; } return $feature; diff --git a/src/Services/ElementSynchronizationService.php b/src/Services/ElementSynchronizationService.php index 141617cce89f1000cd8e63970956c9f2f531b7b1..22eb39a65a074dae5ff2ea0e8d646cc836b5f6e5 100644 --- a/src/Services/ElementSynchronizationService.php +++ b/src/Services/ElementSynchronizationService.php @@ -14,11 +14,18 @@ use App\Document\GoGoLogLevel; class ElementSynchronizationService { + protected $config; + public function __construct(DocumentManager $dm, UrlService $urlService) { $this->dm = $dm; $this->urlService = $urlService; - $this->config = $this->dm->get('Configuration')->findConfiguration(); + } + + public function getConfig() + { + if (!$this->config) $this->config = $this->dm->get('Configuration')->findConfiguration(); + return $this->config; } /* @@ -32,12 +39,12 @@ class ElementSynchronizationService $promise = new Promise(function () use (&$promise, &$contribution, &$preparedData) { try { // Init OSM API handler - $configOsm = $this->config->getOsm(); + $configOsm = $this->getConfig()->getOsm(); $osm = new Services_OpenStreetMap([ 'server' => $this->getOsmServer(), 'user' => $configOsm->getOsmUsername(), 'password' => $configOsm->getOsmPassword(), - 'User-Agent' => $this->config->getAppName(), + 'User-Agent' => $this->getConfig()->getAppName(), 'verbose' => true ]); @@ -123,7 +130,7 @@ class ElementSynchronizationService $changeset->setTag('host', $url); $changeset->setTag('gogocarto:user', $contribution->getUserDisplayName()); $changeset->setTag('created_by:library', 'GoGoCarto'); - $changeset->setTag('created_by', $this->config->getAppName()); + $changeset->setTag('created_by', $this->getConfig()->getAppName()); $changeset->begin($this->getOsmComment($preparedData)); // Add edited feature to changeset @@ -219,7 +226,7 @@ class ElementSynchronizationService * Get OSM server URL, cleaned */ private function getOsmServer() { - $url = $this->config->getOsm()->getOsmHost(); + $url = $this->getConfig()->getOsm()->getOsmHost(); if(isset($url)) { if(!str_starts_with($url, "http://") && !str_starts_with($url, "https://")) { $url = "https://" + $url; diff --git a/src/Services/GoGoCartoJsService.php b/src/Services/GoGoCartoJsService.php index a1e5ea1d1b2b93b0dbb8036d57a3df8df3915332..68034b24e7f4b9fa9935f8de3e212dcbc4ef03fa 100644 --- a/src/Services/GoGoCartoJsService.php +++ b/src/Services/GoGoCartoJsService.php @@ -29,7 +29,8 @@ class GoGoCartoJsService $taxonomyJson = $taxonomyRep->findTaxonomyJson(); $config = $this->dm->get('Configuration')->findConfiguration(); - + if (!$config) return []; + $user = $this->securityContext->getToken() ? $this->securityContext->getToken()->getUser() : null; $roles = is_object($user) ? $user->getRoles() : []; diff --git a/src/Services/WebhookService.php b/src/Services/WebhookService.php index 8cb39d166262c657f58187d0a7b307f7b04ba57c..922008402b47bf6809ad5d3d64efe178ff243ca7 100755 --- a/src/Services/WebhookService.php +++ b/src/Services/WebhookService.php @@ -21,7 +21,7 @@ use Psr\Log\LoggerInterface; class WebhookService { protected $dm; - + protected $config; protected $router; public function __construct(DocumentManager $dm, RouterInterface $router, @@ -34,11 +34,16 @@ class WebhookService $this->router = $router; $this->urlService = $urlService; $this->securityContext = $securityContext; - $this->config = $this->dm->get('Configuration')->findConfiguration(); $this->synchService = $synchService; $this->logger = $commandsLogger; } + public function getConfig() + { + if (!$this->config) $this->config = $this->dm->get('Configuration')->findConfiguration(); + return $this->config; + } + public function processPosts($limit = 5) { $contributions = $this->dm->createQueryBuilder(UserInteractionContribution::class) @@ -139,7 +144,7 @@ class WebhookService private function getNotificationText($result) { - $element = $this->config->getElementDisplayName(); + $element = $this->getConfig()->getElementDisplayName(); switch ($result['action']) { case 'add': return "**AJOUT** {$element} **{$result['data']['name']}** ajouté par {$result['user']}\n[Lien vers la fiche]({$result['link']})"; @@ -157,7 +162,7 @@ class WebhookService private function getBatchNotificationText($result) { - $elements = $this->config->getElementDisplayNamePlural(); + $elements = $this->getConfig()->getElementDisplayNamePlural(); $title = $this->transTitle[$result['action']]; $text = $this->transText[$result['action']]; $count = count($result['data']['ids']); @@ -168,7 +173,7 @@ class WebhookService private function getBotIcon() { /** @var ConfImage $img */ - $img = $this->config->getFavicon() ? $this->config->getFavicon() : $this->config->getLogo(); + $img = $this->getConfig()->getFavicon() ? $this->getConfig()->getFavicon() : $this->getConfig()->getLogo(); return $img ? $img->getImageUrl() : $this->urlService->getAssetUrl('/img/default-icon.png'); } @@ -181,7 +186,7 @@ class WebhookService case WebhookFormat::Mattermost: return [ - 'username' => $this->config->getAppName(), + 'username' => $this->getConfig()->getAppName(), 'icon_url' => $this->getBotIcon(), 'text' => $data['text'], ];