Updated all the contrib modules to their latest versions.
[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\Plugin\MigrationPluginManagerInterface;
10 use Drupal\migrate_plus\Entity\MigrationGroupInterface;
11 use Drupal\migrate_plus\Entity\MigrationInterface as MigratePlusMigrationInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Returns responses for migrate_tools message routes.
16  */
17 class MessageController extends ControllerBase {
18
19   /**
20    * The database service.
21    *
22    * @var \Drupal\Core\Database\Connection
23    */
24   protected $database;
25
26   /**
27    * Plugin manager for migration plugins.
28    *
29    * @var \Drupal\migrate\Plugin\MigrationPluginManagerInterface
30    */
31   protected $migrationPluginManager;
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function create(ContainerInterface $container) {
37     return new static(
38       $container->get('database'),
39       $container->get('plugin.manager.migration')
40     );
41   }
42
43   /**
44    * Constructs a MessageController object.
45    *
46    * @param \Drupal\Core\Database\Connection $database
47    *   A database connection.
48    * @param \Drupal\migrate\Plugin\MigrationPluginManagerInterface $migration_plugin_manager
49    *   The migration plugin manager.
50    */
51   public function __construct(Connection $database, MigrationPluginManagerInterface $migration_plugin_manager) {
52     $this->database = $database;
53     $this->migrationPluginManager = $migration_plugin_manager;
54   }
55
56   /**
57    * Gets an array of log level classes.
58    *
59    * @return array
60    *   An array of log level classes.
61    */
62   public static function getLogLevelClassMap() {
63     return [
64       MigrationInterface::MESSAGE_INFORMATIONAL => 'migrate-message-4',
65       MigrationInterface::MESSAGE_NOTICE => 'migrate-message-3',
66       MigrationInterface::MESSAGE_WARNING => 'migrate-message-2',
67       MigrationInterface::MESSAGE_ERROR => 'migrate-message-1',
68     ];
69   }
70
71   /**
72    * Displays a listing of migration messages.
73    *
74    * Messages are truncated at 56 chars.
75    *
76    * @param \Drupal\migrate_plus\Entity\MigrationGroupInterface $migration_group
77    *   The migration group.
78    * @param \Drupal\migrate_plus\Entity\MigrationInterface $migration
79    *   The $migration.
80    *
81    * @return array
82    *   A render array as expected by drupal_render().
83    */
84   public function overview(MigrationGroupInterface $migration_group, MigratePlusMigrationInterface $migration) {
85     $rows = [];
86     $classes = static::getLogLevelClassMap();
87     $migration_plugin = $this->migrationPluginManager->createInstance($migration->id(), $migration->toArray());
88     $source_id_field_names = array_keys($migration_plugin->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_plugin->getIdMap()->messageTableName();
108     $map_table = $migration_plugin->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 }