tripal_chado.entity.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.
  7. */
  8. function tripal_chado_entity_create(&$entity, $type) {
  9. if ($type == 'TripalEntity') {
  10. // Set some defaults on vars needed by this module.
  11. $entity->chado_table = NULL;
  12. $entity->chado_column = NULL;
  13. $entity->chado_record = NULL;
  14. $entity->chado_record_id = NULL;
  15. // Add in the Chado table information for this entity type.
  16. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  17. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  18. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  19. if ($chado_table) {
  20. $entity->chado_table = $chado_table;
  21. $entity->chado_column = $chado_column;
  22. }
  23. }
  24. }
  25. /**
  26. * Implements hook_entity_presave().
  27. */
  28. function tripal_chado_entity_presave($entity, $type) { }
  29. /**
  30. * Implements hook_entity_postsave().
  31. */
  32. function tripal_chado_entity_postsave($entity, $type) { }
  33. /**
  34. * Implements hook_entity_load().
  35. */
  36. function tripal_chado_entity_load($entities, $type) {
  37. if ($type == 'TripalEntity') {
  38. foreach ($entities as $entity) {
  39. // We want to add in the record ID to the entity.
  40. if (property_exists($entity, 'id')) {
  41. // Set some defaults on vars needed by this module.
  42. $entity->chado_table = NULL;
  43. $entity->chado_column = NULL;
  44. $entity->chado_record = NULL;
  45. $entity->chado_record_id = NULL;
  46. // Add in the Chado table information for this entity type.
  47. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  48. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  49. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  50. if ($chado_table) {
  51. $entity->chado_table = $chado_table;
  52. $entity->chado_column = $chado_column;
  53. }
  54. $chado_entity = db_select('chado_entity' ,'ce')
  55. ->fields('ce')
  56. ->condition('ce.entity_id', $entity->id)
  57. ->execute()
  58. ->fetchObject();
  59. $schema = chado_get_schema($chado_table);
  60. $record = chado_generate_var($chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
  61. $entity->chado_record = $record;
  62. $entity->chado_record_id = $chado_entity->record_id;
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Implements hook_field_attach_form().
  69. */
  70. function tripal_chado_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { }
  71. /**
  72. *
  73. * Implements hook_entity_insert().
  74. */
  75. function tripal_chado_entity_insert($entity, $type) { }
  76. /**
  77. *
  78. * Implements hook_entity_update().
  79. */
  80. function tripal_chado_entity_update($entity, $type) { }
  81. /**
  82. *
  83. * Implements hook_entity_delete().
  84. */
  85. function tripal_chado_entity_delete($entity, $type) {
  86. $record = db_select('chado_entity', 'ce')
  87. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  88. ->condition('entity_id', $entity->id)
  89. ->execute()
  90. ->fetchObject();
  91. if ($record && property_exists($record, 'chado_entity_id')) {
  92. // Delete the corresponding record in Chado
  93. $table = $record->data_table;
  94. $record_id = $record->record_id;
  95. chado_delete_record($table, array($table . '_id' => $record_id));
  96. //Delete the record in the public.chado_entity table
  97. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  98. db_query($sql, array(':id' => $record->chado_entity_id));
  99. }
  100. }
  101. /**
  102. * Determines whether the given user has access to a tripal data entity.
  103. *
  104. * TODO: I'm not sure this function should be at this level. I think all
  105. * access controls should be handled by the tripal_entity module and that
  106. * storage backends should just attach data as requested.
  107. *
  108. * @param $op
  109. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  110. * or just 'edit' (being the same as 'create' or 'update').
  111. * @param $entity
  112. * Optionally a tripal data entity or a tripal data type to check access for.
  113. * If nothing is given, access for all types is determined.
  114. * @param $account
  115. * The user to check for. Leave it to NULL to check for the global user.
  116. * @return boolean
  117. * Whether access is allowed or not.
  118. */
  119. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  120. if (user_access('administer tripal data', $account)) {
  121. return TRUE;
  122. }
  123. if (isset($entity) && $type_name = $entity->type) {
  124. $op = ($op == 'view') ? 'view' : 'edit';
  125. if (user_access("$op any $type_name data", $account)) {
  126. return TRUE;
  127. }
  128. }
  129. return FALSE;
  130. }
  131. /**
  132. * Implements hook_tripal_default_title_format().
  133. */
  134. function tripal_chado_tripal_default_title_format($entity, $available_tokens) {
  135. $format = array();
  136. // Load the term associated with this Tripal Content type.
  137. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  138. $term = reset($term);
  139. // For organism titles we want the genus and species with no comma separation.
  140. if ($term->name == 'organism') {
  141. $format[] = array(
  142. 'format' => '[organism__genus]-[organism__species]',
  143. 'weight' => -5
  144. );
  145. }
  146. return $format;
  147. }