tripal_chado.entity.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Implements hook_entity_presave().
  4. */
  5. function tripal_chado_entity_presave($entity, $type) { }
  6. /**
  7. * Implements hook_entity_postsave().
  8. */
  9. function tripal_chado_entity_postsave($entity, $type) { }
  10. /**
  11. *
  12. * Implements hook_entity_load().
  13. */
  14. function tripal_chado_entity_load($entities, $type) {
  15. // TODO: we should add the cvterm record and the Chado record to the
  16. // entity. This way the kvproperty field (or any other's we add in the
  17. // future do not need to load all of that information. This can create
  18. // multiple loading of the same records all over the place.
  19. }
  20. /**
  21. *
  22. * Implements hook_entity_insert().
  23. */
  24. function tripal_chado_entity_insert($entity, $type) { }
  25. /**
  26. *
  27. * Implements hook_entity_update().
  28. */
  29. function tripal_chado_entity_update($entity, $type) { }
  30. /**
  31. *
  32. * Implements hook_entity_delete().
  33. */
  34. function tripal_chaddo_entity_delete($entity, $type) {
  35. $record = db_select('chado_entity', 'ce')
  36. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  37. ->condition('entity_id', $entity->id)
  38. ->execute()
  39. ->fetchObject();
  40. if ($record && property_exists($record, 'chado_entity_id')) {
  41. // Delete the corresponding record in Chado
  42. $table = $record->data_table;
  43. $record_id = $record->record_id;
  44. chado_delete_record($table, array($table . '_id' => $record_id));
  45. //Delete the record in the public.chado_entity table
  46. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  47. db_query($sql, array(':id' => $record->chado_entity_id));
  48. }
  49. }
  50. /**
  51. * This theme function is meant to override the data_combo theme.
  52. *
  53. * @param $variables
  54. */
  55. function theme_tripal_chado_date_combo($variables) {
  56. $element = $variables['element'];
  57. $field = field_info_field($element['#field_name']);
  58. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  59. // Group start/end items together in fieldset.
  60. $fieldset = array(
  61. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  62. '#value' => '',
  63. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  64. '#attributes' => array(),
  65. '#children' => $element['#children'],
  66. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  67. );
  68. return theme('fieldset', array('element' => $fieldset));
  69. }
  70. /**
  71. * Determines whether the given user has access to a tripal data entity.
  72. *
  73. * TODO: I'm not sure this function should be at this level. I think all
  74. * access controls should be handled by the tripal_entity module and that
  75. * storage backends should just attach data as requested.
  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. }