tripal_chado.entity.inc 5.8 KB

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