Skip to content

Commit

Permalink
Adding method to parser json to object
Browse files Browse the repository at this point in the history
  • Loading branch information
wiltonsr committed Apr 9, 2019
1 parent f310a11 commit 3666768
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 63 deletions.
99 changes: 38 additions & 61 deletions Controller/TrelloJSON2KanboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TrelloJSON2KanboardController extends BaseController
public function create(array $values = array(), array $errors = array())
{
if (!isset($values['is_private'])) {
$values += array('is_private' => 0);
$values += array('is_private' => 0);
}
$this->response->html($this->helper->layout->app('TrelloJSON2Kanboard:json_import/create', array(
'values' => $values,
Expand Down Expand Up @@ -44,7 +44,9 @@ public function save()
if (is_null($jsonObj)) {
$this->create($values, array('file' => array(t('Unable to parse JSON file. Error: %s', json_last_error_msg()))));
} else {
$values += array('name' => $jsonObj->name);
$project = $this->trelloJSON2KanboardModel->parserJSON($jsonObj);

$values += array('name' => $project->name);

//creating the project
$project_id = $this->createNewProject($values);
Expand All @@ -57,78 +59,53 @@ public function save()
}

//getting columns from JSON file
foreach ($jsonObj->lists as $list) {
if ($list->closed) {
//ignore archived lists
continue;
}
foreach ($project->columns as $column) {
//creating column
$column_id = $this->columnModel->create($project_id, $list->name, 0, $list->desc, 0);
$column_id = $this->columnModel->create($project_id, $column->name, 0, '', 0);

//getting tasks from JSON file
foreach ($jsonObj->cards as $card) {
if ($card->closed) {
//ignore archived cards
continue;
foreach ($column->tasks as $task) {
//only get cards that belongs to this column
$values = array(
'title' => $task->name,
'project_id' => $project_id,
'column_id' => $column_id,
'date_due' => $task->date_due,
'description' => $task->desc,
);
//creating task
$task_id = $this->taskCreationModel->create($values);

//getting checklists from JSON file
foreach ($task->subtasks as $subtask) {
$values = array(
'title' => $subtask->content,
'task_id' => $task_id,
'status' => $subtask->status,
);
//creating subtask
$subtask_id = $this->subtaskModel->create($values);
}

//only get cards that belongs to this column
if ($card->idList == $list->id) {
//converting trello due date to kanboard format
$due_date = $card->due !== null ? date('Y-m-d H:i', strtotime($card->due)) : null;
foreach ($task->comments as $comment) {
//only get actions from commentCard type
$values = array(
'title' => $card->name,
'project_id' => $project_id,
'column_id' => $column_id,
'date_due' => $due_date,
'description' => $card->desc,
'task_id' => $task_id,
'user_id' => $this->userSession->getId(),
'comment' => $comment->content,
);
//creating task
$task_id = $this->taskCreationModel->create($values);

//getting checklists from JSON file
foreach ($jsonObj->checklists as $checklist) {
//only get checklists that belongs to this card
if ($checklist->idCard == $card->id) {
foreach ($checklist->checkItems as $checkitem) {
$status = $checkitem->state == 'complete' ? 2 : 0;
$values = array(
'title' => $checkitem->name,
'task_id' => $task_id,
'status' => $status,
);
//creating subtask
$subtask_id = $this->subtaskModel->create($values);
}
}
}

//getting actions from JSON file
foreach ($jsonObj->actions as $action) {
//only get actions from commentCard type
if ($action->type == 'commentCard') {
//only get comments that belongs to this card
if ($action->data->card->id == $card->id) {
$values = array(
'task_id' => $task_id,
'user_id' => $this->userSession->getId(),
'comment' => $action->data->text,
);
//creating comment
$comment_id = $this->commentModel->create($values);
}
}
}
//creating comment
$comment_id = $this->commentModel->create($values);
}
}
}

$this->flash->success(t('Your project have been imported successfully.'));
return $this->response->redirect($this->helper->url->to('ProjectViewController', 'show', array('project_id' => $project_id)));
}

$this->flash->failure(t('Unable to import your project.'));
$this->flash->success(t('Your project have been imported successfully.'));
return $this->response->redirect($this->helper->url->to('ProjectViewController', 'show', array('project_id' => $project_id)));
}

$this->flash->failure(t('Unable to import your project.'));
}
}

Expand Down
135 changes: 135 additions & 0 deletions Model/TrelloJSON2KanboardModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Kanboard\Plugin\TrelloJSON2Kanboard\Model;

use Kanboard\Core\Base;

/**
* TrelloJSON2Kanboard Model
*
* @package Kanboard\Plugin\TrelloJSON2Kanboard\Model
* @author Wilton Rodrigues
*/
class TrelloJSON2KanboardModel extends Base
{
public function parserJSON($jsonObj)
{
$project = new Project($jsonObj->name);

//getting columns from JSON file
foreach ($jsonObj->lists as $list) {
if ($list->closed) {
//ignore archived lists
continue;
}
//creating column
$column = new Column($list->name, $list->id);
array_push($project->columns, $column);

foreach ($jsonObj->cards as $card) {
if ($card->closed) {
//ignore archived lists
continue;
}
if ($card->idList == $column->trello_id) {
$due_date = $card->due !== null ? date('Y-m-d H:i', strtotime($card->due)) : null;
$task = new Task($card->name, $card->id, $card->idList, $due_date, $card->desc);
array_push($column->tasks, $task);

//getting checklists from JSON file
foreach ($jsonObj->checklists as $checklist) {
if ($checklist->idCard == $card->id) {
foreach ($checklist->checkItems as $checkitem) {
$status = $checkitem->state == 'complete' ? 2 : 0;
//creating subtask
$subtask = new Subtask($checkitem->name, $status);
array_push($task->subtasks, $subtask);
}
}
}

//getting actions from JSON file
foreach ($jsonObj->actions as $action) {
//only get actions from commentCard type
if ($action->type == 'commentCard') {
//only get comments that belongs to this card
if ($action->data->card->id == $task->trello_id) {
$comment = new Comment($action->data->text);
array_push($task->comments, $comment);
}
}
}
}
}
}


return $project;
}
}

class Project
{
public $name;
public $columns = array();

public function __construct($name)
{
$this->name = $name;
}
}

class Column
{
var $name;
var $trello_id;
var $tasks = array();

function __construct($name, $trello_id)
{
$this->name = $name;
$this->trello_id = $trello_id;
}
}

class Task
{
var $name;
var $trello_id;
var $trello_column_id;
var $date_due;
var $desc;
var $subtasks = array();
var $comments = array();

function __construct($name, $trello_id, $trello_column_id, $date_due, $desc)
{
$this->name = $name;
$this->trello_id = $trello_id;
$this->trello_column_id = $trello_column_id;
$this->date_due = $date_due;
$this->desc = $desc;
}
}

class Subtask
{
var $content;
var $status;

function __construct($content, $status)
{
$this->content = $content;
$this->status = $status;
}
}

class Comment
{
var $content;

function __construct($content)
{
$this->content = $content;
}
}
12 changes: 10 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ public function initialize()

public function onStartup()
{
Translator::load($this->languageModel->getCurrentLanguage(), __DIR__.'/Locale');
Translator::load($this->languageModel->getCurrentLanguage(), __DIR__ . '/Locale');
}

public function getClasses()
{
return array(
'Plugin\TrelloJSON2Kanboard\Model' => array(
'TrelloJSON2KanboardModel',
)
);
}

public function getPluginName()
Expand Down Expand Up @@ -43,4 +52,3 @@ public function getPluginHomepage()
return 'https://github.com/wiltonsr/TrelloJSON2Kanboard';
}
}

0 comments on commit 3666768

Please sign in to comment.