commit b69bd7f300da4623f1bd98cea6f5221ab95f1fb4 Author: root Date: Mon Oct 8 11:29:39 2018 +0200 initial commit 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