tripal_chado.entity.inc 11 KB

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