[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/installation/src/Controller/ -> InstallationController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Installation
   5   * @subpackage  Controller
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\CMS\Installation\Controller;
  12  
  13  use Joomla\CMS\Application\CMSApplication;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  17  use Joomla\CMS\Session\Session;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Default controller class for the Joomla Installer.
  25   *
  26   * @since  3.1
  27   */
  28  class InstallationController extends JSONController
  29  {
  30      /**
  31       * @param   array                         $config   An optional associative array of configuration settings.
  32       *                                                  Recognized key values include 'name', 'default_task', 'model_path', and
  33       *                                                  'view_path' (this list is not meant to be comprehensive).
  34       * @param   MVCFactoryInterface|null      $factory  The factory.
  35       * @param   CMSApplication|null           $app      The Application for the dispatcher
  36       * @param   \Joomla\CMS\Input\Input|null  $input    The Input object.
  37       *
  38       * @since   3.0
  39       */
  40      public function __construct($config = [], MVCFactoryInterface $factory = null, $app = null, $input = null)
  41      {
  42          parent::__construct($config, $factory, $app, $input);
  43  
  44          $this->registerTask('populate1', 'populate');
  45          $this->registerTask('populate2', 'populate');
  46          $this->registerTask('populate3', 'populate');
  47          $this->registerTask('custom1', 'populate');
  48          $this->registerTask('custom2', 'populate');
  49          $this->registerTask('removeFolder', 'delete');
  50      }
  51  
  52      /**
  53       * Database check task.
  54       *
  55       * @return  void
  56       *
  57       * @since   4.0.0
  58       */
  59      public function dbcheck()
  60      {
  61          $this->checkValidToken();
  62  
  63          // Redirect to the page.
  64          $r = new \stdClass();
  65          $r->view = 'setup';
  66  
  67          // Check the form
  68          /** @var \Joomla\CMS\Installation\Model\SetupModel $model */
  69          $model = $this->getModel('Setup');
  70  
  71          if ($model->checkForm('setup') === false) {
  72              $this->app->enqueueMessage(Text::_('INSTL_DATABASE_VALIDATION_ERROR'), 'error');
  73              $r->validated = false;
  74              $this->sendJsonResponse($r);
  75  
  76              return;
  77          }
  78  
  79          $r->validated = $model->validateDbConnection();
  80  
  81          $this->sendJsonResponse($r);
  82      }
  83  
  84      /**
  85       * Create DB task.
  86       *
  87       * @return  void
  88       *
  89       * @since   4.0.0
  90       */
  91      public function create()
  92      {
  93          $this->checkValidToken();
  94  
  95          $r = new \stdClass();
  96  
  97          /** @var \Joomla\CMS\Installation\Model\DatabaseModel $databaseModel */
  98          $databaseModel = $this->getModel('Database');
  99  
 100          // Create Db
 101          try {
 102              $dbCreated = $databaseModel->createDatabase();
 103          } catch (\RuntimeException $e) {
 104              $this->app->enqueueMessage($e->getMessage(), 'error');
 105  
 106              $dbCreated = false;
 107          }
 108  
 109          if (!$dbCreated) {
 110              $r->view = 'setup';
 111          } else {
 112              if (!$databaseModel->handleOldDatabase()) {
 113                  $r->view = 'setup';
 114              }
 115          }
 116  
 117          $this->sendJsonResponse($r);
 118      }
 119  
 120      /**
 121       * Populate the database.
 122       *
 123       * @return  void
 124       *
 125       * @since   4.0.0
 126       */
 127      public function populate()
 128      {
 129          $this->checkValidToken();
 130          $step = $this->getTask();
 131          /** @var \Joomla\CMS\Installation\Model\DatabaseModel $model */
 132          $model = $this->getModel('Database');
 133  
 134          $r = new \stdClass();
 135          $db = $model->initialise();
 136          $files = [
 137              'populate1' => 'base',
 138              'populate2' => 'supports',
 139              'populate3' => 'extensions',
 140              'custom1' => 'localise',
 141              'custom2' => 'custom'
 142          ];
 143  
 144          $schema = $files[$step];
 145          $serverType = $db->getServerType();
 146  
 147          if (in_array($step, ['custom1', 'custom2']) && !is_file('sql/' . $serverType . '/' . $schema . '.sql')) {
 148              $this->sendJsonResponse($r);
 149  
 150              return;
 151          }
 152  
 153          if (!isset($files[$step])) {
 154              $r->view = 'setup';
 155              Factory::getApplication()->enqueueMessage(Text::_('INSTL_SAMPLE_DATA_NOT_FOUND'), 'error');
 156              $this->sendJsonResponse($r);
 157          }
 158  
 159          // Attempt to populate the database with the given file.
 160          if (!$model->createTables($schema)) {
 161              $r->view = 'setup';
 162          }
 163  
 164          $this->sendJsonResponse($r);
 165      }
 166  
 167      /**
 168       * Config task.
 169       *
 170       * @return  void
 171       *
 172       * @since   4.0.0
 173       */
 174      public function config()
 175      {
 176          $this->checkValidToken();
 177  
 178          /** @var \Joomla\CMS\Installation\Model\SetupModel $setUpModel */
 179          $setUpModel = $this->getModel('Setup');
 180  
 181          // Get the options from the session
 182          $options = $setUpModel->getOptions();
 183  
 184          $r = new \stdClass();
 185          $r->view = 'remove';
 186  
 187          /** @var \Joomla\CMS\Installation\Model\ConfigurationModel $configurationModel */
 188          $configurationModel = $this->getModel('Configuration');
 189  
 190          // Attempt to setup the configuration.
 191          if (!$configurationModel->setup($options)) {
 192              $r->view = 'setup';
 193          }
 194  
 195          $this->sendJsonResponse($r);
 196      }
 197  
 198      /**
 199       * Languages task.
 200       *
 201       * @return  void
 202       *
 203       * @since   4.0.0
 204       */
 205      public function languages()
 206      {
 207          $this->checkValidToken();
 208  
 209          // Get array of selected languages
 210          $lids = (array) $this->input->get('cid', [], 'int');
 211  
 212          // Remove zero values resulting from input filter
 213          $lids = array_filter($lids);
 214  
 215          if (empty($lids)) {
 216              // No languages have been selected
 217              $this->app->enqueueMessage(Text::_('INSTL_LANGUAGES_NO_LANGUAGE_SELECTED'), 'warning');
 218          } else {
 219              // Get the languages model.
 220              /** @var \Joomla\CMS\Installation\Model\LanguagesModel $model */
 221              $model = $this->getModel('Languages');
 222  
 223              // Install selected languages
 224              $model->install($lids);
 225          }
 226  
 227          // Redirect to the page.
 228          $r = new \stdClass();
 229          $r->view = 'remove';
 230  
 231          $this->sendJsonResponse($r);
 232      }
 233  
 234      /**
 235       * Delete installation folder task.
 236       *
 237       * @return  void
 238       *
 239       * @since   4.0.0
 240       */
 241      public function delete()
 242      {
 243          $this->checkValidToken();
 244  
 245          /** @var \Joomla\CMS\Installation\Model\CleanupModel $model */
 246          $model = $this->getModel('Cleanup');
 247  
 248          if (!$model->deleteInstallationFolder()) {
 249              // We can't send a response with sendJsonResponse because our installation classes might not now exist
 250              $error = [
 251                  'token' => Session::getFormToken(true),
 252                  'error' => true,
 253                  'data' => [
 254                      'view' => 'remove'
 255                  ],
 256                  'messages' => [
 257                      'warning' => [
 258                          Text::sprintf('INSTL_COMPLETE_ERROR_FOLDER_DELETE', 'installation')
 259                      ]
 260                  ]
 261              ];
 262  
 263              echo json_encode($error);
 264  
 265              return;
 266          }
 267  
 268          $this->app->getSession()->destroy();
 269  
 270          // We can't send a response with sendJsonResponse because our installation classes now do not exist
 271          echo json_encode(['error' => false]);
 272      }
 273  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer