tripal_entities.api.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Creates a new Tripal Entity type (i.e. bundle).
  4. *
  5. * @param $cvterm
  6. * A cvterm object created using the chado_generate_var() function.
  7. * @param $error
  8. * A string, passed by reference, that is filled with the error message
  9. * if the function fails.
  10. *
  11. * @return
  12. * TRUE if the entity type (bundle) was succesfully created. FALSE otherwise.
  13. */
  14. function tripal_create_entity_type($cvterm, &$error = '') {
  15. // Create the bundle name and entity type name. The bundle name is the
  16. // cvterm ID. This isn't very human readable, but the alternative is to
  17. // use the accession which may not always be alpha-numeric.
  18. $bundle_id = 'bio-data_' . $cvterm->cvterm_id;
  19. // The organism table does not have a type_id so we won't ever find
  20. // a record for it in the tripal_cv_defaults table.
  21. $bundle_data = array();
  22. if ($cvterm->name == 'organism') {
  23. $bundle_data = array(
  24. 'cv_id' => $cvterm->cv_id->cv_id,
  25. 'cvterm_id' => $cvterm->cvterm_id,
  26. 'data_table' => 'organism',
  27. 'type_table' => 'organism',
  28. 'field' => '',
  29. );
  30. }
  31. // The analysis table does not have a type_id so we won't ever find
  32. // a record for it in the tripalcv_defaults table.
  33. else if ($cvterm->name == 'analysis') {
  34. $bundle_data = array(
  35. 'cv_id' => $cvterm->cv_id->cv_id,
  36. 'cvterm_id' => $cvterm->cvterm_id,
  37. 'data_table' => 'analysis',
  38. 'type_table' => 'analysis',
  39. 'field' => '',
  40. );
  41. }
  42. else if ($cvterm->name == 'project') {
  43. $bundle_data = array(
  44. 'cv_id' => $cvterm->cv_id->cv_id,
  45. 'cvterm_id' => $cvterm->cvterm_id,
  46. 'data_table' => 'project',
  47. 'type_table' => 'project',
  48. 'field' => '',
  49. );
  50. }
  51. else {
  52. // TODO: WHAT TO DO IF A VOCABULARY IS USED AS A DEFAULT FOR MULTIPLE
  53. // TABLES.
  54. // Look to see if this vocabulary is used as a default for any table.
  55. $default = db_select('tripal_cv_defaults', 't')
  56. ->fields('t')
  57. ->condition('cv_id', $cvterm->cv_id->cv_id)
  58. ->execute()
  59. ->fetchObject();
  60. if ($default) {
  61. $bundle_data = array(
  62. 'cv_id' => $cvterm->cv_id->cv_id,
  63. 'cvterm_id' => $cvterm->cvterm_id,
  64. 'data_table' => $default->table_name,
  65. 'type_table' => $default->table_name,
  66. 'field' => $default->field_name,
  67. );
  68. }
  69. // If there is no default table then we have an error, and we should
  70. // set a variable so that the form can help the user deal with the problem.
  71. else {
  72. $error = t('There is no default mapping of this term\'s ' .
  73. 'vocabulary to a table in Chado. Therefore, it is not possible to ' .
  74. 'determine how to store data of this type.');
  75. return FALSE;
  76. }
  77. }
  78. // Check to see if this bundle exists. If not then create it
  79. $bundle = db_select('tripal_bundle', 't')
  80. ->fields('t')
  81. ->condition('type', 'TripalEntity')
  82. ->condition('bundle', $bundle_id)
  83. ->execute()
  84. ->fetchObject();
  85. if (!$bundle) {
  86. db_insert('tripal_bundle')
  87. ->fields(array(
  88. 'label' => $cvterm->name,
  89. 'type' => 'TripalEntity',
  90. 'bundle' => $bundle_id,
  91. 'data' => serialize($bundle_data),
  92. 'module' => 'tripal_entities'
  93. ))
  94. ->execute();
  95. }
  96. // Clear the entity cache so that Drupal will read our
  97. // hook_entity_info() implementation.
  98. global $language;
  99. $langcode = $language->language;
  100. cache_clear_all("entity_info:$langcode", 'cache');
  101. variable_set('menu_rebuild_needed', TRUE);
  102. // Allow modules to now add fields to the bundle
  103. module_invoke_all('add_bundle_fields', 'TripalEntity', $bundle_id, $cvterm);
  104. return TRUE;
  105. }
  106. /**
  107. * Get Page Title Format for a given Tripal Entity Type.
  108. *
  109. * @param TripalBundle $entity
  110. * The Entity object for the Tripal Bundle the title format is for.
  111. */
  112. function tripal_get_title_format($entity) {
  113. // Title formats are saved as Tripal Bundle Variables.
  114. // Therefore, first we need the variable_id for title_formats.
  115. $variable_id = db_select('tripal_variables', 'v')
  116. ->fields('v', array('variable_id'))
  117. ->condition('name', 'title_format')
  118. ->execute()
  119. ->fetchField();
  120. // Then we can check if there is already a title format for this bundle/type.
  121. $title_format = db_select('tripal_bundle_variables', 'var')
  122. ->fields('var', array('value'))
  123. ->condition('var.bundle_id', $entity->id)
  124. ->condition('var.variable_id', $variable_id)
  125. ->execute()
  126. ->fetchField();
  127. // If there isn't yet a title format for this bundle/type then we should
  128. // determine the default based on the table unique constraint and save it.
  129. // @TODO: Replace the label with the base table name.
  130. // @TODO: make this chado independant.
  131. if (!$title_format) {
  132. $title_format = chado_node_get_unique_constraint_format($entity->label);
  133. tripal_save_title_format($entity, $title_format);
  134. }
  135. return $title_format;
  136. }
  137. /**
  138. * Save Page Title Format for a given Tripal Entity Type.
  139. *
  140. * @param TripalBundle $entity
  141. * The Entity object for the Tripal Bundle the title format is for.
  142. * @param string $format
  143. * The pattern to be used when generating entity titles for the above type.
  144. */
  145. function tripal_save_title_format($entity, $format) {
  146. // Title formats are saved as Tripal Bundle Variables.
  147. // Thus first we need to grab the variable_id for title_format.
  148. $variable_id = db_select('tripal_variables', 'v')->fields('v', array('variable_id'))->condition('name', 'title_format')->execute()->fetchField();
  149. // And then we need to write the new format to the tripal_bundle_variables table.
  150. $record = array(
  151. 'bundle_id' => $entity->id,
  152. 'variable_id' => $variable_id,
  153. 'value' => $format,
  154. );
  155. // Check whether there is already a format saved.
  156. $bundle_variable_id = db_select('tripal_bundle_variables', 'var')
  157. ->fields('var', array('bundle_variable_id'))
  158. ->condition('var.bundle_id', $record['bundle_id'])
  159. ->condition('var.variable_id', $record['variable_id'])
  160. ->execute()
  161. ->fetchField();
  162. if ($bundle_variable_id) {
  163. $record['bundle_variable_id'] = $bundle_variable_id;
  164. drupal_write_record('tripal_bundle_variables', $record, 'bundle_variable_id');
  165. }
  166. else {
  167. drupal_write_record('tripal_bundle_variables', $record);
  168. }
  169. }