tripal_entities.chado_entity.inc 3.9 KB

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