1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129 |
- <?php
- function tripal_get_cv($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array();
- }
-
- if (!is_array($identifiers)) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cv: The identifier passed in is expected to be an array with the key
- matching a column name in the cv table (ie: cv_id or name). You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
- elseif (empty($identifiers)) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cv: You did not pass in anything to identify the cv you want. The identifier
- is expected to be an array with the key matching a column name in the cv table
- (ie: cv_id or name). You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- $cv = chado_generate_var(
- 'cv',
- $identifiers,
- $options
- );
-
- if (is_array($cv)) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cv: The identifiers you passed in were not unique. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- elseif ($cv === FALSE) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cv: chado_generate_var() failed to return a cv based on the identifiers
- you passed in. You should check that your identifiers are correct, as well as, look
- for a chado_generate_var error for additional clues. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- else {
- return $cv;
- }
- }
- function tripal_get_cv_select_options() {
- $results = chado_select_record('cv', array('cv_id', 'name'), array(), array('order_by' => array('name' => 'ASC')));
- $options = array();
- foreach ($results as $r) {
- $options[$r->cv_id] = $r->name;
- }
- return $options;
- }
- function tripal_get_cvterm($identifiers, $options = array()) {
-
- if (!isset($options['include_fk'])) {
-
- $options['include_fk'] = array('cv_id' => TRUE);
- }
-
- if (!is_array($identifiers)) {
- tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
- "tripal_get_cvterm: The identifier passed in is expected to be an array with the key
- matching a column name in the cvterm table (ie: cvterm_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
- elseif (empty($identifiers)) {
- tripal_report_error('tripal_cv_api', TRIPAL_ERROR,
- "tripal_get_cvterm: You did not pass in anything to identify the cvterm you want. The identifier
- is expected to be an array with the key matching a column name in the cvterm table
- (ie: cvterm_id or name). You passed in %identifier.",
- array('%identifier'=> print_r($identifiers, TRUE))
- );
- }
-
- if (isset($identifiers['synonym'])) {
- $synonym = $identifiers['synonym']['name'];
- $values = array('synonym' => $synonym);
- if (isset($identifiers['synonym']['cv_id'])) {
- $values['cvterm_id'] = array('cv_id' => $identifiers['synonym']['cv_id']);
- }
- if (isset($identifiers['synonym']['cv_name'])) {
- $values['cvterm_id'] = array('cv_id' => array('name' => $identifiers['synonym']['cv_name']));
- }
- $options = array(
- 'case_insensitive_columns' => array('name')
- );
- $result = chado_select_record('cvtermsynonym', array('cvterm_id'), $values, $options);
-
- if (count($result) == 0) {
- return FALSE;
- }
- if (count($result) > 1) {
- return FALSE;
- }
- $identifiers = array('cvterm_id' => $result[0]->cvterm_id);
- }
-
- if (isset($identifiers['property'])) {
- $property = $identifiers['property'];
- unset($identifiers['property']);
- $cvterm = chado_get_record_with_property(
- array('table' => 'cvterm', 'base_records' => $identifiers),
- array('type_name' => $property),
- $options
- );
- }
-
- else {
-
- $cvterm = chado_generate_var('cvterm', $identifiers, $options);
- }
-
- if (is_array($cvterm)) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cvterm: The identifiers you passed in were not unique. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- elseif ($cvterm === FALSE) {
- tripal_report_error(
- 'tripal_cv_api',
- TRIPAL_ERROR,
- "tripal_get_cvterm: chado_generate_var() failed to return a cvterm based on the identifiers
- you passed in. You should check that your identifiers are correct, as well as, look
- for a chado_generate_var error for additional clues. You passed in %identifier.",
- array(
- '%identifier'=> print_r($identifiers, TRUE)
- )
- );
- }
-
- else {
- return $cvterm;
- }
- }
- function tripal_get_cvterm_select_options($cv_id) {
- $columns = array('cvterm_id', 'name');
- $values = array('cv_id' => $cv_id);
- $s_options = array('order_by' => array('name' => 'ASC'));
- $cvterms = chado_select_record('cvterm', $columns, $values, $s_options);
- $options = array();
- $options[0] = 'Select a Type';
- foreach ($cvterms as $cvterm) {
- $options[$cvterm->cvterm_id] = $cvterm->name;
- }
- return $options;
- }
- function tripal_update_cvtermpath($cv_id, $job_id = NULL) {
-
-
- $sql = "SELECT * FROM {cv} WHERE cv_id = :cv_id";
- $cv = chado_query($sql, array(':cv_id' => $cv_id))->fetchObject();
- print "\nUpdating cvtermpath for $cv->name...\n";
- $previous = chado_set_active('chado');
- try {
- $sql = "SELECT * FROM fill_cvtermpath(:name)";
- db_query($sql, array(':name' => $cv->name));
- chado_set_active($previous);
- }
- catch (Exception $e) {
- chado_set_active($previous);
- $error = $e->getMessage();
- tripal_report_error('tripal_cv', TRIPAL_ERROR, "Could not fill cvtermpath table: @error", array('@error' => $error));
- return FALSE;
- }
- return TRUE;
- }
- function tripal_insert_cv($name, $definition) {
-
- $ins_values = array(
- 'name' => $name,
- 'definition' => $definition
- );
-
- $sel_values = array('name' => $name);
- $sel_options = array('statement_name' => 'sel_cv_na');
- $results = chado_select_record('cv', array('*'), $sel_values, $sel_options);
-
- if (count($results) == 0) {
- $ins_options = array('statement_name' => 'ins_cv_nade');
- $success = chado_insert_record('cv', $ins_values, $ins_options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to create the CV record", NULL);
- return FALSE;
- }
- $results = chado_select_record('cv', array('*'), $sel_values, $sel_options);
- }
-
- else {
- $upd_options = array('statement_name' => 'upd_cv_nade');
- $success = chado_update_record('cv', $sel_values, $ins_values, $upd_options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the CV record", NULL);
- return FALSE;
- }
- $results = chado_select_record('cv', array('*'), $sel_values, $sel_options);
- }
-
- return $results[0];
- }
- function tripal_insert_cvterm($term, $options = array()) {
-
- if (isset($term['cv_name'])) {
- $defaultcv = $term['cv_name'];
- }
- else {
- $defaultcv = '_global';
- }
- if (isset($term['is_relationship'])) {
- $is_relationship = $term['is_relationship'];
- }
- else {
- $is_relationship = 0;
- }
- if (isset($term['db_name'])) {
- $dbname = $term['db_name'];
- }
- else {
- $dbname = 'internal';
- }
- if (isset($options['update_existing'])) {
- $update = $options['update_existing'];
- }
- else {
- $update = 1;
- }
-
- $id = (isset($term['id'])) ? $term['id'] : '';
- $name = '';
- $cvname = '';
- $definition = '';
- $is_obsolete = 0;
- $accession = '';
- if (array_key_exists('name', $term)) {
- $name = $term['name'];
- }
- else {
- $name = $id;
- }
- if (array_key_exists('namespace', $term)) {
- $cvname = $term['namespace'];
- }
- else {
- $cvname = $defaultcv;
- }
- if (array_key_exists('definition', $term)) {
- $definition = preg_replace('/^\"(.*)\"/', '\1', $term['definition']);
- }
- else {
- $definition = '';
- }
- if (array_key_exists('is_obsolete', $term)) {
- $is_obsolete = $term['is_obsolete'];
- if (strcmp($is_obsolete, 'true') == 0) {
- $is_obsolete = 1;
- }
- }
- if (!$name and !$id) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find cvterm without 'id' or 'name'", NULL);
- return 0;
- }
- if (!$id) {
- $id = $name;
- }
-
- if ($dbname) {
- $accession = $id;
- }
- if (preg_match('/^.+?:.*$/', $id)) {
- $accession = preg_replace('/^.+?:(.*)$/', '\1', $id);
- $dbname = preg_replace('/^(.+?):.*$/', '\1', $id);
- }
-
- if ($is_relationship and !$dbname) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database name is not provided for this relationship term: $id", NULL);
- return 0;
- }
- if (!$is_relationship and !$dbname) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "A database identifier is missing from the term: $id", NULL);
- return 0;
- }
-
- $cv = tripal_get_cv(array('name' => $cvname));
- if (!$cv) {
- $cv = tripal_insert_cv($cvname, '');
- }
- if (!$cv) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find namespace '$cvname' when adding/updating $id", NULL);
- return 0;
- }
-
-
-
-
- $cvtermsql = "
- SELECT CVT.name, CVT.cvterm_id, CV.cv_id, CV.name as cvname,
- DB.name as dbname, DB.db_id, DBX.accession
- FROM {cvterm} CVT
- INNER JOIN {dbxref} DBX on CVT.dbxref_id = DBX.dbxref_id
- INNER JOIN {db} DB on DBX.db_id = DB.db_id
- INNER JOIN {cv} CV on CV.cv_id = CVT.cv_id
- WHERE DBX.accession = :accession and DB.name = :name
- ";
-
-
- $db = tripal_get_db(array('name' => $dbname));
- if (!$db) {
- $db = tripal_insert_db(array('name' => $dbname));
- }
- if (!$db) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Cannot find database '$dbname' in Chado.", NULL);
- return 0;
- }
-
-
- $values = array(
- 'name' => $name,
- 'is_obsolete' => $is_obsolete,
- 'cv_id' => array(
- 'name' => $cvname,
- ),
- );
- $options = array('statement_name' => 'sel_cvterm_c1');
- $result = chado_select_record('cvterm', array('*'), $values, $options);
-
-
- if (count($result) == 1) {
- $cvterm = $result[0];
-
- $values = array('dbxref_id' => $cvterm->dbxref_id);
- $options = array('statement_name' => 'sel_dbxref_id');
- $result = chado_select_record('dbxref', array('*'), $values, $options);
- $dbxref = $result[0];
-
- $values = array('db_id' => $dbxref->db_id);
- $options = array('statement_name' => 'sel_db_id');
- $result = chado_select_record('db', array('*'), $values, $options);
- $db_check = $result[0];
-
-
-
-
- if ($db_check->name != $db->name) {
-
- $values = array(
- 'db_id' => $db->db_id,
- 'accession' => $accession,
- );
- $options = array('statement_name' => 'sel_dbxref_idac');
- $result = chado_select_record('dbxref', array('*'), $values, $options);
-
-
- if (count($result) > 0) {
- $dbxref = $result[0];
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $values = array('dbxref_id' => $dbxref->dbxref_id);
- $options = array('statement_name' => 'upd_cvterm_db');
- $success = chado_update_record('cvterm', $match, $values, $options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to correct the dbxref id for the cvterm " .
- "'$name' (id: $accession), for database $dbname", NULL);
- return 0;
- }
- }
-
-
- else {
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $options = array('statement_name' => 'del_cvterm_cv');
- chado_delete_record('cvterm', $match, $options);
- }
- }
-
-
-
-
- if ($dbxref->accession != $accession) {
-
- $dbxref_new = tripal_insert_dbxref(array(
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ));
- if (!$dbxref_new) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL);
- return 0;
- }
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'dbxref_id' => $dbxref_new->dbxref_id,
- 'is_for_definition' => 1,
- );
- $options = array('statement_name' => 'sel_cvtermdbxref_cvdbis');
- $result = chado_select_record('cvterm_dbxref', array('*'), $values, $options);
-
- if (count($result)==0) {
- $options = array(
- 'statement_name' => 'ins_cvtermdbxref_cvdbis',
- 'return_record' => FALSE,
- );
- $success = chado_insert_record('cvterm_dbxref', $values, $options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the cvterm_dbxref record for a " .
- "duplicated cvterm: $name (id: $accession), for database $dbname", NULL);
- return 0;
- }
- }
-
- $result = chado_query($cvtermsql, array(':accession' => $dbxref->accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- return $cvterm;
- }
-
-
- }
-
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- if (!$cvterm) {
-
- $dbxref = tripal_insert_dbxref(array(
- 'db_id' => $db->db_id,
- 'accession' => $accession
- ));
- if (!$dbxref) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to find or insert the dbxref record for cvterm, " .
- "$name (id: $accession), for database $dbname", NULL);
- return 0;
- }
-
-
- $values = array('dbxref_id' => $dbxref->dbxref_id);
- $options = array('statement_name' => 'sel_cvterm_db');
- $check = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
- if (count($check) == 0) {
-
- $ins_values = array(
- 'cv_id' => $cv->cv_id,
- 'name' => $name,
- 'definition' => $definition,
- 'dbxref_id' => $dbxref->dbxref_id,
- 'is_obsolete' => $is_obsolete,
- 'is_relationshiptype' => $is_relationship,
- );
- $ins_options = array('statement_name' => 'ins_cvterm_all');
- $success = chado_insert_record('cvterm', $ins_values, $ins_options);
- if (!$success) {
- if (!$is_relationship) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the term: $name ($dbname)", NULL);
- return 0;
- }
- else {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to insert the relationship term: $name (cv: " . $cvname . " db: $dbname)", NULL);
- return 0;
- }
- }
- }
-
- else {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "The dbxref already exists for another cvterm record: $name (cv: " . $cvname . " db: $dbname)", NULL);
- return 0;
- }
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- }
-
- elseif ($update) {
- $match = array('cvterm_id' => $cvterm->cvterm_id);
- $upd_values = array(
- 'name' => $name,
- 'definition' => $definition,
- 'is_obsolete' => $is_obsolete,
- 'is_relationshiptype' => $is_relationship,
- );
- $upd_options = array('statement_name' => 'upd_cvterm_nadeisis');
- $success = chado_update_record('cvterm', $match, $upd_values, $upd_options);
- if (!$success) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING, "Failed to update the term: $name", NULL);
- return 0;
- }
- $result = chado_query($cvtermsql, array(':accession' => $accession, ':name' => $dbname));
- $cvterm = $result->fetchObject();
- }
- else {
-
- }
-
- return $cvterm;
- }
- function tripal_submit_obo_job($obo) {
- global $user;
-
- $obo['obo_id'] = (isset($obo['obo_id'])) ? $obo['obo_id'] : NULL;
- $obo['name'] = (isset($obo['name'])) ? $obo['name'] : NULL;
- $obo['url'] = (isset($obo['url'])) ? $obo['url'] : NULL;
- $obo['file'] = (isset($obo['file'])) ? $obo['file'] : NULL;
- if ($obo['obo_id']) {
- $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
- $result = db_query($sql, array(':obo_id' => $obo['obo_id']))->fetchObject();
- $args = array($result->obo_id);
- return tripal_add_job("Load OBO " . $result->name, 'tripal_cv',
- "tripal_cv_load_obo_v1_2_id", $args, $user->uid);
- }
- else {
- if ($obo['url']) {
- $args = array($obo['name'], $obo['url']);
- return tripal_add_job("Load OBO " . $obo['name'], 'tripal_cv',
- "tripal_cv_load_obo_v1_2_url", $args, $user->uid);
- }
- elseif ($obo['file']) {
- $args = array($obo['name'], $obo['file']);
- return tripal_add_job("Load OBO " . $obo['name'], 'tripal_cv',
- "tripal_cv_load_obo_v1_2_file", $args, $user->uid);
- }
- }
- return FALSE;
- }
- function tripal_insert_obo($name, $path) {
-
- $obo_id = db_select('tripal_cv_obo', 'tco')
- ->fields('tco', array('obo_id'))
- ->condition('name', $name)
- ->execute()
- ->fetchField();
-
- if ($obo_id) {
- db_update('tripal_cv_obo')
- ->fields(array(
- 'path' => $path,
- ))
- ->condition('name', $name)
- ->execute();
- return $obo_id;
- }
- else {
- $record = new stdClass;
- $record->name = $name;
- $record->path = $path;
- drupal_write_record('tripal_cv_obo', $record);
- return $record->obo_id;
- }
- }
- function tripal_autocomplete_cvterm($cv_id, $string = '') {
- $sql = "
- SELECT cvterm_id, name
- FROM {cvterm}
- WHERE cv_id = :cv_id and lower(name) like lower(:name)
- ORDER by name
- LIMIT 25 OFFSET 0
- ";
- $results = chado_query($sql, array(':cv_id' => $cv_id, ':name' => $string . '%'));
- $items = array();
- foreach ($results as $term) {
- $items[$term->name] = $term->name;
- }
- drupal_json_output($items);
- }
- function tripal_associate_cvterm($basetable, $record_id, $cvterm, $options = array()) {
- $linking_table = $basetable . '_cvterm';
- $foreignkey_name = $basetable . '_id';
-
- $options['insert_cvterm'] = (isset($options['insert_cvterm'])) ? $options['insert_cvterm'] : FALSE;
-
- if (!isset($cvterm['cvterm_id'])) {
- $values = array(
- 'name' => $cvterm['name'],
- );
- if (isset($cvterm['cv_id'])) {
- $values['cv_id'] = $cvterm['cv_id'];
- }
- elseif (isset($cvterm['cv_name'])) {
- $values['cv_id'] = array(
- 'name' => $cvterm['cv_name']
- );
- }
- else {
- tripal_report_error('tripal_cv_api', TRIPAL_WARNING,
- "tripal_associate_cvterm: The cvterm needs to have either the cv_name or cv_id
- supplied. You were trying to associate a cvterm with the %base %record_id
- and supplied the cvterm values: %cvterm.",
- array('%base' => $basetable, '%record_id' => $record_id, '%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
-
-
- $select = chado_select_record('cvterm', array('*'), $values);
- if ($select) {
- $cvterm['cvterm_id'] = $select[0]->cvterm_id;
- }
- elseif ($options['insert_cvterm']) {
-
- $insert = tripal_insert_cvterm($values);
- if (isset($insert->cvterm_id)) {
- $cvterm['cvterm_id'] = $insert->cvterm_id;
- }
- else {
- tripal_report_error('tripal_cv_api', TRIPAL_WARNING,
- "tripal_associate_cvterm: Unable to insert the cvterm using the cvterm values: %cvterm.",
- array('%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
- }
- else {
- tripal_report_error('tripal_api', TRIPAL_WARNING,
- "tripal_associate_cvterm: The cvterm doesn't already exist. You supplied the cvterm values: %cvterm.",
- array('%cvterm' => print_r($cvterm,TRUE))
- );
- return FALSE;
- }
- }
-
-
- if ($cvterm['cvterm_id'] > 0) {
- $values = array(
- 'cvterm_id' => $cvterm['cvterm_id'],
- $foreignkey_name => $record_id,
- 'pub_id' => 1,
- );
-
- $result = chado_select_record($linking_table, array('*'), $values);
- if (!$result) {
- $success = chado_insert_record($linking_table, $values);
- if (!$success) {
- tripal_report_error('tripal_api', TRIPAL_WARNING,
- "Failed to insert the %base record %term",
- array('%base' => $linking_table, '%term' => $cvterm['name'])
- );
- return FALSE;
- }
- $result = chado_select_record($linking_table, array('*'), $values);
- }
- if (isset($result[0])) {
- return $result[0];
- }
- else {
- return FALSE;
- }
- }
- return FALSE;
- }
- function tripal_set_default_cv($table, $field, $cv_name, $cv_id = FALSE) {
-
- if ($cv_id) {
- $cv = tripal_get_cv(array('cv_id' => $cv_id));
- }
- else {
- $cv = tripal_get_cv(array('name' => $cv_name));
- }
- if ($cv) {
-
- $num_deleted = db_delete('tripal_cv_defaults')
- ->condition('table_name', $table)
- ->condition('field_name', $field)
- ->execute();
-
- $cv_default_id = db_insert('tripal_cv_defaults')
- ->fields(array(
- 'table_name' => $table,
- 'field_name' => $field,
- 'cv_id' => $cv->cv_id,
- ))
- ->execute();
- if (!$cv_default_id) {
- tripal_report_error('tripal_cv', TRIPAL_WARNING,
- "Cannot set default vocabulary for %table.%field. Check the error logs.",
- array('%table' => $table, '%field' => $field));
- return FALSE;
- }
- }
- else {
- tripal_report_error('tripal_cv', TRIPAL_WARNING,
- "Cannot set default vocabulary for %table.%field. The vocabulary name, '%cvname', doesn't exist.",
- array('%table' => $table, '%field' => $field, '%cvname' => $cv_name));
- return FALSE;
- }
- }
- function tripal_get_default_cv($table, $field) {
- $sql = "
- SELECT cv_id
- FROM {tripal_cv_defaults}
- WHERE table_name = :table and field_name = :field
- ";
- $cv_id = db_query($sql, array(':table' => $table, ':field' => $field))->fetchField();
- return tripal_get_cv(array('cv_id' => $cv_id));
- }
- function tripal_get_cvterm_default_select_options($table, $field, $field_desc) {
- $default_cv = tripal_get_default_cv($table, $field);
- $options = array();
- if ($default_cv) {
- $options = tripal_get_cvterm_select_options($default_cv->cv_id);
- if (count($options) == 0) {
- tripal_set_message('There are no ' . $field_desc . '. Please ' .
- l('add terms',
- 'admin/tripal/chado/tripal_cv/cv/' .$default_cv->cv_id. '/cvterm/add',
- array('attributes' => array('target' => '_blank'))) . ' to the ' .
- $default_cv->name .' vocabulary.',
- TRIPAL_WARNING);
- }
- }
- else {
- tripal_set_message('There is not a default vocabulary set for ' . $field_desc . '. '.
- 'Please set one using the ' .
- l('vocabulary defaults configuration page',
- 'admin/tripal/chado/tripal_cv/defaults',
- array('attributes' => array('target' => '_blank'))) . '.',
- TRIPAL_WARNING);
- }
- return $options;
- }
|