PodcastRorController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Drupal\podcast_ror\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Drupal\Core\Template\TwigEnvironment;
  7. use Drupal\node\NodeInterface;
  8. class PodcastRorController extends ControllerBase {
  9. protected $state;
  10. protected $twig;
  11. public function __construct($state, TwigEnvironment $twig) {
  12. $this->state = $state;
  13. $this->twig = $twig;
  14. }
  15. public static function create(ContainerInterface $container) {
  16. return new static(
  17. $container->get('state'),
  18. $container->get('twig')
  19. );
  20. }
  21. private function query($baseQuery) {
  22. //TODO: estrai argomento dall'url
  23. $arg = array('ror_news', 'redazionali', 'news_trasmissioni', 'rassegna_stampa');
  24. $now = new \Drupal\Core\Datetime\DrupalDateTime('now');
  25. $now_formatted = $now->format(\Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  26. $query = $baseQuery
  27. ->condition('status', '1')
  28. ->condition('type', $arg, 'IN')
  29. ->condition('field_tx_date', $now_formatted, '<')
  30. ->sort('field_tx_date', 'DESC')
  31. ->range(0, 30);
  32. // \Drupal::logger('podcast_ror')->info(var_export(\Drupal::request()->query->get('trxid'), true));
  33. //$trxid = \Drupal::request()->query->get('trxid');
  34. // TODO: se nell'url c'e' un trx=, allora bisogna
  35. // aggiungere che arg=news_trasmissioni
  36. // fare join con node__field_trasmissione ON // node.nid = node__field_trasmissione.entity_id
  37. // condition: node__field_trasmissione.field_trasmissione_target_id=$trx
  38. $nids = $query->execute();
  39. // TODO: load contenuti_audio referenced entities
  40. $data = array();
  41. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  42. $data['nodes'] = array();
  43. foreach($nodes_e as $nid => $node) {
  44. $data['nodes'][$nid] = $node->toArray();
  45. if( count($data['nodes'][$nid]['body']) > 0 ) {
  46. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  47. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  48. 0, 3500), ENT_XML1, 'UTF-8');
  49. } else {
  50. $data['nodes'][$nid]['summary'] = "";
  51. }
  52. }
  53. $first = true;
  54. foreach($nodes_e as $nid => $node) {
  55. $data['nodes'][$nid]['audio'] = array();
  56. $data['nodes'][$nid]['img'] = array();
  57. foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) {
  58. $data['nodes'][$nid]['audio'][$ai] = $entity->toArray();
  59. //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true);
  60. }
  61. if($node->hasField('field_image')) {
  62. $node_images = $node->get('field_image');
  63. foreach( $node_images->referencedEntities() as $ai => $entity) {
  64. $arr = $entity->toArray();
  65. $data['nodes'][$nid]['img'][] = array(
  66. 'uri' => file_create_url($arr['uri'][0]['value']),
  67. // 'raw' => $arr,
  68. );
  69. }
  70. } else { // alcuni content_type non hanno immagine (rassegna stampa)
  71. $data['nodes'][$nid]['img'][] = [];
  72. }
  73. $data['nodes'][$nid]['tx'] = "";
  74. if($node->getType() == 'news_trasmissioni') {
  75. $tx = \Drupal\taxonomy\Entity\Term::load(
  76. $node->get('field_trasmissione')->target_id
  77. );
  78. if($tx !== null) {
  79. $data['nodes'][$nid]['tx'] = $tx->getName();
  80. }
  81. }
  82. if($first === true) {
  83. $first = false;
  84. }
  85. }
  86. return $data;
  87. }
  88. public function podcast($query, $title, $description, $link,
  89. $expected_episodes_per_week
  90. ) {
  91. // TODO: add $title argument to the function
  92. $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
  93. $query_results = $this->query($query);
  94. $tmpl_data = [ 'ror' => array('podcast' => $query_results,
  95. 'url' => \Drupal::service('path.current')->getPath(),
  96. 'description' => $description,
  97. 'title' => $title,
  98. 'link' => $link,
  99. 'expected_episodes_per_week' => $expected_episodes_per_week,
  100. )];
  101. $headers = array(
  102. 'Content-Type' => 'application/rss+xml',
  103. 'Access-Control-Allow-Origin' => '*',
  104. 'Access-Control-Allow-Methods' => 'GET',
  105. );
  106. $nodes = $tmpl_data['ror']['podcast']['nodes'];
  107. $lastmodtime = 0;
  108. if( !empty($nodes) ) {
  109. foreach($nodes as $nid => $node) {
  110. $modtime = intval($node['changed'][0]['value']);
  111. if($modtime > $lastmodtime) {
  112. $lastmodtime = $modtime;
  113. }
  114. }
  115. }
  116. $xml = $template->render($tmpl_data);
  117. $resp = new Response($xml, 200, $headers);
  118. $resp->setPublic();
  119. $resp->setTtl(300);
  120. if($lastmodtime !== 0) {
  121. $resp->setLastModified(\DateTime::createFromFormat('U',$lastmodtime));
  122. }
  123. return $resp;
  124. }
  125. public function podcastAll() {
  126. $query = \Drupal::entityQuery('node');
  127. return $this->podcast($query, 'Radio Onda Rossa', 'Tutti gli aggiornamenti, i redazionali, le trasmissioni della radio di chi se la sente',
  128. 'http://www.ondarossa.info/',
  129. 30
  130. );
  131. }
  132. public function podcastByType($contentType) {
  133. $query = \Drupal::entityQuery('node')
  134. ->condition('type', $contentType);
  135. // TODO: get "nice name" for $contentType
  136. $what = str_replace('+', ', ', $contentType);
  137. return $this->podcast($query,
  138. $what . ' (Radio Onda Rossa)',
  139. $what,
  140. 'http://www.ondarossa.info/',
  141. 30
  142. );
  143. }
  144. public function podcastByTrxID($trxID) {
  145. $term = \Drupal\taxonomy\Entity\Term::load($trxID);
  146. $trxName = $term->getName();
  147. $trxDescriptionHTML = $term->getDescription();
  148. $trxDescription = strip_tags($trxDescriptionHTML);
  149. $trxURL = \Drupal\Core\Url::fromRoute('entity.taxonomy_term.canonical',
  150. ['taxonomy_term' => $trxID],
  151. ['absolute' => TRUE])->toString();
  152. $query = \Drupal::entityQuery('node')
  153. ->condition('type', 'news_trasmissioni')
  154. ->condition('field_trasmissione', $trxID);
  155. return $this->podcast($query,
  156. $trxName . ' (Radio Onda Rossa)',
  157. $trxDescription,
  158. $trxURL,
  159. 1,
  160. );
  161. }
  162. }