tripal_entities.chado_entity.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. foreach ($entities as $entity) {
  25. // For Tripal created entities add in the chado_entity details.
  26. $dbs = tripal_entities_get_db_names_for_published_vocabularies();
  27. if (in_array($entity->type, $dbs)) {
  28. $details = db_select('chado_entity', 'ce')
  29. ->fields('ce')
  30. ->condition('entity_id', $entity->id)
  31. ->execute()
  32. ->fetchObject();
  33. // Add teh chado entity details to the entity in case it's needed
  34. // downstream (e.g. in field widget construction).
  35. $entity->chado_entity = $details;
  36. }
  37. }
  38. }
  39. /**
  40. *
  41. * Implements hook_entity_insert().
  42. */
  43. function tripal_entities_entity_insert($entity, $type) {
  44. }
  45. /**
  46. *
  47. * Implements hook_entity_update().
  48. */
  49. function tripal_entities_entity_update($entity, $type) {
  50. }
  51. /**
  52. *
  53. * Implements hook_entity_delete().
  54. */
  55. function tripal_entities_entity_delete($entity, $type) {
  56. $record = db_select('chado_entity', 'ce')
  57. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  58. ->condition('entity_id', $entity->id)
  59. ->execute()
  60. ->fetchObject();
  61. if ($record && property_exists($record, 'chado_entity_id')) {
  62. // Delete the corresponding record in Chado
  63. $table = $record->data_table;
  64. $record_id = $record->record_id;
  65. chado_delete_record($table, array($table . '_id' => $record_id));
  66. //Delete the record in the public.chado_entity table
  67. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  68. db_query($sql, array(':id' => $record->chado_entity_id));
  69. }
  70. }
  71. /**
  72. *
  73. * @param unknown $display
  74. * @param unknown $context
  75. */
  76. function tripal_entities_field_widget_form_alter(&$element, &$form_state, $context) {
  77. if (array_key_exists('#field_name', $element)) {
  78. $field_name = $element['#field_name'];
  79. $matches = array();
  80. if (preg_match('/(.+?)__(.+?)$/', $field_name, $matches)) {
  81. $tablename = $matches[1];
  82. $colname = $matches[2];
  83. $schema = chado_get_schema($tablename);
  84. // The timelastmodified field exists in many Chado tables. We want
  85. // the form element to update to the most recent time rather than the time
  86. // in the database.
  87. if ($colname == 'timelastmodified' and $schema['fields'][$colname]['type'] == 'datetime') {
  88. // We want the default value for the field to be the current time.
  89. $element['#default_value']['value'] = format_date(time(), 'custom', "Y-m-d H:i:s", 'UTC');
  90. $element['#date_items']['value'] = $element['#default_value']['value'];
  91. }
  92. // We want the date combo fieldset to be collaspible so we will
  93. // add our own theme_wrapper to replace the one added by the date
  94. // module.
  95. if (array_key_exists($colname, $schema['fields']) and $schema['fields'][$colname]['type'] == 'datetime') {
  96. $element['#theme_wrappers'] = array('tripal_entities_date_combo');
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * This theme function is meant to override the data_combo theme.
  103. *
  104. * @param $variables
  105. */
  106. function theme_tripal_entities_date_combo($variables) {
  107. $element = $variables['element'];
  108. $field = field_info_field($element['#field_name']);
  109. $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
  110. // Group start/end items together in fieldset.
  111. $fieldset = array(
  112. '#title' => t($element['#title']) . ' ' . ($element['#delta'] > 0 ? intval($element['#delta'] + 1) : ''),
  113. '#value' => '',
  114. '#description' => !empty($element['#fieldset_description']) ? $element['#fieldset_description'] : '',
  115. '#attributes' => array(),
  116. '#children' => $element['#children'],
  117. '#attributes' => array('class' => array('collapsible', 'collapsed')),
  118. );
  119. return theme('fieldset', array('element' => $fieldset));
  120. }
  121. /**
  122. * Implements hook_chado_field_alter().
  123. *
  124. */
  125. function tripal_entities_chado_field_alter(&$field) {
  126. // If the field doesn't list the Chado table or column then just return.
  127. if (!array_key_exists('chado_table', $field) or !array_key_exists('chado_column', $field)) {
  128. return;
  129. }
  130. // Here we provide new field types and widgets for FK fields
  131. // and fields that need special attention.
  132. if ($field['chado_column'] =='organism_id') {
  133. $field['field_type'] = 'organism_id';
  134. $field['widget_type'] = 'tripal_fields_organism_select_widget';
  135. $field['label'] = 'Organism';
  136. $field['description'] = 'Select an organism.';
  137. }
  138. else if ($field['chado_column'] =='dbxref_id') {
  139. $field['field_type'] = 'dbxref_id';
  140. $field['widget_type'] = 'tripal_fields_primary_dbxref_widget';
  141. $field['label'] = 'Primary Cross Reference';;
  142. $field['description'] = 'This record can be cross-referenced with a
  143. record in another online database. The primary reference is for the
  144. most prominent reference. At a minimum, the database and accession
  145. must be provided. To remove a set reference, change the database
  146. field to "Select a Database".';
  147. }
  148. else if ($field['chado_table'] == 'feature' and
  149. $field['chado_column'] == 'md5checksum') {
  150. $field['field_type'] = 'md5checksum';
  151. $field['widget_type'] = 'tripal_fields_md5checksum_checkbox_widget';
  152. $field['label'] = 'MD5 Checksum';
  153. $field['description'] = 'Generating MD5 checksum for the sequence.';
  154. }
  155. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'seqlen') {
  156. $field['field_type'] = 'seqlen';
  157. $field['widget_type'] = 'tripal_fields_seqlen_hidden_widget';
  158. $field['label'] = 'Seqlen';
  159. $field['description'] = 'The length of the residues.';
  160. }
  161. else if ($field['chado_table'] == 'feature' and $field['chado_column'] == 'residues') {
  162. $field['field_type'] = 'residues';
  163. $field['widget_type'] = 'tripal_fields_residues_textarea_widget';
  164. $field['label'] = 'Residues';
  165. $field['description'] = 'Please provide an IUPAC compatible residues for this feature. Spaces and new lines are allowed.';
  166. }
  167. else if ($field['label'] == 'Timeaccessioned') {
  168. $field['label'] = 'Time Accessioned';
  169. $field['description'] = 'Please enter the time that this record was first added to the database.';
  170. }
  171. else if ($field['label'] == 'Timelastmodified') {
  172. $field['label'] = 'Time Last Modified';
  173. $field['description'] = 'Please enter the time that this record was last modified. The default is the current time.';
  174. }
  175. }
  176. /**
  177. * Determines whether the given user has access to a tripal data entity.
  178. *
  179. * @param $op
  180. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  181. * or just 'edit' (being the same as 'create' or 'update').
  182. * @param $entity
  183. * Optionally a tripal data entity or a tripal data type to check access for.
  184. * If nothing is given, access for all types is determined.
  185. * @param $account
  186. * The user to check for. Leave it to NULL to check for the global user.
  187. * @return boolean
  188. * Whether access is allowed or not.
  189. */
  190. function tripal_entities_entity_access($op, $entity = NULL, $account = NULL) {
  191. if (user_access('administer tripal data', $account)) {
  192. return TRUE;
  193. }
  194. if (isset($entity) && $type_name = $entity->type) {
  195. $op = ($op == 'view') ? 'view' : 'edit';
  196. if (user_access("$op any $type_name data", $account)) {
  197. return TRUE;
  198. }
  199. }
  200. return FALSE;
  201. }
  202. /**
  203. * Menu callback to display an entity.
  204. *
  205. * As we load the entity for display, we're responsible for invoking a number
  206. * of hooks in their proper order.
  207. *
  208. * @see hook_entity_prepare_view()
  209. * @see hook_entity_view()
  210. * @see hook_entity_view_alter()
  211. */
  212. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  213. $content = '';
  214. $controller = entity_get_controller($entity->type);
  215. $content = $controller->view(array($entity->id => $entity));
  216. drupal_set_title($entity->title);
  217. return $content;
  218. }
  219. /**
  220. * Menu title callback for showing individual entities
  221. */
  222. function tripal_entities_entity_title($entity){
  223. if ($entity) {
  224. return $entity->title;
  225. }
  226. }