Kaynağa Gözat

replace the getStructures script by a typo3 http route

Olivier Massot 4 yıl önce
ebeveyn
işleme
55b189245c

+ 48 - 26
ot_core/Resources/Public/getStructures.php → ot_core/Classes/Http/ApiController.php

@@ -1,26 +1,49 @@
 <?php
 
-// Get all of the Opentalent structures from the directly Opentalent DB (API Platform is too slow with so many records),
-// compress the response, and send it to the client
+namespace Opentalent\OtCore\Http;
 
-if ($_SERVER['HTTP_HOST'] == 'typo3' | $_SERVER['HTTP_HOST'] == 'local.sub.opentalent.fr') {
-    $db_host = 'db';
-} elseif ($_SERVER['HTTP_HOST'] == 'preprod.opentalent.fr') {
-    $db_host = 'localhost';
-} else {
-    $db_host = 'prod-back';
-}
+use PDO;
+use TYPO3\CMS\Core\Http\HtmlResponse;
+use TYPO3\CMS\Core\Http\JsonResponse;
+use TYPO3\CMS\Core\Http\Response;
+use TYPO3\CMS\Core\Http\ServerRequest;
+
+/**
+ * Actions for Http API calls
+ *
+ * @package Opentalent\OtCore\Http
+ */
+class ApiController
+{
+    /**
+     * -- Target of the route 'all' --
+     *
+     * Get all of the Opentalent structures directly
+     * from the Opentalent DB (API Platform is too slow with so many records)
+     *
+     * @param ServerRequest $request
+     * @throws \Exception
+     */
+    public function getAllStructures(ServerRequest $request)
+    {
+        if ($_SERVER['HTTP_HOST'] == 'typo3' | $_SERVER['HTTP_HOST'] == 'local.sub.opentalent.fr') {
+            $db_host = 'db';
+        } elseif ($_SERVER['HTTP_HOST'] == 'preprod.opentalent.fr') {
+            $db_host = 'localhost';
+        } else {
+            $db_host = 'prod-back';
+        }
 
-$cnn = new PDO(
-    "mysql:host=" . $db_host . ";dbname=opentalent",
-    'dbcloner',
-    'wWZ4hYcrmHLW2mUK',
-    array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
-);
-$cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        $cnn = new PDO(
+            "mysql:host=" . $db_host . ";dbname=opentalent",
+            'dbcloner',
+            'wWZ4hYcrmHLW2mUK',
+            array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')
+        );
+        $cnn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
-$stmt = $cnn->prepare(
-    "SELECT o.id, o.name, o.logo_id as logoId, o.principalType, p.otherWebsite as website, a.latitude, a.longitude,
+        $stmt = $cnn->prepare(
+            "SELECT o.id, o.name, o.logo_id as logoId, o.principalType, p.otherWebsite as website, a.latitude, a.longitude,
            TRIM(BOTH ' ' FROM CONCAT(a.streetAddress, ' ', a.streetAddressSecond, ' ', a.streetAddressThird)) AS streetAddress,
            a.postalCode, a.addressCity, c.name AS country,
             (SELECT CONCAT('[',GROUP_CONCAT(DISTINCT CONCAT('\"',f.code,'\"')),']')
@@ -43,11 +66,10 @@ $stmt = $cnn->prepare(
         LEFT JOIN (SELECT * FROM NetworkOrganization WHERE parent_id NOT IN (32366, 13) AND (endDate IS NULL OR endDate = '0000-00-00')) n4 on n4.organization_id = n3.parent_id
         LEFT JOIN (SELECT * FROM NetworkOrganization WHERE parent_id NOT IN (32366, 13) AND (endDate IS NULL OR endDate = '0000-00-00')) n5 on n5.organization_id = n4.parent_id
 "
-);
-$stmt->execute();
-$stmt->setFetchMode(PDO::FETCH_ASSOC);
-$data = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
-
-$response = gzdeflate($data, -1, ZLIB_ENCODING_DEFLATE);
-header('Content-Encoding: deflate');
-echo $response;
+        );
+        $stmt->execute();
+        $stmt->setFetchMode(PDO::FETCH_ASSOC);
+        $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
+        return new JsonResponse($data);
+    }
+}

+ 15 - 0
ot_core/Configuration/Backend/Routes.php

@@ -0,0 +1,15 @@
+<?php
+
+use Opentalent\OtCore\Http\ApiController;
+
+// Defines the routes used to trigger the admin actions
+// @see https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/BackendRouting/Index.html
+
+return [
+    // Get a json array containing all of the opentalent structures
+    'all-structures' => [
+        'path' => '/otcore/structures/all',
+        'target' => ApiController::class . '::getAllStructures',
+        'access' => 'public'
+    ]
+];

+ 1 - 4
ot_templating/Resources/Public/assets/Classic/script/structures.js

@@ -1,6 +1,6 @@
 // Specific code for the 'federation-structures' page
 
-const apiGetUrl = "/typo3conf/ext/ot_core/Resources/Public/getStructures.php";
+const apiGetUrl = "/typo3/index.php?route=/otcore/structures/all";
 
 // Converts numeric degrees to radians
 function toRad(Value)
@@ -367,13 +367,11 @@ $(document).ready(function() {
             url: apiGetUrl,
             dataType: "json",
             contentType: "application/json; charset=utf-8",
-            headers : {'Accept-Encoding': 'deflate '},
             timeout : 12000,
         })
         .done(function(res) {
 
             res.forEach(function(item) {
-
                 structure = item;
                 structure.n1Id = parseInt(structure.n1Id);
                 structure.n2Id = parseInt(structure.n2Id);
@@ -384,7 +382,6 @@ $(document).ready(function() {
                 structures.push(structure);
             })
 
-
             populateFederationsSelect();
             refresh();
         })