tripal_entities.chado_entity.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if (array_key_exists('#field_name', $element)) {
  64. $field_name = $element['#field_name'];
  65. $matches = array();
  66. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  67. $tablename = $matches[1];
  68. $colname = $matches[2];
  69. $schema = chado_get_schema($tablename);
  70. // The timelastmodified field exists in many Chado tables. We want
  71. // the form element to update to the most recent time rather than the time
  72. // in the database.
  73. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  74. // We want the default value for the field to be the current time.
  75. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  76. $element['#date_items']['value'] = $element['#default_value']['value'];
  77. }
  78. // We want the date combo fieldset to be collaspible so we will
  79. // add our own theme_wrapper to replace the one added by the date
  80. // module.
  81. if ($schema['fields'][$colname]['type'] == 'datetime') {
  82. $element['#theme_wrappers'] = array('tripal_entities_date_combo');
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * This theme function is meant to override the data_combo theme.
  89. *
  90. * @param $variables
  91. */
  92. function theme_tripal_entities_date_combo($variables) {
  93. $element = $variables['element'];
  94. $field = field_info_field($element['#field_name']);
  95. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  96. // Group start/end items together in fieldset.
  97. $fieldset = array(
  98. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  99. '#value' => '',
  100. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  101. '#attributes' => array(),
  102. '#children' => $element['#children'],
  103. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  104. );
  105. return theme('fieldset', array('element' => $fieldset));
  106. }
  107. /**
  108. * Implements hook_chado_field_alter().
  109. *
  110. */
  111. function tripal_entities_chado_field_alter(&$field) {
  112. // If the field doesn't list the Chado table or column then just return.
  113. if (!array_key_exists('chado_table', $field) or !array_key_exists('chado_column', $field)) {
  114. return;
  115. }
  116. // Here we provide new field types and widgets for FK fields
  117. // and fields that need special attention.
  118. if ($field['chado_column'] =='organism_id') {
  119. $field['field_type'] = 'organism_id';
  120. $field['widget_type'] = 'tripal_fields_organism_select_widget';
  121. $field['label'] = 'Organism';
  122. $field['description'] = 'Select an organism.';
  123. }
  124. else if ($field['chado_column'] =='dbxref_id') {
  125. $field['field_type'] = 'dbxref_id';
  126. $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
  127. $field['label'] = 'Primary Cross Reference';;
  128. $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.';
  129. }
  130. else if ($field['chado_table'] == 'feature' and
  131. $field['chado_column'] == 'md5checksum') {
  132. $field['field_type'] = 'md5checksum';
  133. $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
  134. $field['label'] = 'MD5 Checksum';
  135. $field['description'] = 'Generating MD5 checksum for the sequence.';
  136. }
  137. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'seqlen') {
  138. $field['field_type'] = 'seqlen';
  139. $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
  140. $field['label'] = 'Seqlen';
  141. $field['description'] = 'The length of the residues.';
  142. }
  143. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'residues') {
  144. $field['field_type'] = 'residues';
  145. $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
  146. $field['label'] = 'Residues';
  147. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  148. }
  149. else if ($field['label'] == 'Timeaccessioned') {
  150. $field['label'] = 'Time Accessioned';
  151. $field['description'] = 'Please enter the time that this record was first added to the database.';
  152. }
  153. else if ($field['label'] == 'Timelastmodified') {
  154. $field['label'] = 'Time Last Modified';
  155. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  156. }
  157. }
  158. /**
  159. * Determines whether the given user has access to a tripal data entity.
  160. *
  161. * @param $op
  162. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  163. * or just 'edit' (being the same as 'create' or 'update').
  164. * @param $entity
  165. * Optionally a tripal data entity or a tripal data type to check access for.
  166. * If nothing is given, access for all types is determined.
  167. * @param $account
  168. * The user to check for. Leave it to NULL to check for the global user.
  169. * @return boolean
  170. * Whether access is allowed or not.
  171. */
  172. function tripal_entities_entity_access($op, $entity = NULL, $account = NULL) {
  173. if (user_access('administer tripal data', $account)) {
  174. return TRUE;
  175. }
  176. if (isset($entity) && $type_name = $entity->type) {
  177. $op = ($op == 'view') ? 'view' : 'edit';
  178. if (user_access("$op any $type_name data", $account)) {
  179. return TRUE;
  180. }
  181. }
  182. return FALSE;
  183. }
  184. /**
  185. * Menu callback to display an entity.
  186. *
  187. * As we load the entity for display, we're responsible for invoking a number
  188. * of hooks in their proper order.
  189. *
  190. * @see hook_entity_prepare_view()
  191. * @see hook_entity_view()
  192. * @see hook_entity_view_alter()
  193. */
  194. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  195. $content = '';
  196. $controller = entity_get_controller($entity->type);
  197. $content = $controller->view(array($entity->id => $entity));
  198. drupal_set_title($entity->title);
  199. return $content;
  200. }
  201. /**
  202. * Menu title callback for showing individual entities
  203. */
  204. function tripal_entities_entity_title($entity){
  205. if ($entity) {
  206. return $entity->title;
  207. }
  208. }