Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
todoist_api
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
todoist_api
Commits
9100b81a
Commit
9100b81a
authored
2 months ago
by
Arjun Kumar
Browse files
Options
Downloads
Patches
Plain Diff
Resolve
#3472829
"Create list to"
parent
dc08fa6d
No related branches found
No related tags found
1 merge request
!12
Resolve #3472829 "Create list to"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Controller/TodoistapiTasks.php
+65
-35
65 additions, 35 deletions
src/Controller/TodoistapiTasks.php
with
65 additions
and
35 deletions
src/Controller/TodoistapiTasks.php
+
65
−
35
View file @
9100b81a
...
...
@@ -5,13 +5,14 @@ namespace Drupal\todoist_api\Controller;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Controller\ControllerBase
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\todoist_api
\Exception\TodoistapiException
;
use
GuzzleHttp\ClientInterface
;
use
Drupal\Core\Url
;
use
Drupal\Core\Link
;
use
Drupal\todoist_api
\Rest\Client
;
/**
* Provide a list of tasks.
*/
class
TodoistapiTasks
extends
ControllerBase
{
/**
...
...
@@ -21,17 +22,27 @@ class TodoistapiTasks extends ControllerBase {
*/
protected
$httpClient
;
protected
$todoist_client
;
/**
* Rest cliet instance Todoist_api.
*
* @var \todoist_api\Rest\Client
*/
protected
$todoistclient
;
protected
$config_factory
;
/**
* Config Factory Interface.
*
* @var Drupal\Core\Config\ConfigFactoryInterface
*/
protected
$configfactory
;
/**
* {@inheritdoc}
*/
public
function
__construct
(
ConfigFactoryInterface
$config
_
factory
,
ClientInterface
$http_client
,
Client
$todoist
_
client
)
{
$this
->
config
_
factory
=
$config
_
factory
->
getEditable
(
'todoist_api.settings'
);
public
function
__construct
(
ConfigFactoryInterface
$configfactory
,
ClientInterface
$http_client
,
Client
$todoistclient
)
{
$this
->
configfactory
=
$configfactory
->
getEditable
(
'todoist_api.settings'
);
$this
->
httpClient
=
$http_client
;
$this
->
todoist
_
client
=
$todoist
_
client
;
$this
->
todoistclient
=
$todoistclient
;
}
/**
...
...
@@ -56,12 +67,14 @@ class TodoistapiTasks extends ControllerBase {
* @return array
* A render array used to show the Posts list.
*/
/**
* A simple controller method to explain what the tablesort example is about.
*/
public
function
build
()
{
if
(
!
$this
->
config_factory
->
get
(
'api_endpoints'
)
&&
!
$this
->
config_factory
->
get
(
'access_token'
))
{
throw
new
TodoistapiException
(
'You must set your Todoist Url and Access token.'
);
if
(
!
empty
(
$this
->
configfactory
->
get
(
'api_endpoints'
))
&&
!
empty
(
$this
->
configfactory
->
get
(
'access_token'
)))
{
\Drupal
::
messenger
()
->
addError
(
$this
->
t
(
'To access your task list first, you must set your Access token.'
));
return
$this
->
redirect
(
'todoist_api.configform'
);
}
// We are going to output the results in a table with a nice header.
$header
=
[
...
...
@@ -71,18 +84,22 @@ class TodoistapiTasks extends ControllerBase {
[
'data'
=>
$this
->
t
(
'Title of the task'
),
'field'
=>
'content'
],
[
'data'
=>
$this
->
t
(
'Description'
),
'field'
=>
'description'
],
[
'data'
=>
$this
->
t
(
'Task added date'
),
'field'
=>
'created_at'
],
[
'data'
=>
$this
->
t
(
'Task due date'
),
'field'
=>
'created_at'
,
'initial_click_sort'
=>
'asc'
],
[
'data'
=>
$this
->
t
(
'Task due date'
),
'field'
=>
'created_at'
,
'initial_click_sort'
=>
'asc'
,
],
[
'data'
=>
$this
->
t
(
'Complete status'
),
'field'
=>
'is_completed'
],
[
'data'
=>
$this
->
t
(
'Action'
),
'field'
=>
'created_at'
,
'colspan'
=>
3
]
[
'data'
=>
$this
->
t
(
'Action'
),
'field'
=>
'created_at'
,
'colspan'
=>
3
]
,
];
//Get end-point Url
//
Get end-point Url
.
$end_point
=
'tasks'
;
//Fetch data
$response_data
=
$this
->
todoist
_
client
->
get
(
$end_point
);
//
Fetch data
.
$response_data
=
$this
->
todoistclient
->
get
(
$end_point
);
//Prepare table rows
//
Prepare table rows
.
$rows
=
[];
foreach
(
$response_data
as
$row
)
{
$created_date
=
(
new
\DateTime
(
$row
[
'created_at'
]))
->
getTimestamp
();
...
...
@@ -90,17 +107,18 @@ class TodoistapiTasks extends ControllerBase {
$complete_link
=
$this
->
linkgenerater
(
$row
[
'id'
],
'close'
);
$edit_link
=
$this
->
linkgenerater
(
$row
[
'id'
],
'edit'
);
$delete_link
=
$this
->
linkgenerater
(
$row
[
'id'
],
'delete'
);
$rows
[]
=
[
'data'
=>
[
$row
[
'content'
],
!
empty
(
$row
[
'description'
])
?
$row
[
'description'
]
:
$this
->
t
(
'No description.'
),
isset
(
$row
[
'created_at'
])
?
date
(
'D, j M Y'
,
$created_date
)
:
$this
->
t
(
'No date'
),
isset
(
$row
[
'due'
])
?
date
(
'D, j M Y h:m a'
,
$due_date
)
:
$this
->
t
(
'No date'
),
$row
[
'is_completed'
]
?
'Complete'
:
'Uncomplete'
,
$complete_link
,
$edit_link
,
$delete_link
]
];
$rows
[]
=
[
'data'
=>
[
$row
[
'content'
],
!
empty
(
$row
[
'description'
])
?
$row
[
'description'
]
:
$this
->
t
(
'No description.'
),
isset
(
$row
[
'created_at'
])
?
date
(
'D, j M Y'
,
$created_date
)
:
$this
->
t
(
'No date'
),
isset
(
$row
[
'due'
])
?
date
(
'D, j M Y h:m a'
,
$due_date
)
:
$this
->
t
(
'No date'
),
$row
[
'is_completed'
]
?
'Complete'
:
'Uncomplete'
,
$complete_link
,
$edit_link
,
$delete_link
,
],
];
}
// Build the link to add new task.
...
...
@@ -113,9 +131,9 @@ class TodoistapiTasks extends ControllerBase {
'button'
,
'button-action'
,
'button--small'
,
'button--primary'
]
]
'button--primary'
,
]
,
]
,
];
// Build the table for the nice output.
...
...
@@ -133,25 +151,37 @@ class TodoistapiTasks extends ControllerBase {
return
$build
;
}
protected
function
linkgenerater
(
$id
,
$type
){
switch
(
$type
){
/**
* Function return button link for edit, delte and close task.
*/
protected
function
linkgenerater
(
$id
,
$type
)
{
switch
(
$type
)
{
case
'close'
:
$link_title
=
$this
->
t
(
'Complete'
);
$class
=
'button--primary'
;
break
;
break
;
case
'delete'
:
$link_title
=
$this
->
t
(
'Delete'
);
$class
=
'button--danger'
;
break
;
break
;
case
'edit'
:
$link_title
=
$this
->
t
(
'Edit'
);
$class
=
'button--primary'
;
break
;
break
;
}
$url
=
Url
::
fromUri
(
'internal:/admin/content/todoist/'
.
$id
.
'/'
.
$type
);
$link
=
Link
::
fromTextAndUrl
(
$link_title
,
$url
);
$link
=
$link
->
toRenderable
();
$link
[
'#attributes'
]
=
array
(
'class'
=>
array
(
'button'
,
'button-action'
,
'button--small'
,
$class
));
$link
[
'#attributes'
]
=
[
'class'
=>
[
'button'
,
'button-action'
,
'button--small'
,
$class
,
],
];
return
\Drupal
::
service
(
'renderer'
)
->
render
(
$link
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment