Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / hacked / hacked.report.inc
1 <?php
2
3 use Drupal\Component\Utility\Unicode;
4 use Drupal\Core\Link;
5 use Drupal\Core\Url;
6 use Drupal\hacked\hackedProject;
7
8 /**
9  * Batch callback to build the hacked report.
10  */
11 function hacked_build_report_batch($project_name, &$context) {
12   if (!isset($context['results']['report'])) {
13     $context['results']['report'] = array();
14   }
15   \Drupal::moduleHandler()
16     ->loadInclude('hacked', 'inc', 'includes/hacked_project');
17   $project = new hackedProject($project_name);
18   $context['results']['report'][$project_name] = $project->compute_report();
19   $context['message'] = t('Finished processing: @name', array('@name' => $project->title()));
20 }
21
22 /**
23  * Completion callback for the report batch.
24  *
25  * @param $success
26  * @param $results
27  */
28 function hacked_build_report_batch_finished($success, $results) {
29   if ($success) {
30     // Sort the results.
31     usort($results['report'], '_hacked_project_report_sort_by_status');
32     // Store them.
33     \Drupal::cache(HACKED_CACHE_TABLE)
34       ->set('hacked:full-report', $results['report'], strtotime('+1 day'));
35     \Drupal::state()->set('hacked.last_check', time());
36   }
37 }
38
39 /**
40  * Implements hook_preprocess_update_last_check().
41  */
42 function hacked_preprocess_update_last_check(&$variables) {
43   $route = \Drupal::routeMatch()->getRouteName();
44   if ($route == 'hacked.report') {
45     $variables['link'] = $variables['link'] = Link::fromTextAndUrl(t('Check manually'), new Url('hacked.manual_status', array(), array(
46       'query' => \Drupal::destination()
47         ->getAsArray()
48     )));
49   }
50 }
51
52 /**
53  * Prepares variables for hacked project status templates.
54  *
55  * Default template: hacked-project-status.html.twig.
56  *
57  * @param array $variables
58  *   An associative array containing:
59  *   - project: An array of information about the project.
60  */
61 function hacked_preprocess_update_project_status(&$variables) {
62   $route = \Drupal::routeMatch()->getRouteName();
63   if ($route == 'hacked.report') {
64
65     // Storing by reference because we are sorting the project values.
66     $project = &$variables['project'];
67
68     $variables['install_type'] = $project['install_type'];
69     if ($project['install_type'] == 'dev' && !empty($project['datestamp'])) {
70       $variables['datestamp'] = Drupal::service('date.formatter')
71         ->format($project['datestamp'], 'custom', 'Y-M-d');
72     }
73
74     $variables['existing_version'] = $project['existing_version'];
75
76     $variables['versions'] = [
77       [
78         '#theme'   => 'hacked_project_summary',
79         '#project' => $project,
80       ]
81     ];
82
83     switch ($project['status']) {
84       case HACKED_STATUS_UNHACKED:
85         $uri = 'core/misc/icons/73b355/check.svg';
86         $text = t('Unchanged');
87         $project['status'] = UPDATE_CURRENT;
88         break;
89       case HACKED_STATUS_HACKED:
90         $uri = 'core/misc/icons/e32700/error.svg';
91         $text = t('Changed!');
92         $project['status'] = UPDATE_NOT_CHECKED;
93         break;
94       case HACKED_STATUS_UNCHECKED:
95       default:
96         $uri = 'core/misc/icons/e29700/warning.svg';
97         $text = t('Unchecked');
98         $project['status'] = UPDATE_NOT_CHECKED;
99         break;
100     }
101
102     $variables['status']['label'] = $text;
103     $variables['status']['icon'] = [
104       '#theme'  => 'image',
105       '#width'  => 18,
106       '#height' => 18,
107       '#uri'    => $uri,
108       '#alt'    => $text,
109       '#title'  => $text,
110     ];
111   }
112 }
113
114 /**
115  * Prepares variables for hacked status report templates.
116  *
117  * Default template: hacked-report.html.twig.
118  *
119  * @param array $variables
120  *   An associative array containing:
121  *   - data: An array of data about each project's status.
122  */
123 function template_preprocess_hacked_report(&$variables) {
124   $data = $variables['data'];
125
126   $last = \Drupal::state()->get('hacked.last_check') ?: 0;
127
128   $variables['last_checked'] = [
129     '#theme'    => 'update_last_check',
130     '#last'     => $last,
131     // Attach the library to a variable that gets printed always.
132     '#attached' => [
133       'library' => [
134         'update/drupal.update.admin',
135       ]
136     ],
137   ];
138
139   // For no project update data, populate no data message.
140   if (empty($data)) {
141     $destination = \Drupal::destination()->getAsArray();
142     $variables['no_updates_message'] = t('No hacked information available. <a href=":check_manually">check manually</a>.', array(
143       ':run_cron'       => Url::fromRoute('system.run_cron', [], ['query' => $destination]),
144       ':check_manually' => Url::fromRoute('hacked.manual_status', [], ['query' => $destination]),
145     ));
146   }
147
148   $rows = [];
149
150   foreach ($data as $project) {
151     if (!isset($project['status'])) {
152       continue;
153     }
154
155     $project_status = [
156       '#theme'   => 'update_project_status',
157       '#project' => $project,
158     ];
159
160     // Build project rows.
161     if (!isset($rows[$project['project_type']])) {
162       $rows[$project['project_type']] = [
163         '#type'       => 'table',
164         '#attributes' => ['class' => ['update']],
165       ];
166     }
167     $row_key = !empty($project['title']) ? Unicode::strtolower($project['title']) : Unicode::strtolower($project['name']);
168
169     // Add the project status row and details.
170     $rows[$project['project_type']][$row_key]['status'] = $project_status;
171
172     // Add project status class attribute to the table row.
173     switch ($project['status']) {
174       case HACKED_STATUS_UNHACKED:
175         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-success']];
176         break;
177       case HACKED_STATUS_HACKED:
178         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-error']];
179         break;
180       case HACKED_STATUS_UNCHECKED:
181       default:
182         $rows[$project['project_type']][$row_key]['#attributes'] = ['class' => ['color-warning']];
183         break;
184     }
185   }
186
187   $project_types = [
188     'core'            => t('Drupal core'),
189     'module'          => t('Modules'),
190     'theme'           => t('Themes'),
191     'module-disabled' => t('Uninstalled modules'),
192     'theme-disabled'  => t('Uninstalled themes'),
193   ];
194
195   $variables['project_types'] = [];
196   foreach ($project_types as $type_name => $type_label) {
197     if (!empty($rows[$type_name])) {
198       ksort($rows[$type_name]);
199       $variables['project_types'][] = [
200         'label' => $type_label,
201         'table' => $rows[$type_name],
202       ];
203     }
204   }
205 }
206
207 /**
208  * @param $variables
209  */
210 function template_preprocess_hacked_project_summary(&$variables) {
211   $project = $variables['project'];
212
213   $changes[] = \Drupal::translation()
214     ->formatPlural($project['counts']['different'], '1 file changed', '@count files changed');
215   $changes[] = \Drupal::translation()
216     ->formatPlural($project['counts']['missing'], '1 file deleted', '@count files deleted');
217   if ($project['counts']['access_denied'] > 0) {
218     $changes[] = \Drupal::translation()
219       ->formatPlural($project['counts']['access_denied'], '1 unreadable file', '@count unreadable files');
220   }
221   $variables['changes'] = implode(', ', $changes);
222
223   $variables['link'] = Url::fromRoute('hacked.project', ['project' => $project['project_name']]);
224 }
225