tripal_chado.entity.inc 10.0 KB

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