Passed
Branch 8.x-1.x (e6d5ba)
by Frédéric G.
03:03
created

qa.module::qa_report_form_submit()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 10
nop 2
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * OSInet Quality Assurance module for Drupal.
6
 *
7
 * @copyright Copyright (C) 2005-2018 Frederic G. MARAND for Ouest Systèmes Informatiques (OSInet)
8
 *
9
 * @since DRUPAL-4-6
10
 *
11
 * @license Licensed under the disjunction of the CeCILL, version 2 and General Public License version 2 and later
12
 *
13
 * License note: QA is distributed by OSInet to its customers under the
14
 * CeCILL 2.0 license. OSInet support services only apply to the module
15
 * when distributed by OSInet, not by any third-party further down the
16
 * distribution chain.
17
 *
18
 * If you obtained QA from drupal.org, that site received it under the
19
 * GPLv2 license and can therefore distribute it under the GPLv2, and
20
 * so can you and just anyone down the chain as long as the GPLv2 terms
21
 * are abided by, the module distributor in that case being the
22
 * drupal.org organization or the downstream distributor, not OSInet.
23
 */
24
use Drupal\qa\Variable\Variable;
25
26
/**
27
 * Page callback for qa/dependencies
28
 *
29
 * TODO convert to native Image_GraphViz to remove dependency on graphviz_filter
30
 * XXX convert to Grafizzi to remove dependency on Image_GraphViz
31
 *
32
 * @return string
33
 */
34
function qa_page_dependencies() {
35
  /** @var \Drupal\qa\Dependencies $qaDep */
36
  $qaDep = \Drupal::service('qa.dependencies');
37
  $G = $qaDep->build();
38
  // passed by reference: cannot pass a function return
39
  return graphviz_filter_render($G);
40
}
41
42
/**
43
 * Batch conclusion callback.
44
 *
45
 * @param boolean $success
46
 * @param array $results
47
 * @param array $operations
48
 */
49
function qa_report_finished($success, $results, $operations) {
50
  unset($results['#message']);
51
  if ($success) {
52
    $message = format_plural(count($results), 'One control pass ran.', '@count control passes ran.');
53
  }
54
  else {
55
    $message = t('Finished with an error.');
56
  }
57
  drupal_set_message($message);
58
  $_SESSION['qa_results'] = $results;
59
  drupal_goto('admin/reports/qa/results');
60
}
61
62
/**
63
 * Results page for QA Controls batch.
64
 *
65
 * @link http://www.php.net/manual/fr/function.unserialize.php @endlink
66
 */
67
function qa_report_results() {
68
  if (empty($_SESSION['qa_results'])) {
69
    drupal_goto('admin/reports/qa');
70
  }
71
  // Work around incomplete classes
72
  $results = unserialize(serialize($_SESSION['qa_results']));
73
74
  $header = array(
75
    t('Control'),
76
    t('Status'),
77
    t('Results'),
78
  );
79
  $data = array();
80
  foreach ($results as $pass) {
81
    $control = $pass->control;
82
    $data[] = array(
83
      $control->title,
84
      $pass->status
85
        ? theme('image', array(
86
          'path' => 'misc/watchdog-ok.png',
87
          'alt' => t('OK'),
88
          ))
89
        : theme('image', array(
90
          'path' => 'misc/watchdog-error.png',
91
          'alt' => t('Error'),
92
          )),
93
      $pass->result,
94
    );
95
  }
96
  $ret = theme('table', [
97
    'header' => $header,
98
    'rows' => $data,
99
    'attributes' => [
100
      'id' => 'qa-results',
101
    ],
102
    '#attached' => ['library' => ['qa/results']],
103
  ]);
104
  // unset($_SESSION['qa_results']);
105
  return $ret;
106
}
107
108
/**
109
 * Form builder for QA packages/controls selection form.
110
 *
111
 * @return array
112
 */
113
function qa_report_form($form, $form_state) {
114
  $form = array();
115
  $base = drupal_get_path('module', 'qa');
116
  $packages = Exportable::getClasses($base, 'Drupal\qa\BasePackage');
117
  ksort($packages);
118
  foreach ($packages as $package_name => $package) {
119
    $collapsed = TRUE;
120
    $form[$package_name] = array(
121
      '#type' => 'fieldset',
122
      '#title' => filter_xss_admin($package->title),
123
      '#description' => filter_xss_admin($package->description),
124
      '#collapsible' => TRUE,
125
    );
126
    $controls = $package->getClasses($package->dir, 'Drupal\qa\Plugin\Qa\Control\BaseControl');
127
128
    foreach ($controls as $control_name => $control) {
129
      $default_value = isset($_SESSION[$control_name])
130
        ? $_SESSION[$control_name]
131
        : NULL;
132
      if ($default_value) {
133
        $collapsed = FALSE;
134
      }
135
136
      $deps = array();
137
      $met = TRUE;
138
      foreach ($control->getDependencies() as $dep_name) {
139
        if (module_exists($dep_name)) {
140
          $deps[] = t('@module (<span class="admin-enabled">available</span>)', array('@module' => $dep_name));
141
        }
142
        else {
143
          $deps[] = t('@module (<span class="admin-disabled">unavailable</span>)', array('@module' => $dep_name));
144
          $met = FALSE;
145
        }
146
      }
147
      $form[$package_name][$control_name] = array(
148
        '#type'          => 'checkbox',
149
        '#default_value' => $met ? $default_value : 0,
150
        '#title'         => filter_xss_admin($control->title),
151
        '#description'   => filter_xss_admin($control->description),
152
        '#disabled'      => !$met,
153
      );
154
      $form[$package_name][$control_name .'-dependencies'] = array(
155
        '#value' => t('Depends on: !dependencies', array(
156
          '!dependencies' => implode(', ', $deps),
157
          )),
158
        '#prefix' => '<div class="admin-dependencies">',
159
        '#suffix' => '</div>',
160
      );
161
    }
162
    $form[$package_name]['#collapsed'] = $collapsed;
163
  }
164
165
  $form['submit'] = [
166
    '#type'  => 'submit',
167
    '#value' => t('Run controls'),
168
  ];
169
170
  return $form;
171
}
172
173
/**
174
 * Submit handler for QA packages/controls selection form
175
 *
176
 * @param array $form
177
 * @param array $form_state
178
 */
179
function qa_report_form_submit($form, &$form_state) {
180
  $controls = array();
181
  foreach ($form_state['values'] as $item => $value) {
182
    if (class_exists($item) && is_subclass_of($item, '\Drupal\qa\Plugin\Qa\Control\BaseControl')) {
183
      if ($value) {
184
        $controls[$item] = $value;
185
      }
186
      $_SESSION[$item] = $value;
187
    }
188
    elseif ($value == 1) {
189
      $args = array(
190
        '%control' => $item,
191
      );
192
      drupal_set_message(t('Requested invalid control %control', $args), 'error');
193
      watchdog('qa', 'Requested invalid control %control', $args, WATCHDOG_ERROR);
194
    }
195
  }
196
197
  drupal_set_message(t('Prepare to run these controls: @controls', array(
198
    '@controls' => implode(', ', array_keys($controls)))), 'status');
199
  $batch = array(
200
    'operations'       => array(),
201
    'title'            => t('QA Controls running'),
202
    'init_message'     => t('QA Controls initializing'),
203
    // 'progress_message' => t('current: @current, Remaining: @remaining, Total: @total'),
204
    'error_message'    => t('Error in QA Control'),
205
    'finished'         => 'qa_report_finished',
206
    // 'file'             => '', // only if outside module file
207
  );
208
209
  foreach ($controls as $item => $value) {
210
    $batch['operations'][] = array('qa_report_run_pass', array($item));
211
  }
212
  batch_set($batch);
213
}
214
215
/**
216
 * Batch progress step.
217
 *
218
 * @return void
219
 */
220
function qa_report_run_pass($class_name, &$context) {
221
  $name_arg = array('@class' => $class_name);
222
223
  $control = new $class_name();
224
  if (!is_object($control)) {
225
    drupal_set_message(t('Cannot obtain an instance for @class', $name_arg), 'error');
226
    $context['results']['#message'] = t('Control @class failed to run.', $name_arg);
227
    $context['message'] = t('Control @class failed to run.', $name_arg);
228
    $context['results'][$class_name] = 'wow';
229
  }
230
  else {
231
    drupal_set_message(t('Running a control instance for @class', $name_arg), 'status');
232
    $pass = $control->run();
233
    if (!$pass->status) {
234
      $context['success'] = FALSE;
235
    }
236
    $context['results']['#message'][] = t('Control @class ran', $name_arg);
237
    $context['message'] = theme('item_list', $context['results']['#message']);
238
    $context['results'][$class_name] = $pass;
239
  }
240
}
241
242
function qa_variable_load($name) {
243
  $variable = new Variable($name);
244
  if (!$variable->is_set) {
245
    return FALSE;
246
  }
247
248
  return $variable;
249
}
250