From 92fdfb7f624490e7f432092bec79685659040a73 Mon Sep 17 00:00:00 2001 From: Arjun Kumar <manav_kumar1@yahoo.com> Date: Sun, 10 Nov 2024 15:44:44 +0530 Subject: [PATCH 1/4] Issue #3472829: Create list to show all the tasks --- src/Rest/Client.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Rest/Client.php b/src/Rest/Client.php index 4f2dcaa..3f4f8fc 100644 --- a/src/Rest/Client.php +++ b/src/Rest/Client.php @@ -73,18 +73,32 @@ Class Client extends TodoIstapiClientbase { ] ]; - $end_point = $this->todoistapiConfig->get('api_endpoints'); + $end_point = $this->todoistapiConfig->get('api_endpoints') . $uri; switch($type) { case 'DELETE': - $end_point = $end_point . $uri . '/' . $id; + $end_point = $end_point . '/' . $id; break; case 'GET': - $end_point = $end_point . $uri; $end_point .= !empty($id) ? '/' . $id : ''; break; case 'POST': - $end_point = !empty($body) ? Url::fromUri($end_point . $uri, ['query' => $body])->toString() : $end_point . $uri . '/' . $id . '/' . 'close'; + if(in_array("edit", $body)){ + $end_point = $end_point . '/' . $id; + unset($body['type']); + $end_point = Url::fromUri($end_point, ['query' => $body])->toString(); + } elseif(in_array("close", $body)){ + $end_point = $end_point . '/' . $id . '/' . $body['type']; + unset($body); + } else { + $end_point = Url::fromUri($end_point, ['query' => $body])->toString(); + } + + + // dump($end_point); + // dump($type); + // dump($body); + // die; break; } -- GitLab From 2a9e00875f9839b69e35b192c203135566971517 Mon Sep 17 00:00:00 2001 From: Arjun Kumar <manav_kumar1@yahoo.com> Date: Sun, 10 Nov 2024 15:50:18 +0530 Subject: [PATCH 2/4] Issue #3472829: Create list to show all the tasks --- src/Rest/Client.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Rest/Client.php b/src/Rest/Client.php index 3f4f8fc..60be38a 100644 --- a/src/Rest/Client.php +++ b/src/Rest/Client.php @@ -93,12 +93,6 @@ Class Client extends TodoIstapiClientbase { } else { $end_point = Url::fromUri($end_point, ['query' => $body])->toString(); } - - - // dump($end_point); - // dump($type); - // dump($body); - // die; break; } -- GitLab From 811f46f0c04dab104b0d4c46357a78f553bf604d Mon Sep 17 00:00:00 2001 From: Arjun Kumar <manav_kumar1@yahoo.com> Date: Mon, 11 Nov 2024 22:07:00 +0530 Subject: [PATCH 3/4] Issue #3472829: Code refactoring for all the tasks --- src/Controller/TodoistapiTasks.php | 100 +++++++++++++++++++---------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/src/Controller/TodoistapiTasks.php b/src/Controller/TodoistapiTasks.php index 1fba0c6..f0d38f7 100644 --- a/src/Controller/TodoistapiTasks.php +++ b/src/Controller/TodoistapiTasks.php @@ -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); } -- GitLab From e1bb2f8eafb786ef924974af62497adb3ccbf60c Mon Sep 17 00:00:00 2001 From: Arjun Kumar <manav_kumar1@yahoo.com> Date: Fri, 17 Jan 2025 00:26:37 +0530 Subject: [PATCH 4/4] Issue #3472829: Resolve date issue by using Drupal date services. --- src/Controller/TodoistapiTasks.php | 13 +++++++------ src/Rest/Client.php | 16 ++++------------ src/TodoistapiClientbase.php | 12 ------------ 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/src/Controller/TodoistapiTasks.php b/src/Controller/TodoistapiTasks.php index 211ed71..b42f52c 100644 --- a/src/Controller/TodoistapiTasks.php +++ b/src/Controller/TodoistapiTasks.php @@ -104,18 +104,19 @@ class TodoistapiTasks extends ControllerBase { // Prepare table rows. $rows = []; - foreach ($response_data as $row) { - $created_date = (new \DateTime($row['created_at']))->getTimestamp(); - $due_date = (new \DateTime($row['due']['datetime']))->getTimestamp(); - $complete_link = $this->linkgenerater($row['id'], 'close'); + foreach ($response_data as $row) { + $createdDate = \Drupal::service('date.formatter')->format((new \DateTime($row['created_at']))->getTimestamp(), 'custom', 'D, d M Y h:i a'); + $dueDate = \Drupal::service('date.formatter')->format((new \DateTime($row['due']['datetime']))->getTimestamp(), 'custom', 'D, d M Y h:i a'); + + $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'), + isset($row['created_at']) ? $createdDate : $this->t('No date'), + isset($row['due']) ? $dueDate : $this->t('No date'), $row['is_completed'] ? 'Complete' : 'Uncomplete', $complete_link, $edit_link, diff --git a/src/Rest/Client.php b/src/Rest/Client.php index 60be38a..4f2dcaa 100644 --- a/src/Rest/Client.php +++ b/src/Rest/Client.php @@ -73,26 +73,18 @@ Class Client extends TodoIstapiClientbase { ] ]; - $end_point = $this->todoistapiConfig->get('api_endpoints') . $uri; + $end_point = $this->todoistapiConfig->get('api_endpoints'); switch($type) { case 'DELETE': - $end_point = $end_point . '/' . $id; + $end_point = $end_point . $uri . '/' . $id; break; case 'GET': + $end_point = $end_point . $uri; $end_point .= !empty($id) ? '/' . $id : ''; break; case 'POST': - if(in_array("edit", $body)){ - $end_point = $end_point . '/' . $id; - unset($body['type']); - $end_point = Url::fromUri($end_point, ['query' => $body])->toString(); - } elseif(in_array("close", $body)){ - $end_point = $end_point . '/' . $id . '/' . $body['type']; - unset($body); - } else { - $end_point = Url::fromUri($end_point, ['query' => $body])->toString(); - } + $end_point = !empty($body) ? Url::fromUri($end_point . $uri, ['query' => $body])->toString() : $end_point . $uri . '/' . $id . '/' . 'close'; break; } diff --git a/src/TodoistapiClientbase.php b/src/TodoistapiClientbase.php index c3692aa..30f5803 100644 --- a/src/TodoistapiClientbase.php +++ b/src/TodoistapiClientbase.php @@ -26,7 +26,6 @@ abstract class TodoistapiClientbase { * {@inheritdoc} */ public function __construct(ConfigFactoryInterface $config_factory) { - //$this->httpClient = $http_client; $this->todoistapiConfig = $config_factory->get('todoist_api.settings'); if(!$this->todoistapiConfig->get('api_endpoints') && !$this->todoistapiConfig->get('access_token')) { throw new TodoistapiException('You must set your Todoist Url and Access token.'); @@ -35,17 +34,6 @@ abstract class TodoistapiClientbase { public function GetAlltasks() { - $options = ['headers' => ['Authorization' => 'Bearer a0dc04e0c04e0e19ac7621cd5385f5111920bf2f']]; - $request = $this->httpClient->request('GET', 'https://api.todoist.com/rest/v2/tasks', $options); - $response_data = json_decode($request->getBody()->getContents(), TRUE); - // $client = new Client(); - // $options = ['headers' => ['Authorization' => 'Bearer a0dc04e0c04e0e19ac7621cd5385f5111920bf2f']]; - // $rest = $client->get('https://api.todoist.com/rest/v2/tasks', $options); - // $response_data = json_decode($rest->getBody()->getContents(), TRUE); - dump($response_data); - die; - - return null; } } -- GitLab