]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6775 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 15:44:07 +0000 (16:44 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Mar 2024 15:44:07 +0000 (16:44 +0100)
app/Http/Controllers/Admin/Operations/Tools/WebflowOperation.php
app/Models/ToolWebflow.php
app/Services/Webflow.php

index 1942b039e0b00196e0dbf85d57e29031a7f75e58..dce574f41cd090ae94c5a35fc00c846aed30b929 100644 (file)
@@ -23,6 +23,15 @@ trait WebflowOperation
             $wf->save();
             return redirect(backpack_url('tool-webflow/' . $id . '/edit'));
         });
+
+        Route::match(['get'], 'webflow/{id}/data', function ($id) {
+            if (!can('webflow:write')) {
+                abort(401);
+            }
+            $wf = ToolWebflow::withoutGlobalScopes()->find($id);
+            Webflow::setToken($wf->webflow_api_token);
+            return response()->json(Webflow::getEditableData($wf->webflow));
+        });
     }
 
 }
index c7e4f4e9ef698a3b2591fb8c91dfd08948e95d1e..60160d4734f412b1ac6fe408f0cc45db51c61e54 100644 (file)
@@ -60,8 +60,7 @@ class ToolWebflow extends ToolboxModel
 
     protected function _parsePages()
     {
-        Webflow::setToken($this->webflow_api_token);
-        dd(Webflow::getAllData($this->webflow));
+
     }
 
     public function getLocales()
@@ -198,6 +197,8 @@ class ToolWebflow extends ToolboxModel
             $locales = json_decode($locales, true);
         }
         Cache::put('webflow_' . $this->id . '_locales', $locales);
+        Webflow::setToken($this->webflow_api_token);
+        Webflow::getAllData($this->webflow);
         return parent::onRetrieved();
     }
 
index b73d2e82530894c53b4957f5b0d04435594ec14a..148434ab92dbe2a9bb6e7d6513ba096ddc00f019 100644 (file)
@@ -3,9 +3,12 @@
 namespace App\Services;
 
 
+use App\Services\Webflow\Excel;
+use hollodotme\FastCGI\Requests\GetRequest;
 use Illuminate\Http\Client\Response;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Http;
+use function Symfony\Component\String\b;
 
 class Webflow
 {
@@ -40,6 +43,34 @@ class Webflow
         return $response->json()['access_token'];
     }
 
+    public static function paginatedRequest($url, $key, $data = [], $method = 'get', $ttl = 86400, $force = false)
+    {
+        $res = [];
+
+        $page = 0;
+        $limit = 100;
+        while (true) {
+            $reqData = $data;
+            if ($page > 0) {
+                $reqData['offset'] = $page * $limit;
+            }
+
+            $response = self::request($url, $reqData, $method, $ttl, $force);
+            if (!isset($response['pagination'])) {
+                break;
+            }
+            $pagination = $response['pagination'];
+            $limit = $pagination['limit'];
+            $res = array_merge($res, $response[$key]);
+            if ($pagination['total'] <= $pagination['limit'] * ($page + 1)) {
+                break;
+            }
+
+            $page++;
+        }
+        return self::_arrayKey($res);
+    }
+
     public static function request($url, $data = [], $method = 'get', $ttl = 86400, $force = false)
     {
         $cacheKey = 'webflow_' . static::getToken() . '_' . $method . '_' . $url . '_' . sha1(print_r($data, true));
@@ -119,7 +150,7 @@ class Webflow
 
     public static function getAllData($shortname)
     {
-        return ['pages' => self::getAllPagesContents($shortname), 'cms' => self::getAllCMSContents($shortname)];
+        return ['pages' => self::getAllPagesContents($shortname), 'cms' => self::getAllCMSContents($shortname), 'assets' => self::listAssets($shortname)];
     }
 
     public static function getCMSCollectionDetails($collectionID)
@@ -129,21 +160,84 @@ class Webflow
 
     public static function getCMSCollectionContents($collectionID)
     {
-        return self::request('collections/' . $collectionID . '/items')['items'];
+        return self::paginatedRequest('collections/' . $collectionID . '/items', 'items');
+    }
+
+    protected static function _arrayKey($array)
+    {
+        $res = [];
+        foreach ($array as $item) {
+            $res[$item['id']] = $item;
+        }
+        return $res;
     }
 
     public static function listCMSCollections($shortname)
     {
-        return self::request('sites/' . self::getSiteId($shortname) . '/collections')['collections'];
+        return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/collections', 'collections');
     }
 
     public static function getPageContents($pageID)
     {
-        return self::request('pages/' . $pageID . '/dom');
+        return self::paginatedRequest('pages/' . $pageID . '/dom', 'nodes');
     }
 
     public static function listPages($shortname)
     {
-        return self::request('sites/' . self::getSiteId($shortname) . '/pages')['pages'];
+        return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/pages', 'pages');
+    }
+
+    public static function listAssets($shortname)
+    {
+        return self::paginatedRequest('sites/' . self::getSiteId($shortname) . '/assets', 'assets');
+    }
+
+    public static function getEditableData($shortname)
+    {
+        $data = self::getAllData($shortname);
+
+        $res = ['texts' => [], 'images' => [], 'seo' => []];
+        foreach ($data['pages'] as $pageID => $page) {
+            $details = $page['details'];
+            if ($details['archived']) {
+                continue;
+            }
+            $seo = [
+                'slug' => $details['slug'],
+                'draft' => $details['draft'],
+                'seo_title' => $details['seo']['title'],
+                'seo_description' => $details['seo']['description'],
+                'og_title' => $details['openGraph']['title'],
+                'og_description' => $details['openGraph']['description'],
+                'og_title_copied' => $details['openGraph']['titleCopied'],
+                'og_description_copied' => $details['openGraph']['descriptionCopied'],
+            ];
+
+            $res['seo'][$pageID] = $seo;
+
+            $texts = [];
+            foreach ($page['contents'] as $node) {
+                if ($node['type'] === 'text') {
+                    if (!$node['text']['text']) {
+                        continue;
+                    }
+                    if (!isset($res['texts'][$node['text']['text']])) {
+                        $res['texts'][$node['text']['text']] = [];
+                    }
+                    $res['texts'][$node['text']['text']][] = ['node' => $node['id'], 'page' => $pageID];
+                } else if ($node['type'] === 'image') {
+                    if(!isset($node['image']['assetId'])){
+                        continue;
+                    }
+                    $assetId = $node['image']['assetId'];
+                    if (isset($res['images'][$assetId])) {
+                        continue;
+                    }
+                    $res['images'][$assetId] = ['alt' => $node['image']['alt'], 'url' => $data['assets'][$assetId]['hostedUrl']];
+                }
+            }
+
+        }
+        return $res;
     }
 }