tripal_entities.api.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. * Get Page Title Format for a given Tripal Entity Type.
  104. *
  105. * @param TripalBundle $entity
  106. * The Entity object for the Tripal Bundle the title format is for.
  107. */
  108. function tripal_get_title_format($entity) {
  109. // Title formats are saved as Tripal Bundle Variables.
  110. // Therefore, first we need the variable_id for title_formats.
  111. $variable_id = db_select('tripal_variables', 'v')
  112. ->fields('v', array('variable_id'))
  113. ->condition('name', 'title_format')
  114. ->execute()
  115. ->fetchField();
  116. // Then we can check if there is already a title format for this bundle/type.
  117. $title_format = db_select('tripal_bundle_variables', 'var')
  118. ->fields('var', array('value'))
  119. ->condition('var.bundle_id', $entity->id)
  120. ->condition('var.variable_id', $variable_id)
  121. ->execute()
  122. ->fetchField();
  123. // If there isn't yet a title format for this bundle/type then we should
  124. // determine the default based on the table unique constraint and save it.
  125. // @TODO: Replace the label with the base table name.
  126. // @TODO: make this chado independant.
  127. if (!$title_format) {
  128. $title_format = chado_node_get_unique_constraint_format($entity->label);
  129. tripal_save_title_format($entity, $title_format);
  130. }
  131. return $title_format;
  132. }
  133. /**
  134. * Save Page Title Format for a given Tripal Entity Type.
  135. *
  136. * @param TripalBundle $entity
  137. * The Entity object for the Tripal Bundle the title format is for.
  138. * @param string $format
  139. * The pattern to be used when generating entity titles for the above type.
  140. */
  141. function tripal_save_title_format($entity, $format) {
  142. // Title formats are saved as Tripal Bundle Variables.
  143. // Thus first we need to grab the variable_id for title_format.
  144. $variable_id = db_select('tripal_variables', 'v')->fields('v', array('variable_id'))->condition('name', 'title_format')->execute()->fetchField();
  145. // And then we need to write the new format to the tripal_bundle_variables table.
  146. $record = array(
  147. 'bundle_id' => $entity->id,
  148. 'variable_id' => $variable_id,
  149. 'value' => $format,
  150. );
  151. // Check whether there is already a format saved.
  152. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  153. ->fields('var', array('bundle_variable_id'))
  154. ->condition('var.bundle_id', $record['bundle_id'])
  155. ->condition('var.variable_id', $record['variable_id'])
  156. ->execute()
  157. ->fetchField();
  158. if ($bundle_variable_id) {
  159. $record['bundle_variable_id'] = $bundle_variable_id;
  160. drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  161. }
  162. else {
  163. drupal_write_record('tripal_bundle_variables', $record);
  164. }
  165. }
  166. /**
  167. * A hook for specifying information about the data store for vocabularies.
  168. *
  169. * The storage backend for controlled vocabularies has traditionally been
  170. * the Chado CV term tables. However, Tripal v3.0 introduces APIs for supporting
  171. * other backends. Therefore, this function indicates to Tripal which
  172. * data stores are capable of providing support for terms.
  173. *
  174. * @return
  175. * An array describing the storage backends implemented by the module. The
  176. * keys are storage backend names. To avoid name clashes, storage
  177. * backend names should be prefixed with the name of the module that
  178. * exposes them. The values are arrays describing the storage backend,
  179. * with the following key/value pairs:
  180. *
  181. * label: The human-readable name of the storage backend.
  182. * module: The name of the module providing the support for this backend.
  183. * description: A short description for the storage backend.
  184. * settings: An array whose keys are the names of the settings available for
  185. * the storage backend, and whose values are the default values for
  186. * those settings.
  187. */
  188. function hook_vocab_storage_info() {
  189. return array(
  190. 'term_chado_storage' => array(
  191. 'label' => t('Chado storage'),
  192. 'description' => t('Integrates terms stored in the local Chado database with Tripal entities.'),
  193. 'settings' => array(),
  194. ),
  195. );
  196. }
  197. /**
  198. * Creates a form for specifying a term for TripalEntity creation.
  199. *
  200. * This hook allows the module that implements a vocabulary storage backend
  201. * to provide the form necessary to select a term that will then be used for
  202. * creating a new TripalEntity type. Tripal will expect that a 'namespace' and
  203. * 'term_id' are in the $form_state['storage'] array. The 'namespace' and
  204. * must be the abbreviated uppercase namespace for the vocabulary (e.g. 'RO',
  205. * 'SO', 'PATO', etc.). The 'term_id' must be the unique term ID (or
  206. * accession) for the term in the vocabulary.
  207. *
  208. * @param $form
  209. * @param $form_state
  210. *
  211. * @return
  212. * A form object.
  213. */
  214. function hook_vocab_select_term_form(&$form, &$form_state) {
  215. return $form;
  216. }
  217. /**
  218. * Validates the hook_vocab_select_term_form().
  219. *
  220. * @param $name
  221. */
  222. function hook_vocab_select_term_form_validate($form, &$form_state) {
  223. }