tripal_chado.entity.inc 15 KB

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