Duffer Derek
<?php
/**
* Created by PhpStorm.
* User: jis
* Date: 12/4/17
* Time: 2:24 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;
class StreamCopyRepo
{
private $commentRepo;
private $customPodioAPI;
/**
* StreamCopyRepo constructor.
*/
public function __construct()
{
$this->commentRepo = new CommentRepo();
$this->customPodioAPI = new CustomPodioAPI();
}
public function saveSpaceStreamStatusMessages($identifier)
{
try {
Log::log("saveSpaceStreamStatusMessages started ", null, "info");
$sql = "select src_space_id,id from `work_spaces` where `task_stream_next_step` ='copy_space_stream'
and `identifier`=:identifier";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) {
// todo all spaces completed
Log::log("All spaces completed, identifier: " . $identifier, null, "info");
Log::log("All spaces completed - tasks, identifier: " . $identifier, null);
exit;
Async::callAsync("copy-async/save-tasks-files-comments?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("saveSpaceStreamStatusMessages started $identifier" . $space['src_space_id'], null, "info");
$this->copySpaceStreams($identifier, $space['src_space_id']);
$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" => "completed",
"id" => $space['id'],
"task_stream_completed_step" => ", copy_space_stream"
);
$STH = DB::prepare($sql);
$STH->execute($data);
Log::log("saveSpaceStreamStatusMessages finished $identifier" . $space['src_space_id'], null, "info");
} else {
Log::log("saveSpaceStreamStatusMessages authentication issue, identifier: " . $identifier, null, "warning");
}
Async::callAsync("copy-async/save-stream-status?identifier=" . $identifier);
}
} catch (\Exception $e) {
Log::logError($e . "", null, "saveSpaceStreamStatusMessages");
}
}
public function copySpaceStreams($identifier, $spaceID)
{
/// /stream/space/3520964/v3/?groups_event_types=creation&groups_limit=2
$offset = 0;
do {
$iterationCount = 0;
$limit = 75;
$attributes = array(
'sort_desc' => true,
'limit' => $limit,
'offset' => $offset
);
$streams = $this->customPodioAPI->podioGet("/stream/space/" . $spaceID . "/v3/?groups_event_types=creation", $attributes); //&groups_limit=50
if ($streams && is_array($streams) && count($streams) > 0) {
$iterationCount = count($streams);
$offset += $limit;
foreach ($streams as $stream) {
if ($stream['type'] == 'status') {
$this->saveStream($stream, $identifier);
}
}
}
} while ($iterationCount > 0);
}
public function saveStream($stream, $identifier)
{
try {
$sql = "INSERT IGNORE INTO `stream_status` (`identifier`, `src_space_id`,`status_id`, `file_count`,`rich_value`, `question`, `embed`,
`like_count`, `created_on`, `created_by_user_id`, `created_by_name`, `created_by_url`) VALUES (:identifier,
:src_space_id,:status_id,:file_count,:rich_value,:question, :embed,:like_count,:created_on,:created_by_user_id,:created_by_name,:created_by_url)";
$STH = DB::prepare($sql);
$fileRepo = new FileCopyRepo();
$fileCount = count($stream['files']);
$data = array(
"identifier" => $identifier,
'src_space_id' => $stream['space']['space_id'],
'file_count' => $fileCount,
'status_id' => $stream['id'],
'question' => $this->commentRepo->getQuestion($stream['data']),
'embed' => $this->commentRepo->getEmbed($stream['data']),
'like_count' => $stream['like_count'],
'created_on' => $stream['created_on'],
'created_by_user_id' => $stream['created_by']['user_id'],
'rich_value' => $stream['data']['rich_value'],
'created_by_name' => $stream['created_by']['name'],
'created_by_url' => $stream['created_by']['url']
);
if ($fileCount > 0) {
// add files to db
$fileRepo->saveFilesToDB(array(
"identifier" => $identifier,
'src_ref_type' => "status",
'src_ref_id' => $stream['id']
), $stream['files']);
}
$STH->execute($data);
$this->saveStreamCommentsAndTasks($identifier, $stream['id']);
} catch (\Exception $e) {
Log::logError($e . "", $stream, "saveStream");
}
}
private function saveStreamCommentsAndTasks($identifier, $streamID)
{
try {
$stream = $this->customPodioAPI->podioGet("/status/" . $streamID);
if (isset($stream['comments']) && is_array($stream['comments']) && count($stream['comments']) > 0) {
if ($stream['comments'] < 100) {
$this->commentRepo->saveComments($identifier, $stream['comments']);
} else {
//GET /comment/{type}/{id}/
$limit = 100;
$offset = 0;
do {
$attributes = array('limit' => $limit, 'offset' => $offset);
$iterationCount = 0;
$comments = $this->customPodioAPI->podioGet("/comment/status/" . $streamID . "/", $attributes);
if ($comments && is_array($comments) && count($comments) > 0) {
$iterationCount = count($comments);
$this->commentRepo->saveComments($identifier, $comments);
}
$offset += $limit;
} while ($iterationCount > 0);
}
}
// save tasks
$tasksRepo = new TasksCopyRepo();
$tasksRepo->saveTasks(array(
'ref_type' => "status",
"ref_id" => $streamID,
"identifier" => $identifier
));
} catch (\Exception $e) {
Log::logError($e . "", array($streamID), "saveStreamCommentsAndTasks");
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists