tripal_chado.entity.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Implements hook_entity_create().
  4. *
  5. * This hook is called when brand new entities are created, but
  6. * they are not loaded so the hook_entity_load() is not yet called. We
  7. * can use this hook to add properties to the entity before saving.
  8. *
  9. * @param $entity
  10. * The entity being created.
  11. * @param $type
  12. * The type of entity being created.
  13. * @param $bundle (OPTIONAL)
  14. * The bundle object for the current entity.
  15. */
  16. function tripal_chado_entity_create(&$entity, $type, $bundle = NULL) {
  17. if ($type == 'TripalEntity') {
  18. // Set some defaults on vars needed by this module.
  19. if (!property_exists($entity, 'chado_table')) {
  20. $entity->chado_table = NULL;
  21. $entity->chado_column = NULL;
  22. $entity->chado_linker = NULL;
  23. $entity->chado_type_id = NULL;
  24. $entity->chado_type_value = NULL;
  25. $entity->chado_base_type_id = NULL;
  26. // Add in the Chado table information for this entity type.
  27. if (!$bundle) {
  28. $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
  29. }
  30. if ($bundle->data_table) {
  31. $entity->chado_table = $bundle->data_table;
  32. $entity->chado_column = $bundle->type_column;
  33. $entity->chado_linker = $bundle->type_linker_table;
  34. $entity->chado_type_id = $bundle->type_id;
  35. $entity->chado_type_value = $bundle->type_value;
  36. $entity->chado_base_type_id = $bundle->base_type_id;
  37. }
  38. }
  39. if (!property_exists($entity, 'chado_record')) {
  40. $entity->chado_record = NULL;
  41. $entity->chado_record_id = NULL;
  42. }
  43. }
  44. }
  45. /**
  46. * Implements hook_entity_presave().
  47. */
  48. function tripal_chado_entity_presave($entity, $type) {
  49. }
  50. /**
  51. * Implements hook_entity_postsave().
  52. */
  53. function tripal_chado_entity_postsave($entity, $type) {
  54. }
  55. /**
  56. * Implements hook_entity_load().
  57. */
  58. function tripal_chado_entity_load($entities, $type) {
  59. if ($type == 'TripalBundle') {
  60. foreach ($entities as $bundle) {
  61. // We want to add in the record ID to the entity.
  62. if (property_exists($bundle, 'id')) {
  63. $chado_bundle = db_select('chado_bundle', 'cb')
  64. ->fields('cb')
  65. ->condition('cb.bundle_id', $bundle->id)
  66. ->execute()
  67. ->fetchObject();
  68. if ($chado_bundle) {
  69. $bundle->data_table = $chado_bundle->data_table;
  70. $bundle->type_linker_table = $chado_bundle->type_linker_table;
  71. $bundle->type_column = $chado_bundle->type_column;
  72. $bundle->type_id = $chado_bundle->type_id;
  73. $bundle->type_value = $chado_bundle->type_value;
  74. $bundle->base_type_id = $chado_bundle->base_type_id;
  75. }
  76. }
  77. }
  78. }
  79. if ($type == 'TripalEntity') {
  80. foreach ($entities as $entity) {
  81. // We want to add in the record ID to the entity.
  82. if (property_exists($entity, 'id')) {
  83. // Set some defaults on vars needed by this module.
  84. $entity->chado_table = NULL;
  85. $entity->chado_column = NULL;
  86. $entity->chado_record = NULL;
  87. $entity->chado_record_id = NULL;
  88. // Add in the Chado table information for this entity type.
  89. $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
  90. if (!$bundle) {
  91. continue;
  92. }
  93. $entity->chado_table = $bundle->data_table;
  94. $entity->chado_column = $bundle->type_column;
  95. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  96. $chado_entity = db_select($chado_entity_table, 'ce')
  97. ->fields('ce')
  98. ->condition('ce.entity_id', $entity->id)
  99. ->execute()
  100. ->fetchObject();
  101. if ($chado_entity) {
  102. $schema = chado_get_schema($entity->chado_table);
  103. $record = chado_generate_var($entity->chado_table,
  104. [$schema['primary key'][0] => $chado_entity->record_id]);
  105. $entity->chado_record = $record;
  106. $entity->chado_record_id = $chado_entity->record_id;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. *
  114. * Implements hook_entity_insert().
  115. */
  116. function tripal_chado_entity_insert($entity, $type) {
  117. }
  118. /**
  119. *
  120. * Implements hook_entity_update().
  121. */
  122. function tripal_chado_entity_update($entity, $type) {
  123. }
  124. /**
  125. *
  126. * Implements hook_entity_delete().
  127. */
  128. function tripal_chado_entity_delete($entity, $type) {
  129. if ($type !== 'TripalEntity') {
  130. return;
  131. }
  132. // Remove the base record for this entity from the Chado tables.
  133. $bundle = $entity->bundle;
  134. if (db_table_exists("chado_{$bundle}")) {
  135. $chado_table = $entity->chado_table;
  136. $table_def = chado_get_schema($chado_table);
  137. $pkey_field = $table_def['primary key'][0];
  138. $match = [
  139. $pkey_field => $entity->chado_record_id,
  140. ];
  141. // Here we need to set the active database to Chado
  142. // because records will cascade delete. If one of the tables that
  143. // gets items removed is the featureloc then there will be
  144. // errors about undefined PLSQL functions.
  145. $previous_db = chado_set_active('chado');
  146. try {
  147. chado_delete_record($entity->chado_table, $match);
  148. chado_set_active($previous_db);
  149. } catch (Exception $e) {
  150. chado_set_active($previous_db);
  151. throw $e;
  152. }
  153. }
  154. }
  155. /**
  156. *
  157. * Implements hook_entity_unpublish().
  158. *
  159. * This is not a Drupal hook, but one added by Tripal.
  160. */
  161. function tripal_chado_entity_unpublish($entity, $type) {
  162. if ($type !== 'TripalEntity') {
  163. return;
  164. }
  165. // Remove the entity from the corresponding chado_bio_data_x table
  166. $bundle = $entity->bundle;
  167. if (db_table_exists("chado_{$bundle}")) {
  168. db_delete("chado_{$bundle}")->condition('entity_id', $entity->id)->execute();
  169. }
  170. }
  171. /**
  172. * Implements hook_tripal_default_title_format().
  173. *
  174. * Overrides the default titles.
  175. */
  176. function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
  177. $format = [];
  178. $table = $bundle->data_table;
  179. if ($table == 'organism') {
  180. if (chado_get_version() <= '1.2') {
  181. $format[] = [
  182. 'format' => '[taxrank__genus] [taxrank__species]',
  183. 'weight' => -5,
  184. ];
  185. }
  186. else {
  187. $format[] = [
  188. 'format' => '[taxrank__genus] [taxrank__species] [taxrank__infraspecific_taxon,TAXRANK:0000045]',
  189. 'weight' => -5,
  190. ];
  191. }
  192. }
  193. if ($table == 'arraydesign') {
  194. $format[] = [
  195. 'format' => '[schema__name]',
  196. 'weight' => -5,
  197. ];
  198. }
  199. if ($table == 'assay') {
  200. $format[] = [
  201. 'format' => '[schema__name]',
  202. 'weight' => -5,
  203. ];
  204. }
  205. if ($table == 'biomaterial') {
  206. $format[] = [
  207. 'format' => '[schema__name]',
  208. 'weight' => -5,
  209. ];
  210. }
  211. if ($table == 'analysis') {
  212. $format[] = [
  213. 'format' => '[schema__name]',
  214. 'weight' => -5,
  215. ];
  216. }
  217. if ($table == 'feature') {
  218. $format[] = [
  219. 'format' => '[schema__name]',
  220. 'weight' => -5,
  221. ];
  222. }
  223. if ($table == 'featuremap') {
  224. $format[] = [
  225. 'format' => '[schema__name]',
  226. 'weight' => -5,
  227. ];
  228. }
  229. if ($table == 'stock') {
  230. $format[] = [
  231. 'format' => '[schema__name]',
  232. 'weight' => -5,
  233. ];
  234. }
  235. if ($table == 'pub') {
  236. $format[] = [
  237. 'format' => '[tpub__title]',
  238. 'weight' => -5,
  239. ];
  240. }
  241. if ($table == 'cvterm') {
  242. $format[] = [
  243. 'format' => '[schema__name]',
  244. 'weight' => -5,
  245. ];
  246. }
  247. if ($table == 'project') {
  248. $format[] = [
  249. 'format' => '[schema__name]',
  250. 'weight' => -5,
  251. ];
  252. }
  253. if ($table == 'contact') {
  254. $format[] = [
  255. 'format' => '[schema__name]',
  256. 'weight' => -5,
  257. ];
  258. }
  259. if ($table == 'phylotree') {
  260. $format[] = [
  261. 'format' => '[schema__name]',
  262. 'weight' => -5,
  263. ];
  264. }
  265. if ($table == 'library') {
  266. $format[] = [
  267. 'format' => '[schema__name]',
  268. 'weight' => -5,
  269. ];
  270. }
  271. if ($table == 'protocol') {
  272. $format[] = [
  273. 'format' => '[schema__name]',
  274. 'weight' => -5,
  275. ];
  276. }
  277. return $format;
  278. }
  279. /**
  280. * Implements hook_entity_view().
  281. */
  282. function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
  283. // If this entity is a TripalEntity and is a full view, then
  284. // we want to support the legacy view, but only if the legacy
  285. // module is enabled (the functions exist).
  286. if ($type == 'TripalEntity') {
  287. // Use the generic template to render the fields
  288. if ($view_mode == 'full') {
  289. // Get the Chado table for this data type.
  290. $bundle = tripal_load_bundle_entity(['name' => $entity->bundle]);
  291. $chado_table = $bundle->data_table;
  292. $chado_field = $bundle->type_column;
  293. // Get the list of templates that should be used for entities and generatte
  294. // the key in the array for this entity type (using the chado table the
  295. // entity maps to).
  296. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
  297. []);
  298. $legacy_template = 'legacy_template--chado_' . $chado_table;
  299. // If the site admin has indicated that this entity type should use
  300. // a legacy tmplate then prepare the entity and content to fake a
  301. // node.
  302. if (key_exists($legacy_template,
  303. $enabled_templates) && $enabled_templates[$legacy_template]) {
  304. // Remove the fields added by the chado_field_storage.
  305. $fields = field_info_fields();
  306. foreach ($fields as $field) {
  307. if ($field['storage']['type'] == 'field_chado_storage' or $field['storage']['type'] == 'tripal_no_storage') {
  308. $field_name = $field['field_name'];
  309. if (property_exists($entity, $field_name)) {
  310. $entity->$field_name = NULL;
  311. unset($entity->content[$field_name]);
  312. }
  313. }
  314. }
  315. // Make the entity look like a node.
  316. $entity->type = 'chado_' . $chado_table;
  317. $entity->$chado_table = $entity->chado_record;
  318. // Add any node specific fields to the entity to fake the node.
  319. $node_schema = drupal_get_schema('node');
  320. foreach ($node_schema['fields'] as $field_name => $details) {
  321. if (!property_exists($entity, $field_name)) {
  322. $entity->$field_name = '';
  323. if (array_key_exists('default', $details)) {
  324. $entity->$field_name = $details['default'];
  325. }
  326. }
  327. }
  328. // Now call the module's node_view hook to add additional
  329. // content to our 'fake' entity node.
  330. $modules = module_list();
  331. foreach ($modules as $mname => $details) {
  332. $function_name = $mname . '_node_view';
  333. if (function_exists($function_name)) {
  334. $function_name($entity, $view_mode, $langcode);
  335. }
  336. }
  337. }
  338. }
  339. }
  340. }
  341. /**
  342. * Implements hook_entity_view_alter().
  343. *
  344. * This function is used to support legacy Tripal v2 templates
  345. * for use with Tripal v3 entities.
  346. */
  347. function tripal_chado_entity_view_alter(&$build) {
  348. // For the legacy support, we need to make sure the TOC
  349. // is built.
  350. if ($build['#entity_type'] == 'TripalEntity') {
  351. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates',
  352. []);
  353. $entity = $build['#entity'];
  354. $legacy_template = 'legacy_template--' . $entity->type;
  355. if (key_exists($legacy_template,
  356. $enabled_templates) && $enabled_templates[$legacy_template]) {
  357. $build['#entity']->nid = NULL;
  358. $build['#node'] = $build['#entity'];
  359. $modules = module_list();
  360. foreach ($modules as $mname => $details) {
  361. $function_name = $mname . '_node_view_alter';
  362. if (function_exists($function_name)) {
  363. $function_name($build);
  364. }
  365. }
  366. }
  367. }
  368. }