|
@@ -0,0 +1,135 @@
|
|
|
|
+<?php
|
|
|
|
+
|
|
|
|
+namespace Drupal\edlp_agenda\Controller;
|
|
|
|
+
|
|
|
|
+use Drupal\Core\Controller\ControllerBase;
|
|
|
|
+use Drupal\Core\Datetime\DrupalDateTime;
|
|
|
|
+use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class AgendaController extends ControllerBase {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private function query() {
|
|
|
|
+ // @see https://www.webomelette.com/query-entities-using-dates-drupal-8
|
|
|
|
+
|
|
|
|
+ $now = new DrupalDateTime('now');
|
|
|
|
+ $now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
|
|
|
|
+
|
|
|
|
+ $query = \Drupal::entityQuery('node')
|
|
|
|
+ ->condition('status', 1)
|
|
|
|
+ ->condition('type', 'evenement')
|
|
|
|
+ ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
|
|
|
|
+ ->sort('field_date');
|
|
|
|
+
|
|
|
|
+ $this->future_nids = $query->execute();
|
|
|
|
+ $this->future_nodes = entity_load_multiple('node', $this->future_nids);
|
|
|
|
+
|
|
|
|
+ $this->next_event = array_shift($this->future_nodes);
|
|
|
|
+
|
|
|
|
+ $query = \Drupal::entityQuery('node')
|
|
|
|
+ ->condition('status', 1)
|
|
|
|
+ ->condition('type', 'evenement')
|
|
|
|
+ ->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '<')
|
|
|
|
+ ->sort('field_date', 'DESC');
|
|
|
|
+
|
|
|
|
+ $this->past_nids = $query->execute();
|
|
|
|
+ $this->past_nodes = entity_load_multiple('node', $this->past_nids);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private function toRenderable(){
|
|
|
|
+ $this->query();
|
|
|
|
+ //
|
|
|
|
+ // dpm($this->future_nodes);
|
|
|
|
+ // dpm($this->next_event);
|
|
|
|
+ /*
|
|
|
|
+ @see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
|
|
|
|
+
|
|
|
|
+ $future_list = array (
|
|
|
|
+ '#theme' => 'item_list',
|
|
|
|
+ '#items' => [],
|
|
|
|
+ );
|
|
|
|
+ foreach($this->future_nodes as $node){
|
|
|
|
+ $future_list['#items'][] = $view_builder->view($node, 'teaser');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $past_list = array (
|
|
|
|
+ '#theme' => 'item_list',
|
|
|
|
+ '#items' => [],
|
|
|
|
+ );
|
|
|
|
+ foreach($this->past_nodes as $node){
|
|
|
|
+ $past_list['#items'][] = $view_builder->view($node, 'teaser');
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return array(
|
|
|
|
+ "#type" => "container",
|
|
|
|
+ "#attributes"=>array(
|
|
|
|
+ "id"=>['agenda']
|
|
|
|
+ ),
|
|
|
|
+ // "#markup"=> "<h2>Agenda</h2>",
|
|
|
|
+ "next"=>array(
|
|
|
|
+ "#type"=>"container",
|
|
|
|
+ "#attributes"=>array(
|
|
|
|
+ "class"=>['next-event', 'column', 'os-scroll']
|
|
|
|
+ ),
|
|
|
|
+ "#markup"=>"<h3>Prochaine Date</h3>",
|
|
|
|
+ "event"=>$view_builder->view($this->next_event, 'default')
|
|
|
|
+ ),
|
|
|
|
+ "future_past"=>array(
|
|
|
|
+ "#type"=>"container",
|
|
|
|
+ "#attributes"=>array(
|
|
|
|
+ "class"=>['future-past-events', 'column', 'os-scroll']
|
|
|
|
+ ),
|
|
|
|
+ "future"=>array(
|
|
|
|
+ "#type"=>"container",
|
|
|
|
+ "#attributes"=>array(
|
|
|
|
+ "class"=>['future-events']
|
|
|
|
+ ),
|
|
|
|
+ "#markup"=>"<h3>Dates à venir</h3>",
|
|
|
|
+ "future_events"=>$future_list
|
|
|
|
+ ),
|
|
|
|
+ "past"=>array(
|
|
|
|
+ "#type"=>"container",
|
|
|
|
+ "#attributes"=>array(
|
|
|
|
+ "class"=>['past-events']
|
|
|
|
+ ),
|
|
|
|
+ "#markup"=>"<h3>Dates passées</h3>",
|
|
|
|
+ "past_events"=>$past_list
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Display agenda as a page.
|
|
|
|
+ *
|
|
|
|
+ * @return renderable array
|
|
|
|
+ */
|
|
|
|
+ public function agenda() {
|
|
|
|
+ return $this->toRenderable();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Get agenda data as json through ajax.
|
|
|
|
+ *
|
|
|
|
+ * @return json
|
|
|
|
+ */
|
|
|
|
+ public function agendajson() {
|
|
|
|
+
|
|
|
|
+ $response = new JsonResponse();
|
|
|
|
+ $renderable = $this->toRenderable();
|
|
|
|
+ $rendered = render($renderable);
|
|
|
|
+
|
|
|
|
+ $response->setData([
|
|
|
|
+ 'test'=>'hello',
|
|
|
|
+ 'rendered'=> $rendered
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ return $response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|