Interim commit.
[yaffs-website] / web / modules / contrib / migrate_tools / src / Controller / MigrationListBuilder.php
1 <?php
2
3 namespace Drupal\migrate_tools\Controller;
4
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityHandlerInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Routing\CurrentRouteMatch;
11 use Drupal\migrate_plus\Entity\MigrationGroup;
12 use Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager;
13 use Drupal\Core\Url;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Drupal\Core\Datetime\DateFormatter;
16
17 /**
18  * Provides a listing of migration entities in a given group.
19  *
20  * @package Drupal\migrate_tools\Controller
21  *
22  * @ingroup migrate_tools
23  */
24 class MigrationListBuilder extends ConfigEntityListBuilder implements EntityHandlerInterface {
25
26   /**
27    * Default object for current_route_match service.
28    *
29    * @var \Drupal\Core\Routing\CurrentRouteMatch
30    */
31   protected $currentRouteMatch;
32
33   /**
34    * Plugin manager for migration plugins.
35    *
36    * @var \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager
37    */
38   protected $migrationConfigEntityPluginManager;
39
40   /**
41    * Constructs a new EntityListBuilder object.
42    *
43    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
44    *   The entity type definition.
45    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
46    *   The entity storage class.
47    * @param \Drupal\Core\Routing\CurrentRouteMatch $current_route_match
48    *   The current route match service.
49    * @param \Drupal\migrate_plus\Plugin\MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager
50    *   The plugin manager for config entity-based migrations.
51    */
52   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, CurrentRouteMatch $current_route_match, MigrationConfigEntityPluginManager $migration_config_entity_plugin_manager) {
53     parent::__construct($entity_type, $storage);
54     $this->currentRouteMatch = $current_route_match;
55     $this->migrationConfigEntityPluginManager = $migration_config_entity_plugin_manager;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
62     return new static(
63       $entity_type,
64       $container->get('entity.manager')->getStorage($entity_type->id()),
65       $container->get('current_route_match'),
66       $container->get('plugin.manager.config_entity_migration')
67     );
68   }
69
70   /**
71    * Retrieve the migrations belonging to the appropriate group.
72    *
73    * @return array
74    *   An array of entity IDs.
75    */
76   protected function getEntityIds() {
77     $migration_group = $this->currentRouteMatch->getParameter('migration_group');
78
79     $query = $this->getStorage()->getQuery()
80       ->sort($this->entityType->getKey('id'));
81
82     $migration_groups = MigrationGroup::loadMultiple();
83
84     if (array_key_exists($migration_group, $migration_groups)) {
85       $query->condition('migration_group', $migration_group);
86     }
87     else {
88       $query->notExists('migration_group');
89     }
90     // Only add the pager if a limit is specified.
91     if ($this->limit) {
92       $query->pager($this->limit);
93     }
94     return $query->execute();
95   }
96
97   /**
98    * Builds the header row for the entity listing.
99    *
100    * @return array
101    *   A render array structure of header strings.
102    *
103    * @see Drupal\Core\Entity\EntityListController::render()
104    */
105   public function buildHeader() {
106     $header['label'] = $this->t('Migration');
107     $header['machine_name'] = $this->t('Machine Name');
108     $header['status'] = $this->t('Status');
109     $header['total'] = $this->t('Total');
110     $header['imported'] = $this->t('Imported');
111     $header['unprocessed'] = $this->t('Unprocessed');
112     $header['messages'] = $this->t('Messages');
113     $header['last_imported'] = $this->t('Last Imported');
114     return $header; // + parent::buildHeader();
115   }
116
117   /**
118    * Builds a row for a migration plugin.
119    *
120    * @param \Drupal\Core\Entity\EntityInterface $migration_entity
121    *   The migration plugin for which to build the row.
122    *
123    * @return array
124    *   A render array of the table row for displaying the plugin information.
125    *
126    * @see Drupal\Core\Entity\EntityListController::render()
127    */
128   public function buildRow(EntityInterface $migration_entity) {
129     $migration = $this->migrationConfigEntityPluginManager->createInstance($migration_entity->id());
130     $migration_group = $migration->get('migration_group');
131     if (!$migration_group) {
132       $migration_group = 'default';
133     }
134     $route_parameters = array(
135       'migration_group' => $migration_group,
136       'migration' => $migration->id(),
137     );
138     $row['label'] = array(
139       'data' => array(
140         '#type' => 'link',
141         '#title' => $migration->label(),
142         '#url' => Url::fromRoute("entity.migration.overview", $route_parameters),
143       ),
144     );
145     $row['machine_name'] = $migration->id();
146     $row['status'] = $migration->getStatusLabel();
147
148     // Derive the stats.
149     $source_plugin = $migration->getSourcePlugin();
150     $row['total'] = $source_plugin->count();
151     $map = $migration->getIdMap();
152     $row['imported'] = $map->importedCount();
153     // -1 indicates uncountable sources.
154     if ($row['total'] == -1) {
155       $row['total'] = $this->t('N/A');
156       $row['unprocessed'] = $this->t('N/A');
157     }
158     else {
159       $row['unprocessed'] = $row['total'] - $map->processedCount();
160     }
161     $row['messages'] = array(
162       'data' => array(
163         '#type' => 'link',
164         '#title' => $map->messageCount(),
165         '#url' => Url::fromRoute("migrate_tools.messages", $route_parameters),
166       ),
167     );
168     $migrate_last_imported_store = \Drupal::keyValue('migrate_last_imported');
169     $last_imported =  $migrate_last_imported_store->get($migration->id(), FALSE);
170     if ($last_imported) {
171       /** @var DateFormatter $date_formatter */
172       $date_formatter = \Drupal::service('date.formatter');
173       $row['last_imported'] = $date_formatter->format($last_imported / 1000,
174         'custom', 'Y-m-d H:i:s');
175     }
176     else {
177       $row['last_imported'] = '';
178     }
179     return $row; // + parent::buildRow($migration_entity);
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   public function getDefaultOperations(EntityInterface $entity) {
186     $operations = parent::getDefaultOperations($entity);
187     $migration_group = $entity->get('migration_group');
188     if (!$migration_group) {
189       $migration_group = 'default';
190     }
191 //    $this->addGroupParameter($operations['edit']['url'], $migration_group);
192 //    $this->addGroupParameter($operations['delete']['url'], $migration_group);
193     return $operations;
194   }
195
196   /**
197    * @param \Drupal\Core\Url $url
198    *   The URL associated with an operation.
199    *
200    * @param $migration_group
201    *   The migration's parent group.
202    */
203   protected function addGroupParameter(Url $url, $migration_group) {
204     if (!$migration_group) {
205       $migration_group = 'default';
206     }
207     $route_parameters = $url->getRouteParameters() + ['migration_group' => $migration_group];
208     $url->setRouteParameters($route_parameters);
209   }
210
211 }