diff --git a/includes/TripalImporter/EutilsImporter.inc b/includes/TripalImporter/EutilsImporter.inc new file mode 100644 index 0000000..bc4ef13 --- /dev/null +++ b/includes/TripalImporter/EutilsImporter.inc @@ -0,0 +1,137 @@ + FALSE, + 'file_local' => FALSE, + 'file_remote' => FALSE, + ]; + public static $file_required = FALSE; + + /** + * Implements hook_form(). + * + * @see TripalImporter::form() + */ + public function form($form, &$form_state) { + + $form['instructions'] = [ + '#markup' => t('

Please enter an accession and specify a database.

+

Press the Preview Record button to view the + retrieved data and metadata. Pressing Create Chado + Record will create the record.

'), + ]; + + $db_choices = [ + 'bioproject' => 'BioProject', + 'biosample' => 'Biosample', + 'assembly' => 'Assembly', + ]; + + $form['db'] = [ + '#type' => 'radios', + '#title' => t('NCBI Database'), + '#description' => t('The database to query.'), + '#options' => $db_choices, + ]; + + $form['accession'] = [ + '#type' => 'textfield', + '#title' => t('NCBI Accession Number'), + '#description' => t('Valid examples: (BioSample 744358 120060 SAMN02261463), (Assembly 91111, 751381, GCA_000516895.1), (BioProject 12384, 394253, 66853, PRJNA185471)'), + ]; + + $form['callback'] = [ + '#type' => 'button', + '#value' => "Preview Record", + ]; + + if (isset($form_state['values']['parsed'])) { + $form['data'] = [ + '#type' => 'fieldset', + '#title' => 'Data', + ]; + + $form['data'][] = $form_state['values']['parsed']; + + } + + $form['options'] = [ + '#type' => 'fieldset', + '#title' => "Options", + ]; + + $form['options']['linked_records'] = [ + '#type' => 'checkbox', + '#title' => t('Create Linked Records'), + '#description' => t('Each accession links to other NCBI databases: + you can create those chado records as well.'), + '#default_value' => 1, + ]; + + return $form; + } + + /** + * Validate form keys and check the accession on NCBI. + * + * @see TripalImporter::formValidate() + * TODO only run the EUTils check if submitted. + */ + public function formValidate($form, &$form_state) { + + $vals = $form_state['values']; + + $db = $vals['db']; + $accession = $vals['accession']; + + if (!$db) { + form_set_error('db', 'please select a valid db'); + } + + if (!$accession) { + form_set_error('accession', 'please enter an accession'); + } + + if (!$db or !$accession) { + return; + } + + $connection = new \EUtils(); + try { + $connection->setPreview(); + $parsed = $connection->get($db, $accession); + $form_state['values']['parsed'] = $parsed; + } + catch (\Exception $e) { + tripal_set_message($e->getMessage(), TRIPAL_ERROR); + return; + } + } + + /** + * Runs the importer. + * + * @see TripalImporter::run() + */ + public function run() { + $arguments = $this->arguments['run_args']; + $db = $arguments['db']; + $accession = $arguments['accession']; + $create_linked_records = $arguments['linked_records']; + + tripal_eutils_create_records($db, $accession, $create_linked_records); + + } + +} diff --git a/includes/tripal_euitils_import.form.inc b/includes/tripal_euitils_import.form.inc deleted file mode 100644 index a5d8fd3..0000000 --- a/includes/tripal_euitils_import.form.inc +++ /dev/null @@ -1,129 +0,0 @@ - t('

Please enter an accession and specify a database.

-

Press the Preview Record button to view the - retrieved data and metadata. Pressing Create Chado - Record will create the record.

'), - ]; - - $db_choices = [ - 'bioproject' => 'BioProject', - 'biosample' => 'Biosample', - 'assembly' => 'Assembly', - ]; - - $form['db'] = [ - '#type' => 'radios', - '#title' => t('NCBI Database'), - '#description' => t('The database to query.'), - '#options' => $db_choices, - ]; - - $form['accession'] = [ - '#type' => 'textfield', - '#title' => t('NCBI Accession Number'), - '#description' => t('Valid examples: (BioSample 744358 120060 SAMN02261463), (Assembly 91111, 751381, GCA_000516895.1), (BioProject 12384, 394253, 66853, PRJNA185471)'), - ]; - - $form['callback'] = [ - '#type' => 'button', - '#value' => "Preview Record", - ]; - - if (isset($form_state['values']['parsed'])) { - $form['data'] = [ - '#type' => 'fieldset', - '#title' => 'Data', - ]; - - $form['data'][] = $form_state['values']['parsed']; - - } - - $form['options'] = [ - '#type' => 'fieldset', - '#title' => "Options", - ]; - - $form['options']['linked_records'] = [ - '#type' => 'checkbox', - '#title' => t('Create Linked Records'), - '#description' => t('Each accession links to other NCBI databases: - you can create those chado records as well.'), - '#default_value' => 1, - ]; - - $form['submit'] = [ - '#type' => 'submit', - '#value' => "Create Chado Record", - ]; - - return $form; -} - -/** - * Implements hook_form_validate(). - */ -function tripal_eutils_import_form_validate($form, &$form_state) { - - $vals = $form_state['values']; - - $db = $vals['db']; - $accession = $vals['accession']; - - if (!$db) { - form_set_error('db', 'please select a valid db'); - } - - if (!$accession) { - form_set_error('accession', 'please enter an accession'); - } - - - if (!$db or !$accession) { - return; - } - - $connection = new \EUtils(); - try { - $connection->setPreview(); - $parsed = $connection->get($db, $accession); - $form_state['values']['parsed'] = $parsed; - } - catch (\Exception $e) { - tripal_set_message($e->getMessage(), TRIPAL_ERROR); - return; - } -} - -/** - * Implements hook_form_submit(). - */ -function tripal_eutils_import_form_submit($form, &$form_state) { - global $user; - - $vals = $form_state['values']; - $db = $vals['db']; - $accession = $vals['accession']; - $create_linked_records = (bool) $vals['linked_records']; - - tripal_add_job("EUtils: Create Records for {$db}:{$accession}", - 'tripal_eutils', 'tripal_eutils_create_records', [ - $db, - $accession, - $create_linked_records, - ], $user->uid); - - drupal_set_message('Job to create records have been submitted successfully.'); -} diff --git a/tests/ImportFormTest.php b/tests/ImportFormTest.php index 243deb9..987610e 100644 --- a/tests/ImportFormTest.php +++ b/tests/ImportFormTest.php @@ -16,7 +16,7 @@ class ImportFormTest extends TripalTestCase { public function testAccessibilityToImportForm() { $this->actingAs(1); - $response = $this->get('admin/tripal/loaders/eutils_ncbi_import'); + $response = $this->get('admin/tripal/loaders/eutils_loader'); $response->assertSuccessful() ->assertSee('NCBI Accession Number'); }