Duffer Derek

Current Path : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Repository/Phases/
Upload File :
Current File : /var/www/podiomigration.bitkit.dk/httpdocs/src/classes/Repository/Phases/PhasesRepo.php

<?php

namespace App\Repository\Phases;

use App\Repository\Copy\AppCopyRepo;
use App\Repository\Copy\FileCopyRepo;
use App\Repository\Copy\ItemCopyRepo;
use App\Repository\Create\AppCreateRepo;
use App\Repository\Create\ItemCreateRepo;
use App\Repository\Create\TaskCreateRepo;
use App\Repository\Utils\CustomPodioAPI;
use App\Repository\Utils\PodioAuthRepo;
use PDO;
use App\Repository\General\Log;
use App\Repository\General\Async;

class PhasesRepo
{
    private $customPodioAPI;

    public function __construct()
    {
        $this->customPodioAPI = new CustomPodioAPI();
    }

    public function isProcessRunning($user)
    {
        //todo remove this
        $sql = "select `id` from `transfer_process` where `user`=:user and `end_time` IS NULL";
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'user' => $user,
        ));
        $result = $STH->fetchAll(PDO::FETCH_ASSOC);
        if (!$result || count($result) < 1) {
            return false; // no running process
        }
        return true;
    }

    public function startTransferProcess($param)
    {
        Log::log("TransferProcess started ", $param, "info");
        try {
            if (isset($param['user'])) {
                if (!$this->isProcessRunning($param['user'])) {
                    $userID = $param['user']; //$userID = 4560696;

                    $id = $this->saveProcess($userID, null, 1);
                    exit;
                }
            }
        } catch (\Exception $e) {
            Log::logError($e . "", null, "startTransferProcess");
        }
    }


    public function copyProcess($type, $param)
    {
        Log::log("start copyProcess-Phases " . $type, $param);
        echo "<pre>";

        $app = [
            'id' => $param['id'],
            'identifier' => $param['identifier'],
            'src_app_id' => $param['app_id'],
            'app_id' => $param['app_id'],
        ];
        $repo = new PodioAuthRepo();
        $copyRepo = new PhasesCopyRepo();
        $appRepo = new AppCopyRepo();

        switch ($type) {
            case 'save-app':
                if ($repo->authenticate($app['identifier'])) {
                    $copyRepo->saveAppDetails($app['identifier'], $app);
                    //copy-process/save-item?id=1&identifier=1&ws_id=7457684
                //    Async::callAsync("copy-process/save-item?id=1&identifier=1&ws_id=" . $param['ws_id'] . "&app_name=" . $param['app_name']);
                }
                break;

            case'save-item':
                if ($repo->authenticate($app['identifier'])) {
                    $appRepo->saveItemsInApp($app);
                }
                break;
            case 'save-items-comments-files-tasks':
                if ($repo->authenticate($app['identifier'])) {
                    $repo = new ItemCopyRepo();
                    $repo->saveItemsCommentsFilesTasks($app['identifier'], $app['src_app_id']);
                }
                break;
        }
    }


    public function createProcess($type, $param)
    {
        Log::log("start createProcess-Phases " . $type, $param);
        echo "<pre>";

        $repo = new PodioAuthRepo();
        $createRepo = new PhasesCreateRepo();
        if ($repo->authenticate($param['identifier'])) {
            switch ($type) {
                case 'clone-app':
                    $createRepo->cloneApp($param['identifier']);
                    break;

                case "copy-files":
                    if (isset($param['identifier'])) {
                        $repo = new FileCopyRepo();
                        $repo->uploadToNewPodio($param['identifier']);
                    }
                    break;
                case "save-apps":
                    if (isset($param['identifier'])) {
                        $repo = new AppCreateRepo();
                        $repo->saveApps($param['identifier']);
                    }
                    break;
                case "create-items":
                    if (isset($param['identifier'])) {
                        $repo = new AppCreateRepo();
                        $repo->createItems($param['identifier']);
                    }
                    break;

                case "update-items":
                    if (isset($param['identifier'])) {
                        $repo = new ItemCreateRepo();
                        $repo->updateItems($param['identifier']);
                    }
                    break;

                case "create-item-comments-tasks":
                    if (isset($param['identifier'])) {
                        $repo = new ItemCreateRepo();
                        $repo->createItemComments($param['identifier']);
                    }
                    break;

                // /create-async/create-task-comments-files?identifier=1
                case "create-task-comments-files":
                    if (isset($param['identifier'])) {
                        $repo = new TaskCreateRepo();
                        $repo->createTaskCommentsNAttachFiles($param['identifier']);
                    }
                    break;

                case "create-task-completed-comment":
                    if (isset($param['identifier'])) {
                        $repo = new TaskCreateRepo();
                        $repo->taskCompleteComment($param['identifier']);
                    }
                    break;
            }
        }
    }

    private function saveProcess($user, $spaces, $key)
    {

        $sql = "INSERT INTO  `transfer_process` (`user`,`start_time`,`spaces`,`key`) VALUES(:user,:start_time,:spaces,:key )";
        $STH = DB::prepare($sql);
        $STH->execute(array(
            'user' => $user,
            "start_time" => time(),
            "spaces" => $spaces,
            'key' => $key
        ));
        return 1;

    }

}

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