tripal_chado.entity.inc 3.8 KB

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