Interim commit.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Controller / MessageController.php
1 <?php
2
3 namespace Drupal\migrate_tools\Controller;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Database\Connection;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Returns responses for migrate_tools message routes.
14  */
15 class MessageController extends ControllerBase {
16
17   /**
18    * The database service.
19    *
20    * @var \Drupal\Core\Database\Connection
21    */
22   protected $database;
23
24   /**
25    * Plugin manager for migration plugins.
26    *
27    * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
28    */
29   protected $migrationConfigEntityPluginManager;
30
31   /**
32    * {@inheritdoc}
33    */
34   public static function create(ContainerInterface $container) {
35     return new static(
36       $container->get('database'),
37       $container->get('plugin.manager.config_entity_migration')
38     );
39   }
40
41   /**
42    * Constructs a MessageController object.
43    *
44    * @param \Drupal\Core\Database\Connection $database
45    *   A database connection.
46    * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
47    *   The plugin manager for config entity-based migrations.
48    */
49   public function __construct(Connection $database, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
50     $this->database = $database;
51     $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
52   }
53
54   /**
55    * Gets an array of log level classes.
56    *
57    * @return array
58    *   An array of log level classes.
59    */
60   public static function getLogLevelClassMap() {
61     return [
62       MigrationInterface::MESSAGE_INFORMATIONAL => 'migrate-message-4',
63       MigrationInterface::MESSAGE_NOTICE => 'migrate-message-3',
64       MigrationInterface::MESSAGE_WARNING => 'migrate-message-2',
65       MigrationInterface::MESSAGE_ERROR => 'migrate-message-1',
66     ];
67   }
68
69   /**
70    * Displays a listing of migration messages.
71    *
72    * Messages are truncated at 56 chars.
73    *
74    * @param string $migration_group
75    *   Machine name of the migration's group.
76    *
77    * @param string $migration
78    *   Machine name of the migration.
79    *
80    * @return array
81    *   A render array as expected by drupal_render().
82    */
83   public function overview($migration_group, $migration) {
84     $rows = [];
85     $classes = static::getLogLevelClassMap();
86     /** @var MigrationInterface $migration */
87     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration);
88     $source_id_field_names = array_keys($migration->getSourcePlugin()->getIds());
89     $column_number = 1;
90     foreach ($source_id_field_names as $source_id_field_name) {
91       $header[] = [
92         'data' => $source_id_field_name,
93         'field' => 'sourceid' . $column_number++,
94         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
95       ];
96     }
97     $header[] = [
98       'data' => $this->t('Severity level'),
99       'field' => 'level',
100       'class' => [RESPONSIVE_PRIORITY_LOW],
101     ];
102     $header[] = [
103       'data' => $this->t('Message'),
104       'field' => 'message',
105     ];
106
107     $message_table = $migration->getIdMap()->messageTableName();
108     $map_table = $migration->getIdMap()->mapTableName();
109     $query = $this->database->select($message_table, 'msg')
110       ->extend('\Drupal\Core\Database\Query\PagerSelectExtender')
111       ->extend('\Drupal\Core\Database\Query\TableSortExtender');
112     $query->innerJoin($map_table, 'map', 'msg.source_ids_hash=map.source_ids_hash');
113     $query->fields('msg');
114     $query->fields('map');
115     $result = $query
116       ->limit(50)
117       ->orderByHeader($header)
118       ->execute();
119
120     foreach ($result as $message_row) {
121       $column_number = 1;
122       foreach ($source_id_field_names as $source_id_field_name) {
123         $column_name = 'sourceid' . $column_number++;
124         $row[$column_name] = $message_row->$column_name;
125       }
126       $row['level'] = $message_row->level;
127       $row['message'] = $message_row->message;
128       $row['class'] = [Html::getClass('migrate-message-' . $message_row->level), $classes[$message_row->level]];
129       $rows[] = $row;
130     }
131
132     $build['message_table'] = [
133       '#type' => 'table',
134       '#header' => $header,
135       '#rows' => $rows,
136       '#attributes' => ['id' => $message_table, 'class' => [$message_table]],
137       '#empty' => $this->t('No messages for this migration.'),
138     ];
139     $build['message_pager'] = ['#type' => 'pager'];
140
141     return $build;
142   }
143
144 }