Skip to content
Snippets Groups Projects
Commit 9100b81a authored by Arjun Kumar's avatar Arjun Kumar
Browse files

Resolve #3472829 "Create list to"

parent dc08fa6d
No related branches found
No related tags found
1 merge request!12Resolve #3472829 "Create list to"
......@@ -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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment