c28b7f8fd4947fbb99452d05dbb205edafcbc083
[yaffs-website] / web / core / lib / Drupal / Core / Installer / Form / SiteConfigureForm.php
1 <?php
2
3 namespace Drupal\Core\Installer\Form;
4
5 use Drupal\Core\Extension\ModuleInstallerInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Locale\CountryManagerInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\user\UserStorageInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides the site configuration form.
15  */
16 class SiteConfigureForm extends ConfigFormBase {
17
18   /**
19    * The site path.
20    *
21    * @var string
22    */
23   protected $sitePath;
24
25   /**
26    * The user storage.
27    *
28    * @var \Drupal\user\UserStorageInterface
29    */
30   protected $userStorage;
31
32   /**
33    * The state service.
34    *
35    * @var \Drupal\Core\State\StateInterface
36    */
37   protected $state;
38
39   /**
40    * The module installer.
41    *
42    * @var \Drupal\Core\Extension\ModuleInstallerInterface
43    */
44   protected $moduleInstaller;
45
46   /**
47    * The country manager.
48    *
49    * @var \Drupal\Core\Locale\CountryManagerInterface
50    */
51   protected $countryManager;
52
53   /**
54    * The app root.
55    *
56    * @var string
57    */
58   protected $root;
59
60   /**
61    * Constructs a new SiteConfigureForm.
62    *
63    * @param string $root
64    *   The app root.
65    * @param string $site_path
66    *   The site path.
67    * @param \Drupal\user\UserStorageInterface $user_storage
68    *   The user storage.
69    * @param \Drupal\Core\State\StateInterface $state
70    *   The state service.
71    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
72    *   The module installer.
73    * @param \Drupal\Core\Locale\CountryManagerInterface $country_manager
74    *   The country manager.
75    */
76   public function __construct($root, $site_path, UserStorageInterface $user_storage, StateInterface $state, ModuleInstallerInterface $module_installer, CountryManagerInterface $country_manager) {
77     $this->root = $root;
78     $this->sitePath = $site_path;
79     $this->userStorage = $user_storage;
80     $this->state = $state;
81     $this->moduleInstaller = $module_installer;
82     $this->countryManager = $country_manager;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public static function create(ContainerInterface $container) {
89     return new static(
90       $container->get('app.root'),
91       $container->get('site.path'),
92       $container->get('entity.manager')->getStorage('user'),
93       $container->get('state'),
94       $container->get('module_installer'),
95       $container->get('country_manager')
96     );
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getFormId() {
103     return 'install_configure_form';
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   protected function getEditableConfigNames() {
110     return [
111       'system.date',
112       'system.site',
113       'update.settings',
114     ];
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function buildForm(array $form, FormStateInterface $form_state) {
121     $form['#title'] = $this->t('Configure site');
122
123     // Warn about settings.php permissions risk
124     $settings_dir = $this->sitePath;
125     $settings_file = $settings_dir . '/settings.php';
126     // Check that $_POST is empty so we only show this message when the form is
127     // first displayed, not on the next page after it is submitted. (We do not
128     // want to repeat it multiple times because it is a general warning that is
129     // not related to the rest of the installation process; it would also be
130     // especially out of place on the last page of the installer, where it would
131     // distract from the message that the Drupal installation has completed
132     // successfully.)
133     $post_params = $this->getRequest()->request->all();
134     if (empty($post_params) && (!drupal_verify_install_file($this->root . '/' . $settings_file, FILE_EXIST | FILE_READABLE | FILE_NOT_WRITABLE) || !drupal_verify_install_file($this->root . '/' . $settings_dir, FILE_NOT_WRITABLE, 'dir'))) {
135       drupal_set_message(t('All necessary changes to %dir and %file have been made, so you should remove write permissions to them now in order to avoid security risks. If you are unsure how to do so, consult the <a href=":handbook_url">online handbook</a>.', ['%dir' => $settings_dir, '%file' => $settings_file, ':handbook_url' => 'https://www.drupal.org/server-permissions']), 'warning');
136     }
137
138     $form['#attached']['library'][] = 'system/drupal.system';
139     // Add JavaScript time zone detection.
140     $form['#attached']['library'][] = 'core/drupal.timezone';
141     // We add these strings as settings because JavaScript translation does not
142     // work during installation.
143     $form['#attached']['drupalSettings']['copyFieldValue']['edit-site-mail'] = ['edit-account-mail'];
144
145     $form['site_information'] = [
146       '#type' => 'fieldgroup',
147       '#title' => $this->t('Site information'),
148     ];
149     $form['site_information']['site_name'] = [
150       '#type' => 'textfield',
151       '#title' => $this->t('Site name'),
152       '#required' => TRUE,
153       '#weight' => -20,
154     ];
155     $form['site_information']['site_mail'] = [
156       '#type' => 'email',
157       '#title' => $this->t('Site email address'),
158       '#default_value' => ini_get('sendmail_from'),
159       '#description' => $this->t("Automated emails, such as registration information, will be sent from this address. Use an address ending in your site's domain to help prevent these emails from being flagged as spam."),
160       '#required' => TRUE,
161       '#weight' => -15,
162     ];
163
164     $form['admin_account'] = [
165       '#type' => 'fieldgroup',
166       '#title' => $this->t('Site maintenance account'),
167     ];
168     $form['admin_account']['account']['name'] = [
169       '#type' => 'textfield',
170       '#title' => $this->t('Username'),
171       '#maxlength' => USERNAME_MAX_LENGTH,
172       '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
173       '#required' => TRUE,
174       '#attributes' => ['class' => ['username']],
175     ];
176     $form['admin_account']['account']['pass'] = [
177       '#type' => 'password_confirm',
178       '#required' => TRUE,
179       '#size' => 25,
180     ];
181     $form['admin_account']['account']['#tree'] = TRUE;
182     $form['admin_account']['account']['mail'] = [
183       '#type' => 'email',
184       '#title' => $this->t('Email address'),
185       '#required' => TRUE,
186     ];
187
188     $form['regional_settings'] = [
189       '#type' => 'fieldgroup',
190       '#title' => $this->t('Regional settings'),
191     ];
192     $countries = $this->countryManager->getList();
193     $form['regional_settings']['site_default_country'] = [
194       '#type' => 'select',
195       '#title' => $this->t('Default country'),
196       '#empty_value' => '',
197       '#default_value' => $this->config('system.date')->get('country.default'),
198       '#options' => $countries,
199       '#description' => $this->t('Select the default country for the site.'),
200       '#weight' => 0,
201     ];
202     $form['regional_settings']['date_default_timezone'] = [
203       '#type' => 'select',
204       '#title' => $this->t('Default time zone'),
205       // Use system timezone if set, but avoid throwing a warning in PHP >=5.4
206       '#default_value' => @date_default_timezone_get(),
207       '#options' => system_time_zones(),
208       '#description' => $this->t('By default, dates in this site will be displayed in the chosen time zone.'),
209       '#weight' => 5,
210       '#attributes' => ['class' => ['timezone-detect']],
211     ];
212
213     $form['update_notifications'] = [
214       '#type' => 'fieldgroup',
215       '#title' => $this->t('Update notifications'),
216       '#description' => $this->t('The system will notify you when updates and important security releases are available for installed components. Anonymous information about your site is sent to <a href=":drupal">Drupal.org</a>.', [':drupal' => 'https://www.drupal.org']),
217     ];
218     $form['update_notifications']['enable_update_status_module'] = [
219       '#type' => 'checkbox',
220       '#title' => $this->t('Check for updates automatically'),
221       '#default_value' => 1,
222     ];
223     $form['update_notifications']['enable_update_status_emails'] = [
224       '#type' => 'checkbox',
225       '#title' => $this->t('Receive email notifications'),
226       '#default_value' => 1,
227       '#states' => [
228         'visible' => [
229           'input[name="enable_update_status_module"]' => ['checked' => TRUE],
230         ],
231       ],
232     ];
233
234     $form['actions'] = ['#type' => 'actions'];
235     $form['actions']['submit'] = [
236       '#type' => 'submit',
237       '#value' => $this->t('Save and continue'),
238       '#weight' => 15,
239       '#button_type' => 'primary',
240     ];
241
242     return $form;
243   }
244
245   /**
246    * {@inheritdoc}
247    */
248   public function validateForm(array &$form, FormStateInterface $form_state) {
249     if ($error = user_validate_name($form_state->getValue(['account', 'name']))) {
250       $form_state->setErrorByName('account][name', $error);
251     }
252   }
253
254   /**
255    * {@inheritdoc}
256    */
257   public function submitForm(array &$form, FormStateInterface $form_state) {
258     $this->config('system.site')
259       ->set('name', (string) $form_state->getValue('site_name'))
260       ->set('mail', (string) $form_state->getValue('site_mail'))
261       ->save(TRUE);
262
263     $this->config('system.date')
264       ->set('timezone.default', (string) $form_state->getValue('date_default_timezone'))
265       ->set('country.default', (string) $form_state->getValue('site_default_country'))
266       ->save(TRUE);
267
268     $account_values = $form_state->getValue('account');
269
270     // Enable update.module if this option was selected.
271     $update_status_module = $form_state->getValue('enable_update_status_module');
272     if ($update_status_module) {
273       $this->moduleInstaller->install(['file', 'update'], FALSE);
274
275       // Add the site maintenance account's email address to the list of
276       // addresses to be notified when updates are available, if selected.
277       $email_update_status_emails = $form_state->getValue('enable_update_status_emails');
278       if ($email_update_status_emails) {
279         // Reset the configuration factory so it is updated with the new module.
280         $this->resetConfigFactory();
281         $this->config('update.settings')->set('notification.emails', [$account_values['mail']])->save(TRUE);
282       }
283     }
284
285     // We precreated user 1 with placeholder values. Let's save the real values.
286     $account = $this->userStorage->load(1);
287     $account->init = $account->mail = $account_values['mail'];
288     $account->roles = $account->getRoles();
289     $account->activate();
290     $account->timezone = $form_state->getValue('date_default_timezone');
291     $account->pass = $account_values['pass'];
292     $account->name = $account_values['name'];
293     $account->save();
294
295     // Record when this install ran.
296     $this->state->set('install_time', $_SERVER['REQUEST_TIME']);
297   }
298
299 }