tripal_chado.entity.inc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_tripal_bundle_data_alter().
  59. */
  60. function tripal_chado_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
  61. // Get the cvterm for this entity type.
  62. $bundle_id = $entity->bundle;
  63. $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
  64. $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
  65. // The organism table does not have a type_id so we won't ever find
  66. // a record for it in the tripal_cv_defaults table.
  67. if ($cvterm->name == 'organism') {
  68. $bundle_data = array(
  69. 'cv_id' => $cvterm->cv_id->cv_id,
  70. 'cvterm_id' => $cvterm->cvterm_id,
  71. 'data_table' => 'organism',
  72. 'type_table' => 'organism',
  73. 'field' => '',
  74. );
  75. }
  76. // The analysis table does not have a type_id so we won't ever find
  77. // a record for it in the tripalcv_defaults table.
  78. else if ($cvterm->name == 'analysis') {
  79. $bundle_data = array(
  80. 'cv_id' => $cvterm->cv_id->cv_id,
  81. 'cvterm_id' => $cvterm->cvterm_id,
  82. 'data_table' => 'analysis',
  83. 'type_table' => 'analysis',
  84. 'field' => '',
  85. );
  86. }
  87. else if ($cvterm->name == 'project') {
  88. $bundle_data = array(
  89. 'cv_id' => $cvterm->cv_id->cv_id,
  90. 'cvterm_id' => $cvterm->cvterm_id,
  91. 'data_table' => 'project',
  92. 'type_table' => 'project',
  93. 'field' => '',
  94. );
  95. }
  96. else {
  97. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  98. // TABLES.
  99. // Look to see if this vocabulary is used as a default for any table.
  100. $default = db_select('tripal_cv_defaults', 't')
  101. ->fields('t')
  102. ->condition('cv_id', $cvterm->cv_id->cv_id)
  103. ->execute()
  104. ->fetchObject();
  105. if ($default) {
  106. $bundle_data = array(
  107. 'cv_id' => $cvterm->cv_id->cv_id,
  108. 'cvterm_id' => $cvterm->cvterm_id,
  109. 'data_table' => $default->table_name,
  110. 'type_table' => $default->table_name,
  111. 'field' => $default->field_name,
  112. );
  113. }
  114. }
  115. }
  116. /**
  117. *
  118. * Implements hook_entity_update().
  119. */
  120. function tripal_chado_entity_update($entity, $type) {
  121. }
  122. /**
  123. *
  124. * Implements hook_entity_delete().
  125. */
  126. function tripal_chado_entity_delete($entity, $type) {
  127. $record = db_select('chado_entity', 'ce')
  128. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  129. ->condition('entity_id', $entity->id)
  130. ->execute()
  131. ->fetchObject();
  132. if ($record && property_exists($record, 'chado_entity_id')) {
  133. // Delete the corresponding record in Chado
  134. $table = $record->data_table;
  135. $record_id = $record->record_id;
  136. chado_delete_record($table, array($table . '_id' => $record_id));
  137. //Delete the record in the public.chado_entity table
  138. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  139. db_query($sql, array(':id' => $record->chado_entity_id));
  140. }
  141. }
  142. /**
  143. * This theme function is meant to override the data_combo theme.
  144. *
  145. * @param $variables
  146. */
  147. function theme_tripal_chado_date_combo($variables) {
  148. $element = $variables['element'];
  149. $field = field_info_field($element['#field_name']);
  150. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  151. // Group start/end items together in fieldset.
  152. $fieldset = array(
  153. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  154. '#value' => '',
  155. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  156. '#attributes' => array(),
  157. '#children' => $element['#children'],
  158. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  159. );
  160. return theme('fieldset', array('element' => $fieldset));
  161. }
  162. /**
  163. * Determines whether the given user has access to a tripal data entity.
  164. *
  165. * @param $op
  166. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  167. * or just 'edit' (being the same as 'create' or 'update').
  168. * @param $entity
  169. * Optionally a tripal data entity or a tripal data type to check access for.
  170. * If nothing is given, access for all types is determined.
  171. * @param $account
  172. * The user to check for. Leave it to NULL to check for the global user.
  173. * @return boolean
  174. * Whether access is allowed or not.
  175. */
  176. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  177. if (user_access('administer tripal data', $account)) {
  178. return TRUE;
  179. }
  180. if (isset($entity) && $type_name = $entity->type) {
  181. $op = ($op == 'view') ? 'view' : 'edit';
  182. if (user_access("$op any $type_name data", $account)) {
  183. return TRUE;
  184. }
  185. }
  186. return FALSE;
  187. }
  188. /**
  189. * Menu callback to display an entity.
  190. *
  191. * As we load the entity for display, we're responsible for invoking a number
  192. * of hooks in their proper order.
  193. *
  194. * @see hook_entity_prepare_view()
  195. * @see hook_entity_view()
  196. * @see hook_entity_view_alter()
  197. */
  198. function tripal_chado_view_entity($entity, $view_mode = 'full') {
  199. $content = '';
  200. $controller = entity_get_controller($entity->type);
  201. $content = $controller->view(array($entity->id => $entity));
  202. drupal_set_title($entity->title);
  203. return $content;
  204. }
  205. /**
  206. * Menu title callback for showing individual entities
  207. */
  208. function tripal_chado_entity_title($entity){
  209. if ($entity) {
  210. return $entity->title;
  211. }
  212. }