123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982 |
- <?php
- function chado_add_node_form_relationships(&$form, &$form_state, $details) {
-
- if (!chado_table_exists($details['relationship_table'])) {
- drupal_set_message("Cannot add relationship elements to the form. The relationship table, '" .
- $details['relationship_table'] . "', does not exists", "error");
- tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add relationship elements to the form.
- The relationship table, '%name', cannot be found.", array('%name' => $details['relationship_table']));
- return;
- }
-
- if (isset($details['cv_name'])) {
-
- $result = chado_select_record('cv',array('cv_id'),array('name' => $details['cv_name']));
- if (count($result) == 0) {
- drupal_set_message("Cannot add relationship elements to the form. The CV name, '" .
- $details['cv_name'] . "', does not exists", "error");
- tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add relationship elements to the form.
- The CV named, '%name', cannot be found.", array('%name' => $details['cv_name']));
- return;
- }
-
- $details['cv_id'] = $result[0]->cv_id;
- }
- elseif (isset($details['cv_id'])) {
-
- $result = chado_select_record('cv', array('name'), array('cv_id' => $details['cv_id']));
- if (count($result) == 0) {
- drupal_set_message("Cannot add relationship elements to the form. The CV ID, '" .
- $details['cv_id'] . "', does not exist", "error");
- tripal_report_error('tcprops_form', TRIPAL_ERROR, "Cannot add relationship elements
- to the form. The CV ID, '%id', cannot be found.", array('%id' => $details['cv_id']));
- return;
- }
-
- $details['cv_name'] = $result[0]->name;
- }
- else {
-
-
- $default_cv = tripal_get_default_cv($details['relationship_table'], 'type_id');
- if (!empty($default_cv)) {
- $details['cv_id'] = $default_cv->cv_id;
- $details['cv_name'] = $default_cv->name;
- }
- else {
- $default_form_link = l('vocabulary defaults configuration page',
- 'admin/tripal/chado/tripal_cv/defaults',
- array('attributes' => array('target' => '_blank')));
- $table = ucwords(str_replace('_',' ',$details['base_table']));
- $message = "There is not a default vocabulary set for $table Releationship Types. Please set one using the $default_form_link.";
- tripal_set_message($message, TRIPAL_WARNING);
- tripal_report_error('tcprops_form', TRIPAL_ERROR, "Please provide either a
- 'cv_name' or 'cv_id' as an option for adding relationship to the form", array());
- return;
- }
- }
- $form_state['rebuild'] = TRUE;
-
- $details['fieldset_title'] = (isset($details['fieldset_title'])) ? $details['fieldset_title'] : 'Relationships';
- $details['additional_instructions'] = (isset($details['additional_instructions'])) ? $details['additional_instructions'] : '';
- $details['nodetype_plural'] = (isset($details['nodetype_plural'])) ? $details['nodetype_plural'] : $details['nodetype'] . 's';
- $details['base_name_field'] = (isset($details['base_name_field'])) ? $details['base_name_field'] : 'uniquename';
- $details['subject_field_name'] = (isset($details['subject_field_name'])) ? $details['subject_field_name'] : 'subject_id';
- $details['object_field_name'] = (isset($details['object_field_name'])) ? $details['object_field_name'] : 'object_id';
-
-
- $table_schema = chado_get_schema($details['relationship_table']);
- $details['table_has_rank'] = (isset($table_schema['fields']['rank'])) ? TRUE : FALSE;
-
- if (isset($details['select_options'])) {
- $type_options = $details['select_options'];
- }
- else {
- if (isset($details['cv_name'])) {
- $type_options = array();
- $type_options[] = 'Select a Property';
- $sql = "
- SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.cv_id as cv_id
- FROM {cvterm} CVT
- INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
- WHERE
- CV.name = :cv_name AND
- NOT CVT.is_obsolete = 1
- ORDER BY CVT.name ASC
- ";
- $prop_types = chado_query($sql, array(':cv_name' => $details['cv_name']));
- while ($prop = $prop_types->fetchObject()) {
- $type_options[$prop->cvterm_id] = $prop->name;
- $details['cv_id'] = $prop->cv_id;
- }
- }
- elseif (isset($details['cv_id'])) {
- $type_options = array();
- $type_options[] = 'Select a Property';
- $sql = "
- SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition, CV.name AS cv_name
- FROM {cvterm} CVT
- INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
- WHERE
- CV.cv_id = :cv_id AND
- NOT CVT.is_obsolete = 1
- ORDER BY CVT.name ASC
- ";
- $prop_types = chado_query($sql, array(':cv_id' => $details['cv_id']));
- while ($prop = $prop_types->fetchObject()) {
- $type_options[$prop->cvterm_id] = $prop->name;
- $details['cv_name'] = $prop->cv_name;
- }
- }
- }
-
- if (empty($type_options)) {
- $tripal_message = tripal_set_message(
- t('There are currently no property types! To add additional properties to the drop
- down list, you need to <a href="@cvtermlink">add a controlled vocabulary term</a>
- to the %cv_name controlled vocabulary.',
- array(
- '%cv_name' => $details['cv_name'],
- '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/'.$details['cv_id'].'/cvterm/add')
- )
- ),
- TRIPAL_WARNING,
- array('return_html' => TRUE)
- );
- }
- else {
- $tripal_message = tripal_set_message(
- t('To add additional properties to the drop down list, you need to <a href="@cvtermlink">add
- a controlled vocabulary term</a> to the %cv_name controlled vocabulary.',
- array(
- '%cv_name' => $details['cv_name'],
- '@cvtermlink' => url('admin/tripal/chado/tripal_cv/cv/' . $details['cv_id'] . '/cvterm/add')
- )
- ),
- TRIPAL_INFO,
- array('return_html' => TRUE)
- );
- }
- $form['relationships'] = array(
- '#type' => 'fieldset',
- '#title' => t($details['fieldset_title']),
- '#prefix' => "<div id='relationships-fieldset'>",
- '#suffix' => '</div>',
- '#weight' => 10
- );
- $form['relationships']['descrip'] = array(
- '#type' => 'item',
- '#markup' => t('You may add relationships between this %nodetype and other
- %nodetype_plural by entering the details below. You may add
- as many relationships as desired by clicking the add button on the right. To
- remove a relationship, click the remove button. ' . $details['additional_instructions'],
- array('%nodetype' => $details['nodetype'], '%nodetype_plural' => $details['nodetype_plural'])),
- );
- $form['relationships']['admin_message'] = array(
- '#type' => 'markup',
- '#markup' => $tripal_message
- );
-
-
- $form['relationships']['relationship_table'] = array(
- '#type' => 'markup',
- '#tree' => TRUE,
- '#prefix' => '<div id="tripal-generic-edit-relationships-table">',
- '#suffix' => '</div>',
- '#theme' => 'chado_node_relationships_form_table'
- );
-
- $form['relationships']['relationship_table']['details'] = array(
- '#type' => 'hidden',
- '#value' => serialize($details)
- );
-
-
-
- if (isset($form_state['chado_relationships'])) {
- $existing_rels = $form_state['chado_relationships'];
- }
- else {
- $existing_rels = chado_query(
- "SELECT
- rel.*,
- rel.".$details['subject_field_name']." as subject_id,
- rel.".$details['object_field_name']." as object_id,
- base1.".$details['base_name_field']." as object_name,
- base2.".$details['base_name_field']." as subject_name,
- cvterm.name as type_name
- FROM {".$details['relationship_table']."} rel
- LEFT JOIN {".$details['base_table']."} base1 ON base1.".$details['base_foreign_key']." = rel.".$details['object_field_name']."
- LEFT JOIN {".$details['base_table']."} base2 ON base2.".$details['base_foreign_key']." = rel.".$details['subject_field_name']."
- LEFT JOIN {cvterm} cvterm ON cvterm.cvterm_id = rel.type_id
- WHERE rel.".$details['object_field_name']." = :base_key_value
- OR rel.".$details['subject_field_name']." = :base_key_value",
- array(':base_key_value' => $details['base_key_value'])
- );
- }
-
- foreach ($existing_rels as $relationship) {
- if (array_key_exists($relationship->type_id, $type_options)) {
-
-
-
-
- $rank = uniqid();
- $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = 'markup';
- $form['relationships']['relationship_table'][$relationship->type_id]['#type'] = '';
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['#type'] = 'markup';
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['#value'] = '';
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['object_id'] = array(
- '#type' => 'hidden',
- '#value' => $relationship->object_id
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['subject_id'] = array(
- '#type' => 'hidden',
- '#value' => $relationship->subject_id
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['type_id'] = array(
- '#type' => 'hidden',
- '#value' => $relationship->type_id
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['object_name'] = array(
- '#type' => 'markup',
- '#markup' => $relationship->object_name
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['type_name'] = array(
- '#type' => 'markup',
- '#markup' => $relationship->type_name
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['subject_name'] = array(
- '#type' => 'markup',
- '#markup' => $relationship->subject_name
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['rank'] = array(
- '#type' => 'markup',
- '#markup' => $rank
- );
- $form['relationships']['relationship_table'][$relationship->type_id][$rank]['rel_action'] = array(
- '#type' => 'submit',
- '#value' => t('Remove'),
- '#name' => "rel_remove-".$relationship->type_id.'-'.$rank,
- '#ajax' => array(
- 'callback' => 'chado_add_node_form_relationships_ajax_update',
- 'wrapper' => 'tripal-generic-edit-relationships-table',
- 'effect' => 'fade',
- 'method' => 'replace',
- 'prevent' => 'click'
- ),
-
-
-
-
-
-
-
- '#validate' => array('chado_add_node_form_relationships_form_remove_button_validate'),
- '#submit' => array('chado_add_node_form_relationships_remove_button_submit'),
-
-
-
- '#limit_validation_errors' => array(
- array('relationship_table')
- )
- );
- }
- }
- $form['relationships']['relationship_table']['new']['object_name'] = array(
- '#type' => 'textfield',
- '#autocomplete_path' => 'tripal_ajax/relationship_nodeform/' . $details['base_table'] . '/' . $details['base_name_field'].'/name_to_id'
- );
- $form['relationships']['relationship_table']['new']['object_is_current'] = array(
- '#type' => 'checkbox',
- '#title' => t('Current '.$details['nodetype']),
- );
- $form['relationships']['relationship_table']['new']['type_name'] = array(
- '#type' => 'select',
- '#options' => $type_options,
- );
- $form['relationships']['relationship_table']['new']['subject_name'] = array(
- '#type' => 'textfield',
- '#autocomplete_path' => 'tripal_ajax/relationship_nodeform/' . $details['base_table'] . '/' . $details['base_name_field'].'/name_to_id'
- );
- $form['relationships']['relationship_table']['new']['subject_is_current'] = array(
- '#type' => 'checkbox',
- '#title' => t('Current ' . $details['nodetype']),
- );
- $form['relationships']['relationship_table']['new']['rank'] = array(
- '#type' => 'markup',
- '#markup' => ''
- );
- $form['relationships']['relationship_table']['new']['rel_action'] = array(
- '#type' => 'submit',
- '#value' => t('Add'),
- '#name' => 'rel_add',
- '#ajax' => array(
- 'callback' => 'chado_add_node_form_relationships_ajax_update',
- 'wrapper' => 'tripal-generic-edit-relationships-table',
- 'effect' => 'fade',
- 'method' => 'replace',
- 'prevent' => 'click'
- ),
-
-
-
-
-
-
-
- '#validate' => array('chado_add_node_form_relationships_add_button_validate'),
- '#submit' => array('chado_add_node_form_relationships_add_button_submit'),
-
-
-
- '#limit_validation_errors' => array(
- array('relationship_table')
- )
- );
- }
- function chado_add_node_form_relationships_add_button_validate($form, &$form_state) {
- $details = unserialize($form_state['values']['relationship_table']['details']);
-
-
- if (!empty($form_state['values']['relationship_table']['new']['subject_name'])) {
- if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['subject_name'], $matches)) {
- $form_state['values']['relationship_table']['new']['subject_id'] = $matches[1];
- }
- else {
- form_set_error('subject_name', 'You need to select the subject from the autocomplete drop-down');
- }
- }
- if (!empty($form_state['values']['relationship_table']['new']['object_name'])) {
- if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['object_name'], $matches)) {
- $form_state['values']['relationship_table']['new']['object_id'] = $matches[1];
- }
- else {
- form_set_error('object_name', 'You need to select the subject from the autocomplete drop-down');
- }
- }
-
- if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
-
- if ($form_state['values']['relationship_table']['new']['subject_name'] == $form_state['values']['uniquename']) {
- $form_state['values']['relationship_table']['new']['subject_is_current'] = 1;
- form_set_error('subject_is_current', 'To set the current '.$details['nodetype'].', select the
- checkbox. You entered the unique name of the current '.$details['nodetype'].' as the subject,
- is this what you meant to do?');
- }
- elseif ($form_state['values']['relationship_table']['new']['subject_name'] == $form_state['values']['uniquename']) {
- $form_state['values']['relationship_table']['new']['object_is_current'] = 1;
- form_set_error('subject_is_current', 'To set the current '.$details['nodetype'].', select the
- checkbox. You entered the unique name of the current '.$details['nodetype'].' as the subject,
- is this what you meant to do?');
- }
- else {
- form_set_error('object_is_current', 'At least one member of the relationship must be
- the current '.$details['nodetype'].'. This is specified by checking the "Current '.$details['nodetype'].'"
- checkbox for either the subject or object.');
- }
- }
-
- if (!($form_state['values']['relationship_table']['new']['subject_is_current'])) {
- $result = chado_select_record(
- $details['base_table'],
- array($details['base_name_field']),
- array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['subject_id'])
- );
- if (!isset($result[0])) {
- form_set_error('subject_name', 'The subject must be the unique name of an
- existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
- }
- else {
- $form_state['values']['relationship_table']['new']['subject_name'] = $result[0]->{$details['base_name_field']};
- }
- }
-
- if (!($form_state['values']['relationship_table']['new']['object_is_current'])) {
- $result = chado_select_record(
- $details['base_table'],
- array($details['base_name_field']),
- array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['object_id'])
- );
- if (!isset($result[0])) {
- form_set_error('object_name', 'The object must be the unique name of an
- existing '.$details['nodetype'].' unless the "Current '.$details['nodetype'].'" checkbox is selected');
- }
- else {
- $form_state['values']['relationship_table']['new']['object_name'] = $result[0]->{$details['base_name_field']};
- }
- }
-
- if ($form_state['values']['relationship_table']['new']['type_name']) {
- $form_state['values']['relationship_table']['new']['type_id'] = $form_state['values']['relationship_table']['new']['type_name'];
- $result = chado_select_record(
- 'cvterm',
- array('name'),
- array('cvterm_id' => $form_state['values']['relationship_table']['new']['type_id'])
- );
- if (!isset($result[0])) {
- form_set_error('type_id', 'The select type is not a valid controlled vocabulary term.');
- }
- else {
- $form_state['values']['relationship_table']['new']['type_name'] = $result[0]->name;
- }
- }
- else {
- form_set_error('type_id', 'Please select a type of relationship');
- }
- }
- function chado_add_node_form_relationships_add_button_submit(&$form, &$form_state) {
- $details = unserialize($form_state['values']['relationship_table']['details']);
-
-
- if (!isset($form_state['chado_relationships'])) {
- chado_add_node_form_relationships_create_relationship_formstate_array($form, $form_state);
- }
- $name = (isset($form_state['node']->{$details['base_table']}->uniquename)) ? $form_state['node']->{$details['base_table']}->uniquename : 'CURRENT';
-
- if ($form_state['values']['relationship_table']['new']['subject_is_current']) {
- $relationship = array(
- 'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
- 'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
- 'object_id' => $form_state['values']['relationship_table']['new']['object_id'],
- 'object_name' => $form_state['values']['relationship_table']['new']['object_name'],
- 'subject_id' => $form_state['node']->{$details['base_table']}->{$details['base_foreign_key']},
- 'subject_name' => $name,
- 'rank' => uniqid(),
- );
-
- unset($form_state['input']['relationship_table']['new']['object_id']);
- unset($form_state['input']['relationship_table']['new']['object_name']);
- }
- else {
- $relationship = array(
- 'type_id' => $form_state['values']['relationship_table']['new']['type_id'],
- 'type_name' => $form_state['values']['relationship_table']['new']['type_name'],
- 'object_id' => $form_state['node']->{$details['base_table']}->{$details['base_foreign_key']},
- 'object_name' => $name,
- 'subject_id' => $form_state['values']['relationship_table']['new']['subject_id'],
- 'subject_name' => $form_state['values']['relationship_table']['new']['subject_name'],
- 'rank' => uniqid(),
- );
-
- unset($form_state['input']['relationship_table']['new']['subject_id']);
- unset($form_state['input']['relationship_table']['new']['subject_name']);
- }
- $key = $relationship['type_id'] . '-' . $relationship['rank'];
- $form_state['chado_relationships'][$key] = (object) $relationship;
-
- unset($form_state['input']['relationship_table']['new']['type_id']);
- unset($form_state['input']['relationship_table']['new']['type_name']);
- $form_state['rebuild'] = TRUE;
- }
- function chado_add_node_form_relationships_form_remove_button_validate($form, $form_state) {
-
- }
- function chado_add_node_form_relationships_remove_button_submit(&$form, &$form_state) {
-
-
- if (!isset($form_state['chado_relationships'])) {
- chado_add_node_form_relationships_create_relationship_formstate_array($form, $form_state);
- }
-
- if(preg_match('/rel_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
- $key = $match[1];
- if (array_key_exists($key, $form_state['chado_relationships'])) {
- unset($form_state['chado_relationships'][$key]);
- }
- }
- $form_state['rebuild'] = TRUE;
- }
- function chado_add_node_form_relationships_ajax_update($form, $form_state) {
- return $form['relationships']['relationship_table'];
- }
- function chado_add_node_form_relationships_create_relationship_formstate_array($form, &$form_state) {
- $form_state['chado_relationships'] = array();
- foreach (element_children($form['relationships']['relationship_table']) as $type_id) {
- if ($type_id != 'new') {
- foreach (element_children($form['relationships']['relationship_table'][$type_id]) as $rank) {
- $element = $form['relationships']['relationship_table'][$type_id][$rank];
- $rel = array(
- 'type_id' => $element['type_id']['#value'],
- 'object_id' => $element['object_id']['#value'],
- 'subject_id' => $element['subject_id']['#value'],
- 'type_name' => $element['type_name']['#markup'],
- 'object_name' => $element['object_name']['#markup'],
- 'subject_name' => $element['subject_name']['#markup'],
- 'rank' => $element['rank']['#markup']
- );
- $key = $rel['type_id'] . '-' . $rel['rank'];
- $form_state['chado_relationships'][$key] = (object) $rel;
- }
- }
- }
- }
- function theme_chado_add_node_form_relationships_table($variables) {
- $element = $variables['element'];
- $details = unserialize($element['details']['#value']);
- $header = array(
- 'subject_name' => t('Subject ' . $details['base_name_field']),
- 'type_name' => t('Type'),
- 'object_name' => t('Object ' . $details['base_name_field']),
- 'rel_action' => t('Action')
- );
- $rows = array();
- foreach (element_children($element) as $type_id) {
- if ($type_id == 'new') {
- $row = array();
- $row['data'] = array();
- foreach ($header as $fieldname => $title) {
- if ($fieldname == 'subject_name') {
- $row['data'][] = drupal_render($element[$type_id][$fieldname]) . drupal_render($element[$type_id]['subject_is_current']);
- }
- elseif ($fieldname == 'object_name') {
- $row['data'][] = drupal_render($element[$type_id][$fieldname]) . drupal_render($element[$type_id]['object_is_current']);
- }
- else {
- $row['data'][] = drupal_render($element[$type_id][$fieldname]);
- }
- }
- $rows[] = $row;
- }
- else {
- foreach (element_children($element[$type_id]) as $rank) {
- $row = array();
- $row['data'] = array();
- foreach ($header as $fieldname => $title) {
- $row['data'][] = drupal_render($element[$type_id][$rank][$fieldname]);
- }
- $rows[] = $row;
- }
- }
- }
- return theme('table', array(
- 'header' => $header,
- 'rows' => $rows
- ));
- }
- function chado_retrieve_node_form_relationships($node) {
- $rels = array();
- if (isset($node->relationship_table)) {
- foreach ($node->relationship_table as $type_id => $elements) {
- if ($type_id != 'new' AND $type_id != 'details') {
- foreach ($elements as $rank => $relationships) {
- $rels[$type_id][$rank]['subject_id'] = $relationships['subject_id'];
- $rels[$type_id][$rank]['object_id'] = $relationships['object_id'];
- }
- }
- }
- }
- return $rels;
- }
- function chado_update_node_form_relationships($node, $details, $retrieved_relationships = FALSE) {
- $relationship_table = $details['relationship_table'];
- $current_id = $details['foreignkey_value'];
- if (isset($node->relationship_table) AND ($current_id > 0)) {
-
- $form_details = unserialize($node->relationship_table['details']);
- $has_rank = $form_details['table_has_rank'];
-
- chado_delete_record(
- $relationship_table,
- array($form_details['subject_field_name'] => $current_id)
- );
- chado_delete_record(
- $relationship_table,
- array($form_details['object_field_name'] => $current_id)
- );
-
- if ($retrieved_relationships) {
- $relationships = $retrieved_relationships;
- }
- else {
- $relationships = chado_retrieve_node_form_relationships($node);
- }
- foreach ($relationships as $type_id => $ranks) {
- foreach ($ranks as $rank => $element) {
- $values = array(
- $form_details['subject_field_name'] => $element['subject_id'],
- 'type_id' => $type_id,
- $form_details['object_field_name'] => $element['object_id']
- );
-
-
- if (empty($values[$form_details['subject_field_name']])) {
- $values[$form_details['subject_field_name']] = $current_id;
- }
- if (empty($values[$form_details['object_field_name']])) {
- $values[$form_details['object_field_name']] = $current_id;
- }
- if ($has_rank) {
-
- $rank_select = chado_get_table_max_rank(
- $relationship_table,
- array(
- $form_details['subject_field_name'] => $values['subject_id'],
- 'type_id' => $values['type_id'],
- $form_details['object_field_name'] => $values['object_id'],
- )
- );
- $values['rank'] = $rank_select + 1;
- }
-
- $success_link = chado_insert_record(
- $relationship_table,
- $values
- );
- }
- }
- }
- }
- function chado_add_node_form_relationships_name_to_id_callback($base_table, $name_field, $string) {
- $matches = array();
- $base_key = $base_table.'_id';
- $result = db_select('chado.'.$base_table, 'b')
- ->fields('b', array($base_key, $name_field))
- ->condition($name_field, '%' . db_like($string) . '%', 'LIKE')
- ->execute();
-
- foreach ($result as $row) {
- $key = '('.$row->{$base_key}.') '.substr($row->{$name_field},0,50) . '...';
- $matches[$key] = check_plain($row->{$name_field});
- }
-
- drupal_json_output($matches);
- }
|