123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- /**
- * Add a track to an instance form.
- *
- * @param $form
- * @param $form_state
- * @param $instance_id
- *
- * @return array
- */
- function tripal_jbrowse_mgmt_add_track_form($form, &$form_state, $instance_id) {
- if (empty(tripal_jbrowse_mgmt_get_instance($instance_id))) {
- drupal_not_found();
- return [];
- }
- $form['label'] = [
- '#type' => 'textfield',
- '#title' => t('Track Label'),
- '#description' => t('This will appear on the sidebar.'),
- '#required' => TRUE,
- ];
- $form['instance_id'] = [
- '#type' => 'hidden',
- '#value' => $instance_id,
- ];
- $form['data'] = [
- '#type' => 'fieldset',
- '#title' => t('Track Files'),
- ];
- $form['data']['track_type'] = [
- '#type' => 'select',
- '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
- '#required' => TRUE,
- '#title' => t('Track Type'),
- '#options' => drupal_map_assoc(tripal_jbrowse_mgmt_get_track_types()),
- ];
- $form['data']['file_type'] = [
- '#type' => 'select',
- '#title' => t('File Type'),
- '#options' => drupal_map_assoc(['gff', 'bed', 'gbk', 'vcf', 'bw']),
- '#description' => t('See http://gmod.org/wiki/JBrowse_Configuration_Guide#flatfile-to-json.pl for more info.'),
- '#required' => TRUE,
- ];
- $form['data']['file'] = [
- '#type' => 'file',
- '#title' => t('File'),
- ];
- $form['data']['file2'] = [
- '#type' => 'file',
- '#title' => t('TBI File'),
- '#states' => [
- 'visible' => [
- ':input[name="file_type"]' => ['value' => 'vcf'],
- ],
- ],
- ];
- $form['data']['file_path'] = [
- '#type' => 'textfield',
- '#title' => t('- OR Path to File on Server -'),
- '#description' => t('This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'),
- '#states' => [
- 'invisible' => [
- ':input[name="file_type"]' => ['value' => 'vcf'],
- ],
- ],
- ];
- $form['data']['dir_path'] = [
- '#type' => 'textfield',
- '#title' => t('- OR Path to Directory on Server -'),
- '#description' => t('This path will be ignored if a file is provided above. ' . 'The directory must contain both the .tbi and .gz files.'),
- '#states' => [
- 'visible' => [
- ':input[name="file_type"]' => ['value' => 'vcf'],
- ],
- ],
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => 'Add New Track',
- ];
- return $form;
- }
- /**
- * Validate the add track form.
- *
- * @param array $form
- * @param array $form_state
- */
- function tripal_jbrowse_mgmt_add_track_form_validate($form, &$form_state) {
- $values = $form_state['values'];
- $file = $_FILES['files']['tmp_name']['file'];
- $settings = tripal_jbrowse_mgmt_get_settings();
- $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
- $data = $settings['data_dir'];
- $file_type = $values['file_type'];
- $path = NULL;
- switch ($file_type) {
- case 'vcf':
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title) . '/data/vcf';
- break;
- case 'bw':
- $path = $data . '/' . tripal_jbrowse_mgmt_make_slug($instance->title) . '/data/wig';
- break;
- }
- switch ($file_type) {
- case 'vcf':
- $tbi = $_FILES['files']['tmp_name']['file2'];
- $local_dir = isset($values['dir_path']) ? $values['dir_path'] : NULL;
- if (empty($file) && empty($tbi) && empty($local_dir)) {
- form_set_error('file',
- 'Please provide a local directory path or upload files.');
- }
- elseif (empty($file) && empty($tbi) && !empty($local_dir)) {
- if (!file_exists($local_dir)) {
- form_set_error('file_path', 'The directory provided does not exist.');
- }
- else {
- if (!is_dir($local_dir)) {
- form_set_error('file_path',
- 'The file provided is not a directory.');
- }
- else {
- $file_gz = glob($local_dir . '/*.vcf.gz');
- $file_tbi = glob($local_dir . '/*.vcf.gz.tbi');
- if (count($file_gz) != 1 || count($file_tbi) != 1) {
- form_set_error('file_path',
- 'Please provide a directory with exactly one gz and one tbi file.');
- }
- else {
- if (!tripal_jbrowse_mgmt_copy_file($file_gz[0], $path)) {
- form_set_error('Failed to copy file' . $file_gz[0] . ' to ' . $path);
- }
- else {
- if (!tripal_jbrowse_mgmt_copy_file($file_tbi[0], $path)) {
- form_set_error('Failed to copy file' . $file_gz[0] . ' to ' . $path);
- }
- }
- }
- }
- }
- }
- elseif (empty($file) && !empty($tbi)) {
- form_set_error('file', 'Please upload both a gz and a tbi file.');
- }
- elseif (!empty($file) && empty($tbi)) {
- form_set_error('file2', 'Please upload both a gz and a tbi file.');
- }
- else {
- $gz_uploaded = tripal_jbrowse_mgmt_upload_file('file');
- if (!$gz_uploaded) {
- form_set_error('file', 'Unable to upload file');
- }
- else {
- $gz_uploaded = tripal_jbrowse_mgmt_move_file($gz_uploaded, $path);
- if (!isset($gz_uploaded)) {
- form_set_error('file', 'Failed to move gz file to ' . $path . '.');
- }
- else {
- $form_state['values']['uploaded_gz'] = $gz_uploaded;
- }
- }
- $tbi_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
- if (!$tbi_uploaded) {
- form_set_error('file2', 'Unable to upload file');
- }
- else {
- $tbi_uploaded = tripal_jbrowse_mgmt_move_file($tbi_uploaded, $path);
- if (!isset($tbi_uploaded)) {
- form_set_error('file2',
- 'Failed to move tbi file to ' . $path . '.');
- }
- else {
- $form_state['values']['uploaded_tbi'] = $tbi_uploaded;
- }
- }
- }
- break;
- default:
- $local_file = isset($values['file_path']) ? $values['file_path'] : NULL;
- if (empty($file) && empty($local_file)) {
- form_set_error('file',
- 'Please provide a local file path or upload a new file.');
- }
- elseif (empty($file) && !empty($local_file)) {
- if (!file_exists($local_file)) {
- form_set_error('file_path', 'The file path provided does not exist.');
- }
- else {
- if (!tripal_jbrowse_mgmt_copy_file($local_file, $path)) {
- form_set_error('Failed to copy file ' . $local_file . ' to ' . $path);
- }
- }
- }
- else {
- $uploaded = tripal_jbrowse_mgmt_upload_file('file');
- if (!$uploaded) {
- form_set_error('file', 'Unable to upload file');
- }
- else {
- $uploaded = tripal_jbrowse_mgmt_move_file($uploaded, $path);
- if (!isset($uploaded)) {
- form_set_error('file', 'Failed to move file to ' . $path);
- }
- else {
- $form_state['values']['uploaded_file'] = $uploaded;
- }
- }
- }
- break;
- }
- }
- /**
- * Handle form submission for adding a track.
- *
- * @param array $form
- * @param array $form_state
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_add_track_form_submit($form, &$form_state) {
- global $user;
- $values = $form_state['values'];
- $file = isset($values['file_path']) ? $values['file_path'] : NULL;
- if (!empty($values['dir_path'])) {
- $file = $values['dir_path'];
- }
- if (!empty($values['uploaded_gz'])) {
- $file = $values['uploaded_gz'];
- }
- if (!empty($values['uploaded_file'])) {
- $file = $values['uploaded_file'];
- }
- $instance = tripal_jbrowse_mgmt_get_instance($values['instance_id']);
- $track_id = tripal_jbrowse_mgmt_create_track($instance, [
- 'label' => $values['label'],
- 'track_type' => $values['track_type'],
- 'file_type' => $values['file_type'],
- 'file' => $file,
- 'created_at' => time(),
- ]);
- tripal_add_job('Add JBrowse track to ' . $instance->title,
- 'tripal_jbrowse_mgmt', 'tripal_jbrowse_mgmt_add_track_to_instance', [$track_id],
- $user->uid);
- drupal_goto('admin/tripal_jbrowse_mgmt/instances/' . $instance->id);
- }
- /**
- * Delete a track form.
- *
- * @param array $form
- * @param array $form_state
- * @param int $track_id
- *
- * @return array
- */
- function tripal_jbrowse_mgmt_delete_track_form($form, &$form_state, $track_id) {
- $track = tripal_jbrowse_mgmt_get_track($track_id);
- if (!$track->id) {
- $form['error'] = [
- '#type' => 'item',
- '#markup' => '<p style="color: red">Unable to find track.</p>',
- ];
- return $form;
- }
- $form['description'] = [
- '#type' => 'item',
- '#markup' => 'Are you sure you want to delete the ' . $track->label . ' track?',
- ];
- $form['track_id'] = [
- '#type' => 'hidden',
- '#value' => $track_id,
- ];
- $form['submit'] = [
- '#type' => 'submit',
- '#value' => 'Delete Track',
- ];
- $form['cancel'] = [
- '#type' => 'markup',
- '#markup' => l('Cancel',
- 'admin/tripal_jbrowse_mgmt/instances/' . $track->instance_id),
- ];
- return $form;
- }
- /**
- * @param $form
- * @param $form_state
- *
- * @throws \Exception
- */
- function tripal_jbrowse_mgmt_delete_track_form_submit($form, &$form_state) {
- global $user;
- $values = $form_state['values'];
- $track = tripal_jbrowse_mgmt_get_track($values['track_id']);
- tripal_add_job('Delete JBrowse track', 'tripal_jbrowse_mgmt',
- 'tripal_jbrowse_mgmt_delete_track_from_instance', [$values['track_id']],
- $user->uid);
- tripal_jbrowse_mgmt_update_track($track, ['is_deleted' => 1]);
- drupal_goto('admin/tripal_jbrowse_mgmt/instances/' . $track->instance_id);
- }
|