tripal_chado.entity.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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($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. if ($type == 'TripalEntity') {
  35. // Set the title for this entity.
  36. // This needs to be done post save because it uses the saved data and a format.
  37. $ec = new TripalEntityController($entity->type);
  38. $ec->setTitle($entity);
  39. }
  40. }
  41. /**
  42. * Implements hook_entity_load().
  43. */
  44. function tripal_chado_entity_load($entities, $type) {
  45. if ($type == 'TripalEntity') {
  46. foreach ($entities as $entity) {
  47. // We want to add in the record ID to the entity.
  48. if (property_exists($entity, 'id')) {
  49. // Set some defaults on vars needed by this module.
  50. $entity->chado_table = NULL;
  51. $entity->chado_column = NULL;
  52. $entity->chado_record = NULL;
  53. $entity->chado_record_id = NULL;
  54. // Add in the Chado table information for this entity type.
  55. $bundle = tripal_load_bundle_entity($entity->bundle);
  56. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  57. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  58. if ($chado_table) {
  59. $entity->chado_table = $chado_table;
  60. $entity->chado_column = $chado_column;
  61. }
  62. $chado_entity = db_select('chado_entity' ,'ce')
  63. ->fields('ce')
  64. ->condition('ce.entity_id', $entity->id)
  65. ->execute()
  66. ->fetchObject();
  67. $schema = chado_get_schema($chado_table);
  68. $record = chado_generate_var($chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
  69. $entity->chado_record = $record;
  70. $entity->chado_record_id = $chado_entity->record_id;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Implements hook_field_attach_form().
  77. */
  78. function tripal_chado_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  79. }
  80. /**
  81. *
  82. * Implements hook_entity_insert().
  83. */
  84. function tripal_chado_entity_insert($entity, $type) {
  85. }
  86. /**
  87. *
  88. * Implements hook_entity_update().
  89. */
  90. function tripal_chado_entity_update($entity, $type) {
  91. }
  92. /**
  93. *
  94. * Implements hook_entity_delete().
  95. */
  96. function tripal_chaddo_entity_delete($entity, $type) {
  97. $record = db_select('chado_entity', 'ce')
  98. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  99. ->condition('entity_id', $entity->id)
  100. ->execute()
  101. ->fetchObject();
  102. if ($record && property_exists($record, 'chado_entity_id')) {
  103. // Delete the corresponding record in Chado
  104. $table = $record->data_table;
  105. $record_id = $record->record_id;
  106. chado_delete_record($table, array($table . '_id' => $record_id));
  107. //Delete the record in the public.chado_entity table
  108. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  109. db_query($sql, array(':id' => $record->chado_entity_id));
  110. }
  111. }
  112. /**
  113. * Determines whether the given user has access to a tripal data entity.
  114. *
  115. * TODO: I'm not sure this function should be at this level. I think all
  116. * access controls should be handled by the tripal_entity module and that
  117. * storage backends should just attach data as requested.
  118. *
  119. * @param $op
  120. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  121. * or just 'edit' (being the same as 'create' or 'update').
  122. * @param $entity
  123. * Optionally a tripal data entity or a tripal data type to check access for.
  124. * If nothing is given, access for all types is determined.
  125. * @param $account
  126. * The user to check for. Leave it to NULL to check for the global user.
  127. * @return boolean
  128. * Whether access is allowed or not.
  129. */
  130. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  131. if (user_access('administer tripal data', $account)) {
  132. return TRUE;
  133. }
  134. if (isset($entity) && $type_name = $entity->type) {
  135. $op = ($op == 'view') ? 'view' : 'edit';
  136. if (user_access("$op any $type_name data", $account)) {
  137. return TRUE;
  138. }
  139. }
  140. return FALSE;
  141. }