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: 
<?php
namespace Omeka\Controller;

use Omeka\Form\InstallationForm;
use Omeka\Installation\Installer;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class InstallController extends AbstractActionController
{
    /**
     * @var Installer
     */
    protected $installer;

    /**
     * @param Installer $installer
     */
    public function __construct(Installer $installer)
    {
        $this->installer = $installer;
    }

    public function indexAction()
    {
        if ($this->status()->isInstalled()) {
            return $this->redirect()->toRoute('admin');
        }

        $form = $this->getForm(InstallationForm::class);
        $view = new ViewModel;

        if ($this->getRequest()->isPost()) {
            $form->setData($this->getRequest()->getPost());
            if ($form->isValid()) {
                $data = $form->getData();
                $this->installer->registerVars(
                    'Omeka\Installation\Task\CreateFirstUserTask',
                    $data['user']
                );
                $this->installer->registerVars(
                    'Omeka\Installation\Task\AddDefaultSettingsTask',
                    [
                        'administrator_email' => $data['user']['email'],
                        'installation_title' => $data['settings']['installation_title'],
                        'time_zone' => $data['settings']['time_zone'],
                        'locale' => $data['settings']['locale'],
                    ]
                );
                if ($this->installer->install()) {
                    // Success. Redirect to login.
                    $this->messenger()->addSuccess('Installation successful. Please log in.'); // @translate
                    return $this->redirect()->toRoute('login');
                } else {
                    // Error during installation.
                    $this->messenger()->addError('There were errors during installation.'); // @translate
                    foreach ($this->installer->getErrors() as $error) {
                        $this->messenger()->addError($error);
                    }
                }
            } else {
                $this->messenger()->addFormErrors($form);
            }
        } else {
            if (!$this->installer->preInstall()) {
                $view->setVariable('preErrors', $this->installer->getErrors());
            }
        }

        $view->setVariable('form', $form);
        return $view;
    }
}