tripal_chado.entity.inc 13 KB

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