tripal_chado.entity.inc 4.2 KB

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