tripal_entities.api.inc 6.9 KB

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