Further modules included.
[yaffs-website] / web / modules / contrib / linkchecker / src / Form / LinkCheckerEditLinkSettingsForm.php
1 <?php
2
3 namespace Drupal\linkchecker\Form;
4
5 use Drupal\Core\Datetime\DateFormatter;
6 use Drupal\Core\Session\AccountInterface;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Builds edit link settings form.
11  */
12 class LinkCheckerEditLinkSettingsForm {
13
14   /**
15    * Edit link settings form.
16    */
17   public function buildForm(array $form, FormStateInterface $form_state, $link) {
18     $config = $this->config('linkchecker.settings');
19
20     $form['settings'] = [
21       '#type' => 'details',
22       '#title' => $this->t('Settings'),
23       '#description' => $this->t('The link <a href=":url">:url</a> was last checked on @last_checked and failed @fail_count times.', [':url' => $link->url, '@fail_count' => $link->fail_count, '@last_checked' => DateFormatter::format($link->last_checked)]),
24       '#open' => TRUE,
25     ];
26
27     $form['settings']['lid'] = ['#type' => 'value', '#value' => $link->lid];
28     $form['settings']['url'] = ['#type' => 'value', '#value' => $link->url];
29
30     $form['settings']['method'] = [
31       '#type' => 'select',
32       '#title' => $this->t('Select request method'),
33       '#default_value' => $link->method,
34       '#options' => [
35         'HEAD' => $this->t('HEAD'),
36         'GET' => $this->t('GET'),
37       ],
38       '#description' => $this->t('Select the request method used for link checks of this link. If you encounter issues like status code 500 errors with the HEAD request method you should try the GET request method before ignoring a link.'),
39     ];
40
41     $form['settings']['status'] = [
42       '#default_value' => $link->status,
43       '#type' => 'checkbox',
44       '#title' => $this->t('Check link status'),
45       '#description' => $this->t('Uncheck if you wish to ignore this link. Use this setting only as a last resort if there is no other way to solve a failed link check.'),
46     ];
47
48     $form['maintenance'] = [
49       '#type' => 'details',
50       '#title' => $this->t('Maintenance'),
51       '#open' => TRUE,
52     ];
53
54     $form['maintenance']['recheck'] = [
55       '#default_value' => 0,
56       '#type' => 'checkbox',
57       '#title' => $this->t('Re-check link status on next cron run'),
58       '#description' => $this->t('Enable this checkbox if you want to re-check the link during the next cron job rather than wait for the next scheduled check on @date.', ['@date' => DateFormatter::format($link->last_checked + $config->get('check.interval'))]),
59     ];
60
61     return parent::buildForm($form, $form_state);
62   }
63
64   /**
65    * Edit link settings form submit handler.
66    */
67   public function submitForm(array &$form, FormStateInterface $form_state) {
68     // Force link re-check asap.
69     if ($form_state->getValue('recheck')) {
70       db_update('linkchecker_link')
71         ->condition('lid', $form_state->getValue('lid'))
72         ->fields(array('last_checked' => 0))
73         ->execute();
74       drupal_set_message(t('The link %url will be checked again on the next cron run.', ['%url' => $form_state->getValue('url')]));
75     }
76
77     if ($form_state->getValue('method') != $form['settings']['method']['#default_value']) {
78       // Update settings and reset statistics for a quick re-check.
79       db_update('linkchecker_link')
80         ->condition('lid', $form_state->getValue('lid'))
81         ->fields(array(
82           'method' => $form_state->getValue('method'),
83           'fail_count' => 0,
84           'last_checked' => 0,
85           'status' => $form_state->getValue('status'),
86         ))
87         ->execute();
88       drupal_set_message(t('The link settings for %url have been saved and the fail counter has been reset.', array('%url' => $form_state->getValue('url'))));
89     }
90     else {
91       // Update setting only.
92       db_update('linkchecker_link')
93         ->condition('lid', $form_state->getValue('lid'))
94         ->fields(array(
95           'method' => $form_state->getValue('method'),
96           'status' => $form_state->getValue('status'),
97         ))
98         ->execute();
99       drupal_set_message(t('The link settings for %url have been saved.', array('%url' => $form_state->getValue('url'))));
100     }
101   }
102
103   /**
104    * Checks access for a specific request.
105    *
106    * @param \Drupal\Core\Session\AccountInterface $account
107    *   Run access checks for this account.
108    */
109   // @FIXME
110   public function access($link) {
111     // Check permissions and combine that with any custom access checking needed. Pass forward
112     // parameters from the route and/or request as needed.
113     return AccessResult::allowedIf($account->hasPermission('edit link settings') && _linkchecker_link_access($link));
114         //$this->someOtherCustomCondition());
115   }
116
117 }