123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- <?php
- function chado_get_property($record, $property) {
- $base_table = array_key_exists('table', $record) ? $record['table'] : '';
- $base_id = array_key_exists('id', $record) ? $record['id'] : '';
- $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
- $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
- $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
- $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
- $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
- $value = array_key_exists('value', $property) ? $property['value'] : '';
- $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
-
-
- $type = array();
- if ($cv_id) {
- $type['cv_id'] = $cv_id;
- }
- if ($cv_name) {
- $type['cv_id'] = array(
- 'name' => $cv_name,
- );
- }
- if ($type_name) {
- $type['name'] = $type_name;
- }
- if ($type_id) {
- $type['cvterm_id'] = $type_id;
- }
-
- $options = array();
- $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
- if (!$term or count($term) == 0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
- "Cannot find the term described by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- if (count($term) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_get_property: " .
- "Multiple terms found. Cannot add the property. Property was described " .
- "by: %property.",
- array('%property' => print_r($property, TRUE))); return FALSE;
- }
-
- $table_desc = chado_get_schema($base_table . 'prop');
- $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
-
- $values = array(
- $fkcol => $base_id,
- 'type_id' => $type,
- );
-
- if ($prop_id) {
- $property_pkey = $table_desc['primary key'][0];
- $values[$property_pkey] = $prop_id;
- }
- $results = chado_generate_var($base_table . 'prop', $values);
- if ($results) {
- $results = chado_expand_var($results, 'field', $base_table . 'prop.value');
- }
- return $results;
- }
- function chado_insert_property($record, $property, $options = array()) {
- $base_table = array_key_exists('table', $record) ? $record['table'] : '';
- $base_id = array_key_exists('id', $record) ? $record['id'] : '';
- $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
- $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
- $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
- $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
- $value = array_key_exists('value', $property) ? $property['value'] : '';
- $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
- $update_if_present = array_key_exists('update_if_present', $options) ? $options['update_if_present'] : FALSE;
- $force_rank = array_key_exists('force_rank', $options) ? $options['force_rank'] : FALSE;
-
- $props = chado_get_property($record, $property);
- if (!is_array($props)) {
- if ($props) {
- $props = array($props);
- }
- else {
- $props = array();
- }
- }
- if (count($props) > 0) {
-
- if ($update_if_present) {
- return chado_update_property($record, $property);
- }
- else {
- if (!$force_rank) {
-
-
-
- foreach ($props as $prop) {
- if ($prop->rank > $rank) {
- $rank = $prop->rank;
- }
- if (strcmp($prop->value, $value) == 0) {
- return TRUE;
- }
- }
-
- $rank++;
- }
- else {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
- "The property is already assigned to the record with the following " .
- "rank. And, because the 'force_rank' option is used, the property " .
- "cannot be added: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- }
- }
-
-
- $values = array();
- if ($cv_id) {
- $values['cv_id'] = $cv_id;
- }
- if ($cv_name) {
- $values['cv_id'] = array(
- 'name' => $cv_name,
- );
- }
- if ($type_name) {
- $values['name'] = $type_name;
- }
- if ($type_id) {
- $values['cvterm_id'] = $type_id;
- }
-
- $options = array();
- $term = chado_select_record('cvterm', array('cvterm_id'), $values, $options);
- if (!$term or count($term) == 0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
- "Cannot find the term described by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- if (count($term) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_insert_property: " .
- "Multiple terms found. Cannot add the property. Property was described " .
- "by: %property.",
- array('%property' => print_r($property, TRUE))); return FALSE;
- }
-
- $table_desc = chado_get_schema($base_table . 'prop');
- $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
-
- $values = array(
- $fkcol => $base_id,
- 'type_id' => $values,
- 'value' => $value,
- 'rank' => $rank,
- );
- $result = chado_insert_record($base_table . 'prop', $values);
- return $result;
- }
- function chado_update_property($record, $property, $options = array()) {
- $base_table = array_key_exists('table', $record) ? $record['table'] : '';
- $base_id = array_key_exists('id', $record) ? $record['id'] : '';
- $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
- $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
- $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
- $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
- $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
- $value = array_key_exists('value', $property) ? $property['value'] : '';
- $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
- $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
-
- $prop = chado_get_property($record, $property);
- if (count($prop) == 0) {
- if ($insert_if_missing) {
- return chado_insert_property($record, $property);
- }
- else {
- return FALSE;
- }
- }
-
- $type = array();
- if ($cv_id) {
- $type['cv_id'] = $cv_id;
- }
- if ($cv_name) {
- $type['cv_id'] = array(
- 'name' => $cv_name,
- );
- }
- if ($type_name) {
- $type['name'] = $type_name;
- }
- if ($type_id) {
- $type['cvterm_id'] = $type_id;
- }
-
- $options = array();
- $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
- if (!$term or count($term) == 0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
- "Cannot find the term described by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- if (count($term) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
- "Multiple terms found. Cannot add the property. Property was described " .
- "by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
-
- $table_desc = chado_get_schema($base_table . 'prop');
- $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
-
- $match = array(
- $fkcol => $base_id,
- 'type_id' => $type,
- );
-
-
-
- if ($prop_id) {
- $property_pkey = $table_desc['primary key'][0];
- $match = array(
- $property_pkey => $prop_id,
- );
- }
-
- $values = array();
- $values['value'] = $value;
- if ($rank) {
- $values['rank'] = $rank;
- }
-
-
- if ($prop_id) {
- $values['type_id'] = $type;
- }
- return chado_update_record($base_table . 'prop', $match, $values);
- }
- function chado_delete_property($record, $property) {
- $base_table = array_key_exists('table', $record) ? $record['table'] : '';
- $base_id = array_key_exists('id', $record) ? $record['id'] : '';
- $prop_id = array_key_exists('prop_id', $record) ? $record['prop_id'] : '';
- $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
- $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
- $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
- $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
- $value = array_key_exists('value', $property) ? $property['value'] : '';
- $rank = array_key_exists('rank', $property) ? $property['rank'] : 0;
-
- $type = array();
- if ($cv_id) {
- $type['cv_id'] = $cv_id;
- }
- if ($cv_name) {
- $type['cv_id'] = array(
- 'name' => $cv_name,
- );
- }
- if ($type_name) {
- $type['name'] = $type_name;
- }
- if ($type_id) {
- $type['cvterm_id'] = $type_id;
- }
-
- $options = array();
- $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
- if (!$term or count($term) == 0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
- "Cannot find the term described by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- if (count($term) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_delete_property: " .
- "Multiple terms found. Cannot add the property. Property was described " .
- "by: %property.",
- array('%property' => print_r($property, TRUE))); return FALSE;
- }
-
- $table_desc = chado_get_schema($base_table . 'prop');
- $fkcol = key($table_desc['foreign keys'][$base_table]['columns']);
-
-
- if ($prop_id) {
- $property_pkey = $table_desc['primary key'][0];
- $match = array(
- $property_pkey => $prop_id
- );
- }
-
- else {
- $match = array(
- $fkcol => $record_id,
- 'type_id' => $type,
- );
- }
- return chado_delete_record($base_table . 'prop', $match);
- }
- function chado_get_record_with_property($record, $property, $options = array()) {
- $base_table = array_key_exists('table', $record) ? $record['table'] : '';
- $base_records= array_key_exists('base_records', $record) ? $record['base_records'] : array();
- $type_name = array_key_exists('type_name', $property) ? $property['type_name'] : '';
- $type_id = array_key_exists('type_id', $property) ? $property['type_id'] : '';
- $cv_name = array_key_exists('cv_name', $property) ? $property['cv_name'] : '';
- $cv_id = array_key_exists('cv_id', $property) ? $property['cv_id'] : '';
- $value = array_key_exists('value', $property) ? $property['value'] : '';
- $rank = array_key_exists('rank', $property) ? $property['rank'] : '';
- $property_table = $base_table . 'prop';
- $foreignkey_name = $base_table . '_id';
-
-
- $type = array();
- if ($cv_id) {
- $type['cv_id'] = $cv_id;
- }
- if ($cv_name) {
- $type['cv_id'] = array(
- 'name' => $cv_name,
- );
- }
- if ($type_name) {
- $type['name'] = $type_name;
- }
- if ($type_id) {
- $type['cvterm_id'] = $type_id;
- }
-
- $term = chado_select_record('cvterm', array('cvterm_id'), $type);
- if (!$term or count($term) == 0) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
- "Cannot find the term described by: %property.",
- array('%property' => print_r($property, TRUE)));
- return FALSE;
- }
- if (count($term) > 1) {
- tripal_report_error('tripal_chado', TRIPAL_ERROR, "chado_update_property: " .
- "Multiple terms found. Cannot add the property. Property was described " .
- "by: %property.",
- array('%property' => print_r($property, TRUE))); return FALSE;
- }
-
- $values = array();
- $values['type_id'] = $type;
- if ($rank) {
- $values['rank'] = $rank;
- }
- if ($value) {
- $values['value'] = $value;
- }
-
- if (!empty($base_records)) {
- $values[$foreignkey_name] = $base_records;
- }
-
- $select = chado_select_record($property_table, array($foreignkey_name), $values);
-
- $records = array();
- foreach ($select as $s) {
- $id = $s->{$foreignkey_name};
- $values = array($foreignkey_name => $id);
- $records[$id] = chado_generate_var($base_table, $values, $options);
- }
- return $records;
- }
|