vendor/contao/news-bundle/src/Resources/contao/modules/ModuleNewsArchive.php line 229

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Contao.
  4. *
  5. * (c) Leo Feyer
  6. *
  7. * @license LGPL-3.0-or-later
  8. */
  9. namespace Contao;
  10. use Contao\CoreBundle\Exception\PageNotFoundException;
  11. /**
  12. * Front end module "news archive".
  13. *
  14. * @property array $news_archives
  15. * @property string $news_jumpToCurrent
  16. * @property string $news_format
  17. * @property string $news_order
  18. * @property int $news_readerModule
  19. */
  20. class ModuleNewsArchive extends ModuleNews
  21. {
  22. /**
  23. * Template
  24. * @var string
  25. */
  26. protected $strTemplate = 'mod_newsarchive';
  27. /**
  28. * Display a wildcard in the back end
  29. *
  30. * @return string
  31. */
  32. public function generate()
  33. {
  34. $request = System::getContainer()->get('request_stack')->getCurrentRequest();
  35. if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
  36. {
  37. $objTemplate = new BackendTemplate('be_wildcard');
  38. $objTemplate->wildcard = '### ' . $GLOBALS['TL_LANG']['FMD']['newsarchive'][0] . ' ###';
  39. $objTemplate->title = $this->headline;
  40. $objTemplate->id = $this->id;
  41. $objTemplate->link = $this->name;
  42. $objTemplate->href = StringUtil::specialcharsUrl(System::getContainer()->get('router')->generate('contao_backend', array('do'=>'themes', 'table'=>'tl_module', 'act'=>'edit', 'id'=>$this->id)));
  43. return $objTemplate->parse();
  44. }
  45. $this->news_archives = $this->sortOutProtected(StringUtil::deserialize($this->news_archives));
  46. // No news archives available
  47. if (empty($this->news_archives) || !\is_array($this->news_archives))
  48. {
  49. return '';
  50. }
  51. // Show the news reader if an item has been selected
  52. if ($this->news_readerModule > 0 && (isset($_GET['items']) || (Config::get('useAutoItem') && isset($_GET['auto_item']))))
  53. {
  54. return $this->getFrontendModule($this->news_readerModule, $this->strColumn);
  55. }
  56. // Hide the module if no period has been selected
  57. if ($this->news_jumpToCurrent == 'hide_module' && !isset($_GET['year']) && !isset($_GET['month']) && !isset($_GET['day']))
  58. {
  59. return '';
  60. }
  61. // Tag the news archives (see #2137)
  62. if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
  63. {
  64. $responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
  65. $responseTagger->addTags(array_map(static function ($id) { return 'contao.db.tl_news_archive.' . $id; }, $this->news_archives));
  66. }
  67. return parent::generate();
  68. }
  69. /**
  70. * Generate the module
  71. */
  72. protected function compile()
  73. {
  74. /** @var PageModel $objPage */
  75. global $objPage;
  76. $limit = null;
  77. $offset = 0;
  78. $intBegin = 0;
  79. $intEnd = 0;
  80. $intYear = (int) Input::get('year');
  81. $intMonth = (int) Input::get('month');
  82. $intDay = (int) Input::get('day');
  83. // Jump to the current period
  84. if (!isset($_GET['year']) && !isset($_GET['month']) && !isset($_GET['day']) && $this->news_jumpToCurrent != 'all_items')
  85. {
  86. switch ($this->news_format)
  87. {
  88. case 'news_year':
  89. $intYear = date('Y');
  90. break;
  91. default:
  92. case 'news_month':
  93. $intMonth = date('Ym');
  94. break;
  95. case 'news_day':
  96. $intDay = date('Ymd');
  97. break;
  98. }
  99. }
  100. // Create the date object
  101. try
  102. {
  103. if ($intYear)
  104. {
  105. $strDate = $intYear;
  106. $objDate = new Date($strDate, 'Y');
  107. $intBegin = $objDate->yearBegin;
  108. $intEnd = $objDate->yearEnd;
  109. $this->headline .= ' ' . date('Y', $objDate->tstamp);
  110. }
  111. elseif ($intMonth)
  112. {
  113. $strDate = $intMonth;
  114. $objDate = new Date($strDate, 'Ym');
  115. $intBegin = $objDate->monthBegin;
  116. $intEnd = $objDate->monthEnd;
  117. $this->headline .= ' ' . Date::parse('F Y', $objDate->tstamp);
  118. }
  119. elseif ($intDay)
  120. {
  121. $strDate = $intDay;
  122. $objDate = new Date($strDate, 'Ymd');
  123. $intBegin = $objDate->dayBegin;
  124. $intEnd = $objDate->dayEnd;
  125. $this->headline .= ' ' . Date::parse($objPage->dateFormat, $objDate->tstamp);
  126. }
  127. elseif ($this->news_jumpToCurrent == 'all_items')
  128. {
  129. $intBegin = 0; // 1970-01-01 00:00:00
  130. $intEnd = min(4294967295, PHP_INT_MAX); // 2106-02-07 07:28:15
  131. }
  132. }
  133. catch (\OutOfBoundsException $e)
  134. {
  135. throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
  136. }
  137. $this->Template->articles = array();
  138. // Split the result
  139. if ($this->perPage > 0)
  140. {
  141. // Get the total number of items
  142. $intTotal = NewsModel::countPublishedFromToByPids($intBegin, $intEnd, $this->news_archives);
  143. if ($intTotal > 0)
  144. {
  145. $total = $intTotal;
  146. // Get the current page
  147. $id = 'page_a' . $this->id;
  148. $page = (int) (Input::get($id) ?? 1);
  149. // Do not index or cache the page if the page number is outside the range
  150. if ($page < 1 || $page > max(ceil($total/$this->perPage), 1))
  151. {
  152. throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
  153. }
  154. // Set limit and offset
  155. $limit = $this->perPage;
  156. $offset = (max($page, 1) - 1) * $this->perPage;
  157. // Add the pagination menu
  158. $objPagination = new Pagination($total, $this->perPage, Config::get('maxPaginationLinks'), $id);
  159. $this->Template->pagination = $objPagination->generate("\n ");
  160. }
  161. }
  162. // Determine sorting
  163. $t = NewsModel::getTable();
  164. $arrOptions = array();
  165. switch ($this->news_order)
  166. {
  167. case 'order_headline_asc':
  168. $arrOptions['order'] = "$t.headline";
  169. break;
  170. case 'order_headline_desc':
  171. $arrOptions['order'] = "$t.headline DESC";
  172. break;
  173. case 'order_random':
  174. $arrOptions['order'] = "RAND()";
  175. break;
  176. case 'order_date_asc':
  177. $arrOptions['order'] = "$t.date";
  178. break;
  179. default:
  180. $arrOptions['order'] = "$t.date DESC";
  181. }
  182. // Get the news items
  183. if (isset($limit))
  184. {
  185. $objArticles = NewsModel::findPublishedFromToByPids($intBegin, $intEnd, $this->news_archives, $limit, $offset, $arrOptions);
  186. }
  187. else
  188. {
  189. $objArticles = NewsModel::findPublishedFromToByPids($intBegin, $intEnd, $this->news_archives, 0, 0, $arrOptions);
  190. }
  191. // Add the articles
  192. if ($objArticles !== null)
  193. {
  194. $this->Template->articles = $this->parseArticles($objArticles);
  195. }
  196. $this->Template->headline = trim($this->headline);
  197. $this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
  198. $this->Template->empty = $GLOBALS['TL_LANG']['MSC']['empty'];
  199. }
  200. }
  201. class_alias(ModuleNewsArchive::class, 'ModuleNewsArchive');