tripal_entities.chado_entity.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. *
  4. * Implements hook_entity_load().
  5. */
  6. function tripal_entities_entity_presave($entity, $type) {
  7. }
  8. /**
  9. *
  10. * @param $entity
  11. * @param $type
  12. */
  13. function tripal_entities_entity_postsave($entity, $type) {
  14. // Set the title for this entity using the chado data.
  15. $title = chado_get_entity_title($entity);
  16. $ec = new TripalEntityController($entity->type);
  17. $ec->setTitle($entity, $title);
  18. }
  19. /**
  20. *
  21. * Implements hook_entity_load().
  22. */
  23. function tripal_entities_entity_load($entities, $type) {
  24. }
  25. /**
  26. *
  27. * Implements hook_entity_insert().
  28. */
  29. function tripal_entities_entity_insert($entity, $type) {
  30. }
  31. /**
  32. *
  33. * Implements hook_entity_update().
  34. */
  35. function tripal_entities_entity_update($entity, $type) {
  36. }
  37. /**
  38. *
  39. * Implements hook_entity_delete().
  40. */
  41. function tripal_entities_entity_delete($entity, $type) {
  42. $record = db_select('chado_entity', 'ce')
  43. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  44. ->condition('entity_id', $entity->id)
  45. ->execute()
  46. ->fetchObject();
  47. if ($record && property_exists($record, 'chado_entity_id')) {
  48. // Delete the corresponding record in Chado
  49. $table = $record->data_table;
  50. $record_id = $record->record_id;
  51. chado_delete_record($table, array($table . '_id' => $record_id));
  52. //Delete the record in the public.chado_entity table
  53. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  54. db_query($sql, array(':id' => $record->chado_entity_id));
  55. }
  56. }
  57. /**
  58. *
  59. * @param unknown $display
  60. * @param unknown $context
  61. */
  62. function tripal_entities_field_widget_form_alter(&$element, &$form_state, $context) {
  63. // The timelastmodified field exists in many Chado tables. We want
  64. // the form element to update to the most recent time rather than the time
  65. // in the database.
  66. if (array_key_exists('#field_name', $element)) {
  67. $field_name = $element['#field_name'];
  68. $matches = array();
  69. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  70. $tablename = $matches[1];
  71. $colname = $matches[2];
  72. $schema = chado_get_schema($tablename);
  73. if ($colname == 'timelastmodified' and
  74. $schema['fields'][$colname]['type'] == 'datetime') {
  75. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s");
  76. $element['#date_items']['value'] = $element['#default_value']['value'];
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * Implements hook_chado_field_alter().
  83. *
  84. */
  85. function tripal_entities_chado_field_alter(&$field) {
  86. // Here we provide new field types and widgets for FK fields
  87. // and fields that need special attention.
  88. if ($field['chado_column'] =='organism_id') {
  89. $field['field_type'] = 'organism_id';
  90. $field['widget_type'] = 'tripal_fields_organism_select_widget';
  91. $field['label'] = 'Organism';
  92. $field['description'] = 'Select an organism.';
  93. }
  94. else if ($field['chado_column'] =='dbxref_id') {
  95. $field['field_type'] = 'dbxref_id';
  96. $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
  97. $field['label'] = 'Primary Cross Reference';;
  98. $field['description'] = 'This record can be cross-referenced with a record in another online database. This field is intended for the most prominent reference. At a minimum, the database and accession must be provided.';
  99. }
  100. else if ($field['chado_table'] == 'feature' and
  101. $field['chado_column'] == 'md5checksum') {
  102. $field['field_type'] = 'md5checksum';
  103. $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
  104. $field['label'] = 'MD5 Checksum';
  105. $field['description'] = 'Generating MD5 checksum for the sequence.';
  106. }
  107. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'seqlen') {
  108. $field['field_type'] = 'seqlen';
  109. $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
  110. $field['label'] = 'Seqlen';
  111. $field['description'] = 'The length of the residues.';
  112. }
  113. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'residues') {
  114. $field['field_type'] = 'residues';
  115. $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
  116. $field['label'] = 'Residues';
  117. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  118. }
  119. }
  120. /**
  121. * Determines whether the given user has access to a tripal data entity.
  122. *
  123. * @param $op
  124. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  125. * or just 'edit' (being the same as 'create' or 'update').
  126. * @param $entity
  127. * Optionally a tripal data entity or a tripal data type to check access for.
  128. * If nothing is given, access for all types is determined.
  129. * @param $account
  130. * The user to check for. Leave it to NULL to check for the global user.
  131. * @return boolean
  132. * Whether access is allowed or not.
  133. */
  134. function tripal_entities_entity_access($op, $entity = NULL, $account = NULL) {
  135. if (user_access('administer tripal data', $account)) {
  136. return TRUE;
  137. }
  138. if (isset($entity) && $type_name = $entity->type) {
  139. $op = ($op == 'view') ? 'view' : 'edit';
  140. if (user_access("$op any $type_name data", $account)) {
  141. return TRUE;
  142. }
  143. }
  144. return FALSE;
  145. }
  146. /**
  147. * Menu callback to display an entity.
  148. *
  149. * As we load the entity for display, we're responsible for invoking a number
  150. * of hooks in their proper order.
  151. *
  152. * @see hook_entity_prepare_view()
  153. * @see hook_entity_view()
  154. * @see hook_entity_view_alter()
  155. */
  156. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  157. $content = '';
  158. $controller = entity_get_controller($entity->type);
  159. $content = $controller->view(array($entity->id => $entity));
  160. drupal_set_title($entity->title);
  161. return $content;
  162. }
  163. /**
  164. * Menu title callback for showing individual entities
  165. */
  166. function tripal_entities_entity_title($entity){
  167. if ($entity) {
  168. return $entity->title;
  169. }
  170. }