|
@@ -0,0 +1,56 @@
|
|
|
+<?php
|
|
|
+namespace Drupal\calendar_ror\Controller;
|
|
|
+
|
|
|
+use Drupal\Core\Controller\ControllerBase;
|
|
|
+use Symfony\Component\HttpFoundation\Response;
|
|
|
+use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
+use Drupal\Core\Template\TwigEnvironment;
|
|
|
+use Drupal\node\NodeInterface;
|
|
|
+
|
|
|
+class CalendarRorController extends ControllerBase {
|
|
|
+ protected $state;
|
|
|
+ protected $twig;
|
|
|
+ public function __construct($state, TwigEnvironment $twig) {
|
|
|
+ $this->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;
|
|
|
+ }
|
|
|
+}
|