123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- <?php
- /**
- * @file
- * @todo Add file header description
- */
- /**
- * Inserts/Updates a tripal bulk loading job constant
- *
- * @param $nid
- * The node ID of the the tripal bulk loading job the constant is associated with
- * @param $table
- * The chado table the constant is associated with
- * @param $field
- * The chado field the constant is associated with
- * @param $record_id
- * The index in the template array for this record
- * @param $field_id
- * The index in the template array for this field
- *
- * NOTE: $template_array[$record_id]['table'] = $table and $template_array[$record_id]['fields'][$field_id]['field'] = $field
- * both are included as a means of double-checking the constant still is still in thesame place in the template array.
- * For example, that the template was not edited and the records moved around after the job was submitted but before it was run.
- *
- * @return
- * On success it returns the object (with primary key if inserted);
- * on failure it returns FALSE
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_update_constant($nid, $group_id, $table, $field, $record_id, $field_id, $value) {
- $record = array(
- 'nid' => $nid,
- 'group_id' => $group_id,
- 'chado_table' => $table,
- 'chado_field' => $field,
- 'record_id' => $record_id,
- 'field_id' => $field_id,
- 'value' => $value
- );
- // Check to see if already exists
- $exists = db_fetch_object(db_query(
- "SELECT constant_id FROM {tripal_bulk_loader_constants} WHERE nid=%d AND record_id=%d AND field_id=%d AND group_id=%d",
- $record['nid'],
- $record['record_id'],
- $record['field_id'],
- $record['group_id']
- ));
- if ($exists->constant_id) {
- $record['constant_id'] = $exists->constant_id;
- $status = drupal_write_record('tripal_bulk_loader_constants', $record, 'constant_id');
- if ($status) {
- return $record;
- }
- else {
- return FALSE;
- }
- }
- else {
- $status = drupal_write_record('tripal_bulk_loader_constants', $record);
- if ($status) {
- return $record;
- }
- else {
- return FALSE;
- }
- }
- }
- /**
- * Check if a bulk loading job has exposed constants
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_has_exposed_fields($node) {
- // exposed fields isn't set
- if (!isset($node->exposed_fields)) {
- return FALSE;
- }
- // exposed fields has at least one element
- if (sizeof($node->exposed_fields) == 1) {
- // need to check if single element is an empty array
- $element = reset($node->exposed_fields);
- if ($element) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- elseif (sizeof($node->exposed_fields) > 1) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- return FALSE;
- }
- ///////////////////////////////////////////////////////////
- // Set Constants Form (on Bulk Loader Node)
- ///////////////////////////////////////////////////////////
- /**
- * Set constants (exposed fields in template)
- *
- * @param $form_state
- * The current state of the form
- * @param $node
- * The node to set constants for
- *
- * @return
- * A form array to be rendered by drupal_get_form()
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_set_constants_form($form_state, $node) {
- $form = array();
- $form['nid'] = array(
- '#type' => 'hidden',
- '#value' => $node->nid
- );
- if (!tripal_bulk_loader_has_exposed_fields($node)) {
- return $form;
- }
- $form['exposed_array'] = array(
- '#type' => 'hidden',
- '#value' => serialize($node->exposed_fields),
- );
- $form['exposed_fields'] = array(
- '#type' => 'fieldset',
- '#title' => t('Constant Values'),
- '#collapsible' => TRUE,
- '#collapsed' => ($node->template_id) ? FALSE : TRUE,
- '#prefix' => '<div id="set-constants">',
- '#suffix' => '</div>',
- );
- // Display table of already added constant sets with the ability to re-arrange and delete
- $first_constant = reset($node->constants);
- if (sizeof($node->constants) > 0 AND !empty($first_constant)) {
- $form['exposed_fields']['explanation-1'] = array(
- '#type' => 'item',
- '#value' => t('You have already added constants to this bulk loading job. Each '
- .'row in the following table represents a set of constants. Each set will be used '
- .'to load your data file with the specified template resulting in the each record '
- .'in the template to be loaded x number of times where there are x sets of '
- .'constants (rows in the following table).')
- );
- $form['exposed_fields']['existing'] = array(
- '#tree' => TRUE,
- );
- foreach ($node->constants as $set) {
- foreach ($set as $record) {
- foreach ($record as $field) {
- $index = $field['record_id'] . '-' . $field['field_id'];
- $group = $field['group_id'];
- $form['exposed_fields']['existing'][$group][$index] = array(
- '#type' => 'markup',
- '#value' => filter_xss($field['value']),
- );
- }
- }
- $form['exposed_fields']['existing'][$group]['delete'] = array(
- '#type' => 'markup',
- '#value' => filter_xss(l(t('Edit'), 'node/' . $node->nid . '/constants/' . $group . '/edit') . ' | ' .
- l(t('Delete'), 'node/' . $node->nid . '/constants/' . $group . '/delete')),
- );
- }
- }
- $form['exposed_fields']['new'] = array(
- '#type' => 'fieldset',
- '#title' => t('New set of Constants'),
- );
- $form['exposed_fields']['new']['explanation-2'] = array(
- '#type' => 'item',
- '#value' => t('The following fields are constants in the selected template that you need to set values for.')
- );
- // Add textifelds for exposed fields of the current template
- $exposed_fields = FALSE;
- $indexes = array();
- if (tripal_bulk_loader_has_exposed_fields($node)) {
- foreach ($node->exposed_fields as $exposed_index) {
- $record_id = $exposed_index['record_id'];
- $field_id = $exposed_index['field_id'];
- $field = $node->template->template_array[$record_id]['fields'][$field_id];
- if ($field['exposed']) {
- $exposed_fields = TRUE;
- $indexes[$record_id][] = $field_id;
- switch ($field['type']) {
- case 'table field':
- $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
- '#type' => 'textfield',
- '#title' => t('%title', array('%title' => $field['title'])),
- '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
- '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
- );
- break;
- case 'constant':
- $form['exposed_fields']['new'][$record_id . '-' . $field_id] = array(
- '#type' => 'textfield',
- '#title' => t('%title', array('%title' => $field['title']) ),
- '#description' => t('Enter the case-sensitive value of this constant for your data file'),
- '#default_value' => (isset($node->constants[$record_id][$field_id]['value'])) ? $node->constants[$record_id][$field_id]['value'] : $field['constant value'],
- );
- break;
- }
- $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-table'] = array(
- '#type' => 'hidden',
- '#value' => $node->template->template_array[$record_id]['table'],
- );
- $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-field'] = array(
- '#type' => 'hidden',
- '#value' => $field['field'],
- );
- $form['exposed_fields']['new'][$record_id . '-' . $field_id . '-type'] = array(
- '#type' => 'hidden',
- '#value' => $field['type'],
- );
- }
- }
- }
- $form['template'] = array(
- '#type' => 'hidden',
- '#value' => serialize($node->template->template_array)
- );
- $form['exposed_fields']['new']['indexes'] = array(
- '#type' => 'hidden',
- '#value' => serialize($indexes),
- );
- if (!$exposed_fields) {
- $form['exposed_fields']['new']['explanation'] = array(
- '#type' => 'item',
- '#value' => t('There are no exposed fields for this template.')
- );
- }
- $form['exposed_fields']['new']['submit-2'] = array(
- '#type' => 'submit',
- '#name' => 'add_constant',
- '#value' => t('Add Constant Set')
- );
- return $form;
- }
- /**
- * Validate that the values entered exist in the database
- * if indicated in hte template array
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_set_constants_form_validate($form, $form_state) {
- $template = unserialize($form_state['values']['template']);
- $indexes = unserialize($form_state['values']['indexes']);
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
- if (strcmp('Add Constant Set', $op) == 0) {
- foreach ($indexes as $record_id => $array) {
- foreach ($array as $field_id) {
- if ($template[$record_id]['fields'][$field_id]['exposed_validate']) {
- $result = db_fetch_object(chado_query(
- "SELECT 1 as valid FROM {%s} WHERE %s='%s'",
- $template[$record_id]['table'],
- $template[$record_id]['fields'][$field_id]['field'],
- $form_state['values'][$record_id . '-' . $field_id]
- ));
- if (!$result->valid) {
- $msg = 'A ' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'] . ' of "' . $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value'] . '" must already exist!';
- form_set_error($record_id . '-' . $field_id, $msg);
- }
- else {
- drupal_set_message(
- t(
- 'Confirmed a %title of "%value" already exists.',
- array(
- '%title' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#title'],
- '%value' => $form['exposed_fields']['new'][$record_id . '-' . $field_id]['#value']
- )
- )
- );
- }
- }
- }
- }
- }
- }
- /**
- * Insert/update the constants associated with this node
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_set_constants_form_submit($form, $form_state) {
- // Insert/Update constants
- $template = unserialize($form_state['values']['template']);
- $indexes = unserialize($form_state['values']['indexes']);
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
- if (strcmp('Add Constant Set', $op) == 0) {
- $max_group = db_fetch_object(db_query("SELECT max(group_id) as value FROM {tripal_bulk_loader_constants} WHERE nid=%d", $form_state['values']['nid']));
- foreach ($indexes as $record_id => $array) {
- foreach ($array as $field_id) {
- tripal_bulk_loader_update_constant(
- $form_state['values']['nid'],
- $max_group->value+1,
- $form_state['values'][$record_id . '-' . $field_id . '-table'],
- $form_state['values'][$record_id . '-' . $field_id . '-field'],
- $record_id,
- $field_id,
- $form_state['values'][$record_id . '-' . $field_id]
- );
- }
- }
- }
- }
- /**
- * Themes the bulk loading job set constants form
- *
- * @ingroup tripal_bulk_loader
- */
- function theme_tripal_bulk_loader_set_constants_form($form) {
- $output = '';
- $exposed_fields = unserialize($form['exposed_array']['#value']);
- // need to put in the context of a node so we can use the has_exposed_fields function
- if ($exposed_fields) {
- $node->exposed_fields = $exposed_fields;
- }
- else {
- $node->exposed_fields = array();
- }
- // Add draggable table for constant sets
- if (tripal_bulk_loader_has_exposed_fields($node)) {
- $i=1;
- foreach (element_children($form['exposed_fields']['existing']) as $key) {
- $element = &$form['exposed_fields']['existing'][$key];
- $element['group']['#attributes']['class'] = 'weight-group';
- $row = array();
- foreach ($exposed_fields as $exposed) {
- if ($i==1) {
- $header[] = $exposed['title'];
- }
- $k = $exposed['record_id'] . '-' . $exposed['field_id'];
- $row[] = drupal_render($element[$k]);
- }
- $row[] = drupal_render($element['delete']);
- $row[] = drupal_render($element['group']) . drupal_render($element['id']);
- if (!empty($row[0])) {
- $rows[] = array('data' => $row, 'class' => 'draggable');
- }
- $i++;
- }
- //drupal_add_tabledrag('mytable', 'order', 'sibling', 'weight-group');
- // @coder-ignore: no user input thus don't need to filter
- $form['exposed_fields']['existing'] = array(
- '#type' => 'markup',
- '#value' => theme('table', $header, $rows, array('id' => 'mytable')) . '<br />',
- );
- }
- $output .= drupal_render($form);
- return $output;
- }
- ///////////////////////////////////////////////////////////
- // Set Constants Form (on Bulk Loader Node)
- ///////////////////////////////////////////////////////////
- /**
- * Edit a constant set (exposed fields in template)
- *
- * @param $form_state
- * The current state of the form
- * @param $node
- * The node to set constants for
- * @param $group_id
- * The constant set to edit
- *
- * @return
- * A form array to be rendered by drupal_get_form()
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_edit_constant_set_form($form_state, $node, $group_id) {
- $form = array();
- $form['#redirect'] = 'node/' . $node->nid;
- $form['nid'] = array(
- '#type' => 'hidden',
- '#value' => $node->nid,
- );
- $form['group_id'] = array(
- '#type' => 'hidden',
- '#value' => $group_id,
- );
- $form['explanation'] = array(
- '#type' => 'item',
- '#value' => t('The following fields are constants in the selected template that you need to set values for.')
- );
- // Add textifelds for exposed fields of the current template
- $exposed_fields = FALSE;
- $indexes = array();
- if (tripal_bulk_loader_has_exposed_fields($node)) {
- foreach ($node->exposed_fields as $exposed_index) {
- $record_id = $exposed_index['record_id'];
- $record = $node->template->template_array[$record_id];
- $field_id = $exposed_index['field_id'];
- $field = $node->template->template_array[$record_id]['fields'][$field_id];
- if ($field['exposed']) {
- $exposed_fields = TRUE;
- $indexes[$record_id][] = $field_id;
- switch ($field['type']) {
- case 'table field':
- $form[$record_id . '-' . $field_id] = array(
- '#type' => 'textfield',
- '#title' => t('%title', array('%title' => $field['title'])),
- '#description' => t('%exposed_description', array('%exposed_description' => $field['exposed_description'])),
- '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
- );
- break;
- case 'constant':
- $form[$record_id . '-' . $field_id] = array(
- '#type' => 'textfield',
- '#title' => t('%title', array('%title' => $field['title'])),
- '#description' => t('Enter the case-sensitive value of this constant for your data file'),
- '#default_value' => (isset($node->constants[$group_id][$record_id][$field_id]['value'])) ? $node->constants[$group_id][$record_id][$field_id]['value'] : $field['constant value'],
- );
- break;
- }
- $form[$record_id . '-' . $field_id . '-table'] = array(
- '#type' => 'hidden',
- '#value' => $record['table'],
- );
- $form[$record_id . '-' . $field_id . '-field'] = array(
- '#type' => 'hidden',
- '#value' => $field['field'],
- );
- $form[$record_id . '-' . $field_id . '-type'] = array(
- '#type' => 'hidden',
- '#value' => $field['type'],
- );
- }
- }
- }
- $form['template'] = array(
- '#type' => 'hidden',
- '#value' => serialize($node->template->template_array)
- );
- $form['indexes'] = array(
- '#type' => 'hidden',
- '#value' => serialize($indexes),
- );
- $form['save'] = array(
- '#type' => 'submit',
- '#value' => 'Save',
- );
- $form['cancel'] = array(
- '#type' => 'submit',
- '#value' => 'Cancel',
- );
- return $form;
- }
- /**
- * Edit constants in the current constant set
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_edit_constant_set_form_submit($form, $form_state) {
- // Update constants
- $template = unserialize($form_state['values']['template']);
- $indexes = unserialize($form_state['values']['indexes']);
- $op = $form_state['values'][ $form_state['clicked_button']['#name'] ];
- if (strcmp('Save', $op) == 0) {
- foreach ($indexes as $record_id => $array) {
- foreach ($array as $field_id) {
- tripal_bulk_loader_update_constant(
- $form_state['values']['nid'],
- $form_state['values']['group_id'],
- $form_state['values'][$record_id . '-' . $field_id . '-table'],
- $form_state['values'][$record_id . '-' . $field_id . '-field'],
- $record_id,
- $field_id,
- $form_state['values'][$record_id . '-' . $field_id]
- );
- }
- }
- drupal_set_message(t('The constant set was successfully updated.'));
- }
- }
- /**
- * Delete a constant set (exposed fields in template)
- *
- * @param $form_state
- * The current state of the form
- * @param $node
- * The node to set constants for
- * @param $group_id
- * The constant set to delete
- *
- * @return
- * A form array to be rendered by drupal_get_form()
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_delete_constant_set_form($form_state, $node, $group_id) {
- $form = array();
- $form['#redirect'] = 'node/' . $node->nid;
- $form['nid'] = array(
- '#type' => 'value',
- '#value' => $node->nid,
- );
- $form['group_id'] = array(
- '#type' => 'hidden',
- '#value' => $group_id,
- );
- return confirm_form($form,
- t('Are you sure you want to delete this constant set?'),
- 'node/' . $node->nid,
- t('This action cannot be undone.'),
- t('Delete'),
- t('Cancel')
- );
- }
- /**
- * Delete the current constant set
- *
- * @ingroup tripal_bulk_loader
- */
- function tripal_bulk_loader_delete_constant_set_form_submit($form, $form_state) {
- $group_id = $form_state['values']['group_id'];
- $nid = $form_state['values']['nid'];
- if ($nid && $form_state['values']['confirm']) {
- db_query("DELETE FROM {tripal_bulk_loader_constants} WHERE nid=%d AND group_id=%d", $nid, $group_id);
- drupal_set_message(t('Constant set successfully deleted.'));
- }
- }
|