Duffer Derek
<?php
/**
* Created by PhpStorm.
* User: jis
* Date: 31/3/17
* Time: 5:08 PM
*/
namespace App\Repository\Copy;
use App\Repository\General\Async;
use App\Repository\General\Constants;
use App\Repository\General\DB;
use App\Repository\General\Log;
use App\Repository\Utils\CustomPodioAPI;
use App\Repository\Utils\PodioAuthRepo;
use PDO;
use PodioTask;
class TasksCopyRepo
{
private $customPodioAPI;
/**
* TasksRepo constructor.
*/
public function __construct()
{
$this->customPodioAPI = new CustomPodioAPI();
}
public function saveTasksCommentsAndFiles($identifier)
{
try {
$repo = new PodioAuthRepo();
if ($repo->authenticate($identifier)) {
// if ($repo->authenticateSF($identifier)) {
Log::log("saveTaskCommentsAndFiles - $identifier started ", null, "info");
do {
$haveMoreItems = false;
$limit = 100;
$sql = "select id,identifier,src_task_id from tasks where `identifier`=:identifier
and `next_step` ='save_comments_and_files' limit $limit";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
if (count($result) == $limit)
$haveMoreItems = true;
$updateSql = "UPDATE tasks SET next_step='copy_comments_and_files' WHERE id=:id";
$UPDATE_STH = DB::prepare($updateSql);
foreach ($result as $task) {
$this->saveTaskFilesAndComments($task);
$UPDATE_STH->execute(array("id" => $task['id']));
}
}
} while ($haveMoreItems);
Log::log("saveTaskCommentsAndFiles - $identifier ended ", null, "info");
}
} catch (\Exception $e) {
Log::logError($e . "", null, "saveTasksCommentsAndFiles");
}
}
private function saveTaskFilesAndComments($task)
{
try {
$podioTask = $this->customPodioAPI->podioGet("/task/" . $task['src_task_id']);
$this->saveCreatedDetails($podioTask, $task['id']);
if (isset($podioTask['files']) && is_array($podioTask['files']) && count($podioTask['files'])) {
$fileRepo = new FileCopyRepo();
$fileRepo->saveFilesToDB(array(
"identifier" => $task['identifier'],
'src_ref_type' => "task",
'src_ref_id' => $task['src_task_id']
), $podioTask['files']);
}
// comments
$commentRepo = new CommentRepo();
if (isset($podioTask['comments']) && is_array($podioTask['comments']) && count($podioTask['comments'])) {
if (count($podioTask['comments']) < 100) {
$commentRepo->saveComments($task['identifier'], $podioTask['comments']);
} else {
//GET /comment/{type}/{id}/
$limit = 100;
$offset = 0;
do {
$attributes = array('limit' => $limit, 'offset' => $offset);
$iterationCount = 0;
$comments = $this->customPodioAPI->podioGet("/comment/task/" . $task['src_task_id'] . "/", $attributes);
if ($comments && is_array($comments) && count($comments) > 0) {
$iterationCount = count($comments);
$commentRepo->saveComments($task['identifier'], $comments);
}
$offset += $limit;
} while ($iterationCount > 0);
}
}
} catch (\Exception $e) {
Log::logError($e . "", $task, "saveTaskFilesAndComments");
}
}
private function saveCreatedDetails($podioTask, $id)
{
try {
$sql = "UPDATE tasks SET created_on=:created_on,created_by_name=:created_by_name,
created_by_user_id=:created_by_user_id,created_by_url=:created_by_url WHERE id=:id";
$STH = DB::prepare($sql);
$STH->execute(array(
'id' => $id,
"created_by_user_id" => $podioTask['created_by']['user_id'],
"created_by_name" => $podioTask['created_by']['name'],
"created_by_url" => $podioTask['created_by']['url'],
"created_on" => $podioTask['created_on'],
));
} catch (\Exception $e) {
Log::logError($e . "", $podioTask, "saveCreatedDetails");
}
}
public function saveSpaceAppTasks($identifier)
{
try {
Log::log("saveSpaceAppTasks started ", null, "info");
$sql = "select src_space_id,id from `work_spaces` where `task_stream_next_step` is null and `identifier`=:identifier limit 1";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) {
// save stream status messages
exit;
Async::callAsync("copy-async/save-stream-status?identifier=" . $identifier);
} else {
$space = $result[0];
$result = NULL;
$STH = NULL;
$repo = new PodioAuthRepo();
if ($repo->authenticate($identifier)) {
// if ($repo->authenticateSF($identifier)) {
Constants::setREFTYPE("workspace " . $identifier);
Constants::setREFID($space['src_space_id']);
Log::log("saveSpaceAppTasks started $identifier" . $space['src_space_id'], null, "info");
$this->saveTasks(array("ref_type" => "space", "ref_id" => $space['src_space_id'], "identifier" => $identifier));
$sql = "UPDATE work_spaces SET task_stream_next_step=:task_stream_next_step,
task_stream_completed_step=CONCAT(COALESCE(task_stream_completed_step,''),:task_stream_completed_step) WHERE id=:id";
$data = array("task_stream_next_step" => "copy_app_tasks", "id" => $space['id'], "task_stream_completed_step" => "save_space_tasks");
$STH = DB::prepare($sql);
$STH->execute($data);
$this->saveAppTasksInSpace($space['src_space_id'], $identifier);
$sql = "UPDATE work_spaces SET task_stream_next_step=:task_stream_next_step,
task_stream_completed_step=CONCAT(COALESCE(task_stream_completed_step,''),:task_stream_completed_step) WHERE id=:id";
$data = array(
"task_stream_next_step" => "copy_space_stream",
"id" => $space['id'],
"task_stream_completed_step" => ", save_space_app_tasks"
);
$STH = DB::prepare($sql);
$STH->execute($data);
Log::log("saveSpaceAppTasks finished $identifier" . $space['src_space_id'], null, "info");
} else {
Log::log("processFirstSpace authentication issue, identifier: " . $identifier, null, "warning");
}
Async::callAsync("copy-async/save-first-space-app-tasks?identifier=" . $identifier);
}
} catch (\Exception $e) {
Log::logError($e . "", null, "saveSpaceAppTasks");
}
// Async::callAsync("copy-async/process-first-space?identifier=" . $id);
}
private function saveAppTasksInSpace($srcSpace, $identifier)
{
try {
$sql = "select src_app_id from `apps` where `src_space_id` =:src_space_id and `identifier`=:identifier limit 1";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
'src_space_id' => $srcSpace
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if ($result && count($result) > 0) {
foreach ($result as $app) {
$this->saveTasks(array("ref_type" => "app", "ref_id" => $app['src_app_id'], "identifier" => $identifier));
}
}
} catch (\Exception $e) {
Log::logError($e . "", array($srcSpace, $identifier), "saveAppTasksInSpace");
}
}
public function saveTasks($data)
{
try {
print_r('saveTasks');
// GET /task/
$limit = 100;
$offset = 0;
$attributes = array("reference" => $data['ref_type'] . ":" . $data['ref_id'], 'limit' => $limit, 'offset' => $offset);
$taskData = array("identifier" => $data['identifier'], 'src_ref_type' => $data['ref_type'], 'src_ref_id' => $data['ref_id']);
do {
print_r('-------------------------');
$iterationCount = 0;
$attributes['limit'] = $limit;
$attributes['offset'] = $offset;
$tasks = $this->customPodioAPI->podioGet("/task/", $attributes);
if ($tasks && is_array($tasks) && count($tasks)) {
print_r('***************************************');
$this->saveTasksToDB($taskData, $tasks);
$iterationCount = count($tasks);
$offset += $limit;
}
} while ($iterationCount == $limit);
} catch (\Exception $e) {
Log::logError($e . "", $data, "saveTasks");
}
}
private function saveTasksToDB($taskData, $tasks)
{
try {
print_r('saveTasksToDB');
$sql = "INSERT IGNORE INTO `tasks` ( `identifier`, `src_task_id`, `src_ref_type`, `src_ref_id`, `status`, `due_date`,
`responsible`, `description`, `labels`, `recurrence`, `due_time`, `text`, `due_on`, `reminder`, `private`,`completed_on`,`completed_by`,`next_step`)
VALUES (:identifier,:src_task_id,:src_ref_type,:src_ref_id,:status,:due_date,:responsible,:description,
:labels,:recurrence,:due_time,:text,:due_on,:reminder,:private,:completed_on,:completed_by,:next_step) ";
$STH = DB::prepare($sql);
foreach ($tasks as $task) {
$taskDatanew = $taskData;
$task = $this->customPodioAPI->podioGet("/task/" . $task['task_id'], array());
$responsible = array(
'user_id' => $task['responsible']['user_id'],
'profile_id' => $task['responsible']['profile_id'],
'link' => $task['responsible']['link'],
'name' => $task['responsible']['name']
);
$task['private'] = $task['private'] ? 1 : 0;
$completedBy = isset($task['completed_by']) ? json_encode($task['completed_by']) : NULL;
$completedOn = isset($task['completed_on']) ? $task['completed_on'] : NULL;
$data = array(
'status' => $task['status'],
'src_task_id' => $task['task_id'],
'due_date' => $task['due_date'],
'responsible' => json_encode($responsible),
'description' => $task['description'],
'labels' => json_encode($task['labels']),
'recurrence' => json_encode($task['recurrence']),
'due_time' => $task['due_time'],
'text' => $task['text'],
'due_on' => $task['due_on'],
'reminder' => json_encode($task['reminder']),
'private' => $task['private'],
'completed_on' => $completedOn,
'completed_by' => $completedBy,
"next_step" => "save_comments_and_files"
);
$data = array_merge($data, $taskDatanew);
$resp = $STH->execute($data);
}
$STH = NULL;
} catch (\Exception $e) {
Log::logError($e . "", $tasks, "saveTasksToDB");
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists