|
12 | 12 | use Drupal\va_gov_notifications\Service\NotificationsManager;
|
13 | 13 | use Drupal\va_gov_workflow\Service\Flagger;
|
14 | 14 | use Drush\Commands\DrushCommands;
|
| 15 | +use League\Csv\Reader; |
| 16 | +use League\Csv\Statement; |
15 | 17 | use Psr\Log\LoggerInterface;
|
16 | 18 | use Psr\Log\LogLevel;
|
17 | 19 |
|
@@ -87,7 +89,7 @@ public function __construct(
|
87 | 89 | Flagger $flaggerservice,
|
88 | 90 | DataFetcherPluginManager $data_fetcher_plugin_manager,
|
89 | 91 | LoggerInterface $migrate_channel_logger,
|
90 |
| - NotificationsManager $notifications_manager |
| 92 | + NotificationsManager $notifications_manager, |
91 | 93 | ) {
|
92 | 94 | parent::__construct();
|
93 | 95 | $this->database = $data_base;
|
@@ -138,6 +140,54 @@ public function cleanRevs() {
|
138 | 140 | $this->logger->warning('The following revisions were not deleted: ' . implode(', ', $missed_vids));
|
139 | 141 | }
|
140 | 142 |
|
| 143 | + /** |
| 144 | + * Archive IntranetOnly forms in the CMS. |
| 145 | + * |
| 146 | + * @command va_gov_migrate:archive-intranet-only-forms |
| 147 | + * @aliases va-gov-archive-intranet-only-forms |
| 148 | + */ |
| 149 | + public function archiveIntranetOnlyForms() { |
| 150 | + $csv = Reader::createFromPath(DRUPAL_ROOT . '/sites/default/files/migrate_source/va_forms_data.csv', 'r'); |
| 151 | + $csv->setHeaderOffset(0); |
| 152 | + $csv->setEnclosure('"'); |
| 153 | + $csv->setDelimiter(','); |
| 154 | + |
| 155 | + // Create a Statement. |
| 156 | + $stmt = (new Statement()) |
| 157 | + ->select('rowid') |
| 158 | + // Start from first record. |
| 159 | + ->offset(0) |
| 160 | + ->limit(-1) |
| 161 | + ->andWhere('IntranetOnly', '=', '1') |
| 162 | + // Get all records. |
| 163 | + ->orderByAsc('rowid'); |
| 164 | + |
| 165 | + // Process the CSV with the statement and filter for IntranetOnly = 1. |
| 166 | + $records = $stmt->process($csv); |
| 167 | + |
| 168 | + $intranet_only = []; |
| 169 | + foreach ($records as $record) { |
| 170 | + // Make an array of the rowids. |
| 171 | + $intranet_only[] = $record['rowid']; |
| 172 | + } |
| 173 | + // Get all the non-archived forms in the CMS that are IntranetOnly. |
| 174 | + $select = $this->database->select('node__field_va_form_row_id', 'nfvfri'); |
| 175 | + $select->join('content_moderation_state_field_data', 'cmsfd', 'nfvfri.entity_id = cmsfd.content_entity_id'); |
| 176 | + $select->fields('nfvfri', ['entity_id']) |
| 177 | + ->condition('nfvfri.field_va_form_row_id_value', $intranet_only, 'IN') |
| 178 | + ->condition('cmsfd.moderation_state', 'archived', '<>'); |
| 179 | + $nids = $select->execute()->fetchCol(); |
| 180 | + |
| 181 | + $forms_to_archive = $this->entityTypeManager->getStorage('node')->loadMultiple(array_values($nids)); |
| 182 | + $message = 'Archived due to being set IntranetOnly in Forms CSV.'; |
| 183 | + |
| 184 | + // Archive the nodes. |
| 185 | + foreach ($forms_to_archive as $form_to_archive) { |
| 186 | + $this->archiveNode($form_to_archive, $message); |
| 187 | + } |
| 188 | + |
| 189 | + } |
| 190 | + |
141 | 191 | /**
|
142 | 192 | * Flag any facilities that no longer exist in Facilty API.
|
143 | 193 | *
|
@@ -210,18 +260,31 @@ public function flagMissingFacilities() {
|
210 | 260 | */
|
211 | 261 | protected function archiveRemovedFacility(NodeInterface $facility) {
|
212 | 262 | $this->clearStatusData($facility);
|
213 |
| - $facility->set('moderation_state', 'archived'); |
214 |
| - $facility->setRevisionLogMessage('Archived due to removal from Facility API.'); |
215 |
| - $facility->setNewRevision(TRUE); |
216 |
| - $facility->setUnpublished(); |
| 263 | + $message = 'Archived due to removal from Facility API.'; |
| 264 | + $this->archiveNode($facility, $message); |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Archive a node. |
| 269 | + * |
| 270 | + * @param \Drupal\node\NodeInterface $node |
| 271 | + * The node to archive. |
| 272 | + * @param string $message |
| 273 | + * The revision message. |
| 274 | + */ |
| 275 | + protected function archiveNode(NodeInterface $node, string $message) { |
| 276 | + $node->set('moderation_state', 'archived'); |
| 277 | + $node->setRevisionLogMessage($message); |
| 278 | + $node->setNewRevision(TRUE); |
| 279 | + $node->setUnpublished(); |
217 | 280 | // Assign to CMS Migrator user.
|
218 |
| - $facility->setRevisionUserId(1317); |
| 281 | + $node->setRevisionUserId(1317); |
219 | 282 | // Prevents some other actions.
|
220 |
| - $facility->setSyncing(TRUE); |
221 |
| - $facility->setChangedTime(time()); |
222 |
| - $facility->isDefaultRevision(TRUE); |
223 |
| - $facility->setRevisionCreationTime(time()); |
224 |
| - $facility->save(); |
| 283 | + $node->setSyncing(TRUE); |
| 284 | + $node->setChangedTime(time()); |
| 285 | + $node->isDefaultRevision(TRUE); |
| 286 | + $node->setRevisionCreationTime(time()); |
| 287 | + $node->save(); |
225 | 288 | }
|
226 | 289 |
|
227 | 290 | /**
|
|
0 commit comments