Duffer Derek
<?php
namespace App\Repository\Copy;
use App\Repository\General\Async;
use App\Repository\General\Constants;
use App\Repository\General\DB;
use App\Repository\General\GenMongoModel;
use App\Repository\General\Log;
use App\Repository\Utils\CustomPodioAPI;
use App\Repository\Utils\PodioAuthRepo;
use \Exception as Exception;
use PDO;
use Podio;
use PodioFlow;
use PodioWidget;
class AppCopyRepo
{
private $customPodioAPI;
/**
* AppRepo constructor.
*/
public function __construct()
{
$this->customPodioAPI = new CustomPodioAPI();
}
// copy-async/process-first-app-in-the-space?identifier=1&space=2397456
public function processFirstAppInTheSpace($identifier, $space)
{
try {
do {
$haveMoreItems = false;
$limit = 100;
$sql = "select * from `apps` where `identifier`=:identifier and src_space_id=:src_space_id and `copy_next_step` !='completed' limit $limit";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
'src_space_id' => $space
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) {
//all apps are saved now, call for next step of the space
Log::log("all apps are saved now, call for next step of the space $identifier " . $space, null, "info");
$this->saveAndCallNextStepOfSpace($identifier, $space);
} else {
if (count($result) == $limit)
$haveMoreItems = true;
$app = $result;
foreach ($result as $app) {
$result = NULL;
$STH = NULL;
$repo = new PodioAuthRepo();
if ($repo->authenticate($identifier)) {
// if ($repo->authenticateSF($identifier)) {
Constants::setREFTYPE("app " . $identifier);
Constants::setREFID($app['src_app_id']);
$this->doNextStep($app, $identifier);
} else {
Log::log("processFirstAppInTheSpace authentication issue, identifier: " . $identifier, null, "warning");
}
}
}
} while ($haveMoreItems);
} catch (\Exception $e) {
Log::logError($e . "", null, "processFirstAppInTheSpace");
}
}
public function processFirstApp($identifier)
{
try {
$sql = "select * from `apps` where `identifier`=:identifier and `copy_next_step` !='completed'limit 1";
$STH = DB::prepare($sql);
$STH->execute(array(
'identifier' => $identifier,
));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) {
//all apps are saved now, call for next step of the space
Log::log("all apps are saved now $identifier ", null, "info");
// $this->saveAndCallNextStepOfSpace($identifier, $space);
} else {
$app = $result[0];
$result = NULL;
$STH = NULL;
$repo = new PodioAuthRepo();
if ($repo->authenticate($identifier)) {
Constants::setREFTYPE("app " . $identifier);
Constants::setREFID($app['src_app_id']);
$this->doNextStep($app);
} else {
Log::log("processFirstAppInTheSpace authentication issue, identifier: " . $identifier, null, "warning");
}
}
} catch (\Exception $e) {
Log::logError($e . "", null, "processFirstAppInTheSpace");
}
}
private function saveAndCallNextStepOfSpace($identifier, $space)
{
$nextStep = "completed";
$sql = "UPDATE work_spaces SET copy_next_step=:copy_next_step,copy_completed_steps=CONCAT(COALESCE(copy_completed_steps,''),:copy_completed_steps)
WHERE `identifier`=:identifier and src_space_id=:src_space_id";
$data = array(
"copy_next_step" => $nextStep,
"copy_completed_steps" => ", process_first_app",
"identifier" => $identifier,
"src_space_id" => $space
);
$STH = DB::prepare($sql);
$STH->execute($data);
$STH = NULL;
// print("http://podiomigration.bitkit.dk/copy-async/process-first-space?identifier=1");
// exit;
Async::callAsync("copy-async/process-first-space?identifier=" . $identifier);
}
private function doNextStep($app, $identifier = null)
{
try {
Log::log("AppRepo doNextStep " . $app['copy_next_step'] . " " . $app['src_app_id'], $app);
if ($this->makeSureNextStepIsNotDoneBefore($app)) {
switch ($app['copy_next_step']) {
case "save_app":
$this->saveApp($app);
break;
case "save_items_in_app":
$this->saveItemsInApp($app);
break;
case "save_items_comments_files_tasks":
// start processing first space
// print_r("http://podiomigration.bitkit.dk/copy-async/save-items-comments-files-tasks?identifier=1&app=" . $app['src_app_id']);
// exit;
// Async::callAsync("copy-async/save-items-comments-files-tasks?identifier="
// . $app['identifier'] . "&app=" . $app['src_app_id']);
break;
}
} else {
// all steps in the app is completed
$this->markAppAsCompleted($app);
}
} catch (\Exception $e) {
Log::logError($e . "", $app, "doNextStep");
}
}
private function markAppAsCompleted($app)
{
$sql = "UPDATE apps SET copy_next_step =:copy_next_step WHERE id=:id";
$data = array("copy_next_step" => "completed", "id" => $app['id']);
$STH = DB::prepare($sql);
$STH->execute($data);
}
private function makeSureNextStepIsNotDoneBefore(&$app)
{
// $appSteps = array("save_app", "save_items_in_app", "save_items_comments_files_tasks");
$appSteps = array("save_app", "save_items_in_app");
if (in_array($app['copy_next_step'], $appSteps)) {
$pos = strpos($app['copy_completed_steps'], $app['copy_next_step']);
if ($pos === false) {
return true;
}
// already completed 'copy_next_step'
// find the first not completed step
foreach ($appSteps as $step) {
$pos = strpos($app['copy_completed_steps'], $step);
if ($pos === false) {
$app['copy_next_step'] = $step; // make this step as 'copy_next_step'
return true;
}
}
// all steps in the app is completed
}
return false;
}
private function saveApp($app)
{
try {
$podioApp = $this->customPodioAPI->podioGet("/app/" . $app['src_app_id']);
if ($podioApp) {
$this->saveAppViews($app['src_app_id']);
$srcFields = $podioApp['fields'];
$this->saveAppFields($app['identifier'], $app['src_app_id'], $srcFields);
$srcFields = json_encode($srcFields);
$voting = $this->getVoting($app);
$flows = $this->getAppFlows($app);
$widgets = $this->getWidget($app);
$sql = "UPDATE apps SET voting=:voting ,src_fields =:src_fields,src_app_type=:src_app_type,src_app_url=:src_app_url,
copy_next_step=:copy_next_step,copy_completed_steps=:copy_completed_steps,src_app_url_label=:src_app_url_label,
flows=:flows,widgets=:widgets
WHERE id=:id";
$data = array(
"copy_next_step" => "save_items_in_app",
"id" => $app['id'],
"src_fields" => $srcFields,
'voting' => $voting,
'src_app_url' => $podioApp['url'],
"src_app_type" => $podioApp['config']['type'],
"copy_completed_steps" => "save_app",
'src_app_url_label' => $podioApp['url_label'],
'flows' => $flows,
'widgets' => $widgets
);
$STH = DB::prepare($sql);
$STH->execute($data);
$app['copy_next_step'] = "save_items_in_app";
$app['copy_completed_steps'] = $app['copy_completed_steps'] . "save_app";
$STH = NULL;
//UNCOMMENT THIS
$this->doNextStep($app);
// $itemRepo = new ItemCopyRepo();
// $itemRepo->processNextApp($app['identifier'], $app['src_app_id']);
} else {
Log::log("saveApp App is Null", $app, "warning");
}
} catch (\Exception $e) {
Log::logError($e . "", $app, "saveApp");
}
}
public function saveAppViews($appId)
{
try {
$sql = "INSERT INTO `app_views` (`src_app_id`,`src_view_d`,`fields`,`view_type`) VALUES (:src_app_id, :src_view_d,:fields,
:view_type)";
$STH = DB::prepare($sql);
$views = Podio::get("/view/app/{$appId}/")->json_body();
foreach ($views as $view) {
$data = array("src_app_id" => $appId, "src_view_d" => $view['view_id'], "view_type" => $view["type"], 'fields' => json_encode($view));
$STH->execute($data);
}
$STH = NULL;
} catch (\Exception $e) {
Log::logError($e . "", array($appId), "saveAppViews");
}
}
private function getAppFlows($app)
{
try {
//save flow structure
$flows = PodioFlow::get_flows('app', $app['src_app_id']);
return json_encode($flows);
} catch (Exception $e) {
Log::logError($e . "", $app, "saveApp");
}
return NULL;
}
private function getVoting($app)
{
// to get voting field like answer and fivestar
try {
$voting = $this->customPodioAPI->podioGet("/voting/app/" . $app['src_app_id'] . "/voting");
if (isset($voting) && is_array($voting)) {
return json_encode($voting);
}
} catch (\Exception $e) {
Log::logError($e . "", $app, "getVoting");
}
return NULL;
}
public function getWidget($app)
{
try {
$widgets = $this->customPodioAPI->podioGet("/widget/app/" . $app['src_app_id']);
if ($widgets)
return json_encode($widgets);
} catch (\Exception $e) {
Log::logError($e . "", $app, "getWidget");
}
return NULL;
}
// private function saveAppFields($identifier, $app, $fields)
// {
// if ($fields && is_array($fields) && count($fields) > 0) {
// $sql = "INSERT INTO `app_fields` ( `identifier`, `src_app_id`, `src_field_id`,`status`, `field_type`,
// `src_field_config`, `src_field_external_id`, `label`) VALUES (:identifier, :src_app_id,
// :src_field_id,:status, :field_type,:src_field_config, :src_field_external_id, :label)";
//
// $STH = DB::prepare($sql);
// foreach ($fields as $field) {
// $config = $field['config'];
// $config = json_encode($config);
// $data = array("identifier" => $identifier, "src_app_id" => $app, "src_field_id" => $field['field_id'],
// 'status' => $field['status'], 'field_type' => $field['type'], 'src_field_config' => $config,
// 'src_field_external_id' => $field['external_id'], 'label' => $field['label']);
//
// $STH->execute($data);
// }
// $STH = NULL;
// }
// }
private function saveAppFields($identifier, $app, $fields)
{
try {
if ($fields && is_array($fields) && count($fields) > 0) {
// $fields[] = array(
// "type" => "contact",
// "config" => array(
// "default_value" => null,
// "unique" => false,
// "description" => null,
// "hidden_create_view_edit" => false,
// "required" => false,
// "mapping" => null,
// "label" => "Item Created User",
// "visible" => true,
// "delta" => 99,
// "hidden" => false,
// "settings" => array(
// "type" => "space_users", "valid_types" => ["user"]
// )
// ),
// "field_id" => NULL,
// "status" => "active",
// "external_id" => "item_created_user",
// "label" => "Item Created User",
// "src_delta" => 99
// );
// $fields[] = array(
// "type" => "date",
// "config" => array(
// "default_value" => null,
// "unique" => false,
// "description" => null,
// "hidden_create_view_edit" => false,
// "required" => false,
// "mapping" => null,
// "label" => "Item Created Date",
// "visible" => true,
// "delta" => 100,
// "hidden" => false,
// "settings" => array(
// "color" => "DCEBD8",
// "calendar" => false, "end" => "disabled", "time" => "enabled"
// )
// ),
// "field_id" => NULL,
// "status" => "active",
// "external_id" => "item_created_date",
// "label" => "Item Created Date",
// "src_delta" => 100
// );
$sql = "INSERT INTO `app_fields` ( `identifier`, `src_app_id`, `src_field_id`,`status`, `field_type`,
`src_field_config`, `src_field_external_id`, `label`,`src_delta`) VALUES (:identifier, :src_app_id,
:src_field_id,:status, :field_type,:src_field_config, :src_field_external_id, :label,:src_delta) ON DUPLICATE KEY
UPDATE src_delta=:src_delta ";
$STH = DB::prepare($sql);
foreach ($fields as $field) {
$config = $field['config'];
$src_delta = $config['delta'];
$config = json_encode($config);
$data = array(
"identifier" => $identifier,
"src_app_id" => $app,
"src_field_id" => $field['field_id'],
'status' => $field['status'],
'field_type' => $field['type'],
'src_field_config' => $config,
'src_field_external_id' => $field['external_id'],
'label' => $field['label'],
'src_delta' => $src_delta
);
$STH->execute($data);
}
$STH = NULL;
}
} catch (\Exception $e) {
Log::logError($e . "", $fields, "saveAppFields");
}
}
public function saveAppItemId($identifier)
{
// app_item_id
$repo = new PodioAuthRepo();
// if ($repo->authenticate($identifier)) {
if ($repo->authenticateSF($identifier)) {
Log::log("saveAppItemId started " . $identifier, null, "info");
$sql = "select id,src_app_id from `apps` where `identifier`=:identifier and `temp_step` IS NULL";
$STH = DB::prepare($sql);
if ($identifier == 8)
$STH->execute(array("identifier" => 4));
else if ($identifier == 5)
$STH->execute(array("identifier" => 7));
$result = $STH->fetchAll(PDO::FETCH_ASSOC);
if (!$result || count($result) < 1) {
//all apps are saved now, call for next step of the space
} else {
$STH = NULL;
foreach ($result as $app) {
Constants::setREFTYPE("app " . $identifier);
Constants::setREFID($app['src_app_id']);
$this->saveItemAppItemIDInApp($app);
}
}
Log::log("saveAppItemId finished " . $identifier, null, "info");
} else {
Log::log("saveAppItemId authentication issue, identifier: " . $identifier, null, "warning");
}
}
private function saveItemAppItemIDInApp($app)
{
Log::log("saveItemAppItemIDInApp started " . $app['src_app_id'], $app, "info");
$this->saveItemCountInApp($app);
$offset = 0;
$limit = 75;
$attributes = array(
'filters' => array(),
'sort_by' => 'created_on',
'sort_desc' => false
);
$attributes['limit'] = $limit;
do {
$attributes['offset'] = $offset;
$iterationCount = 0;
$filterItems = $this->customPodioAPI->filterPodioItems($app['src_app_id'], $attributes);
if ($filterItems) {
$iterationCount = count($filterItems['items']);
if ($iterationCount > 0) {
$this->updateAppItemID($filterItems['items']);
}
}
$offset += $limit;
} while ($iterationCount > 0);
$sql = "UPDATE apps SET temp_step=:temp_step WHERE id=:id";
$data = array("temp_step" => "app_item_id_saved", "id" => $app['id']);
$STH = DB::prepare($sql);
$STH->execute($data);
Log::log("saveItemAppItemIDInApp finished " . $app['src_app_id'], $app, "info");
}
private function updateAppItemID($items)
{
if ($items && is_array($items) && count($items) > 0) {
foreach ($items as $item) {
try {
$sql = "UPDATE `items` SET app_item_id=:app_item_id where src_item_id=:src_item_id";
$STH = DB::prepare($sql);
$STH->execute(array('src_item_id' => $item['item_id'], "app_item_id" => $item['app_item_id']));
} catch (Exception $e) {
Log::logError($e . "", $item, "updateAppItemID");
}
}
}
}
private function saveItemsInApp($app)
{
Log::log("saveItemsInApp started " . $app['src_app_id'], $app, "info");
$this->saveItemCountInApp($app);
$repo = new ItemCopyRepo();
$offset = 0;
$limit = 100;
$attributes = array(
'filters' => array(),
'sort_by' => 'created_on',
'sort_desc' => false
);
$attributes['limit'] = $limit;
do {
$attributes['offset'] = $offset;
$iterationCount = 0;
$filterItems = $this->customPodioAPI->filterPodioItems($app['src_app_id'], $attributes);
if ($filterItems) {
$iterationCount = count($filterItems['items']);
if ($iterationCount > 0) {
$repo->saveItemsToDB($app['identifier'], $app['src_app_id'], $filterItems['items']);
}
}
$offset += $limit;
} while ($iterationCount > 0);
Log::log("saveItemsInApp finished " . $app['src_app_id'], $app, "info");
$this->updateNextStep($app, "save_items_comments_files_tasks", ", save_items_in_app");
}
public function deltaCopy($identifier)
{
$repo = new PodioAuthRepo();
if ($repo->authenticate($identifier)) {
do {
$haveMoreItems = false;
$limit = 100;
$sql = "SELECT src_app_id,id,identifier FROM `apps`
WHERE identifier=:identifier and delta_copy_next_step IS NULL 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;
foreach ($result as $app) {
$this->deltaCopyItemsInApp($app);
}
}
} while ($haveMoreItems);
}
}
private function deltaCopyItemsInApp($app)
{
Log::log("deltaCopyItemsInApp started " . $app['src_app_id'], $app, "info");
$this->saveItemCountInApp($app);
$repo = new ItemCopyRepo();
$offset = 0;
$limit = 75;
$attributes = array(
'filters' => array(),
'sort_by' => 'activity',
'sort_desc' => true,
);
$attributes['limit'] = $limit;
do {
$attributes['offset'] = $offset;
$iterationCount = 0;
$filterItems = $this->customPodioAPI->filterPodioItems($app['src_app_id'], $attributes);
if ($filterItems) {
$iterationCount = count($filterItems['items']);
if ($iterationCount > 0) {
$isDelta = $repo->saveDeltaItemsToDB($app['identifier'], $app['src_app_id'], $filterItems['items']);
if (!$isDelta)
$iterationCount = 0;
}
}
$offset += $limit;
} while ($iterationCount > 0);
Log::log("deltaCopyItemsInApp finished " . $app['src_app_id'], $app, "info");
$sql = "UPDATE apps SET delta_copy_next_step=:delta_copy_next_step,
copy_completed_steps=CONCAT(COALESCE(copy_completed_steps,''),:copy_completed_steps) WHERE id=:id";
$data = array("delta_copy_next_step" => "delta_update_items", "id" => $app['id'], "copy_completed_steps" => "delta_copy_items");
$STH = DB::prepare($sql);
$STH->execute($data);
}
private function updateNextStep($app, $nextStep, $currentStep)
{
$sql = "UPDATE apps SET copy_next_step=:copy_next_step,copy_completed_steps=CONCAT(COALESCE(copy_completed_steps,''),:copy_completed_steps) WHERE id=:id";
$data = array("copy_next_step" => $nextStep, "id" => $app['id'], "copy_completed_steps" => $currentStep);
$STH = DB::prepare($sql);
$STH->execute($data);
$app['copy_next_step'] = $nextStep;
$app['copy_completed_steps'] = $app['copy_completed_steps'] . $currentStep;
$this->doNextStep($app);
}
private function saveItemCountInApp($app)
{
// /item/app/{app_id}/count
$itemCount = $this->customPodioAPI->podioGet("/item/app/" . $app['src_app_id'] . "/count");
if ($itemCount && isset($itemCount['count'])) {
$sql = "UPDATE apps SET src_app_item_count=:src_app_item_count WHERE id=:id";
$data = array("src_app_item_count" => $itemCount['count'], "id" => $app['id']);
$STH = DB::prepare($sql);
$STH->execute($data);
} else {
Log::log("saveItemsInApp app get item count return null", $app, "warning");
}
}
/**
* @return string
*/
public static function helloWorld()
{
try {
try {
$model = new GenMongoModel("test_table");
$model->createIndex(array("name" => 1));
$model->create(array(
"name" => "test",
));
} catch (Exception $e) {
Log::logError($e . "");
}
return 'HELLO WORLD!!!';
} catch (Exception $e) {
// Exception handler
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists