tripal_chado.entity.inc 9.7 KB

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