Issues (281)

Branch: master

src/Backend/Modules/Faq/Installer/Installer.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\Faq\Installer;
4
5
use Backend\Core\Installer\ModuleInstaller;
6
use Common\ModuleExtraType;
7
8
/**
9
 * Installer for the faq module
10
 */
11
class Installer extends ModuleInstaller
12
{
13
    /** @var int */
14
    private $defaultCategoryId;
15
16
    /** @var int */
17
    private $faqBlockId;
18
19
    public function install(): void
20
    {
21
        $this->addModule('Faq');
22
        $this->makeSearchable($this->getModule());
23
        $this->importSQL(__DIR__ . '/Data/install.sql');
24
        $this->importLocale(__DIR__ . '/Data/locale.xml');
25
        $this->configureSettings();
26
        $this->configureBackendNavigation();
27
        $this->configureBackendRights();
28
        $this->configureBackendWidgets();
29
        $this->configureFrontendExtras();
30
        $this->configureFrontendPages();
31
    }
32
33
    private function configureBackendActionRightsForFaqCategory(): void
34
    {
35
        $this->setActionRights(1, $this->getModule(), 'AddCategory');
36
        $this->setActionRights(1, $this->getModule(), 'Categories');
37
        $this->setActionRights(1, $this->getModule(), 'DeleteCategory');
38
        $this->setActionRights(1, $this->getModule(), 'EditCategory');
39
        $this->setActionRights(1, $this->getModule(), 'Sequence'); // AJAX
40
    }
41
42
    private function configureBackendActionRightsForFaqQuestion(): void
43
    {
44
        $this->setActionRights(1, $this->getModule(), 'Add');
45
        $this->setActionRights(1, $this->getModule(), 'Delete');
46
        $this->setActionRights(1, $this->getModule(), 'Edit');
47
        $this->setActionRights(1, $this->getModule(), 'Index');
48
        $this->setActionRights(1, $this->getModule(), 'SequenceQuestions'); // AJAX
49
    }
50
51
    private function configureBackendActionRightsForFaqQuestionFeedback(): void
52
    {
53
        $this->setActionRights(1, $this->getModule(), 'DeleteFeedback');
54
    }
55
56
    private function configureBackendNavigation(): void
57
    {
58
        // Set navigation for "Modules"
59
        $navigationModulesId = $this->setNavigation(null, 'Modules');
60
        $navigationFaqId = $this->setNavigation($navigationModulesId, $this->getModule());
61
        $this->setNavigation(
62
            $navigationFaqId,
63
            'Questions',
64
            'faq/index',
65
            ['faq/add', 'faq/edit']
66
        );
67
        $this->setNavigation(
68
            $navigationFaqId,
69
            'Categories',
70
            'faq/categories',
71
            ['faq/add_category', 'faq/edit_category']
72
        );
73
74
        // Set navigation for "Settings"
75
        $navigationSettingsId = $this->setNavigation(null, 'Settings');
76
        $navigationModulesId = $this->setNavigation($navigationSettingsId, 'Modules');
77
        $this->setNavigation($navigationModulesId, $this->getModule(), 'faq/settings');
78
    }
79
80
    private function configureBackendRights(): void
81
    {
82
        $this->setModuleRights(1, $this->getModule());
83
84
        // Configure backend rights for entities
85
        $this->configureBackendActionRightsForFaqCategory();
86
        $this->configureBackendActionRightsForFaqQuestion();
87
        $this->configureBackendActionRightsForFaqQuestionFeedback();
88
89
        $this->setActionRights(1, $this->getModule(), 'Settings');
90
    }
91
92
    private function configureBackendWidgets(): void
93
    {
94
        $this->insertDashboardWidget($this->getModule(), 'Feedback');
95
    }
96
97
    /**
98
     * Configure frontend extras
99
     * Note: Category faq widgets will be added on the fly
100
     */
101
    private function configureFrontendExtras(): void
102
    {
103
        $this->faqBlockId = $this->insertExtra($this->getModule(), ModuleExtraType::block(), $this->getModule());
104
        $this->insertExtra($this->getModule(), ModuleExtraType::widget(), 'MostReadQuestions', 'MostReadQuestions');
105
        $this->insertExtra($this->getModule(), ModuleExtraType::widget(), 'AskOwnQuestion', 'AskOwnQuestion');
106
        $this->insertExtra($this->getModule(), ModuleExtraType::widget(), 'Categories', 'Categories');
107
    }
108
109
    private function configureFrontendPages(): void
110
    {
111
        foreach ($this->getLanguages() as $language) {
112
            $this->defaultCategoryId = $this->getDefaultCategoryIdForLanguage($language);
113
114
            // no category exists
115
            if ($this->defaultCategoryId === 0) {
116
                $this->defaultCategoryId = $this->insertCategory($language, 'Default', 'default');
117
            }
118
119
            // check if a page for the faq already exists in this language
120
            $faqPageExists = (bool) $this->getDatabase()->getVar(
121
                'SELECT 1
122
                 FROM pages AS p
123
                 INNER JOIN pages_blocks AS b ON b.revision_id = p.revision_id
124
                 WHERE b.extra_id = ? AND p.language = ?
125
                 LIMIT 1',
126
                [$this->faqBlockId, $language]
127
            );
128
129
            if (!$faqPageExists) {
130
                // insert page
131
                $this->insertPage(
132
                    [
133
                        'title' => 'FAQ',
134
                        'language' => $language,
135
                    ],
136
                    null,
137
                    ['extra_id' => $this->faqBlockId]
138
                );
139
            }
140
        }
141
    }
142
143
    private function configureSettings(): void
144
    {
145
        $this->setSetting($this->getModule(), 'allow_feedback', false);
146
        $this->setSetting($this->getModule(), 'allow_multiple_categories', true);
147
        $this->setSetting($this->getModule(), 'allow_own_question', false);
148
        $this->setSetting($this->getModule(), 'most_read_num_items', 5);
149
        $this->setSetting($this->getModule(), 'overview_num_items_per_category', 10);
150
        $this->setSetting($this->getModule(), 'related_num_items', 5);
151
        $this->setSetting($this->getModule(), 'send_email_on_new_feedback', false);
152
        $this->setSetting($this->getModule(), 'spamfilter', false);
153
    }
154
155
    private function getDefaultCategoryIdForLanguage(string $language): int
156
    {
157
        return (int) $this->getDatabase()->getVar(
158
            'SELECT id
159
             FROM faq_categories
160
             WHERE language = ?',
161
            [$language]
162
        );
163
    }
164
165
    /**
166
     * @todo: When FAQ entities are available, use DataFixtures instead of this method.
167
     *
168
     * @param string $language
169
     * @param string $title
170
     * @param string $url
171
     *
172
     * @return int
173
     */
174
    private function insertCategory(string $language, string $title, string $url): int
175
    {
176
        $database = $this->getDatabase();
177
178
        // get sequence for widget
179
        $sequenceExtra = $database->getVar(
180
            'SELECT MAX(i.sequence) + 1
181
             FROM modules_extras AS i
182
             WHERE i.module = ?',
183
            ['faq']
184
        );
185
186
        // build array
187
        $item = [];
188
        $item['meta_id'] = $this->insertMeta($title, $title, $title, $url);
189
        $item['extra_id'] = $this->insertExtra(
190
            $this->getModule(),
191
            ModuleExtraType::widget(),
192
            $this->getModule(),
193
            'CategoryList',
194
            null,
195
            false,
196
            $sequenceExtra
0 ignored issues
show
$sequenceExtra of type string is incompatible with the type integer|null expected by parameter $sequence of Backend\Core\Installer\M...nstaller::insertExtra(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
            /** @scrutinizer ignore-type */ $sequenceExtra
Loading history...
197
        );
198
        $item['language'] = $language;
199
        $item['title'] = $title;
200
        $item['sequence'] = 1;
201
202
        // insert category
203
        $item['id'] = (int) $database->insert('faq_categories', $item);
204
205
        // build data for widget
206
        $extra = [
207
            'data' => serialize(
208
                [
209
                    'id' => $item['id'],
210
                    'extra_label' => 'Category: ' . $item['title'],
211
                    'language' => $item['language'],
212
                    'edit_url' => '/private/' . $language . '/faq/edit_category?id=' . $item['id'],
213
                ]
214
            ),
215
        ];
216
217
        // update widget
218
        $database->update(
219
            'modules_extras',
220
            $extra,
221
            'id = ?',
222
            [$item['extra_id']]
223
        );
224
225
        return $item['id'];
226
    }
227
}
228