tripal_chado.entity.inc

File

tripal_chado/includes/tripal_chado.entity.inc
View source
  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. function tripal_chado_entity_create(&$entity, $type) {
  10. if ($type == 'TripalEntity') {
  11. // Set some defaults on vars needed by this module.
  12. if (!property_exists($entity, 'chado_table')) {
  13. $entity->chado_table = NULL;
  14. $entity->chado_column = NULL;
  15. $entity->chado_linker = NULL;
  16. // Add in the Chado table information for this entity type.
  17. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  18. if ($bundle->data_table) {
  19. $entity->chado_table = $bundle->data_table;
  20. $entity->chado_column = $bundle->type_column;
  21. $entity->chado_linker = $bundle->type_linker_table;
  22. }
  23. }
  24. if (!property_exists($entity, 'chado_record')) {
  25. $entity->chado_record = NULL;
  26. $entity->chado_record_id = NULL;
  27. }
  28. }
  29. }
  30. /**
  31. * Implements hook_entity_presave().
  32. */
  33. function tripal_chado_entity_presave($entity, $type) {
  34. }
  35. /**
  36. * Implements hook_entity_postsave().
  37. */
  38. function tripal_chado_entity_postsave($entity, $type) {
  39. }
  40. /**
  41. * Implements hook_entity_load().
  42. */
  43. function tripal_chado_entity_load($entities, $type) {
  44. if ($type == 'TripalBundle') {
  45. foreach ($entities as $bundle) {
  46. // We want to add in the record ID to the entity.
  47. if (property_exists($bundle, 'id')) {
  48. $chado_bundle = db_select('chado_bundle', 'cb')
  49. ->fields('cb')
  50. ->condition('cb.bundle_id', $bundle->id)
  51. ->execute()
  52. ->fetchObject();
  53. if ($chado_bundle) {
  54. $bundle->data_table = $chado_bundle->data_table;
  55. $bundle->type_linker_table = $chado_bundle->type_linker_table;
  56. $bundle->type_column = $chado_bundle->type_column;
  57. $bundle->type_id = $chado_bundle->type_id;
  58. $bundle->type_value = $chado_bundle->type_value;
  59. }
  60. }
  61. }
  62. }
  63. if ($type == 'TripalEntity') {
  64. foreach ($entities as $entity) {
  65. // We want to add in the record ID to the entity.
  66. if (property_exists($entity, 'id')) {
  67. // Set some defaults on vars needed by this module.
  68. $entity->chado_table = NULL;
  69. $entity->chado_column = NULL;
  70. $entity->chado_record = NULL;
  71. $entity->chado_record_id = NULL;
  72. // Add in the Chado table information for this entity type.
  73. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  74. if (!$bundle) {
  75. continue;
  76. }
  77. // TODO: this may need fixing. The chado_column may not always
  78. // be the type_id of the base table. Is it expected to be so here?
  79. $entity->chado_table = $bundle->data_table;
  80. $entity->chado_column = $bundle->type_column;
  81. $chado_entity_table = chado_get_bundle_entity_table($bundle);
  82. $chado_entity = db_select($chado_entity_table, 'ce')
  83. ->fields('ce')
  84. ->condition('ce.entity_id', $entity->id)
  85. ->execute()
  86. ->fetchObject();
  87. if ($chado_entity) {
  88. $schema = chado_get_schema($entity->chado_table);
  89. $record = chado_generate_var($entity->chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
  90. $entity->chado_record = $record;
  91. $entity->chado_record_id = $chado_entity->record_id;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. *
  99. * Implements hook_entity_insert().
  100. */
  101. function tripal_chado_entity_insert($entity, $type) {
  102. }
  103. /**
  104. *
  105. * Implements hook_entity_update().
  106. */
  107. function tripal_chado_entity_update($entity, $type) {
  108. }
  109. /**
  110. *
  111. * Implements hook_entity_delete().
  112. */
  113. function tripal_chado_entity_delete($entity, $type) {
  114. if ($type == 'TripalEntity') {
  115. }
  116. }
  117. /**
  118. * Implements hook_tripal_default_title_format().
  119. *
  120. * Overrides the default titles.
  121. */
  122. function tripal_chado_tripal_default_title_format($bundle, $available_tokens) {
  123. $format = array();
  124. $table = $bundle->data_table;
  125. if ($table == 'organism') {
  126. if (chado_get_version() <= '1.2') {
  127. $format[] = array(
  128. 'format' => '[taxrank__genus] [taxrank__species]',
  129. 'weight' => -5
  130. );
  131. }
  132. else {
  133. $format[] = array(
  134. 'format' => '[taxrank__genus] [taxrank__species] [taxrank__infraspecific_taxon]',
  135. 'weight' => -5
  136. );
  137. }
  138. }
  139. if ($table == 'biomaterial') {
  140. $format[] = array(
  141. 'format' => '[schema__name]',
  142. 'weight' => -5
  143. );
  144. }
  145. if ($table == 'analysis') {
  146. $format[] = array(
  147. 'format' => '[schema__name]',
  148. 'weight' => -5
  149. );
  150. }
  151. if ($table == 'feature') {
  152. $format[] = array(
  153. 'format' => '[schema__name]',
  154. 'weight' => -5
  155. );
  156. }
  157. if ($table == 'featuremap') {
  158. $format[] = array(
  159. 'format' => '[schema__name]',
  160. 'weight' => -5
  161. );
  162. }
  163. if ($table == 'stock') {
  164. $format[] = array(
  165. 'format' => '[schema__name]',
  166. 'weight' => -5
  167. );
  168. }
  169. if ($table == 'pub') {
  170. $format[] = array(
  171. 'format' => '[tpub__title]',
  172. 'weight' => -5,
  173. );
  174. }
  175. if ($table == 'cvterm') {
  176. $format[] = array(
  177. 'format' => '[schema__name]',
  178. 'weight' => -5,
  179. );
  180. }
  181. if ($table == 'project') {
  182. $format[] = array(
  183. 'format' => '[schema__name]',
  184. 'weight' => -5,
  185. );
  186. }
  187. if ($table == 'contact') {
  188. $format[] = array(
  189. 'format' => '[schema__name]',
  190. 'weight' => -5,
  191. );
  192. }
  193. if ($table == 'phylotree') {
  194. $format[] = array(
  195. 'format' => '[schema__name]',
  196. 'weight' => -5,
  197. );
  198. }
  199. if ($table == 'library') {
  200. $format[] = array(
  201. 'format' => '[schema__name]',
  202. 'weight' => -5,
  203. );
  204. }
  205. return $format;
  206. }
  207. /**
  208. * Implements hook_entity_view().
  209. */
  210. function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
  211. // If this entity is a TripalEntity and is a full view, then
  212. // we want to support the legacy view, but only if the legacy
  213. // module is enabled (the functions exist).
  214. if ($type =='TripalEntity') {
  215. // Use the generic template to render the fields
  216. if ($view_mode == 'full') {
  217. // Get the Chado table for this data type.
  218. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  219. $chado_table = $bundle->data_table;
  220. $chado_field = $bundle->type_column;
  221. // Get the list of templates that should be used for entities and generatte
  222. // the key in the array for this entity type (using the chado table the
  223. // entity maps to).
  224. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  225. $legacy_template = 'legacy_template--chado_' . $chado_table;
  226. // If the site admin has indicated that this entity type should use
  227. // a legacy tmplate then prepare the entity and content to fake a
  228. // node.
  229. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  230. // Remove the fields added by the chado_field_storage.
  231. $fields = field_info_fields();
  232. foreach($fields as $field) {
  233. if ($field['storage']['type'] == 'field_chado_storage' or
  234. $field['storage']['type'] == 'tripal_no_storage') {
  235. $field_name = $field['field_name'];
  236. if (property_exists($entity, $field_name)) {
  237. $entity->$field_name = NULL;
  238. unset($entity->content[$field_name]);
  239. }
  240. }
  241. }
  242. // Make the entity look like a node.
  243. $entity->type = 'chado_' . $chado_table;
  244. $entity->$chado_table = $entity->chado_record;
  245. // Add any node specific fields to the entity to fake the node.
  246. $node_schema = drupal_get_schema('node');
  247. foreach ($node_schema['fields'] as $field_name => $details) {
  248. if (!property_exists($entity, $field_name)) {
  249. $entity->$field_name = '';
  250. if (array_key_exists('default', $details)) {
  251. $entity->$field_name = $details['default'];
  252. }
  253. }
  254. }
  255. // Now call the module's node_view hook to add additional
  256. // content to our 'fake' entity node.
  257. $modules = module_list();
  258. foreach ($modules as $mname => $details) {
  259. $function_name = $mname . '_node_view';
  260. if (function_exists($function_name)) {
  261. $function_name($entity, $view_mode, $langcode);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. /**
  269. * Implements hook_entity_view_alter().
  270. *
  271. * This function is used to support legacy Tripal v2 templates
  272. * for use with Tripal v3 entities.
  273. */
  274. function tripal_chado_entity_view_alter(&$build) {
  275. // For the legacy support, we need to make sure the TOC
  276. // is built.
  277. if ($build['#entity_type'] == 'TripalEntity') {
  278. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  279. $entity = $build['#entity'];
  280. $legacy_template = 'legacy_template--' . $entity->type;
  281. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  282. $build['#entity']->nid = NULL;
  283. $build['#node'] = $build['#entity'];
  284. $modules = module_list();
  285. foreach ($modules as $mname => $details) {
  286. $function_name = $mname . '_node_view_alter';
  287. if (function_exists($function_name)) {
  288. $function_name($build);
  289. }
  290. }
  291. }
  292. }
  293. }