tripal_chado.entity.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * Implements hook_entity_create().
  4. *
  5. * This hook is called when brand new entities are created, but
  6. * they are not loaded so the hook_entity_load() is not yet called.
  7. */
  8. function tripal_chado_entity_create(&$entity, $type) {
  9. if ($type == 'TripalEntity') {
  10. // Set some defaults on vars needed by this module.
  11. $entity->chado_table = NULL;
  12. $entity->chado_column = NULL;
  13. $entity->chado_record = NULL;
  14. $entity->chado_record_id = NULL;
  15. // Add in the Chado table information for this entity type.
  16. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  17. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  18. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  19. if ($chado_table) {
  20. $entity->chado_table = $chado_table;
  21. $entity->chado_column = $chado_column;
  22. }
  23. }
  24. }
  25. /**
  26. * Implements hook_entity_presave().
  27. */
  28. function tripal_chado_entity_presave($entity, $type) {
  29. }
  30. /**
  31. * Implements hook_entity_postsave().
  32. */
  33. function tripal_chado_entity_postsave($entity, $type) {
  34. }
  35. /**
  36. * Implements hook_entity_load().
  37. */
  38. function tripal_chado_entity_load($entities, $type) {
  39. if ($type == 'TripalEntity') {
  40. foreach ($entities as $entity) {
  41. // We want to add in the record ID to the entity.
  42. if (property_exists($entity, 'id')) {
  43. // Set some defaults on vars needed by this module.
  44. $entity->chado_table = NULL;
  45. $entity->chado_column = NULL;
  46. $entity->chado_record = NULL;
  47. $entity->chado_record_id = NULL;
  48. // Add in the Chado table information for this entity type.
  49. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  50. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  51. $chado_column = tripal_get_bundle_variable('chado_column', $bundle->id);
  52. if ($chado_table) {
  53. $entity->chado_table = $chado_table;
  54. $entity->chado_column = $chado_column;
  55. }
  56. $chado_entity = db_select('chado_entity' ,'ce')
  57. ->fields('ce')
  58. ->condition('ce.entity_id', $entity->id)
  59. ->execute()
  60. ->fetchObject();
  61. $schema = chado_get_schema($chado_table);
  62. $record = chado_generate_var($chado_table, array($schema['primary key'][0] => $chado_entity->record_id));
  63. $entity->chado_record = $record;
  64. $entity->chado_record_id = $chado_entity->record_id;
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. *
  71. * Implements hook_entity_insert().
  72. */
  73. function tripal_chado_entity_insert($entity, $type) {
  74. }
  75. /**
  76. *
  77. * Implements hook_entity_update().
  78. */
  79. function tripal_chado_entity_update($entity, $type) {
  80. }
  81. /**
  82. *
  83. * Implements hook_entity_delete().
  84. */
  85. function tripal_chado_entity_delete($entity, $type) {
  86. $record = db_select('chado_entity', 'ce')
  87. ->fields('ce', array('chado_entity_id', 'data_table', 'record_id'))
  88. ->condition('entity_id', $entity->id)
  89. ->execute()
  90. ->fetchObject();
  91. if ($record && property_exists($record, 'chado_entity_id')) {
  92. // Delete the corresponding record in Chado
  93. $table = $record->data_table;
  94. $record_id = $record->record_id;
  95. chado_delete_record($table, array($table . '_id' => $record_id));
  96. //Delete the record in the public.chado_entity table
  97. $sql = "DELETE FROM {chado_entity} WHERE chado_entity_id = :id";
  98. db_query($sql, array(':id' => $record->chado_entity_id));
  99. }
  100. }
  101. /**
  102. * Determines whether the given user has access to a tripal data entity.
  103. *
  104. * TODO: I'm not sure this function should be at this level. I think all
  105. * access controls should be handled by the tripal_entity module and that
  106. * storage backends should just attach data as requested.
  107. *
  108. * @param $op
  109. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  110. * or just 'edit' (being the same as 'create' or 'update').
  111. * @param $entity
  112. * Optionally a tripal data entity or a tripal data type to check access for.
  113. * If nothing is given, access for all types is determined.
  114. * @param $account
  115. * The user to check for. Leave it to NULL to check for the global user.
  116. * @return boolean
  117. * Whether access is allowed or not.
  118. */
  119. function tripal_chado_entity_access($op, $entity = NULL, $account = NULL) {
  120. if (user_access('administer tripal data', $account)) {
  121. return TRUE;
  122. }
  123. if (isset($entity) && $type_name = $entity->type) {
  124. $op = ($op == 'view') ? 'view' : 'edit';
  125. if (user_access("$op any $type_name data", $account)) {
  126. return TRUE;
  127. }
  128. }
  129. return FALSE;
  130. }
  131. /**
  132. * Implements hook_tripal_default_title_format().
  133. */
  134. function tripal_chado_tripal_default_title_format($entity, $available_tokens) {
  135. $format = array();
  136. // Load the term associated with this Tripal Content type.
  137. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  138. $term = reset($term);
  139. // Load the bundle
  140. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  141. $bundle_id = $bundle->id;
  142. $table = tripal_get_bundle_variable('chado_table', $bundle_id);
  143. $column = tripal_get_bundle_variable('chado_column', $bundle_id);
  144. $cvterm_id = tripal_get_bundle_variable('chado_cvterm_id', $bundle_id);
  145. // For organism titles we want the genus and species with no comma separation.
  146. if ($table == 'organism') {
  147. $format[] = array(
  148. 'format' => '[organism.genus] [organism.species]',
  149. 'weight' => -5
  150. );
  151. }
  152. if ($table == 'analysis') {
  153. $format[] = array(
  154. 'format' => '[analysis__name]',
  155. 'weight' => -5
  156. );
  157. }
  158. if ($table == 'feature') {
  159. $format[] = array(
  160. 'format' => '[feature__name]',
  161. 'weight' => -5
  162. );
  163. }
  164. if ($table == 'stock') {
  165. $format[] = array(
  166. 'format' => '[stock__name]',
  167. 'weight' => -5
  168. );
  169. }
  170. return $format;
  171. }
  172. /**
  173. * Implements hook_entity_property_info_alter().
  174. *
  175. * This is being implemented to ensure chado fields are exposed for search api
  176. * indexing. All fields are available for index by default but the getter
  177. * function set by default is not actually capable of getting the value from
  178. * chado. Thus we change the getter function to one that can :-).
  179. */
  180. function tripal_chado_entity_property_info_alter(&$info) {
  181. // Get a list of fields with the chado storage backend.
  182. // Loop through all of the bundles.
  183. if (isset($info['TripalEntity']['bundles'])) {
  184. foreach ($info['TripalEntity']['bundles'] as $bundle_id => $bundle) {
  185. // Loop through each of the fields for a given bundle.
  186. foreach ($bundle['properties'] as $field_name => $field_info) {
  187. // If the field is a chado field, then change the callback.
  188. // @todo check this properly.
  189. if (preg_match('/(\w+)__(\w+)/', $field_name, $matches)) {
  190. $info['TripalEntity']['bundles'][$bundle_id]['properties'][$field_name]['getter callback'] =
  191. 'tripal_chado_entity_property_get_value';
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Provides a way for the search api to grab the value of a chado field.
  199. *
  200. * @param $entity
  201. * The fully-loaded entity object to be indexed.
  202. * @param $options
  203. * Options that can be ued when retrieving the value.
  204. * @param $field_name
  205. * The machine name of the field we want to retrieve.
  206. * @param $entity_type
  207. * The type of entity (ie: TripalEntity).
  208. *
  209. * @return
  210. * The rendered value of the field specified by $field_name.
  211. */
  212. function tripal_chado_entity_property_get_value($entity, $options, $field_name, $entity_type) {
  213. $display = array(
  214. 'type' => '',
  215. 'label' => 'hidden',
  216. );
  217. $langcode = LANGUAGE_NONE;
  218. $items = field_get_items($entity_type, $entity, $field_name);
  219. if (count($items) == 1) {
  220. $render_array = field_view_value($entity_type, $entity, $field_name, $items[0], $display, $langcode);
  221. }
  222. // @todo: handle fields with multiple values.
  223. else {
  224. $render_array = field_view_value($entity_type, $entity, $field_name, $items[0], $display, $langcode);
  225. drupal_set_message('Tripal Chado currently only supports views integration ' .
  226. 'for single value fields. The first value has been shown.', 'warning');
  227. }
  228. return drupal_render($render_array);
  229. }
  230. /**
  231. * Implements hook_entity_view().
  232. */
  233. function tripal_chado_entity_view($entity, $type, $view_mode, $langcode) {
  234. // If this entity is a TripalEntity and is a full view, then
  235. // we want to support the legacy view, but only if the legacy
  236. // module is enabled (the functions exist).
  237. if ($type =='TripalEntity') {
  238. // Use the generic template to render the fields
  239. if ($view_mode == 'full') {
  240. // Get the Chado table for this data type.
  241. $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));
  242. $chado_table = tripal_get_bundle_variable('chado_table', $bundle->id);
  243. $chado_field = tripal_get_bundle_variable('chado_column', $bundle->id);
  244. // Get the list of templates that should be used for entities and generatte
  245. // the key in the array for this entity type (using the chado table the
  246. // entity maps to).
  247. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  248. $legacy_template = 'legacy_template--chado_' . $chado_table;
  249. // If the site admin has indicated that thhis entity type should use
  250. // a legacy tmplate then prepare the entity and content to fake a
  251. // node.
  252. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  253. // Remove the fields added by the chado_field_storage.
  254. $fields = field_info_fields();
  255. foreach($fields as $field) {
  256. if ($field['storage']['type'] == 'field_chado_storage' or
  257. $field['storage']['type'] == 'tripal_no_storage') {
  258. $field_name = $field['field_name'];
  259. if (property_exists($entity, $field_name)) {
  260. $entity->$field_name = NULL;
  261. unset($entity->content[$field_name]);
  262. }
  263. }
  264. }
  265. // Make the entity look like a node.
  266. $entity->type = 'chado_' . $chado_table;
  267. $entity->$chado_table = $entity->chado_record;
  268. // Add any node specific fields to the entity to fake the node.
  269. $node_schema = drupal_get_schema('node');
  270. foreach ($node_schema['fields'] as $field_name => $details) {
  271. if (!property_exists($entity, $field_name)) {
  272. $entity->$field_name = '';
  273. if (array_key_exists('default', $details)) {
  274. $entity->$field_name = $details['default'];
  275. }
  276. }
  277. }
  278. // Now call the module's node_view hook to add additional
  279. // content to our 'fake' entity node.
  280. $modules = module_list();
  281. foreach ($modules as $mname => $details) {
  282. $function_name = $mname . '_node_view';
  283. if (function_exists($function_name)) {
  284. $function_name($entity, $view_mode, $langcode);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. /**
  292. * Implements hook_entity_view_alter().
  293. *
  294. * This function is used to support legacy Tripal v2 templates
  295. * for use with Tripal v3 entities.
  296. */
  297. function tripal_chado_entity_view_alter(&$build) {
  298. // For the legacy support, we need to make sure the TOC
  299. // is built.
  300. if ($build['#entity_type'] == 'TripalEntity') {
  301. $enabled_templates = variable_get('tripal_chado_enabled_legacy_templates', array());
  302. $entity = $build['#entity'];
  303. $legacy_template = 'legacy_template--' . $entity->type;
  304. if (key_exists($legacy_template, $enabled_templates) && $enabled_templates[$legacy_template]) {
  305. $build['#entity']->nid = NULL;
  306. $build['#node'] = $build['#entity'];
  307. $modules = module_list();
  308. foreach ($modules as $mname => $details) {
  309. $function_name = $mname . '_node_view_alter';
  310. if (function_exists($function_name)) {
  311. $function_name($build);
  312. }
  313. }
  314. }
  315. }
  316. }