Skip to content

Commit 93ce056

Browse files
committed
VACMS-16888: Makes a drupal command and adds to tasks.
1 parent 5aa3e5e commit 93ce056

File tree

3 files changed

+75
-131
lines changed

3 files changed

+75
-131
lines changed

docroot/modules/custom/va_gov_batch/src/cbo_scripts/ArchiveVaFormsIntranetOnly.php

Lines changed: 0 additions & 120 deletions
This file was deleted.

docroot/modules/custom/va_gov_migrate/src/Commands/Commands.php

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Drupal\va_gov_notifications\Service\NotificationsManager;
1313
use Drupal\va_gov_workflow\Service\Flagger;
1414
use Drush\Commands\DrushCommands;
15+
use League\Csv\Reader;
16+
use League\Csv\Statement;
1517
use Psr\Log\LoggerInterface;
1618
use Psr\Log\LogLevel;
1719

@@ -87,7 +89,7 @@ public function __construct(
8789
Flagger $flaggerservice,
8890
DataFetcherPluginManager $data_fetcher_plugin_manager,
8991
LoggerInterface $migrate_channel_logger,
90-
NotificationsManager $notifications_manager
92+
NotificationsManager $notifications_manager,
9193
) {
9294
parent::__construct();
9395
$this->database = $data_base;
@@ -138,6 +140,54 @@ public function cleanRevs() {
138140
$this->logger->warning('The following revisions were not deleted: ' . implode(', ', $missed_vids));
139141
}
140142

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+
141191
/**
142192
* Flag any facilities that no longer exist in Facilty API.
143193
*
@@ -210,18 +260,31 @@ public function flagMissingFacilities() {
210260
*/
211261
protected function archiveRemovedFacility(NodeInterface $facility) {
212262
$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();
217280
// Assign to CMS Migrator user.
218-
$facility->setRevisionUserId(1317);
281+
$node->setRevisionUserId(1317);
219282
// 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();
225288
}
226289

227290
/**

tasks-periodic.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ tasks:
109109
- drush $DRUSH_ALIAS scr scripts/tasks/VACMS-10735-Forms-CSV-migration-validation.php
110110
- drush $DRUSH_ALIAS migrate:reset-status va_node_form
111111
- drush $DRUSH_ALIAS migrate:import va_node_form
112+
- drush $DRUSH_ALIAS va_gov_migrate:archive-intranet-only-forms
112113

113114
va/background/daily/update/taxonomy_entity_index:
114115
desc: Update the taxonomy_entity_index nightly.

0 commit comments

Comments
 (0)