Updated to Drupal 8.5. Core Media not yet in use.
[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.',
24         [
25           ':url' => $link->url,
26           '@fail_count' => $link->fail_count,
27           '@last_checked' => DateFormatter::format($link->last_checked),
28         ]),
29       '#open' => TRUE,
30     ];
31
32     $form['settings']['lid'] = ['#type' => 'value', '#value' => $link->lid];
33     $form['settings']['url'] = ['#type' => 'value', '#value' => $link->url];
34
35     $form['settings']['method'] = [
36       '#type' => 'select',
37       '#title' => $this->t('Select request method'),
38       '#default_value' => $link->method,
39       '#options' => [
40         'HEAD' => $this->t('HEAD'),
41         'GET' => $this->t('GET'),
42       ],
43       '#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.'),
44     ];
45
46     $form['settings']['status'] = [
47       '#default_value' => $link->status,
48       '#type' => 'checkbox',
49       '#title' => $this->t('Check link status'),
50       '#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.'),
51     ];
52
53     $form['maintenance'] = [
54       '#type' => 'details',
55       '#title' => $this->t('Maintenance'),
56       '#open' => TRUE,
57     ];
58
59     $form['maintenance']['recheck'] = [
60       '#default_value' => 0,
61       '#type' => 'checkbox',
62       '#title' => $this->t('Re-check link status on next cron run'),
63       '#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'))]),
64     ];
65
66     return parent::buildForm($form, $form_state);
67   }
68
69   /**
70    * Edit link settings form submit handler.
71    */
72   public function submitForm(array &$form, FormStateInterface $form_state) {
73     // Force link re-check asap.
74     if ($form_state->getValue('recheck')) {
75       \Drupal::database()->update('linkchecker_link')
76         ->condition('lid', $form_state->getValue('lid'))
77         ->fields(['last_checked' => 0])
78         ->execute();
79       drupal_set_message(t('The link %url will be checked again on the next cron run.', ['%url' => $form_state->getValue('url')]));
80     }
81
82     if ($form_state->getValue('method') != $form['settings']['method']['#default_value']) {
83       // Update settings and reset statistics for a quick re-check.
84       \Drupal::database()->update('linkchecker_link')
85         ->condition('lid', $form_state->getValue('lid'))
86         ->fields([
87           'method' => $form_state->getValue('method'),
88           'fail_count' => 0,
89           'last_checked' => 0,
90           'status' => $form_state->getValue('status'),
91         ])
92         ->execute();
93       drupal_set_message(t('The link settings for %url have been saved and the fail counter has been reset.', ['%url' => $form_state->getValue('url')]));
94     }
95     else {
96       // Update setting only.
97       \Drupal::database()->update('linkchecker_link')
98         ->condition('lid', $form_state->getValue('lid'))
99         ->fields([
100           'method' => $form_state->getValue('method'),
101           'status' => $form_state->getValue('status'),
102         ])
103         ->execute();
104       drupal_set_message(t('The link settings for %url have been saved.', ['%url' => $form_state->getValue('url')]));
105     }
106   }
107
108   /**
109    * Checks access for a specific request.
110    *
111    * @param $link
112    *
113    * @return
114    *   Run access checks for this account.
115    *
116    * @FIXME
117    */
118   public function access($link) {
119     // Check permissions and combine that with any custom access checking needed. Pass forward
120     // parameters from the route and/or request as needed.
121     $account = \Drupal::currentUser();
122     return AccessResult::allowedIf($account->hasPermission('edit link settings') && _linkchecker_link_access($link));
123   }
124
125 }