PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/webform/src/Form/WebformSettingsForm.php

https://gitlab.com/guillaumev/alkarama
PHP | 334 lines | 274 code | 39 blank | 21 comment | 1 complexity | f2c181c52aae80dc2e087d5eb98216d2 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\webform\Form\WebformSettingsForm.
  5. */
  6. namespace Drupal\webform\Form;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Form\ConfigFormBase;
  9. use Drupal\Core\Datetime\Entity\DateFormat;
  10. /**
  11. * Configure Webform admin settings.
  12. */
  13. class WebformSettingsForm extends ConfigFormBase {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function getFormId() {
  18. return 'webform_settings';
  19. }
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function getEditableConfigNames() {
  24. return ['webform.settings'];
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function buildForm(array $form, FormStateInterface $form_state) {
  30. $config = $this->config('webform.settings');
  31. $form['#tree'] = TRUE;
  32. $form['components'] = array(
  33. '#type' => 'details',
  34. '#title' => t('Available components'),
  35. '#description' => t('These are the available field types for your installation of Webform. You may disable any of these components by unchecking its corresponding box. Only checked components will be available in existing or new webforms.'),
  36. '#open' => TRUE,
  37. '#theme' => 'webform_admin_settings_components_table',
  38. );
  39. // Add each component to the form.
  40. $manager = \Drupal::service('plugin.manager.webform.component');
  41. $component_types = $manager->getDefinitions();
  42. foreach ($component_types as $key => $component_type) {
  43. $component = $manager->createInstance($component_type['id']);
  44. $form['components'][$key] = array(
  45. '#type' => 'checkbox',
  46. '#title' => $component->getLabel(),
  47. '#description' => $component->getDescription(),
  48. '#return_value' => 1,
  49. '#default_value' => 1,//$component['enabled'],
  50. );
  51. }
  52. $form['email'] = array(
  53. '#type' => 'details',
  54. '#title' => t('Default e-mail values'),
  55. '#open' => TRUE,
  56. );
  57. $form['email']['default_from_address'] = array(
  58. '#type' => 'textfield',
  59. '#title' => t('From address'),
  60. '#default_value' => $config->get('email.default_from_address'),
  61. '#description' => t('The default sender address for emailed webform results; often the e-mail address of the maintainer of your forms.'),
  62. );
  63. $form['email']['default_from_name'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('From name'),
  66. '#default_value' => $config->get('email.default_from_name'),
  67. '#description' => t('The default sender name which is used along with the default from address.'),
  68. );
  69. $form['email']['default_subject'] = array(
  70. '#type' => 'textfield',
  71. '#title' => t('Default subject'),
  72. '#default_value' => $config->get('email.default_subject'),
  73. '#description' => t('The default subject line of any e-mailed results.'),
  74. );
  75. $form['email']['replyto'] = array(
  76. '#type' => 'checkbox',
  77. '#title' => t('Use Reply-To header'),
  78. '#default_value' => $config->get('email.replyto'),
  79. '#description' => t('Sends all e-mail from the domain of the default address above and sets the "Reply-To" header to the actual sender. Helps prevent e-mail from being flagged as spam.'),
  80. );
  81. $form['email']['html_capable'] = array(
  82. '#type' => 'checkbox',
  83. '#title' => t('HTML mail system'),
  84. '#default_value' => $config->get('email.html_capable'),
  85. '#description' => t('Whether the mail system configured for webform is capable of sending mail in HTML format.'),
  86. );
  87. $form['email']['default_format'] = array(
  88. '#type' => 'radios',
  89. '#title' => t('Format'),
  90. '#options' => array(
  91. 0 => t('Plain text'),
  92. 1 => t('HTML'),
  93. ),
  94. '#default_value' => $config->get('email.default_format'),
  95. '#description' => t('The default format for new e-mail settings. Webform e-mail options take precedence over the settings for system-wide e-mails configured in MIME mail.'),
  96. '#states' => array(
  97. 'visible' => array(
  98. ':input[name="email[html_capable]"]' => array('checked' => TRUE),
  99. ),
  100. ),
  101. );
  102. $form['email']['format_override'] = array(
  103. '#type' => 'radios',
  104. '#title' => t('Format override'),
  105. '#options' => array(
  106. 0 => t('Per-webform configuration of e-mail format'),
  107. 1 => t('Send all e-mails in the default format'),
  108. ),
  109. '#default_value' => $config->get('email.format_override'),
  110. '#description' => t('Force all webform e-mails to be sent in the default format.'),
  111. '#states' => array(
  112. 'visible' => array(
  113. ':input[name="email[html_capable]"]' => array('checked' => TRUE),
  114. ),
  115. ),
  116. );
  117. $form['progressbar'] = array(
  118. '#type' => 'details',
  119. '#title' => t('Progress bar'),
  120. '#open' => TRUE,
  121. );
  122. $form['progressbar']['style'] = array(
  123. '#type' => 'checkboxes',
  124. '#title' => t('Progress bar style'),
  125. '#options' => array(
  126. 'progressbar_bar' => t('Show progress bar'),
  127. 'progressbar_page_number' => t('Show page number as number of completed (i.e. Page 1 of 10)'),
  128. 'progressbar_percent' => t('Show percentage completed (i.e. 10%)'),
  129. 'progressbar_pagebreak_labels' => t('Show page labels from page break components'),
  130. 'progressbar_include_confirmation' => t('Include confirmation page in progress bar'),
  131. ),
  132. '#default_value' => $config->get('progressbar.style'),
  133. '#description' => t('Choose how the progress bar should be displayed for multi-page forms.'),
  134. );
  135. $form['progressbar']['label_first'] = array(
  136. '#type' => 'textfield',
  137. '#title' => t('First page label'),
  138. '#default_value' => $config->get('progressbar.label_first'),
  139. '#maxlength' => 255,
  140. );
  141. $form['progressbar']['label_confirmation'] = array(
  142. '#type' => 'textfield',
  143. '#title' => t('Confirmation page label'),
  144. '#default_value' => $config->get('progressbar.label_confirmation'),
  145. '#maxlength' => 255,
  146. );
  147. $form['advanced'] = array(
  148. '#type' => 'details',
  149. '#title' => t('Advanced options'),
  150. '#open' => FALSE,
  151. );
  152. $form['advanced']['tracking_mode'] = array(
  153. '#type' => 'radios',
  154. '#title' => t('Track anonymous users by:'),
  155. '#options' => array(
  156. 'cookie' => t('Cookie only (least strict)'),
  157. 'ip_address' => t('IP address only'),
  158. 'strict' => t('Both cookie and IP address (most strict)'),
  159. ),
  160. '#default_value' => $config->get('advanced.tracking_mode'),
  161. '#description' => t('<a href="http://www.wikipedia.org/wiki/HTTP_cookie">Cookies</a> can be used to help prevent the same user from repeatedly submitting a webform. Limiting by IP address is more effective against repeated submissions, but may result in unintentional blocking of users sharing the same address. Confidential submissions are tracked by cookie only. Logged-in users are always tracked by their user ID and are not affected by this option.'),
  162. );
  163. $form['advanced']['email_address_format'] = array(
  164. '#type' => 'radios',
  165. '#title' => t('E-mail address format'),
  166. '#options' => array(
  167. 'long' => t('Long format: "Example Name" &lt;name@example.com&gt;'),
  168. 'short' => t('Short format: name@example.com'),
  169. ),
  170. '#default_value' => $config->get('advanced.email_address_format'),
  171. '#description' => t('Most servers support the "long" format which will allow for more friendly From addresses in e-mails sent. However many Windows-based servers are unable to send in the long format. Change this option if experiencing problems sending e-mails with Webform.'),
  172. );
  173. $form['advanced']['email_address_individual'] = array(
  174. '#type' => 'radios',
  175. '#title' => t('E-mailing multiple recipients'),
  176. '#options' => array(
  177. 0 => t('Send a single e-mail to all recipients'),
  178. 1 => t('Send individual e-mails to each recipient'),
  179. ),
  180. '#default_value' => $config->get('advanced.email_address_individual'),
  181. '#description' => t('Individual e-mails increases privacy by not revealing the addresses of other recipients. A single e-mail to all recipients lets them use "Reply All" to communicate.'),
  182. );
  183. $date_types = DateFormat::loadMultiple();
  184. $date_formatter = \Drupal::service('date.formatter');
  185. $date_format_options = array();
  186. foreach ($date_types as $machine_name => $format) {
  187. $date_format_options[$machine_name] = t('@name - @sample', array('@name' => $format->get('label'), '@sample' => $date_formatter->format(REQUEST_TIME, $machine_name)));
  188. }
  189. $form['advanced']['date_type'] = array(
  190. '#type' => 'select',
  191. '#title' => t('Date format'),
  192. '#options' => $date_format_options,
  193. '#default_value' => $config->get('advanced.date_type'),
  194. '#description' => t('Choose the format for the display of date components. Only the date portion of the format is used. Reporting and export use the short format.'),
  195. );
  196. module_load_include('inc', 'webform', 'includes/webform.export');
  197. $form['advanced']['export_format'] = array(
  198. '#type' => 'radios',
  199. '#title' => t('Default export format'),
  200. '#options' => webform_export_list(),
  201. '#default_value' => $config->get('advanced.export_format'),
  202. );
  203. $form['advanced']['csv_delimiter'] = array(
  204. '#type' => 'select',
  205. '#title' => t('Default export delimiter'),
  206. '#description' => t('This is the delimiter used in the CSV/TSV file when downloading Webform results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'),
  207. '#default_value' => $config->get('advanced.csv_delimiter'),
  208. '#options' => array(
  209. ',' => t('Comma (,)'),
  210. '\t' => t('Tab (\t)'),
  211. ';' => t('Semicolon (;)'),
  212. ':' => t('Colon (:)'),
  213. '|' => t('Pipe (|)'),
  214. '.' => t('Period (.)'),
  215. ' ' => t('Space ( )'),
  216. ),
  217. );
  218. $form['advanced']['export_wordwrap'] = array(
  219. '#type' => 'radios',
  220. '#title' => t('Export word-wrap'),
  221. '#options' => array(
  222. '0' => t('Only text containing return characters'),
  223. '1' => t('All text'),
  224. ),
  225. '#default_value' => $config->get('advanced.export_wordwrap'),
  226. '#description' => t('Some export formats, such as Microsoft Excel, support word-wrapped text cells.'),
  227. );
  228. $form['advanced']['submission_access_control'] = array(
  229. '#type' => 'radios',
  230. '#title' => t('Submission access control'),
  231. '#options' => array(
  232. '1' => t('Select the user roles that may submit each individual webform'),
  233. '0' => t('Disable Webform submission access control'),
  234. ),
  235. '#default_value' => $config->get('advanced.submission_access_control'),
  236. '#description' => t('By default, the configuration form for each webform allows the administrator to choose which roles may submit the form. You may want to allow users to always submit the form if you are using a separate node access module to control access to webform nodes themselves.'),
  237. );
  238. $form['advanced']['token_access'] = array(
  239. '#type' => 'radios',
  240. '#title' => t('Token access'),
  241. '#options' => array(
  242. '1' => t('Allow tokens to be used in Webforms.'),
  243. '0' => t('Disable tokens in Webforms'),
  244. ),
  245. '#default_value' => $config->get('advanced.token_access'),
  246. '#description' => t('Tokens can be used to reveal sensitive information. Allow tokens if Webform creators are trusted.'),
  247. );
  248. $form['advanced']['email_select_max'] = array(
  249. '#type' => 'textfield',
  250. '#title' => t("Select email mapping limit"),
  251. '#default_value' => $config->get('advanced.email_select_max'),
  252. '#description' => t('When mapping emails addresses to a select component, limit the choice to components with less than the amount of options indicated. This is to avoid flooding the email settings form.'),
  253. );
  254. return parent::buildForm($form, $form_state);
  255. }
  256. /**
  257. * {@inheritdoc}
  258. */
  259. public function submitForm(array &$form, FormStateInterface $form_state) {
  260. $values = $form_state->getValues();
  261. $disabled_components = array();
  262. foreach ($values['components'] as $name => $enabled) {
  263. if (!$enabled) {
  264. $disabled_components[] = $name;
  265. }
  266. }
  267. $values['disabled_components'] = $disabled_components;
  268. // Trim out empty options in the progress bar options.
  269. $values['progressbar']['style'] = array_keys(array_filter($values['progressbar']['style']));
  270. $this->config('webform.settings')
  271. ->set('disabled_components', $values['disabled_components'])
  272. ->set('email.default_from_address', $values['email']['default_from_address'])
  273. ->set('email.default_from_name', $values['email']['default_from_name'])
  274. ->set('email.default_subject', $values['email']['default_subject'])
  275. ->set('email.replyto', $values['email']['replyto'])
  276. ->set('email.html_capable', $values['email']['html_capable'])
  277. ->set('email.default_format', $values['email']['default_format'])
  278. ->set('email.format_override', $values['email']['format_override'])
  279. ->set('progressbar.style', $values['progressbar']['style'])
  280. ->set('progressbar.label_first', $values['progressbar']['label_first'])
  281. ->set('progressbar.label_confirmation', $values['progressbar']['label_confirmation'])
  282. ->set('advanced.tracking_mode', $values['advanced']['tracking_mode'])
  283. ->set('advanced.email_address_format', $values['advanced']['email_address_format'])
  284. ->set('advanced.email_address_individual', $values['advanced']['email_address_individual'])
  285. ->set('advanced.date_type', $values['advanced']['date_type'])
  286. ->set('advanced.export_format', $values['advanced']['export_format'])
  287. ->set('advanced.csv_delimiter', $values['advanced']['csv_delimiter'])
  288. ->set('advanced.export_wordwrap', $values['advanced']['export_wordwrap'])
  289. ->set('advanced.submission_access_control', $values['advanced']['submission_access_control'])
  290. ->set('advanced.token_access', $values['advanced']['token_access'])
  291. ->set('advanced.email_select_max', $values['advanced']['email_select_max'])
  292. ->save();
  293. parent::submitForm($form, $form_state);
  294. }
  295. }