tripal_chado.entity.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // TODO: we should add the cvterm record and the Chado record to the
  22. // entity. This way the kvproperty field (or any other's we add in the
  23. // future do not need to load all of that information. This can create
  24. // multiple loading of the same records all over the place.
  25. }
  26. /**
  27. *
  28. * Implements hook_entity_insert().
  29. */
  30. function tripal_entities_entity_insert($entity, $type) {
  31. }
  32. /**
  33. *
  34. * Implements hook_entity_update().
  35. */
  36. function tripal_chado_entity_update($entity, $type) {
  37. }
  38. /**
  39. *
  40. * Implements hook_entity_delete().
  41. */
  42. function tripal_chaddo_entity_delete($entity, $type) {
  43. $record = db_select('chado_entity', 'ce')
  44. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  45. ->condition('entity_id', $entity->id)
  46. ->execute()
  47. ->fetchObject();
  48. if ($record && property_exists($record, 'chado_entity_id')) {
  49. // Delete the corresponding record in Chado
  50. $table = $record->data_table;
  51. $record_id = $record->record_id;
  52. chado_delete_record($table, array($table . '_id' => $record_id));
  53. //Delete the record in the public.chado_entity table
  54. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  55. db_query($sql, array(':id' => $record->chado_entity_id));
  56. }
  57. }
  58. /**
  59. * This theme function is meant to override the data_combo theme.
  60. *
  61. * @param $variables
  62. */
  63. function theme_tripal_chado_date_combo($variables) {
  64. $element = $variables['element'];
  65. $field = field_info_field($element['#field_name']);
  66. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  67. // Group start/end items together in fieldset.
  68. $fieldset = array(
  69. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  70. '#value' => '',
  71. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  72. '#attributes' => array(),
  73. '#children' => $element['#children'],
  74. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  75. );
  76. return theme('fieldset', array('element' => $fieldset));
  77. }
  78. /**
  79. * Determines whether the given user has access to a tripal data entity.
  80. *
  81. * TODO: I'm not sure this function should be at this level. I think all
  82. * access controls should be handled by the tripal_entity module and that
  83. * storage backends should just attach data as requested.
  84. *
  85. * @param $op
  86. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  87. * or just 'edit' (being the same as 'create' or 'update').
  88. * @param $entity
  89. * Optionally a tripal data entity or a tripal data type to check access for.
  90. * If nothing is given, access for all types is determined.
  91. * @param $account
  92. * The user to check for. Leave it to NULL to check for the global user.
  93. * @return boolean
  94. * Whether access is allowed or not.
  95. */
  96. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  97. if (user_access('administer tripal data', $account)) {
  98. return TRUE;
  99. }
  100. if (isset($entity) && $type_name = $entity->type) {
  101. $op = ($op == 'view') ? 'view' : 'edit';
  102. if (user_access("$op any $type_name data", $account)) {
  103. return TRUE;
  104. }
  105. }
  106. return FALSE;
  107. }
  108. /**
  109. * Menu callback to display an entity.
  110. *
  111. * As we load the entity for display, we're responsible for invoking a number
  112. * of hooks in their proper order.
  113. *
  114. * @see hook_entity_prepare_view()
  115. * @see hook_entity_view()
  116. * @see hook_entity_view_alter()
  117. */
  118. function tripal_chado_view_entity($entity, $view_mode = 'full') {
  119. $content = '';
  120. $controller = entity_get_controller($entity->type);
  121. $content = $controller->view(array($entity->id => $entity));
  122. drupal_set_title($entity->title);
  123. return $content;
  124. }
  125. /**
  126. * Menu title callback for showing individual entities
  127. */
  128. function tripal_chado_entity_title($entity){
  129. if ($entity) {
  130. return $entity->title;
  131. }
  132. }