tripal_chado.entity.inc 12 KB

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