Procházet zdrojové kódy

structures - replace api call by direct call to db

Olivier Massot před 4 roky
rodič
revize
107e00ae23

+ 49 - 26
ot_core/Resources/Public/getStructures.php

@@ -1,30 +1,53 @@
 <?php
 
-// Get all of the Opentalent structures from the Opentalent API,
-// gzip the response, and send it to the client
-
-$default_uri = "https://api.opentalent.fr";
-$variants_uris = [
-    "preprod.opentalent.fr" => "https://api.preprod.opentalent.fr",
-    "local.sub.opentalent.fr" => "http://docker.nginx.opentalent.fr",
-    "typo3" => "http://docker.nginx.opentalent.fr"
-];
-
-$base_uri = $variants_uris[$_SERVER['HTTP_HOST']] ?? $default_uri;
-$apiGetUrl = $base_uri . '/api/public/federation_structures?_format=json&page=1&itemsPerPage=99999';
-
-$ch = curl_init($apiGetUrl);
-curl_setopt($ch, CURLOPT_HEADER, 0);
-curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-$response = curl_exec($ch);
-
-$errors = curl_error($ch);
-$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-curl_close($ch);
-
-$response = gzdeflate($response, 9, ZLIB_ENCODING_DEFLATE);
-
+// 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
+
+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);
+
+$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,'\"')),']')
+            FROM Activity AS a
+            LEFT JOIN activity_categories AS ac ON(ac.activity_id = a.id)
+            LEFT JOIN Categories AS cs ON (cs.id = ac.categories_id)
+            LEFT JOIN Familly AS f ON(f.id = cs.familly_id)
+            WHERE a.organization_id = o.id) AS categories,
+           n1.parent_id as n1Id, net1.name as n1Name, n2.parent_id as n2Id, n3.parent_id as n3Id, n4.parent_id as n4Id, n5.parent_id as n5Id,
+           CONCAT_WS(',', n1.parent_id, n2.parent_id, n3.parent_id, n4.parent_id, n5.parent_id) as parents
+    FROM opentalent.Organization o
+        INNER JOIN opentalent.Parameters p on o.parameters_id = p.id
+        INNER JOIN (SELECT * FROM opentalent.OrganizationAddressPostal WHERE `type`='ADDRESS_HEAD_OFFICE') oa on oa.organization_id = o.id
+        INNER JOIN opentalent.AddressPostal a on oa.addressPostal_id = a.id
+        LEFT JOIN opentalent.Country c ON (c.id = a.addressCountry_id)
+        INNER JOIN (SELECT * FROM NetworkOrganization WHERE parent_id NOT IN (32366, 13) AND (endDate IS NULL OR endDate = '0000-00-00')) n1 on n1.organization_id = o.id
+        INNER JOIN Organization net1 ON net1.id = n1.parent_id
+        LEFT JOIN (SELECT * FROM NetworkOrganization WHERE parent_id NOT IN (32366, 13) AND (endDate IS NULL OR endDate = '0000-00-00')) n2 on n2.organization_id = n1.parent_id
+        LEFT JOIN (SELECT * FROM NetworkOrganization WHERE parent_id NOT IN (32366, 13) AND (endDate IS NULL OR endDate = '0000-00-00')) n3 on n3.organization_id = n2.parent_id
+        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;

+ 17 - 5
ot_templating/Resources/Public/assets/Classic/script/structures.js

@@ -205,8 +205,7 @@ $(document).ready(function() {
 
         structures.forEach(function (structure) {
             if (structure.n1Id === organizationId && structure.principalType.endsWith('FEDERATION')) {
-                let id = structure['@id'].split('/').pop()
-                let option = '<option value="' + id + '">' + structure.name + '</option>';
+                let option = '<option value="' + structure.id + '">' + structure.name + '</option>';
                 federationSelect.append(option);
                 has_options = true;
             }
@@ -358,7 +357,7 @@ $(document).ready(function() {
     }
 
     // #### Load structures data and refresh
-    var structures = [];
+    var structures;
 
     function loadStructures() {
         structures = []
@@ -369,10 +368,23 @@ $(document).ready(function() {
             dataType: "json",
             contentType: "application/json; charset=utf-8",
             headers : {'Accept-Encoding': 'deflate '},
-            timeout : 5000,
+            timeout : 12000,
         })
         .done(function(res) {
-            structures = res["hydra:member"];
+
+            res.forEach(function(item) {
+
+                structure = item;
+                structure.n1Id = parseInt(structure.n1Id);
+                structure.n2Id = parseInt(structure.n2Id);
+                structure.n3Id = parseInt(structure.n3Id);
+                structure.n4Id = parseInt(structure.n4Id);
+                structure.n5Id = parseInt(structure.n5Id);
+                structure.categories = structure.categories || [];
+                structures.push(structure);
+            })
+
+
             populateFederationsSelect();
             refresh();
         })