tripal_chado.entity.inc 4.8 KB

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