diff --git a/src/Controller/TodoistapiTasks.php b/src/Controller/TodoistapiTasks.php index 211ed719ab66792cb30aca441deedc759cab0d64..08d8fa8db5dbfec7fa16b8bd40eeee1fb6b711a8 100644 --- a/src/Controller/TodoistapiTasks.php +++ b/src/Controller/TodoistapiTasks.php @@ -72,9 +72,7 @@ class TodoistapiTasks extends ControllerBase { * A simple controller method to explain what the tablesort example is about. */ public function build() { - - // Check if the access-token is available or not. - if (empty($this->configfactory->get('api_endpoints')) || empty($this->configfactory->get('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'); } @@ -104,18 +102,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 60be38aa6c416097a3fbda7685e7b75e40832bff..4f2dcaa294cf3fc1417cf48c8983696d84a34f57 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 c3692aab1bcc15d64e2ef5e188a3016a73d6362c..30f580374338836a697bdf0a684233ff3dbf931b 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; } }