123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?php
- function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
- if ($type == 'TripalEntity') {
-
- if (!property_exists($entity, 'chado_table')) {
- $entity->chado_table = NULL;
- $entity->chado_column = NULL;
- $entity->chado_linker = NULL;
- $entity->chado_type_id = NULL;
- $entity->chado_type_value = NULL;
- $entity->chado_base_type_id = NULL;
-
- if (!$bundle) {
- $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
- }
- if ($bundle->data_table) {
- $entity->chado_table = $bundle->data_table;
- $entity->chado_column = $bundle->type_column;
- $entity->chado_linker = $bundle->type_linker_table;
- $entity->chado_type_id = $bundle->type_id;
- $entity->chado_type_value = $bundle->type_value;
- $entity->chado_base_type_id = $bundle->base_type_id;
- }
- }
- if (!property_exists($entity, 'chado_record')) {
- $entity->chado_record = NULL;
- $entity->chado_record_id = NULL;
- }
- }
- }
- function tripal_chado_entity_presave($entity, $type) {
- }
- function tripal_chado_entity_postsave($entity, $type) {
- }
- function tripal_chado_entity_load($entities, $type) {
- if ($type == 'TripalBundle') {
- foreach ($entities as $bundle) {
-
- if (property_exists($bundle, 'id')) {
- $chado_bundle = db_select('chado_bundle', 'cb')
- ->fields('cb')
- ->condition('cb.bundle_id', $bundle->id)
- ->execute()
- ->fetchObject();
- if ($chado_bundle) {
- $bundle->data_table = $chado_bundle->data_table;
- $bundle->type_linker_table = $chado_bundle->type_linker_table;
- $bundle->type_column = $chado_bundle->type_column;
- $bundle->type_id = $chado_bundle->type_id;
- $bundle->type_value = $chado_bundle->type_value;
- $bundle->base_type_id = $chado_bundle->base_type_id;
- }
- }
- }
- }
- if ($type == 'TripalEntity') {
- foreach ($entities as $entity) {
-
- if (property_exists($entity, 'id')) {
-
- $entity->chado_table = NULL;
- $entity->chado_column = NULL;
- $entity->chado_record = NULL;
- $entity->chado_record_id = NULL;
-
- $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
- if (!$bundle) {
- continue;
- }
- $entity->chado_table = $bundle->data_table;
- $entity->chado_column = $bundle->type_column;
- $chado_entity_table = chado_get_bundle_entity_table($bundle);
- $chado_entity = db_select($chado_entity_table, 'ce')
- ->fields('ce')
- ->condition('ce.entity_id', $entity->id)
- ->execute()
- ->fetchObject();
- if ($chado_entity) {
- $schema = chado_get_schema($entity->chado_table);
- $record = chado_generate_var($entity->chado_table,
- [$schema['primary key'][0] => $chado_entity->record_id]);
- $entity->chado_record = $record;
- $entity->chado_record_id = $chado_entity->record_id;
- }
- }
- }
- }
- }
- function tripal_chado_entity_insert($entity, $type) {
- }
- function tripal_chado_entity_update($entity, $type) {
- }
- function tripal_chado_entity_delete($entity, $type) {
- if ($type !== 'TripalEntity') {
- return;
- }
-
- $bundle = $entity->bundle;
- if (db_table_exists("chado_{$bundle}")) {
- $chado_table = $entity->chado_table;
- $table_def = chado_get_schema($chado_table);
- $pkey_field = $table_def['primary key'][0];
- $match = [
- $pkey_field => $entity->chado_record_id,
- ];
-
-
-
-
- $previous_db = chado_set_active('chado');
- try {
- chado_delete_record($entity->chado_table, $match);
- chado_set_active($previous_db);
- } catch (Exception $e) {
- chado_set_active($previous_db);
- throw $e;
- }
- }
- }
- function tripal_chado_entity_unpublish($entity, $type) {
- if ($type !== 'TripalEntity') {
- return;
- }
-
- $bundle = $entity->bundle;
- if (db_table_exists("chado_{$bundle}")) {
- db_delete("chado_{$bundle}")->condition('entity_id', $entity->id)->execute();
- }
- }
- function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
- $format = [];
- $table = $bundle->data_table;
- if ($table == 'organism') {
- if (chado_get_version() <= '1.2') {
- $format[] = [
- 'format' => '[taxrank__genus] [taxrank__species]',
- 'weight' => -5,
- ];
- }
- else {
- $format[] = [
- 'format' => '[taxrank__genus] [taxrank__species] [taxrank__infraspecific_taxon,TAXRANK:0000045]',
- 'weight' => -5,
- ];
- }
- }
- if ($table == 'arraydesign') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'assay') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'biomaterial') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'analysis') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'feature') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'featuremap') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'stock') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'pub') {
- $format[] = [
- 'format' => '[tpub__title]',
- 'weight' => -5,
- ];
- }
- if ($table == 'cvterm') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'project') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'contact') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'phylotree') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'library') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- if ($table == 'protocol') {
- $format[] = [
- 'format' => '[schema__name]',
- 'weight' => -5,
- ];
- }
- return $format;
- }
- function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
-
-
-
- if ($type == 'TripalEntity') {
-
- if ($view_mode == 'full') {
-
- $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
- $chado_table = $bundle->data_table;
- $chado_field = $bundle->type_column;
-
-
-
- $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
- []);
- $legacy_template = 'legacy_template--chado_' . $chado_table;
-
-
-
- if (key_exists($legacy_template,
- $enabled_templates) && $enabled_templates[$legacy_template]) {
-
- $fields = field_info_fields();
- foreach ($fields as $field) {
- if ($field['storage']['type'] == 'field_chado_storage' or $field['storage']['type'] == 'tripal_no_storage') {
- $field_name = $field['field_name'];
- if (property_exists($entity, $field_name)) {
- $entity->$field_name = NULL;
- unset($entity->content[$field_name]);
- }
- }
- }
-
- $entity->type = 'chado_' . $chado_table;
- $entity->$chado_table = $entity->chado_record;
-
- $node_schema = drupal_get_schema('node');
- foreach ($node_schema['fields'] as $field_name => $details) {
- if (!property_exists($entity, $field_name)) {
- $entity->$field_name = '';
- if (array_key_exists('default', $details)) {
- $entity->$field_name = $details['default'];
- }
- }
- }
-
-
- $modules = module_list();
- foreach ($modules as $mname => $details) {
- $function_name = $mname . '_node_view';
- if (function_exists($function_name)) {
- $function_name($entity, $view_mode, $langcode);
- }
- }
- }
- }
- }
- }
- function tripal_chado_entity_view_alter(&$build) {
-
-
- if ($build['#entity_type'] == 'TripalEntity') {
- $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
- []);
- $entity = $build['#entity'];
- $legacy_template = 'legacy_template--' . $entity->type;
- if (key_exists($legacy_template,
- $enabled_templates) && $enabled_templates[$legacy_template]) {
- $build['#entity']->nid = NULL;
- $build['#node'] = $build['#entity'];
- $modules = module_list();
- foreach ($modules as $mname => $details) {
- $function_name = $mname . '_node_view_alter';
- if (function_exists($function_name)) {
- $function_name($build);
- }
- }
- }
- }
- }
|