tripal_entities.api.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * Retrieves a TripalTerm entity that matches the given arguments.
  4. *
  5. * @param $namespace
  6. * The namespace for the vocabulary
  7. * @param $term_id
  8. * The ID (accession) of the term in the vocabulary.
  9. *
  10. * @return
  11. * A TripalTerm entity object or NULL if not found.
  12. */
  13. function tripal_load_term_entity($namespace, $term_id) {
  14. $query = db_select('tripal_term', 'tt');
  15. $query->join('tripal_vocab' ,'tv', 'tv.id = tt.vocab_id');
  16. $query->fields('tt', array('id', 'term_id'))
  17. ->fields('tv', array('namespace'))
  18. ->condition('tv.namespace', $namespace)
  19. ->condition('tt.term_id', $term_id);
  20. $term = $query->execute()->fetchObject();
  21. if ($term) {
  22. $entity = entity_load('TripalTerm', array($term->id));
  23. return reset($entity);
  24. }
  25. return NULL;
  26. }
  27. /**
  28. * Retrieves a TripalVocab entity that maches the given arguments.
  29. *
  30. * @param $namespace
  31. *
  32. * @return
  33. * A TripalVocab entity object or NULL if not found.
  34. */
  35. function tripal_load_vocab_entity($namespace) {
  36. $vocab = db_select('tripal_vocab', 'tv')
  37. ->fields('tv')
  38. ->condition('tv.namespace', $namespace)
  39. ->execute()
  40. ->fetchObject();
  41. if ($vocab) {
  42. $entity = entity_load('TripalVocab', array($vocab->id));
  43. return reset($entity);
  44. }
  45. return NULL;
  46. }
  47. /**
  48. * Creates a new Tripal Entity type (i.e. bundle).
  49. *
  50. * @param $namespace
  51. * The abbreviated namespace for the vocabulary (e.g. RO, SO, PATO).
  52. * @param $term_id
  53. * The unique term ID in the vocabulary $namespace (i.e. an accession).
  54. * @param $term_name
  55. * A human-readable name for this term. This will became the name that
  56. * appears for the content type. In practice, this should be the name
  57. * of the term. (E.g. the name for SO:0000704 is gene).
  58. * @param $error
  59. * A string, passed by reference, that is filled with the error message
  60. * if the function fails.
  61. *
  62. * @return
  63. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  64. */
  65. function tripal_create_bundle($namespace, $term_id, $term_name, &$error = '') {
  66. // First create the TripalVocab if it doesn't already exist.
  67. $vocab = tripal_load_vocab_entity($namespace);
  68. if (!$vocab) {
  69. $vocab = entity_get_controller('TripalVocab')->create(array('namespace' => $namespace));
  70. $vocab->save();
  71. }
  72. // Next create the TripalTerm if it doesn't already exist.
  73. $term = tripal_load_term_entity($namespace, $term_id);
  74. if (!$term) {
  75. $args = array('vocab_id' => $vocab->id, 'term_id' => $term_id, 'name' => $term_name);
  76. $term = entity_get_controller('TripalTerm')->create($args);
  77. $term = $term->save();
  78. }
  79. // If the bundle doesn't already exist, then add it.
  80. $bundle_id = 'bio-data_' . $term->id;
  81. $einfo = entity_get_info('TripalEntity');
  82. if (!in_array($bundle_id, array_keys($einfo['bundles']))) {
  83. // Inser the bundle.
  84. db_insert('tripal_bundle')
  85. ->fields(array(
  86. 'label' => $term_name,
  87. 'type' => 'TripalEntity',
  88. 'bundle' => $bundle_id,
  89. ))
  90. ->execute();
  91. }
  92. // Clear the entity cache so that Drupal will read our
  93. // hook_entity_info() implementation.
  94. global $language;
  95. $langcode = $language->language;
  96. cache_clear_all("entity_info:$langcode", 'cache');
  97. variable_set('menu_rebuild_needed', TRUE);
  98. // Allow modules to now add fields to the bundle
  99. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $term);
  100. return TRUE;
  101. }
  102. /**
  103. * Allows a module to add key/value pairs about a bundle.
  104. *
  105. * If a module needs to associate variables with a particular TripalEntity
  106. * type (bundle), it can do so by setting the $bundle_data array passed into
  107. * this function. This hook is called prior to creation of a new entity type.
  108. *
  109. * @param $bundle_data
  110. * An array for key/value pairs to be associated with a bundle.
  111. * @param $bundle_id
  112. * The ID for the bundle.
  113. * @param $cvterm
  114. * The CV term object that the bundle represents.
  115. */
  116. function hook_tripal_bundle_data_alter(&$bundle_data, $bundle_id, $cvterm) {
  117. // Get the cvterm for this entity type.
  118. $bundle_id = $entity->bundle;
  119. $cvterm_id = preg_replace('/bio-data_/', $bundle_id);
  120. $cvterm = tripal_get_cv(array('cvterm_id' => $cvterm_id));
  121. // Add any key/value pairs to the $bundle_data array as desired.
  122. }
  123. /**
  124. * A hook for specifying information about the data store for vocabularies.
  125. *
  126. * The storage backend for controlled vocabularies has traditionally been
  127. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  128. * other backends. Therefore, this function indicates to Tripal which
  129. * data stores are capable of providing support for terms.
  130. *
  131. * @return
  132. * An array describing the storage backends implemented by the module. The
  133. * keys are storage backend names. To avoid name clashes, storage
  134. * backend names should be prefixed with the name of the module that
  135. * exposes them. The values are arrays describing the storage backend,
  136. * with the following key/value pairs:
  137. *
  138. * label: The human-readable name of the storage backend.
  139. * module: The name of the module providing the support for this backend.
  140. * description: A short description for the storage backend.
  141. * settings: An array whose keys are the names of the settings available for
  142. * the storage backend, and whose values are the default values for
  143. * those settings.
  144. */
  145. function hook_vocab_storage_info() {
  146. return array(
  147. 'term_chado_storage' => array(
  148. 'label' => t('Chado storage'),
  149. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  150. 'settings' => array(),
  151. ),
  152. );
  153. }
  154. /**
  155. * Creates a form for specifying a term for TripalEntity creation.
  156. *
  157. * This hook allows the module that implements a vocabulary storage backend
  158. * to provide the form necessary to select a term that will then be used for
  159. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  160. * 'term_id' are in the $form_state['storage'] array. The 'namespace' and
  161. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  162. * 'SO', 'PATO', etc.). The 'term_id' must be the unique term ID (or
  163. * accession) for the term in the vocabulary.
  164. *
  165. * @param $form
  166. * @param $form_state
  167. *
  168. * @return
  169. * A form object.
  170. */
  171. function hook_vocab_select_term_form(&$form, &$form_state) {
  172. return $form;
  173. }
  174. /**
  175. * Validates the hook_vocab_select_term_form().
  176. *
  177. * @param $name
  178. */
  179. function hook_vocab_select_term_form_validate($form, &$form_state) {
  180. }