123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- function tripal_load_term_entity($namespace, $accession) {
- $query = db_select('tripal_term', 'tt');
- $query->join('tripal_vocab' ,'tv', 'tv.id = tt.vocab_id');
- $query->fields('tt', array('id', 'accession'))
- ->fields('tv', array('namespace'))
- ->condition('tv.namespace', $namespace)
- ->condition('tt.accession', $accession);
- $term = $query->execute()->fetchObject();
- if ($term) {
- $entity = entity_load('TripalTerm', array($term->id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_load_vocab_entity($namespace) {
- $vocab = db_select('tripal_vocab', 'tv')
- ->fields('tv')
- ->condition('tv.namespace', $namespace)
- ->execute()
- ->fetchObject();
- if ($vocab) {
- $entity = entity_load('TripalVocab', array($vocab->id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_load_bundle_entity($name) {
- $bundle = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->condition('tb.name', $name)
- ->execute()
- ->fetchObject();
- if ($bundle) {
- $entity = entity_load('TripalBundle', array($bundle->id));
- return reset($entity);
- }
- return NULL;
- }
- function tripal_create_bundle($namespace, $accession, $term_name, &$error = '') {
-
- $vocab = tripal_load_vocab_entity($namespace);
- if (!$vocab) {
- $vocab = entity_get_controller('TripalVocab')->create(array('namespace' => $namespace));
- $vocab->save();
- }
-
- $term = tripal_load_term_entity($namespace, $accession);
- if (!$term) {
- $args = array('vocab_id' => $vocab->id, 'accession' => $accession, 'name' => $term_name);
- $term = entity_get_controller('TripalTerm')->create($args);
- $term = $term->save();
- }
-
- $bundle_name = 'bio-data_' . $term->id;
- $einfo = entity_get_info('TripalEntity');
- if (!in_array($bundle_name, array_keys($einfo['bundles']))) {
-
- db_insert('tripal_bundle')
- ->fields(array(
- 'label' => $term_name,
- 'type' => 'TripalEntity',
- 'name' => $bundle_name,
- 'term_id' => $term->id,
- ))
- ->execute();
- }
-
-
- global $language;
- $langcode = $language->language;
- cache_clear_all("entity_info:$langcode", 'cache');
- variable_set('menu_rebuild_needed', TRUE);
-
- $bundle = tripal_load_bundle_entity($bundle_name);
-
- module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle, $term);
- return TRUE;
- }
- function hook_entity_create(&$entity, $entity_type) {
- }
- function hook_add_bundle_fields($entity_type, $bundle, $term) {
- }
- function tripal_get_bundle_variable($variable_name, $bundle_id, $default = FALSE) {
- $variable = tripal_get_variable($variable_name);
-
- if (!$variable) {
- return $default;
- }
-
- $value = db_select('tripal_bundle_variables', 'var')
- ->fields('var', array('value'))
- ->condition('var.bundle_id', $bundle_id)
- ->condition('var.variable_id', $variable->variable_id)
- ->execute()
- ->fetchField();
-
- if (!$value) {
- return $default;
- }
- return $value;
- }
- function tripal_set_bundle_variable($variable_name, $bundle_id, $value) {
- $variable = tripal_get_variable($variable_name);
-
- $record = array(
- 'bundle_id' => $bundle_id,
- 'variable_id' => $variable->variable_id,
- 'value' => $value,
- );
-
- $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
- ->fields('var', array('bundle_variable_id'))
- ->condition('var.bundle_id', $record['bundle_id'])
- ->condition('var.variable_id', $record['variable_id'])
- ->execute()
- ->fetchField();
- if ($bundle_variable_id) {
- $record['bundle_variable_id'] = $bundle_variable_id;
- return drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
- }
- else {
- return drupal_write_record('tripal_bundle_variables', $record);
- }
- }
- function tripal_get_title_format($entity) {
-
- $title_format = tripal_get_bundle_variable('title_format', $entity->id);
-
-
- if (!$title_format) {
- $title_format = tripal_get_default_title_format($entity);
- tripal_save_title_format($entity, $title_format);
- }
- return $title_format;
- }
- function tripal_save_title_format($entity, $format) {
- return tripal_set_bundle_variable('title_format', $entity->id, $format);
- }
- function tripal_get_default_title_format($entity) {
- $format = array();
-
- $tokens = tripal_get_tokens($entity);
- foreach($tokens as $token) {
- if ($token['required']) {
- $format[] = $token['token'];
- }
- }
- return implode(', ', $format);
- }
- function tripal_get_tokens($entity) {
- $tokens = array();
- $fields = field_info_instances('TripalEntity', $entity->name);
- foreach ($fields as $f) {
-
- $token = '[' . $f['field_name'] . ']';
- $tokens[$token] = array(
- 'label' => $f['label'],
- 'description' => $f['description'],
- 'token' => $token,
- 'field_name' => $f['field_name'],
- 'required' => $f['required']
- );
- }
- return $tokens;
- }
- function theme_token_list($tokens) {
- $header = array('Token', 'Name', 'Description');
- $rows = array();
- foreach ($tokens as $details) {
- $rows[] = array(
- '[' . $details['field_name'] . ']',
- $details['label'],
- $details['description'],
- );
- }
- return theme('table', array('header' => $header, 'rows' => $rows));
- }
- function hook_vocab_storage_info() {
- return array(
- 'term_chado_storage' => array(
- 'label' => t('Chado storage'),
- 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
- 'settings' => array(),
- ),
- );
- }
- function hook_vocab_select_term_form(&$form, &$form_state) {
- return $form;
- }
- function hook_vocab_select_term_form_validate($form, &$form_state) {
- }
- function hook_vocab_get_term($namespace, $accession) {
- }
|