tripal_chado.entity.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. *
  4. * Implements hook_entity_load().
  5. */
  6. function tripal_chado_entity_presave($entity, $type) {
  7. // Add the Chado table and column to an entity to which
  8. // data of this cvterm will map.
  9. if ($type == 'TripalEntity') {
  10. $bundle_id = $entity->bundle;
  11. $bundle = db_select('tripal_bundle', 'tb')
  12. ->fields('tb')
  13. ->condition('bundle', $bundle_id)
  14. ->execute()
  15. ->fetchObject();
  16. $data = unserialize($bundle->data);
  17. $entity->chado_table = $data['data_table'];
  18. $entity->chado_field = $data['field'];
  19. // If we have an ID then this entity has been saved and it will
  20. // also therefore have a chado_entity record. We want to
  21. // load this record so it is always part of the entity object.
  22. if (property_exists($entity, 'id') and $entity->id) {
  23. $details = db_select('chado_entity', 'ce')
  24. ->fields('ce')
  25. ->condition('entity_id', $entity->id)
  26. ->execute()
  27. ->fetchObject();
  28. // Add the chado entity details to the entity in case it's needed
  29. // downstream (e.g. in field widget construction).
  30. $entity->chado_entity_id = $details->chado_entity_id;
  31. // Add in the record.
  32. $schema = chado_get_schema($entity->chado_table);
  33. $pkey = $schema['primary key'][0];
  34. $entity->chado_record_id = $details->record_id;
  35. $entity->chado_record = chado_generate_var($entity->chado_table, array($pkey =>+ $details->record_id));
  36. }
  37. }
  38. }
  39. /**
  40. *
  41. * @param $entity
  42. * @param $type
  43. */
  44. function tripal_chado_entity_postsave($entity, $type) {
  45. // Set the title for this entity using the chado data.
  46. $title = chado_get_entity_title($entity);
  47. $ec = new TripalEntityController($entity->type);
  48. $ec->setTitle($entity, $title);
  49. }
  50. /**
  51. *
  52. * Implements hook_entity_load().
  53. */
  54. function tripal_chado_entity_load($entities, $type) {
  55. }
  56. /**
  57. *
  58. * Implements hook_entity_update().
  59. */
  60. function tripal_chado_entity_update($entity, $type) {
  61. }
  62. /**
  63. *
  64. * Implements hook_entity_delete().
  65. */
  66. function tripal_chado_entity_delete($entity, $type) {
  67. $record = db_select('chado_entity', 'ce')
  68. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  69. ->condition('entity_id', $entity->id)
  70. ->execute()
  71. ->fetchObject();
  72. if ($record && property_exists($record, 'chado_entity_id')) {
  73. // Delete the corresponding record in Chado
  74. $table = $record->data_table;
  75. $record_id = $record->record_id;
  76. chado_delete_record($table, array($table . '_id' => $record_id));
  77. //Delete the record in the public.chado_entity table
  78. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  79. db_query($sql, array(':id' => $record->chado_entity_id));
  80. }
  81. }
  82. /**
  83. * This theme function is meant to override the data_combo theme.
  84. *
  85. * @param $variables
  86. */
  87. function theme_tripal_chado_date_combo($variables) {
  88. $element = $variables['element'];
  89. $field = field_info_field($element['#field_name']);
  90. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  91. // Group start/end items together in fieldset.
  92. $fieldset = array(
  93. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  94. '#value' => '',
  95. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  96. '#attributes' => array(),
  97. '#children' => $element['#children'],
  98. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  99. );
  100. return theme('fieldset', array('element' => $fieldset));
  101. }
  102. /**
  103. * Determines whether the given user has access to a tripal data entity.
  104. *
  105. * @param $op
  106. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  107. * or just 'edit' (being the same as 'create' or 'update').
  108. * @param $entity
  109. * Optionally a tripal data entity or a tripal data type to check access for.
  110. * If nothing is given, access for all types is determined.
  111. * @param $account
  112. * The user to check for. Leave it to NULL to check for the global user.
  113. * @return boolean
  114. * Whether access is allowed or not.
  115. */
  116. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  117. if (user_access('administer tripal data', $account)) {
  118. return TRUE;
  119. }
  120. if (isset($entity) && $type_name = $entity->type) {
  121. $op = ($op == 'view') ? 'view' : 'edit';
  122. if (user_access("$op any $type_name data", $account)) {
  123. return TRUE;
  124. }
  125. }
  126. return FALSE;
  127. }
  128. /**
  129. * Menu callback to display an entity.
  130. *
  131. * As we load the entity for display, we're responsible for invoking a number
  132. * of hooks in their proper order.
  133. *
  134. * @see hook_entity_prepare_view()
  135. * @see hook_entity_view()
  136. * @see hook_entity_view_alter()
  137. */
  138. function tripal_chado_view_entity($entity, $view_mode = 'full') {
  139. $content = '';
  140. $controller = entity_get_controller($entity->type);
  141. $content = $controller->view(array($entity->id => $entity));
  142. drupal_set_title($entity->title);
  143. return $content;
  144. }
  145. /**
  146. * Menu title callback for showing individual entities
  147. */
  148. function tripal_chado_entity_title($entity){
  149. if ($entity) {
  150. return $entity->title;
  151. }
  152. }