');
$breadcrumb[] = l('Administration', 'admin');
$breadcrumb[] = l('Tripal', 'admin/tripal');
$breadcrumb[] = l('Chado Modules', 'admin/tripal/legacy');
$breadcrumb[] = l('Vocabularies', 'admin/tripal/legacy/vocab');
drupal_set_breadcrumb($breadcrumb);
// Add the view
$cvs_view = views_embed_view('tripal_cv_admin_cvs','default');
$cvterms_view = views_embed_view('tripal_cv_admin_cvterms','default');
if (isset($cvs_view) && isset($cvterms_view)) {
$output .= $cvs_view;
}
else {
$output .= '
The Tripal Controlled Vocabulary module uses primarily views to provide an '
. 'administrative interface. Currently one or more views needed for this '
. 'administrative interface are disabled. Click each of the following links to '
. 'enable the pertinent views:
';
$output .= '';
if (!isset($cvs_view)) {
$output .= '- '.l('Tripal Vocabularies', 'admin/tripal/vocab/views/cvs/enable').'
';
}
if (!isset($cvterm_view)) {
$output .= '- '.l('Tripal Vocabulary Terms', 'admin/tripal/vocab/views/cvterms/enable').'
';
}
$output .= '
';
}
return $output;
}
/**
* Provides the actual "Select CV" form on the Update/Delete Controlled
* Vocabulary page. This form also triggers the edit javascript
* @todo Modify this form to use Drupal AJAX
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_edit_form($form, &$form_state) {
// get the cv_d if form was submitted via AJAX
$cv_id = 0;
if (array_key_exists('values', $form_state)) {
$cv_id = $form_state['values']['cv_id'];
}
elseif (isset($form_state['build_info']['args'][0])) {
$cv_id = $form_state['build_info']['args'][0];
}
// get a list of db from chado for user to choose
$sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
$results = chado_query($sql);
$cvs = array();
$cvs[] = 'Select a vocabulary';
foreach ($results as $cv) {
$cvs[$cv->cv_id] = $cv->name;
}
$form['cv_id'] = array(
'#title' => t('Controlled Vocabulary Name'),
'#type' => 'select',
'#options' => $cvs,
'#ajax' => array(
'callback' => 'tripal_cv_edit_form_ajax',
'wrapper' => 'cv-edit-div',
'effect' => 'fade',
'event' => 'change',
'method' => 'replace',
),
'#default_value' => $cv_id,
);
// if we don't have a db_id then we can return the form, otherwise
// add in the other fields
if ($cv_id) {
tripal_cv_add_cv_form_fields($form, $form_state, $cv_id);
$form['update'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
);
}
else {
// if we don't have a dbid then this is the first time the form has
// benn loaded and we need to create the div where ajax replacement elements get stored
$form['div_replace'] = array(
'#type' => 'item',
'#prefix' => '',
'#suffix' => '
',
);
}
return $form;
}
/**
* Form to add contolled vocabularies
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_add_form($form, &$form_state) {
// add in the form fields to this form
tripal_cv_add_cv_form_fields($form, $form_state);
$form['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#weight' => 5,
);
return $form;
}
/**
* Form fields in common between the cv add & edit form.
*
* @ingroup tripal_cv
*/
function tripal_cv_add_cv_form_fields(&$form, $form_state, $cv_id = NULL) {
$default_name = '';
$default_desc = '';
if ($cv_id) {
$values = array('cv_id' => $cv_id);
$result = chado_select_record('cv', array('*'), $values);
$cv = $result[0];
$default_name = $cv->name;
$default_desc = $cv->definition;
}
// add a fieldset for the Drupal Schema API
$form = array(
'#type' => 'fieldset',
'#title' => 'Controlled Vocabulary Details',
'#collapsible' => 0,
);
$form['name']= array(
'#type' => 'textfield',
'#title' => t("Controlled Vocabulary name"),
'#description' => t('Please enter the name for this vocabulary.'),
'#required' => TRUE,
'#default_value' => $default_name,
'#maxlength' => 255,
);
$form['definition']= array(
'#type' => 'textarea',
'#title' => t('Description'),
'#description' => t('Please enter a definition for this vocabulary'),
'#default_value' => $default_desc,
);
return $form;
}
/**
* Validation fucntion for tripal_cv_cv_add_form
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_add_form_validate($form, &$form_state) {
tripal_cv_form_fields_validate($form, $form_state);
}
/**
* Validation fucntion for tripal_cv_cv_edit_form
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_edit_form_validate($form, &$form_state) {
tripal_cv_form_fields_validate($form, $form_state);
}
/**
* Generic validation form for shared fields of both the edit and add forms
*
* @ingroup tripal_cv
*/
function tripal_cv_form_fields_validate($form, &$form_state) {
$name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
$desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
$cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
// make sure the cv name is unique
$values = array('name' => $name);
$results = chado_select_record('cv', array('cv_id'), $values);
if (count($results) > 0 and $results[0]->cv_id != $cv_id) {
form_set_error('name', 'The vocabulary name must be unique');
}
}
/**
* Submit cv add form
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_add_form_submit($form, &$form_state) {
$name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
$desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
$values = array(
'name' => $name,
'definition' => $desc,
);
$success = chado_insert_record('cv', $values);
if ($success) {
drupal_set_message(t("Controlled vocabulary added"));
}
else {
drupal_set_message(t("Failed to add controlled vocabulary."));
}
}
/**
* Submit cv edit form
*
* @ingroup tripal_cv
*/
function tripal_cv_cv_edit_form_submit($form, &$form_state) {
$name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
$desc = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
$cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
$op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
$values = array(
'name' => $name,
'definition' => $desc,
);
if (strcmp($op, 'Update')==0) {
$match = array('cv_id' => $cv_id);
$success = chado_update_record('cv', $match, $values);
if ($success) {
drupal_set_message(t("Controlled vocabulary updated"));
}
else {
drupal_set_message(t("Failed to update controlled vocabulary."));
}
}
if (strcmp($op, 'Delete')==0) {
$match = array('cv_id' => $cv_id);
$success = chado_delete_record('cv', $match);
if ($success) {
drupal_set_message(t("Controlled vocabulary deleted"));
}
else {
drupal_set_message(t("Failed to delete controlled vocabulary."));
}
}
}
/**
* Ajax callback for the tripal_cv_form
*
* @ingroup tripal_cv
*/
function tripal_cv_edit_form_ajax($form, $form_state) {
$elements = array();
// add in the form fields and the buttons
if (array_key_exists('cv_id', $form_state['values'])) {
$elements['fields'] = $form;
$elements['update'] = $form['update'];
$elements['delete'] = $form['delete'];
}
// add back in the cv-edit-div that is used for the next round of AJAX
$elements['fields']['#prefix'] = '';
$elements['fields']['#suffix'] = '
';
// reset the values for the fields to the defaults
$elements['fields']['name']['#value'] = $elements['fields']['name']['#default_value'];
$elements['fields']['definition']['#value'] = $elements['fields']['definition']['#default_value'];
//drupal_set_message('' . print_r($elements, TRUE) . '
', "status");
return $elements;
}
/**
* Form for editing cvterms
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_edit_form($form, &$form_state) {
$step = 0;
if (empty($form_state['storage']['step'])) {
$form_state['storage']['step'] = 0;
}
else {
$step = $form_state['storage']['step'];
}
$cv_id = 0;
if ($step == 1) {
$cv_id = $form_state['storage']['cv_id'];
$cvterm_name = $form_state['storage']['name'];
$cvterm_id = $form_state['storage']['cvterm_id'];
}
// get the cv if form was submitted via AJAX
$cvterm = '';
if (array_key_exists('values', $form_state)) {
$cv_id = $form_state['values']['cv_id'];
if (array_key_exists('cvterm', $form_state['values'])) {
$cvterm = $form_state['values']['cvterm'];
}
}
elseif (isset($form_state['build_info']['args'][0])) {
$cv_id = $form_state['build_info']['args'][0];
$cvterm_id = $form_state['build_info']['args'][1];
if ($form_state['build_info']['args'][1]) {
$result = db_select('chado.cvterm','c')
->fields('c', array('name'))
->condition('c.cvterm_id',$cvterm_id)
->execute();
$cvterm_name = $result->fetchObject()->name;
$step = 1;
}
}
// get a list of CVs
$cvs = array();
$sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
$results = chado_query($sql);
$cvs[] = 'Select a vocabulary';
foreach ($results as $cv) {
$cvs[$cv->cv_id] = $cv->name;
}
$form['cv_id'] = array(
'#title' => t('Controlled Vocabulary Name'),
'#type' => 'select',
'#options' => $cvs,
'#required' => TRUE,
'#default_value' => $cv_id,
'#ajax' => array(
'callback' => 'tripal_cv_cvterm_edit_form_ajax',
'wrapper' => 'cvterm-edit-div',
'event' => 'change',
'method' => 'replace',
'event' => 'change',
),
);
if ($cv_id and $step == 0) {
$form['name']= array(
'#type' => 'textfield',
'#title' => t("Term Name"),
'#default_value' => $cvterm,
'#required' => TRUE,
'#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
'#description' => t('Enter the term to edit.')
);
$form['continue']= array(
'#type' => 'submit',
'#value' => 'continue',
);
}
elseif ($step == 1) {
tripal_cv_add_cvterm_form_fields($form, $form_state, $cv_id, $cvterm_name);
// when editing there are certain fields the user should not change for a term
// let's mark those as disabled
$form['cv_id']['#disabled'] = TRUE;
$form['db_id']['#disabled'] = TRUE;
$form['accession']['#disabled'] = TRUE;
// add in the div for replacing the fields if needed
$form['#prefix'] = '';
$form['#suffix'] = '
';
// add in the cvterm id
$form['cvterm_id'] = array(
'#type' => 'hidden',
'#value' => $cvterm_id,
);
$form['update'] = array(
'#type' => 'submit',
'#value' => t('Update'),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
);
}
if ($step == 0) {
// if we don't have a cv_id then this is the first time the form has
// benn loaded and we need to create the div where ajax replacement elements get stored
$form['div_replace'] = array(
'#type' => 'item',
'#prefix' => '',
'#suffix' => '
',
);
}
return $form;
}
/**
* Form for adding cvterms
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_add_form($form, &$form_state) {
$cv_id = 0;
if (array_key_exists('values', $form_state)) {
$cv_id = $form_state['values']['cv_id'];
}
elseif (isset($form_state['build_info']['args'][0])) {
$cv_id = $form_state['build_info']['args'][0];
}
// get a list of CVs
$cvs = array();
$sql = "SELECT * FROM {cv} WHERE NOT name = 'tripal' ORDER BY name ";
$results = chado_query($sql);
$cvs[] = 'Select a vocabulary';
foreach ($results as $cv) {
$cvs[$cv->cv_id] = $cv->name;
}
$form['cv_id'] = array(
'#title' => t('Controlled Vocabulary (Ontology) Name'),
'#type' => 'select',
'#options' => $cvs,
'#required' => TRUE,
'#default_value' => $cv_id,
);
tripal_cv_add_cvterm_form_fields($form, $form_state);
$form['add'] = array(
'#type' => 'submit',
'#value' => t('Add Term'),
);
return $form;
}
/**
* Form fields in common between add/edit forms
*
* @ingroup tripal_cv
*/
function tripal_cv_add_cvterm_form_fields(&$form, $form_state, $cv_id = 0, $cvterm_name = '') {
$name = '';
$definition = '';
$is_relationship = '';
$is_obsolete = '';
$db_id = '';
$accession = '';
$cvterm = NULL;
// get default values
if ($cvterm_name) {
$values = array('cv_id' => $cv_id, 'name' => $cvterm_name);
$cvterm = chado_generate_var('cvterm', $values);
$name = $cvterm->name;
$definition = $cvterm->definition;
$is_relationship = $cvterm->is_relationshiptype;
$is_obsolete = $cvterm->is_obsolete;
$db_id = $cvterm->dbxref_id->db_id->db_id;
$accession = $cvterm->dbxref_id->accession;
}
$form['name']= array(
'#type' => 'textfield',
'#title' => t("Term Name"),
'#default_value' => $name,
'#description' => t('The term must be unique within the database selected below.'),
'#required' => TRUE,
);
$form['internal_id']= array(
'#type' => 'item',
'#title' => t("Internal ID"),
'#markup' => $cvterm ? $cvterm->cvterm_id : '',
);
$form['definition']= array(
'#type' => 'textarea',
'#title' => t('Description'),
'#description' => t('Please enter a description for this term'),
'#default_value' => $definition,
);
$form['is_relationship'] = array(
'#type' => 'checkbox',
'#title' => t('This term describes a relationship?'),
'#default_value' => $is_relationship,
);
$form['is_obsolete'] = array(
'#type' => 'checkbox',
'#title' => t('This term is obsolete?'),
'#default_value' => $is_obsolete,
);
$values = array();
$columns = array('db_id', 'name');
$options = array('order_by' => array('name' => 'ASC'));
$results = chado_select_record('db', $columns, $values, $options);
$dbs = array();
$dbs[] = '';
foreach ($results as $db) {
$dbs[$db->db_id] = $db->name;
}
$form['db_id'] = array(
'#type' => 'select',
'#title' => t('Database'),
'#description' => t('All terms must be assocated with a database. If there is no database for this term (e.g. it is a custom term specific to this site) then select the database \'null\' or consider creating a database specific for your site and use that anytime you would like to add terms.'),
'#options' => $dbs,
'#default_value' => $db_id,
'#required' => TRUE,
);
$form['accession']= array(
'#type' => 'textfield',
'#title' => t("Accession"),
'#description' => t('If this term has an existing accession (unique identifier) in the database
please enter that here. If the accession is numeric with a database prefix (e.g. GO:003023), please
enter just the numeric value. The database prefix will be appended whenever the term is displayed.
If you do not have a numeric value consider entering the term name as the accession.'),
'#required' => TRUE,
'#default_value' => $accession,
);
}
/**
* Validate cvterm edit form
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_edit_form_validate($form, &$form_state) {
$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
$db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
$name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
$cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? $form_state['values']['cvterm_id'] : '';
$accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
$step = $form_state['storage']['step'];
// make sure the cv term name is unique for this vocabulary
if ($step == 1) {
$values = array('name' => $name, 'cv_id' => $cv_id);
$results = chado_select_record('cvterm', array('cvterm_id'), $values);
foreach ($results as $r) {
if ($r->cvterm_id != $cvterm_id) {
form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
}
}
}
}
/**
* Validate cv add form
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_add_form_validate($form, &$form_state) {
$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
$db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
$name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
$accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
$values = array('cv_id' => $cv_id);
$results = chado_select_record('cv', array('name'), $values);
if (!$results or count($results) == 0) {
form_set_error('cv_id', 'The controlled vocabulary does not exist');
}
// make sure the DB exists
$values = array('db_id' => $db_id);
$results = chado_select_record('db', array('name'), $values);
if (!$results or count($results) == 0) {
form_set_error('db_id', 'The database name does not exist');
}
// make sure the cv term name is unique for this vocabulary
$values = array('name' => $name, 'cv_id' => $cv_id);
$results = chado_select_record('cvterm', array('cvterm_id'), $values);
if (count($results) > 0) {
form_set_error('name', 'The term name must be unique for this vocabulary. Another term with this name already exists.');
}
// make sure this accession is unique for the database
$values = array('accession' => $accession, 'db_id' => $db_id);
$results = chado_select_record('dbxref', array('dbxref_id'), $values);
if (count($results) > 0 ) {
form_set_error('accession', 'The accession is not uniuqe for this vocabulary\'s database.');
}
}
/**
* Edits existing controlled vocabulary terms
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_edit_form_submit($form, &$form_state) {
$cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
$name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
$definition = array_key_exists('definition', $form_state['values']) ? trim($form_state['values']['definition']) : '';
$is_relationship = array_key_exists('is_relationship', $form_state['values']) ? trim($form_state['values']['is_relationship']) : '';
$is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? trim($form_state['values']['is_obsolete']) : '';
$cvterm_id = array_key_exists('cvterm_id', $form_state['values']) ? trim($form_state['values']['cvterm_id']) : '';
$db_id = array_key_exists('db_id', $form_state['values']) ? trim($form_state['values']['db_id']) : '';
$accession = array_key_exists('accession', $form_state['values']) ? trim($form_state['values']['accession']) : '';
$op = array_key_exists('op', $form_state['values']) ? trim($form_state['values']['op']) : '';
$step = $form_state['storage']['step'];
switch ($step) {
case 0: // a cvterm name has been selected
$cv_id = array_key_exists('cv_id', $form_state['values']) ? trim($form_state['values']['cv_id']) : '';
$name = array_key_exists('name', $form_state['values']) ? trim($form_state['values']['name']) : '';
// get the original cvterm_id
$values = array('name' => $name, 'cv_id' => $cv_id);
$results = chado_select_record('cvterm', array('cvterm_id'), $values);
$cvterm = $results[0];
$form_state['storage']['cv_id'] = $cv_id;
$form_state['storage']['name'] = $name;
$form_state['storage']['step'] = 1;
$form_state['storage']['cvterm_id'] = $cvterm->cvterm_id;
$form_state['rebuild'] = TRUE;
break;
case 1: // update/delete button has been clicked
if ($op == 'Update') {
// get the cv
$values = array('cv_id' => $cv_id);
$results = chado_select_record('cv', array('name'), $values);
$cv = $results[0];
// get the db
$values = array('db_id' => $db_id);
$results = chado_select_record('db', array('name'), $values);
$db = $results[0];
// now add the term
$term = array(
'name' => $name,
'namespace' => $cv->name,
'id' => $accession,
'definition' => $definition,
'is_obsolete' => $is_obsolete,
'cv_name' => $cv->name,
'is_relationship' => $is_relationship,
'db_name' => $db_name
);
$cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
if ($cvterm) {
drupal_set_message('Term updated successfully.');
}
else {
drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
}
}
if ($op == 'Delete') {
$values = array('cvterm_id' => $cvterm_id);
$success = chado_delete_record('cvterm', $values);
if ($success) {
drupal_set_message('Term deleted successfully.');
}
else {
drupal_set_message('Could not delete term term. Check Drupal recent logs for error messages.', 'error');
}
}
break;
}
}
/**
* Adds new terms to an existing cv
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_add_form_submit($form, &$form_state) {
$cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : '';
$name = array_key_exists('name', $form_state['values']) ? $form_state['values']['name'] : '';
$definition = array_key_exists('definition', $form_state['values']) ? $form_state['values']['definition'] : '';
$is_relationship = array_key_exists('is_relationship', $form_state['values']) ? $form_state['values']['is_relationship'] : '';
$is_obsolete = array_key_exists('is_obsolete', $form_state['values']) ? $form_state['values']['is_obsolete'] : '';
$db_id = array_key_exists('db_id', $form_state['values']) ? $form_state['values']['db_id'] : '';
$accession = array_key_exists('accession', $form_state['values']) ? $form_state['values']['accession'] : '';
// get the database
$values = array('db_id' => $db_id);
$results = chado_select_record('db', array('name'), $values);
$db = $results[0];
// get the cv
$values = array('cv_id' => $cv_id);
$results = chado_select_record('cv', array('name'), $values);
$cv = $results[0];
// now add the term
$term = array(
'name' => $name,
'namespace' => $cv->name,
'id' => $accession,
'definition' => $definition,
'is_obsolete' => $is_obsolete,
'cv_name' => $cv->name,
'is_relationship' => $is_relationship,
'db_name' => $db->name
);
$cvterm = tripal_insert_cvterm($term, array('update_existing' => TRUE));
if ($cvterm) {
drupal_set_message('Term added successfully.');
}
else {
drupal_set_message('Could not add term. Check Drupal recent logs for error messages.', 'error');
}
}
/**
* Ajax callback for the tripal_cv_form
*
* @ingroup tripal_cv
*/
function tripal_cv_cvterm_edit_form_ajax($form, $form_state) {
$elements = array();
$elements['name'] = $form['name'];
$elements['continue'] = $form['continue'];
// add back in the cv-edit-div that is used for the next round of AJAX
$elements['name']['#prefix'] = '';
$elements['name']['#suffix'] = '
';
return $elements;
}