From b69bd7f300da4623f1bd98cea6f5221ab95f1fb4 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 8 Oct 2018 11:29:39 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + calendar_ror.info.yml | 9 ++++ calendar_ror.routing.yml | 9 ++++ src/Controller/CalendarRorController.php | 56 ++++++++++++++++++++++++ templates/calendar.ics.twig | 15 +++++++ 5 files changed, 90 insertions(+) create mode 100644 .gitignore create mode 100644 calendar_ror.info.yml create mode 100644 calendar_ror.routing.yml create mode 100644 src/Controller/CalendarRorController.php create mode 100644 templates/calendar.ics.twig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..726c3ca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.sw* diff --git a/calendar_ror.info.yml b/calendar_ror.info.yml new file mode 100644 index 0000000..799736a --- /dev/null +++ b/calendar_ror.info.yml @@ -0,0 +1,9 @@ +name: Calendario ROR +type: module +description: 'Calendario ICS delle iniziative di ROR' +core: 8.x +version: 0.1 +package: Ondarossa +dependencies: + - field + - link diff --git a/calendar_ror.routing.yml b/calendar_ror.routing.yml new file mode 100644 index 0000000..85e21b7 --- /dev/null +++ b/calendar_ror.routing.yml @@ -0,0 +1,9 @@ +calendar_ror.ics: + path: '/calendar/calendar.ics' + defaults: + _controller: '\Drupal\calendar_ror\Controller\CalendarRorController::ics' + _title: 'Global calendar' + requirements: + _access: 'TRUE' + options: + no_cache: 'TRUE' diff --git a/src/Controller/CalendarRorController.php b/src/Controller/CalendarRorController.php new file mode 100644 index 0000000..020b683 --- /dev/null +++ b/src/Controller/CalendarRorController.php @@ -0,0 +1,56 @@ +state = $state; + $this->twig = $twig; + } + + public static function create(ContainerInterface $container) { + return new static( + $container->get('state'), + $container->get('twig') + ); + } + + private function query() { + $query = \Drupal::entityQuery('node'); + $query + ->condition('status', '1') + ->condition('type', 'eventi') + ->sort('nid', 'DESC') + ->range(0, 20); + $nids = $query->execute(); + $data = array(); + $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids); + $data['nodes'] = array(); + foreach($nodes_e as $nid => $node) { + $data['nodes'][$nid] = $node->toArray(); + $data['nodes'][$nid]['summary'] = htmlspecialchars(substr( + html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])), + 0, 3500), ENT_XML1, 'UTF-8'); + } + + return $data; + } + public function ics() { + // TODO: use $path + $template = $this->twig->loadTemplate(drupal_get_path('module', 'calendar_ror') . '/templates/calendar.ics.twig'); + $tmpl_data = [ 'ror' => array('calendar' => $this->query(), + 'url' => \Drupal::service('path.current')->getPath(), + )]; + //$variables['ror']['url'] = + $xml = $template->render($tmpl_data); + $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml')); + return $resp; + } +} diff --git a/templates/calendar.ics.twig b/templates/calendar.ics.twig new file mode 100644 index 0000000..41f8160 --- /dev/null +++ b/templates/calendar.ics.twig @@ -0,0 +1,15 @@ +BEGIN:VCALENDAR +PRODID:drupal-calendar-ror +VERSION:2.0 +{% for nid, node in ror.calendar.nodes %} + +BEGIN:VEVENT +SUMMARY: {{node.title.0.value}} +UID: node-{{node.nid.0.value}}@www.ondarossa.info +DTSTAMP: {{node.created.0.value}} +DTSTART: {{node.field_data_evento.0.value|date("Ymd\\THis")}}Z +DURATION: PT1H +URL: http://www.ondarossa.info{{node.path.0.alias}} +END:VEVENT +{% endfor %} +END:VCALENDAR