File size: 12,894 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php

namespace Mautic\InstallBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Mautic\CoreBundle\Configurator\Configurator;
use Mautic\CoreBundle\Controller\CommonController;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Loader\ParameterLoader;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Service\FlashBag;
use Mautic\CoreBundle\Translation\Translator;
use Mautic\InstallBundle\Install\InstallService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class InstallController extends CommonController
{
    public function __construct(
        private Configurator $configurator,
        private InstallService $installer,
        ManagerRegistry $doctrine,
        MauticFactory $factory,
        ModelFactory $modelFactory,
        UserHelper $userHelper,
        CoreParametersHelper $coreParametersHelper,
        EventDispatcherInterface $dispatcher,
        Translator $translator,
        FlashBag $flashBag,
        RequestStack $requestStack,
        CorePermissions $security
    ) {
        parent::__construct($doctrine, $factory, $modelFactory, $userHelper, $coreParametersHelper, $dispatcher, $translator, $flashBag, $requestStack, $security);
    }

    /**
     * Controller action for install steps.
     *
     * @param int $index The step number to process
     *
     * @return JsonResponse|Response
     *
     * @throws \Doctrine\DBAL\Exception
     */
    public function stepAction(Request $request, EntityManagerInterface $entityManager, PathsHelper $pathsHelper, float $index = 0)
    {
        // We're going to assume a bit here; if the config file exists already and DB info is provided, assume the app
        // is installed and redirect
        if ($this->installer->checkIfInstalled()) {
            return $this->redirectToRoute('mautic_dashboard_index');
        }

        if ($index - floor($index) > 0) {
            $subIndex = (int) (round($index - floor($index), 1) * 10);
            $index    = floor($index);
        }
        $index = (int) $index;

        $params = $this->configurator->getParameters();

        $session        = $request->getSession();
        $completedSteps = $session->get('mautic.installer.completedsteps', []);

        // Check to ensure the installer is in the right place
        if ((empty($params) || empty($params['db_driver'])) && $index > 1) {
            $session->set('mautic.installer.completedsteps', [0]);

            return $this->redirectToRoute('mautic_installer_step', ['index' => 1]);
        }

        /** @var \Mautic\CoreBundle\Configurator\Step\StepInterface $step */
        $step   = $this->configurator->getStep($index)[0];
        $action = $this->generateUrl('mautic_installer_step', ['index' => $index]);

        $form = $this->createForm($step->getFormType(), $step, ['action' => $action]);
        $tmpl = $request->isXmlHttpRequest() ? $request->get('tmpl', 'index') : 'index';

        // Note if this step is complete
        $complete = false;

        if ('POST' === $request->getMethod()) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                // Post-step processing
                $formData = $form->getData();

                switch ($index) {
                    case InstallService::CHECK_STEP:
                        $complete = true;

                        break;
                    case InstallService::DOCTRINE_STEP:
                        // password field does not retain configured defaults
                        if (empty($formData->password) && !empty($params['db_password'])) {
                            $formData->password = $params['db_password'];
                        }
                        $dbParams = (array) $formData;
                        $messages = $this->installer->createDatabaseStep($step, $dbParams);
                        if (!empty($messages)) {
                            $this->handleInstallerErrors($form, $messages);
                            break;
                        }

                        /**
                         * We need to clear the ORM metadata cache before creating the schema. If the user provided a database
                         * table prefix in the UI installer, cached table names don't have the prefix yet (e.g. oauth2_clients).
                         * After clearing the metadata cache, Doctrine automatically recreates it with the correct prefixes (e.g.
                         * mau_oauth2_clients), if applicable.
                         */
                        $entityManager->getConfiguration()->getMetadataCache()->clear();

                        // Refresh to install schema with new connection information in the container
                        return $this->redirectToRoute('mautic_installer_step', ['index' => 1.1]);
                    case InstallService::USER_STEP:
                        $adminParam = (array) $formData;
                        $messages   = $this->installer->createAdminUserStep($adminParam);

                        if (!empty($messages)) {
                            $this->handleInstallerErrors($form, $messages);
                            break;
                        }

                        // Store the data to repopulate the form
                        unset($formData->password);
                        $session->set('mautic.installer.user', $formData);

                        $complete = true;
                        break;
                }
            }
        } elseif (!empty($subIndex)) {
            switch ($index) {
                case InstallService::DOCTRINE_STEP:
                    $dbParams = (array) $step;

                    switch ($subIndex) {
                        case 1:
                            $messages = $this->installer->createSchemaStep($dbParams);
                            if (!empty($messages)) {
                                $this->handleInstallerErrors($form, $messages);

                                return $this->redirectToRoute('mautic_installer_step', ['index' => 1]);
                            }

                            return $this->redirectToRoute('mautic_installer_step', ['index' => 1.2]);
                        case 2:
                            $messages = $this->installer->createFixturesStep();
                            if (!empty($messages)) {
                                $this->handleInstallerErrors($form, $messages);

                                return $this->redirectToRoute('mautic_installer_step', ['index' => 1]);
                            }

                            $complete = true;
                            break;
                    }
                    break;
            }
        }

        if ($complete) {
            $completedSteps[] = $index;
            $session->set('mautic.installer.completedsteps', $completedSteps);
            ++$index;

            if ($index < $this->configurator->getStepCount()) {
                // On to the next step

                return $this->redirectToRoute('mautic_installer_step', ['index' => (int) $index]);
            } else {
                $siteUrl  = $request->getSchemeAndHttpHost().$request->getBaseUrl();
                $messages = $this->installer->createFinalConfigStep($siteUrl);

                if (!empty($messages)) {
                    $this->handleInstallerErrors($form, $messages);
                }

                return $this->postActionRedirect(
                    [
                        'viewParameters'    => [
                            'welcome_url' => $this->generateUrl('mautic_dashboard_index'),
                            'parameters'  => $this->configurator->render(),
                            'version'     => MAUTIC_VERSION,
                            'tmpl'        => $tmpl,
                        ],
                        'returnUrl'         => $this->generateUrl('mautic_installer_final'),
                        'contentTemplate'   => '@MauticInstall/Install/final.html.twig',
                        'forwardController' => false,
                    ]
                );
            }
        } else {
            // Redirect back to last step if the user advanced ahead via the URL
            $last = (int) end($completedSteps) + 1;
            if ($index && $index > $last) {
                return $this->redirectToRoute('mautic_installer_step', ['index' => $last]);
            }
        }

        return $this->delegateView(
            [
                'viewParameters'  => [
                    'form'           => $form->createView(),
                    'index'          => $index,
                    'count'          => $this->configurator->getStepCount(),
                    'version'        => MAUTIC_VERSION,
                    'tmpl'           => $tmpl,
                    'majors'         => $this->configurator->getRequirements(),
                    'minors'         => $this->configurator->getOptionalSettings(),
                    'appRoot'        => $this->coreParametersHelper->get('mautic.application_dir').'/app',
                    'cacheDir'       => $this->coreParametersHelper->get('kernel.cache_dir'),
                    'logDir'         => $this->coreParametersHelper->get('kernel.logs_dir'),
                    'configFile'     => ParameterLoader::getLocalConfigFile($pathsHelper->getSystemPath('root').'/app'),
                    'completedSteps' => $completedSteps,
                ],
                'contentTemplate' => $step->getTemplate(),
                'passthroughVars' => [
                    'route' => $this->generateUrl('mautic_installer_step', ['index' => $index]),
                ],
            ]
        );
    }

    /**
     * Controller action for the final step.
     *
     * @return JsonResponse|Response
     *
     * @throws \Exception
     */
    public function finalAction(Request $request, PathsHelper $pathsHelper): \Symfony\Component\HttpFoundation\RedirectResponse|Response
    {
        $session = $request->getSession();

        // We're going to assume a bit here; if the config file exists already and DB info is provided, assume the app is installed and redirect
        if ($this->installer->checkIfInstalled()) {
            if (!$session->has('mautic.installer.completedsteps')) {
                // Arrived here by directly browsing to URL so redirect to the dashboard

                return $this->redirectToRoute('mautic_dashboard_index');
            }
        } else {
            // Shouldn't have made it to this step without having a successful install
            return $this->redirectToRoute('mautic_installer_home');
        }

        // Remove installer session variables
        $session->remove('mautic.installer.completedsteps');
        $session->remove('mautic.installer.user');

        $this->installer->finalMigrationStep();

        $welcomeUrl = $this->generateUrl('mautic_dashboard_index');

        $tmpl = $request->isXmlHttpRequest() ? $request->get('tmpl', 'index') : 'index';

        return $this->delegateView(
            [
                'viewParameters' => [
                    'welcome_url' => $welcomeUrl,
                    'parameters'  => $this->configurator->render(),
                    'config_path' => ParameterLoader::getLocalConfigFile($pathsHelper->getSystemPath('root').'/app'),
                    'is_writable' => $this->configurator->isFileWritable(),
                    'version'     => MAUTIC_VERSION,
                    'tmpl'        => $tmpl,
                ],
                'contentTemplate' => '@MauticInstall/Install/final.html.twig',
                'passthroughVars' => [
                    'activeLink'    => '#mautic_installer_index',
                    'mauticContent' => 'installer',
                    'route'         => $this->generateUrl('mautic_installer_final'),
                ],
            ]
        );
    }

    /**
     * Handle installer errors.
     */
    private function handleInstallerErrors(Form $form, array $messages): void
    {
        foreach ($messages as $type => $message) {
            match ($type) {
                'warning', 'error', 'notice' => $this->addFlashMessage($message, [], $type),
                default => $form[$type]->addError(new FormError($message)),
            };
        }
    }
}