/
ModuleNewsReader.php
211 lines (173 loc) · 5.67 KB
/
ModuleNewsReader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\CoreBundle\Exception\RedirectResponseException;
use Patchwork\Utf8;
/**
* Front end module "news reader".
*
* @property Comments $Comments
* @property string $com_template
* @property array $news_archives
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ModuleNewsReader extends ModuleNews
{
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_newsreader';
/**
* Display a wildcard in the back end
*
* @throws InternalServerErrorException
*
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['newsreader'][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
// Set the item from the auto_item parameter
if (!isset($_GET['items']) && isset($_GET['auto_item']) && Config::get('useAutoItem'))
{
Input::setGet('items', Input::get('auto_item'));
}
// Return an empty string if "items" is not set (to combine list and reader on same page)
if (!Input::get('items'))
{
return '';
}
$this->news_archives = $this->sortOutProtected(StringUtil::deserialize($this->news_archives));
if (empty($this->news_archives) || !\is_array($this->news_archives))
{
throw new InternalServerErrorException('The news reader ID ' . $this->id . ' has no archives specified.', $this->id);
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
/** @var PageModel $objPage */
global $objPage;
$this->Template->articles = '';
$this->Template->referer = 'javascript:history.go(-1)';
$this->Template->back = $GLOBALS['TL_LANG']['MSC']['goBack'];
// Get the news item
$objArticle = NewsModel::findPublishedByParentAndIdOrAlias(Input::get('items'), $this->news_archives);
// The news item does not exist (see #33)
if ($objArticle === null)
{
throw new PageNotFoundException('Page not found: ' . Environment::get('uri'));
}
// Redirect if the news item has a target URL (see #1498)
switch ($objArticle->source) {
case 'internal':
if ($page = PageModel::findPublishedById($objArticle->jumpTo))
{
throw new RedirectResponseException($page->getAbsoluteUrl(), 301);
}
throw new InternalServerErrorException('Invalid "jumpTo" value or target page not public');
break;
case 'article':
if (($article = ArticleModel::findByPk($objArticle->articleId)) && ($page = PageModel::findPublishedById($article->pid)))
{
throw new RedirectResponseException($page->getAbsoluteUrl('/articles/' . ($article->alias ?: $article->id)), 301);
}
throw new InternalServerErrorException('Invalid "articleId" value or target page not public');
break;
case 'external':
if ($objArticle->url)
{
throw new RedirectResponseException($objArticle->url, 301);
}
throw new InternalServerErrorException('Empty target URL');
break;
}
// Set the default template
if (!$this->news_template)
{
$this->news_template = 'news_full';
}
$arrArticle = $this->parseArticle($objArticle);
$this->Template->articles = $arrArticle;
// Overwrite the page title (see #2853, #4955 and #87)
if ($objArticle->pageTitle)
{
$objPage->pageTitle = $objArticle->pageTitle;
}
elseif ($objArticle->headline)
{
$objPage->pageTitle = strip_tags(StringUtil::stripInsertTags($objArticle->headline));
}
// Overwrite the page description
if ($objArticle->description)
{
$objPage->description = $objArticle->description;
}
elseif ($objArticle->teaser)
{
$objPage->description = $this->prepareMetaDescription($objArticle->teaser);
}
$bundles = System::getContainer()->getParameter('kernel.bundles');
// HOOK: comments extension required
if ($objArticle->noComments || !isset($bundles['ContaoCommentsBundle']))
{
$this->Template->allowComments = false;
return;
}
/** @var NewsArchiveModel $objArchive */
$objArchive = $objArticle->getRelated('pid');
$this->Template->allowComments = $objArchive->allowComments;
// Comments are not allowed
if (!$objArchive->allowComments)
{
return;
}
// Adjust the comments headline level
$intHl = min((int) str_replace('h', '', $this->hl), 5);
$this->Template->hlc = 'h' . ($intHl + 1);
$this->import(Comments::class, 'Comments');
$arrNotifies = array();
// Notify the system administrator
if ($objArchive->notify != 'notify_author')
{
$arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL'];
}
/** @var UserModel $objAuthor */
if ($objArchive->notify != 'notify_admin' && ($objAuthor = $objArticle->getRelated('author')) instanceof UserModel && $objAuthor->email != '')
{
$arrNotifies[] = $objAuthor->email;
}
$objConfig = new \stdClass();
$objConfig->perPage = $objArchive->perPage;
$objConfig->order = $objArchive->sortOrder;
$objConfig->template = $this->com_template;
$objConfig->requireLogin = $objArchive->requireLogin;
$objConfig->disableCaptcha = $objArchive->disableCaptcha;
$objConfig->bbcode = $objArchive->bbcode;
$objConfig->moderate = $objArchive->moderate;
$this->Comments->addCommentsToTemplate($this->Template, $objConfig, 'tl_news', $objArticle->id, $arrNotifies);
}
}
class_alias(ModuleNewsReader::class, 'ModuleNewsReader');