123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- <?php
- function tripal_contact_node_info() {
- return array(
- 'chado_contact' => array(
- 'name' => t('Contact'),
- 'base' => 'chado_contact',
- 'description' => t('A contact from the Chado database'),
- 'title_label' => t('Article Title'),
- 'body_label' => t('Abstract'),
- 'has_title' => TRUE,
- 'has_body' => FALSE,
- 'chado_node_api' => array(
- 'base_table' => 'contact',
- 'hook_prefix' => 'chado_contact',
- 'record_type_title' => array(
- 'singular' => t('Contact'),
- 'plural' => t('Contacts')
- ),
- 'sync_filters' => array(
- 'type_id' => FALSE,
- 'organism_id' => FALSE
- ),
- )
- ),
- );
- }
- function chado_contact_form(&$node, $form_state) {
- $form = array();
-
-
-
-
-
-
-
-
-
- $contact_id = null;
- $type_id = 0;
- $title = '';
- $description = '';
-
- if (property_exists($node, 'contact')) {
- $contact = $node->contact;
- $contact_id = $contact->contact_id;
-
- $type_id = $contact->type_id->cvterm_id;
- $title = $contact->name;
-
-
-
-
-
-
-
-
-
-
- $description = '';
- if (property_exists($node, 'body')) {
- $description = $node->body;
- }
- else {
- $description = $contact->description;
- }
- if (!$description) {
- $contactprop = tripal_contact_get_property($contact->contact_id, 'contact_description');
- $description = (isset($contactprop->value)) ? $contactprop->value : '';
- }
-
- $form['contact_id'] = array(
- '#type' => 'value',
- '#value' => $contact->contact_id,
- );
- }
-
-
- if (array_key_exists('values', $form_state)) {
- $type_id = $form_state['values']['type_id'];
- $title = $form_state['values']['title'];
- $description = $form_state['values']['description'];
- }
-
-
- if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
- $type_id = $form_state['input']['type_id'];
- $title = $form_state['input']['title'];
- $description = $form_state['input']['description'];
- }
-
-
-
- $sql = "
- SELECT CVTS.cvterm_id, CVTS.name
- FROM {cvtermpath} CVTP
- INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
- INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
- INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
- WHERE
- CV.name = 'tripal_contact' AND
- CVTO.name = 'Contact Type' AND
- CVTP.pathdistance = 1
- ORDER BY CVTS.name ASC
- ";
- $results = chado_query($sql);
- $contact_types = array(3723 => 'Person');
- while ($contact_type = $results->fetchObject()) {
- $contact_types[$contact_type->cvterm_id] = $contact_type->name;
- if (strcmp($contact_type->name, "Person") == 0 and !$type_id) {
- $type_id = $contact_type->cvterm_id;
- }
- }
- $form['type_id'] = array(
- '#type' => 'select',
- '#title' => t('Contact Type'),
- '#options' => $contact_types,
- '#required' => TRUE,
- '#default_value' => $type_id,
- );
- $form['title']= array(
- '#type' => 'textfield',
- '#title' => t('Contact Name'),
- '#description' => t('Enter the name of this contact'),
- '#required' => TRUE,
- '#default_value' => $title,
- '#maxlength' => 255,
- );
- $form['description']= array(
- '#type' => 'textarea',
- '#title' => t('Contact Description'),
- '#description' => t('A brief description of the contact'),
- '#required' => TRUE,
- '#default_value' => $description,
- );
-
-
-
- $select_options = array();
- $select_options[] = 'Select a Property';
- $sql = "
- SELECT CVTS.cvterm_id, CVTS.name
- FROM {cvtermpath} CVTP
- INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
- INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
- INNER JOIN {cv} CV ON CVTO.cv_id = CV.cv_id
- WHERE
- CV.name = 'tripal_contact' AND
- NOT CVTO.name = 'Contact Type'
- ORDER BY CVTS.name ASC";
- $prop_types = chado_query($sql);
- while ($prop = $prop_types->fetchObject()) {
-
- if ($prop->name != 'Citation') {
- if (!isset($select_options[$prop->cvterm_id])) {
- $select_options[$prop->cvterm_id] = $prop->name;
- }
- }
- }
- $details = array(
- 'property_table' => 'contactprop',
- 'base_foreign_key' => 'contact_id',
- 'base_key_value' => $contact_id,
- 'cv_name' => 'tripal_contact',
- 'select_options' => $select_options
- );
- chado_node_properties_form($form, $form_state, $details);
-
-
-
-
- $cv_result = tripal_core_chado_select('cv',array('cv_id'),array('name' => 'contact_relationship_types'));
- $cv_id = $cv_result[0]->cv_id;
- $select_options = tripal_cv_get_cvterm_options($cv_id);
- if (empty($select_options)) {
- $cv_result = tripal_core_chado_select('cv',array('cv_id'),array('name' => 'relationship'));
- $cv_id = $cv_result[0]->cv_id;
- $select_options = tripal_cv_get_cvterm_options($cv_id);
- }
-
- $details = array(
- 'relationship_table' => 'contact_relationship',
- 'base_table' => 'contact',
- 'base_foreign_key' => 'contact_id',
- 'base_key_value' => $contact_id,
- 'nodetype' => 'contact',
- 'cv_name' => 'contact_relationship_types',
- 'base_name_field' => 'name',
- 'select_options' => $select_options
- );
-
- chado_node_relationships_form($form, $form_state, $details);
- return $form;
- }
- function chado_contact_validate($node, $form, &$form_state) {
-
- $node->title = trim($node->title);
- $node->description = trim($node->description);
-
- if($node->op == 'Delete') {
- return;
- }
-
-
- if (is_null($node->nid) and property_exists($node, 'contact_id') and $node->contact_id != 0) {
- return;
- }
-
- if (property_exists($node, 'nid')) {
-
- $values = array('contact_id' => $node->contact_id);
- $result = tripal_core_chado_select('contact', array('*'), $values);
- $contact = $result[0];
-
- if ($contact->name != $node->title) {
- $values = array('name' => $node->title);
- $result = tripal_core_chado_select('contact', array('contact_id'), $values);
- if ($result and count($result) > 0) {
- form_set_error('title', 'Cannot update the contact with this contact name. A contact with this name already exists.');
- return;
- }
- }
- }
-
- else {
-
- $values = array(
- 'name' => $node->title,
- );
- $contact = tripal_core_chado_select('contact', array('contact_id'), $values);
- if ($contact and count($contact) > 0) {
- form_set_error('title', 'Cannot add the contact with this name. A contact with these values already exists.');
- return;
- }
- }
- }
- function chado_contact_node_access($node, $op, $account ) {
- if ($op == 'create') {
- if (!user_access('create chado_contact content', $account)) {
- return FALSE;
- }
- return TRUE;
- }
- if ($op == 'update') {
- if (!user_access('edit chado_contact content', $account)) {
- return FALSE;
- }
- }
- if ($op == 'delete') {
- if (!user_access('delete chado_contact content', $account)) {
- return FALSE;
- }
- }
- if ($op == 'view') {
- if (!user_access('access chado_contact content', $account)) {
- return FALSE;
- }
- }
- return NULL;
- }
- function chado_contact_insert($node) {
-
- $node->title = trim($node->title);
- $node->description = trim($node->description);
-
-
-
- if (!property_exists($node, 'contact_id')) {
-
- $values = array(
- 'name' => $node->title,
- 'description' => '',
- 'type_id' => $node->type_id,
- );
- $contact = tripal_core_chado_insert('contact', $values);
- if (!$contact) {
- drupal_set_message(t('Unable to add contact.', 'warning'));
- watchdog('tripal_contact', 'Insert contact: Unable to create contact where values: %values',
- array('%values' => print_r($values, TRUE)), WATCHDOG_ERROR);
- return;
- }
- $contact_id = $contact['contact_id'];
-
- $properties = chado_node_properties_form_retreive($node);
- $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
- $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
-
- $details = array(
- 'property_table' => 'contactprop',
- 'base_table' => 'contact',
- 'foreignkey_name' => 'contact_id',
- 'foreignkey_value' => $contact_id
- );
- chado_node_properties_form_update_properties($node, $details, $properties);
-
- $details = array(
- 'relationship_table' => 'contact_relationship',
- 'foreignkey_value' => $contact_id
- );
- chado_node_relationships_form_update_relationships($node, $details);
- }
- else {
- $contact_id = $node->contact_id;
- }
-
-
- $check_org_id = chado_get_id_for_node('contact', $node->nid);
- if (!$check_org_id) {
- $record = new stdClass();
- $record->nid = $node->nid;
- $record->vid = $node->vid;
- $record->contact_id = $contact_id;
- drupal_write_record('chado_contact', $record);
- }
- return TRUE;
- }
- function chado_contact_update($node) {
-
- $node->title = trim($node->title);
- $node->description = trim($node->description);
- $contact_id = chado_get_id_for_node('contact', $node->nid) ;
-
- $match = array(
- 'contact_id' => $contact_id,
- );
- $values = array(
- 'name' => $node->title,
- 'description' => '',
- 'type_id' => $node->type_id
- );
- $status = tripal_core_chado_update('contact', $match, $values);
- if (!$status) {
- drupal_set_message("Error updating contact", "error");
- watchdog('t_contact', "Error updating contact", array(), WATCHDOG_ERROR);
- return;
- }
-
- $properties = chado_node_properties_form_retreive($node);
- $contact_descrip_id = tripal_cv_get_cvterm_by_name('contact_description', NULL, 'tripal_contact');
- $properties[$contact_descrip_id->cvterm_id][0] = $node->description;
-
-
- $details = array(
- 'property_table' => 'contactprop',
- 'base_table' => 'contact',
- 'foreignkey_name' => 'contact_id',
- 'foreignkey_value' => $contact_id
- );
- chado_node_properties_form_update_properties($node, $details, $properties);
-
- $details = array(
- 'relationship_table' => 'contact_relationship',
- 'foreignkey_value' => $contact_id
- );
- chado_node_relationships_form_update_relationships($node, $details);
- }
- function chado_contact_load($nodes) {
- foreach ($nodes as $nid => $node) {
-
- $contact_id = chado_get_id_for_node('contact', $nid);
-
- $values = array('contact_id' => $contact_id);
- $contact = tripal_core_generate_chado_var('contact', $values);
-
-
-
- $values = array(
- 'contact_id' => $contact->contact_id,
- 'type_id' => array(
- 'name' => 'contact_description',
- ),
- );
- $options = array(
- 'return_array' => 1,
- 'include_fk' => array('type_id' => 1),
- );
- $description = tripal_core_generate_chado_var('contactprop', $values, $options);
- if (count($description) == 1) {
- $description = tripal_core_expand_chado_vars($description, 'field', 'contactprop.value');
- $contact->description = $description[0]->value;
- }
- $nodes[$nid]->contact = $contact;
- }
- }
- function chado_contact_delete(&$node) {
- $contact_id = chado_get_id_for_node('contact', $node->nid);
-
-
- if (!$contact_id) {
- return;
- }
-
-
- $sql_del = "DELETE FROM {chado_contact} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
- $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
- $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
- db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
-
- chado_query("DELETE FROM {contactprop} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
- chado_query("DELETE FROM {contact} WHERE contact_id = :contact_id", array(':contact_id' => $contact_id));
- }
- function tripal_contact_node_view($node, $view_mode, $langcode) {
- switch ($node->type) {
- case 'chado_contact':
-
- if ($view_mode == 'full') {
- $node->content['tripal_contact_base'] = array(
- '#value' => theme('tripal_contact_base', array('node' => $node)),
- '#tripal_toc_id' => 'base',
- '#tripal_toc_title' => 'Details',
- '#weight' => 0,
- );
- $node->content['tripal_contact_properties'] = array(
- '#value' => theme('tripal_contact_properties', array('node' => $node)),
- '#tripal_toc_id' => 'properties',
- '#tripal_toc_title' => 'Properties',
- );
- $node->content['tripal_contact_publications'] = array(
- '#value' => theme('tripal_contact_publications', array('node' => $node)),
- '#tripal_toc_id' => 'publications',
- '#tripal_toc_title' => 'Publications',
- );
- $node->content['tripal_contact_relationships'] = array(
- '#value' => theme('tripal_contact_relationships', array('node' => $node)),
- '#tripal_toc_id' => 'relationships',
- '#tripal_toc_title' => 'Relationships',
- );
- }
- if ($view_mode == 'teaser') {
- $node->content['tripal_contact_teaser'] = array(
- '#value' => theme('tripal_contact_teaser', array('node' => $node)),
- );
- }
- break;
- }
- }
- function tripal_contact_node_presave($node) {
- switch ($node->type) {
- case 'chado_contact':
-
-
- if(property_exists($node, 'contact')) {
-
- $node->title = $node->contact->name;
- }
- else {
-
- }
- break;
- }
- }
|