tripal_entities.chado_entity.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. *
  4. * Implements hook_entity_load().
  5. */
  6. function tripal_entities_entity_presave($entity, $type) {
  7. }
  8. /**
  9. *
  10. * @param $entity
  11. * @param $type
  12. */
  13. function tripal_entities_entity_postsave($entity, $type) {
  14. // Set the title for this entity using the chado data.
  15. $title = chado_get_entity_title($entity);
  16. $ec = new TripalEntityController($entity->type);
  17. $ec->setTitle($entity, $title);
  18. }
  19. /**
  20. *
  21. * Implements hook_entity_load().
  22. */
  23. function tripal_entities_entity_load($entities, $type) {
  24. }
  25. /**
  26. *
  27. * Implements hook_entity_insert().
  28. */
  29. function tripal_entities_entity_insert($entity, $type) {
  30. }
  31. /**
  32. *
  33. * Implements hook_entity_update().
  34. */
  35. function tripal_entities_entity_update($entity, $type) {
  36. }
  37. /**
  38. *
  39. * Implements hook_entity_delete().
  40. */
  41. function tripal_entities_entity_delete($entity, $type) {
  42. $record = db_select('chado_entity', 'ce')
  43. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  44. ->condition('entity_id', $entity->id)
  45. ->execute()
  46. ->fetchObject();
  47. if ($record && property_exists($record, 'chado_entity_id')) {
  48. // Delete the corresponding record in Chado
  49. $table = $record->data_table;
  50. $record_id = $record->record_id;
  51. chado_delete_record($table, array($table . '_id' => $record_id));
  52. //Delete the record in the public.chado_entity table
  53. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  54. db_query($sql, array(':id' => $record->chado_entity_id));
  55. }
  56. }
  57. /**
  58. * This theme function is meant to override the data_combo theme.
  59. *
  60. * @param $variables
  61. */
  62. function theme_tripal_entities_date_combo($variables) {
  63. $element = $variables['element'];
  64. $field = field_info_field($element['#field_name']);
  65. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  66. // Group start/end items together in fieldset.
  67. $fieldset = array(
  68. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  69. '#value' => '',
  70. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  71. '#attributes' => array(),
  72. '#children' => $element['#children'],
  73. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  74. );
  75. return theme('fieldset', array('element' => $fieldset));
  76. }
  77. /**
  78. * Determines whether the given user has access to a tripal data entity.
  79. *
  80. * @param $op
  81. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  82. * or just 'edit' (being the same as 'create' or 'update').
  83. * @param $entity
  84. * Optionally a tripal data entity or a tripal data type to check access for.
  85. * If nothing is given, access for all types is determined.
  86. * @param $account
  87. * The user to check for. Leave it to NULL to check for the global user.
  88. * @return boolean
  89. * Whether access is allowed or not.
  90. */
  91. function tripal_entities_entity_access($op, $entity = NULL, $account = NULL) {
  92. if (user_access('administer tripal data', $account)) {
  93. return TRUE;
  94. }
  95. if (isset($entity) && $type_name = $entity->type) {
  96. $op = ($op == 'view') ? 'view' : 'edit';
  97. if (user_access("$op any $type_name data", $account)) {
  98. return TRUE;
  99. }
  100. }
  101. return FALSE;
  102. }
  103. /**
  104. * Menu callback to display an entity.
  105. *
  106. * As we load the entity for display, we're responsible for invoking a number
  107. * of hooks in their proper order.
  108. *
  109. * @see hook_entity_prepare_view()
  110. * @see hook_entity_view()
  111. * @see hook_entity_view_alter()
  112. */
  113. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  114. $content = '';
  115. $controller = entity_get_controller($entity->type);
  116. $content = $controller->view(array($entity->id => $entity));
  117. drupal_set_title($entity->title);
  118. return $content;
  119. }
  120. /**
  121. * Menu title callback for showing individual entities
  122. */
  123. function tripal_entities_entity_title($entity){
  124. if ($entity) {
  125. return $entity->title;
  126. }
  127. }