Duffer Derek

Current Path : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Controllers/
Upload File :
Current File : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Controllers/TransferController.php

<?php

namespace App\Controllers;

use App\Repository\Copy\CopyRepo;

use App\Repository\Create\CreateRepo;
use App\Repository\General\Log;
use App\Repository\Transfer\TransferRepo;
use App\Repository\Utils\TestingRepo;
use App\Repository\Utils\TestRepo;
use Podio;
use Slim\Views\Twig as View;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Interop\Container\ContainerInterface;
use PodioSpace;
use PodioOrganization;
use Exception;
use PodioApp;


class TransferController
{

    public function __construct(ContainerInterface $ci)
    {
        // Config...

    }

    /**
     * @param Request $request
     * @param Response $response
     * @param View $view
     * @return Response
     */
    public function index(Request $request, Response $response, View $view)
    {
        try {
            $helloWorld = "Welcome";
            return $view->render(
                $response,
                'app/index/index.twig',
                array(
                    'helloWorld' => $helloWorld
                )
            );
        } catch (\Exception $e) {
            // Exception handling
        }
    }

    /**
     * /start-transfer
     * @param Request $request
     * @param Response $response
     * @param View $view
     */
    public function transfer(Request $request, Response $response, View $view)
    {
        try {
            $repo = new TransferRepo();
            $repo->startTransfer();
        } catch (\Exception $e) {
            // Exception handling
        }
    }

    public function copyAsync($type, Request $request)
    {
        try {
            Log::log("copyAsyncProcess ", "Hi");
            $param = $request->getQueryParams();
            $repo = new CopyRepo();
            $repo->copyAsyncProcess($type, $param);
        } catch (\Exception $e) {
            Log::log($e);
            // Exception handling
        }
    }

    public function createAsync($type, Request $request)
    {
        try {
            $param = $request->getQueryParams();
            $repo = new CreateRepo();
            $repo->createAsyncProcess($type, $param);
        } catch (\Exception $e) {
            // Exception handling
        }
    }


    public function test($type, Request $request)
    {
        try {
            $param = $request->getQueryParams();
            $repo = new TestRepo();
            $repo->testProcess($type, $param);
        } catch (\Exception $e) {
            // Exception handling
        }
    }

    public function saveFollower($type, Request $request)
    {
        $param = $request->getQueryParams();
        echo $param;
    }

    public function testing($type, Request $request)
    {
        try {
            echo "here";
            $param = $request->getQueryParams();
            $repo = new TestingRepo();
            $repo->testProcess($type, $param);
        } catch (\Exception $e) {
            // Exception handling
            echo "error";
        }
    }

    public function home(Response $response, View $view)
    {
        try {
            $helloWorld = "Welcome";

            if (!isset($_SESSION['saved'])) {
                return $response->withRedirect('/workspace');
            }

            // Optional: unset flag so user can't revisit with refresh
            unset($_SESSION['saved']);

            return $view->render(
                $response,
                'app/index/home.twig',
                array(
                    'helloWorld' => $helloWorld
                )
            );
        } catch (\Exception $e) {
            // Exception handling
        }
    }

    public function workspace(Response $response, View $view)
    {

        // ❗ Prevent browser from caching this page
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");

        try {
            $helloWorld = "Welcome";

            if (isset($_SESSION['podio_refresh_token'])) {

                Podio::setup('podio-data-migration-6lq9pd', 'TG9trKsJ942e3RIjvk4FC3IY5fZ00WiESmruWLjuQSzYtqG5XZziQsxMSYjb1UX3');
                Podio::authenticate('refresh_token', array(
                    'refresh_token' => $_SESSION['podio_refresh_token']
                ));

                if (Podio::is_authenticated()) {
                    $orgs = PodioOrganization::get_all();

                    $orgsWithWorkspaces = [];

                    foreach ($orgs as $org) {
                        $spaces = PodioSpace::get_for_org($org->org_id);
                        $orgsWithWorkspaces[] = [
                            'org_id' => $org->org_id,
                            'org_name' => $org->name,
                            'spaces' => $spaces
                        ];
                    }

                    return $view->render(
                        $response,
                        'app/index/workspace.twig',
                        array(
                            'orgsWithWorkspaces' => $orgsWithWorkspaces
                        )
                    );
                } else {
                    echo 'not authenticated';
                }
            } else {
                //redirect to login
                echo 'NO token';
                return $response->withRedirect('/login'); // No token, redirect to login
            }
        } catch (\Exception $e) {
            // Exception handling
            Log::logError($e . "", null, "workspaceList");
        }
    }

    /**
     * To get the overview of the podio structure
     * including the space along with app ad total item count in that app
     * @param Response $response
     * @param View $view
     * @return Response
     */
    public function spaceOverview(Response $response, View $view)
    {
        // ❗ Prevent browser from caching this page
        header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        try {
            if (isset($_SESSION['podio_refresh_token'])) {
                Podio::authenticate('refresh_token', array(
                    'refresh_token' => $_SESSION['podio_refresh_token']
                ));
                echo '1';
                if (Podio::is_authenticated()) {
                    echo '2';
                    $orgs = PodioOrganization::get_all();
                    $orgsWithWorkspaces = [];

                    foreach ($orgs as $org) {
                        $spaces = PodioSpace::get_for_org($org->org_id);
                        $spacesWithApps = [];

                        foreach ($spaces as $space) {
                            $apps = PodioApp::get_for_space($space->space_id);
                            $appsWithItemCount = [];

                            foreach ($apps as $app) {
                                try {
                                    // Option 1: Use PodioApp::get for metadata, including item_count
                                    $appDetails = PodioApp::get($app->app_id);
                                    $appsWithItemCount[] = [
                                        'app_id' => $app->app_id,
                                        'app_name' => $app->config['name'],
                                        'item_count' => $appDetails->item_count
                                    ];

                                    echo '<pre>';
                                    print_r($orgs);
                                } catch (\Exception $e) {
                                    // App might be inaccessible or deleted
                                    Log::logError("Error fetching app data: " . $e->getMessage(), null, "workspaceAppDetail");
                                }
                            }

                            $spacesWithApps[] = [
                                'space_id' => $space->space_id,
                                'space_name' => $space->name,
                                'apps' => $appsWithItemCount
                            ];
                        }

                        $orgsWithWorkspaces[] = [
                            'org_id' => $org->org_id,
                            'org_name' => $org->name,
                            'spaces' => $spacesWithApps
                        ];
                    }

                    // return $view->render(
                    //     $response,
                    //     'app/index/overview.twig',
                    //     array(
                    //         'orgsWithWorkspaces' => $orgsWithWorkspaces
                    //     )
                    // );
                } else {
                    echo '----';
                    echo 'Not authenticated';
                }
            } else {
                echo 'No token';
                // return $response->withRedirect('/login');
            }
        } catch (\Exception $e) {
            Log::logError("Error in workspace(): " . $e->getMessage(), null, "workspaceList");
        }
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists