forked from contao/contao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleFaqPage.php
170 lines (137 loc) · 4.63 KB
/
ModuleFaqPage.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
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Image\Studio\LegacyFigureBuilderTrait;
use Patchwork\Utf8;
/**
* Class ModuleFaqPage
*
* @property array $faq_categories
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class ModuleFaqPage extends Module
{
use LegacyFigureBuilderTrait;
/**
* Template
* @var string
*/
protected $strTemplate = 'mod_faqpage';
/**
* Display a wildcard in the back end
*
* @return string
*/
public function generate()
{
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### ' . Utf8::strtoupper($GLOBALS['TL_LANG']['FMD']['faqpage'][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();
}
$this->faq_categories = StringUtil::deserialize($this->faq_categories);
// Return if there are no categories
if (empty($this->faq_categories) || !\is_array($this->faq_categories))
{
return '';
}
// Tag the FAQ categories (see #2137)
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags(array_map(static function ($id) { return 'contao.db.tl_faq_category.' . $id; }, $this->faq_categories));
}
return parent::generate();
}
/**
* Generate the module
*/
protected function compile()
{
$objFaqs = FaqModel::findPublishedByPids($this->faq_categories);
if ($objFaqs === null)
{
$this->Template->faq = array();
return;
}
/** @var PageModel $objPage */
global $objPage;
$tags = array();
$arrFaqs = array_fill_keys($this->faq_categories, array());
// Add FAQs
foreach ($objFaqs as $objFaq)
{
/** @var FaqModel $objFaq */
$objTemp = (object) $objFaq->row();
// Clean the RTE output
$objTemp->answer = StringUtil::toHtml5($objFaq->answer);
$objTemp->answer = StringUtil::encodeEmail($objTemp->answer);
$objTemp->addImage = false;
$objTemp->addBefore = false;
// Add an image
if ($objFaq->addImage && null !== ($figureBuilder = $this->getFigureBuilderIfResourceExists($objFaq->singleSRC)))
{
$figureBuilder
->setSize($objFaq->size)
->setMetadata($objFaq->getOverwriteMetadata())
->setLightboxGroupIdentifier('lightbox[' . substr(md5('mod_faqpage_' . $objFaq->id), 0, 6) . ']')
->enableLightbox((bool) $objFaq->fullsize)
->build()
->applyLegacyTemplateData($objTemp, $objFaq->imagemargin, $objFaq->floating);
}
$objTemp->enclosure = array();
// Add enclosure
if ($objFaq->addEnclosure)
{
$this->addEnclosuresToTemplate($objTemp, $objFaq->row());
}
/** @var UserModel $objAuthor */
$objAuthor = $objFaq->getRelated('author');
$objTemp->info = sprintf($GLOBALS['TL_LANG']['MSC']['faqCreatedBy'], Date::parse($objPage->dateFormat, $objFaq->tstamp), $objAuthor->name);
/** @var FaqCategoryModel $objPid */
$objPid = $objFaq->getRelated('pid');
// Order by PID
$arrFaqs[$objFaq->pid]['items'][] = $objTemp;
$arrFaqs[$objFaq->pid]['headline'] = $objPid->headline;
$arrFaqs[$objFaq->pid]['title'] = $objPid->title;
$tags[] = 'contao.db.tl_faq.' . $objFaq->id;
}
// Tag the FAQs (see #2137)
if (System::getContainer()->has('fos_http_cache.http.symfony_response_tagger'))
{
$responseTagger = System::getContainer()->get('fos_http_cache.http.symfony_response_tagger');
$responseTagger->addTags($tags);
}
$arrFaqs = array_values(array_filter($arrFaqs));
$limit_i = \count($arrFaqs) - 1;
// Add classes first, last, even and odd
for ($i=0; $i<=$limit_i; $i++)
{
$class = (($i == 0) ? 'first ' : '') . (($i == $limit_i) ? 'last ' : '') . (($i%2 == 0) ? 'even' : 'odd');
$arrFaqs[$i]['class'] = trim($class);
$limit_j = \count($arrFaqs[$i]['items']) - 1;
for ($j=0; $j<=$limit_j; $j++)
{
$class = (($j == 0) ? 'first ' : '') . (($j == $limit_j) ? 'last ' : '') . (($j%2 == 0) ? 'even' : 'odd');
$arrFaqs[$i]['items'][$j]->class = trim($class);
}
}
$this->Template->faq = $arrFaqs;
$this->Template->request = Environment::get('indexFreeRequest');
$this->Template->topLink = $GLOBALS['TL_LANG']['MSC']['backToTop'];
}
}
class_alias(ModuleFaqPage::class, 'ModuleFaqPage');