123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?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($values) {
- $query = db_select('tripal_bundle', 'tb');
- $query->fields('tb');
- if (array_key_exists('name', $values)) {
- $query->condition('tb.name', $values['name']);
- }
- if (array_key_exists('label', $values)) {
- $query->condition('tb.label', $values['label']);
- }
- $bundle = $query->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(array('name' => $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 = NULL;
-
- $tokens = tripal_get_tokens($entity);
-
-
- $suggestions = module_invoke_all('tripal_default_title_format', $entity, $tokens);
- if ($suggestions) {
-
- $lightest_key = NULL;
- foreach ($suggestions as $k => $s) {
- if ($lightest_key === NULL) $lightest_key = $k;
- if ($s['weight'] < $lightest_key) $lightest_key = $k;
- }
- $format = $suggestions[$lightest_key]['format'];
- }
-
- $name_fields = preg_grep('/name/', array_keys($tokens));
- if ($name_fields AND !$format) {
- $format = implode(', ', $name_fields);
- }
-
- if (!$format) {
- $tmp = array();
-
- foreach($tokens as $token) {
- if ($token['required']) {
- $tmp[] = $token['token'];
- }
- }
- $format = implode(', ', $tmp);
- }
- return $format;
- }
- function hook_tripal_default_title_format($entity, $available_tokens) {
- $format = array();
-
-
-
- $term = entity_load('TripalTerm', array('id' => $entity->term_id));
- $term = reset($term);
-
- if ($term->name == 'organism') {
-
- $format[] = array(
-
- 'format' => '[organism__genus] [organism__species]',
-
- 'weight' => -5
- );
- }
-
-
-
- $name_field = preg_grep('/__name]$/', array_keys($available_tokens));
- $name_field = reset($name_field);
- if (is_string($name_field)) {
- $format[] = array(
- 'format' => $name_field,
- 'weight' => -2,
- );
- }
- return $format;
- }
- function tripal_get_tokens($entity, $options = array()) {
- $tokens = array();
-
- $options['required only'] = (isset($options['required only'])) ? $options['required only'] : FALSE;
- $options['include id'] = (isset($options['include id'])) ? $options['include id'] : TRUE;
- if ($options['include id']) {
- $token = '[TripalBundle__bundle_id]';
- $tokens[$token] = array(
- 'label' => 'Bundle ID',
- 'description' => 'The unique identifier for this Tripal Content Type.',
- 'token' => $token,
- 'field_name' => NULL,
- 'required' => TRUE
- );
- $token = '[TripalEntity__entity_id]';
- $tokens[$token] = array(
- 'label' => 'Content/Entity ID',
- 'description' => 'The unique identifier for an individual piece of Tripal Content.',
- 'token' => $token,
- 'field_name' => NULL,
- 'required' => TRUE
- );
- }
- $fields = field_info_instances('TripalEntity', $entity->name);
- foreach ($fields as $f) {
-
- $token = '[' . $f['field_name'] . ']';
- $current_token = array(
- 'label' => $f['label'],
- 'description' => $f['description'],
- 'token' => $token,
- 'field_name' => $f['field_name'],
- 'required' => $f['required']
- );
-
-
- if ($options['required only'] AND $current_token['required']) {
- $tokens[$token] = $current_token;
- }
-
- elseif (!$options['required only']) {
- $tokens[$token] = $current_token;
- }
- }
- return $tokens;
- }
- function tripal_replace_tokens($string, $entity, $bundle_entity = NULL) {
-
- if (preg_match_all('/\[\w+\]/', $string, $matches)) {
- $used_tokens = $matches[0];
- foreach($used_tokens as $token) {
- $field = str_replace(array('.','[',']'),array('__','',''),$token);
- $value = '';
- if (isset($entity->{$field})) {
-
-
- $field_value = field_get_items('TripalEntity', $entity, $field);
- if (isset($field_value[0])) {
-
- $field_render_arr = field_view_value('TripalEntity', $entity, $field, $field_value[0]);
-
- $value = render($field_render_arr);
- }
- }
- elseif ($field === 'TripalBundle__bundle_id') {
-
- if (!$bundle_entity) {
- $bundle_entity = tripal_load_bundle_entity(array('name' => $entity->bundle));
- }
-
- $value = $bundle_entity->id;
- }
- elseif ($field === 'TripalEntity__entity_id') {
-
- $value = $entity->id;
- }
- $string = str_replace($token, $value, $string);
- }
- }
- return $string;
- }
- function theme_token_list($tokens) {
- $header = array('Token', 'Name', 'Description');
- $rows = array();
- foreach ($tokens as $details) {
- $rows[] = array(
- $details['token'],
- $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) {
-
- }
|