chado_linker__cvterm_adder.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. class chado_linker__cvterm_addr extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Add an Annotation Type'),
  9. 'description' => t('This record may have any number of types of
  10. annotations. Use this field to first add the type.'),
  11. 'default_widget' => 'chado_linker__cvterm_adder_widget',
  12. 'default_formatter' => 'hidden',
  13. 'settings' => array(),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. 'no_ui' => TRUE
  20. );
  21. }
  22. /**
  23. * @see TripalField::can_attach()
  24. */
  25. protected function can_attach($entity_type, $bundle, $details) {
  26. $field_info = array();
  27. $table_name = $details['chado_table'];
  28. $type_table = $details['chado_type_table'];
  29. $type_field = $details['chado_type_column'];
  30. $cv_id = $details['chado_cv_id'];
  31. $cvterm_id = $details['chado_cvterm_id'];
  32. // If the linker table does not exists then we don't want to add attach.
  33. $cvterm_table = $table_name . '_cvterm';
  34. if (chado_table_exists($cvterm_table)) {
  35. return TRUE;
  36. }
  37. return FALSE;
  38. }
  39. /**
  40. * @see TripalField::create_info()
  41. */
  42. function create_info($entity_type, $bundle, $details) {
  43. if (!$this->can_attach($entity_type, $bundle, $details)) {
  44. return;
  45. }
  46. $table_name = $details['chado_table'];
  47. $type_table = $details['chado_type_table'];
  48. $type_field = $details['chado_type_column'];
  49. $cv_id = $details['chado_cv_id'];
  50. $cvterm_id = $details['chado_cvterm_id'];
  51. return array(
  52. 'field_name' => $table_name . '_cvterm',
  53. 'type' => 'chado_linker__cvterm_adder',
  54. 'cardinality' => 1,
  55. 'locked' => FALSE,
  56. 'storage' => array(
  57. 'type' => 'field_chado_storage',
  58. ),
  59. 'settings' => array(
  60. ),
  61. );
  62. }
  63. /**
  64. * @see TripalField::create_instance_info()
  65. */
  66. function create_instance_info($entity_type, $bundle, $details) {
  67. if (!$this->can_attach($entity_type, $bundle, $details)) {
  68. return;
  69. }
  70. $table_name = $details['chado_table'];
  71. $type_table = $details['chado_type_table'];
  72. $type_field = $details['chado_type_column'];
  73. $cv_id = $details['chado_cv_id'];
  74. $cvterm_id = $details['chado_cvterm_id'];
  75. return array(
  76. 'field_name' => $table_name . '_cvterm',
  77. 'entity_type' => $entity_type,
  78. 'bundle' => $bundle->name,
  79. 'label' => 'Add Annotation Types',
  80. 'description' => 'Add additional annotations types to this record.',
  81. 'required' => FALSE,
  82. 'settings' => array(
  83. 'auto_attach' => FALSE,
  84. ),
  85. 'widget' => array(
  86. 'type' => 'chado_linker__cvterm_adder_widget',
  87. 'settings' => array(
  88. 'display_label' => 1,
  89. ),
  90. ),
  91. 'display' => array(
  92. 'deafult' => array(
  93. 'label' => 'above',
  94. 'type' => 'chado_linker__cvterm_adder_formatter',
  95. 'settings' => array(),
  96. ),
  97. ),
  98. );
  99. }
  100. /**
  101. * @see TripalField::widget_info()
  102. */
  103. function widget_info() {
  104. return array(
  105. 'label' => t('Add an Annotation'),
  106. 'field types' => array('chado_linker__cvterm_adder'),
  107. );
  108. }
  109. /**
  110. * @see TripalField::widget_form()
  111. */
  112. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  113. $langcode, $items, $delta, $element) {
  114. // This field has no value field. Just a fieldset for adding new annotation types.
  115. $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  116. $widget['#type'] = 'fieldset';
  117. $widget['#title'] = $element['#title'];
  118. $widget['#description'] = $element['#description'];
  119. $widget['#group'] = 'entity_form_vtabs';
  120. $widget['cvterm_class_adder_instructions'] = array(
  121. '#type' => 'item',
  122. '#markup' => t('You may add annotation types to this form by
  123. providing a vocabulary name in the field below
  124. and clicking the "Add Annotation Type" button. This will add a
  125. new field to the form above for the vocabulary you entered which
  126. will allow users to associate terms from that vocabulary to
  127. this record.'),
  128. );
  129. $options = tripal_get_cv_select_options();
  130. $widget['value'] = array(
  131. '#type' => 'select',
  132. '#title' => t('Vocabulary'),
  133. '#options' => $options,
  134. '#description' => t("Please enter the vocabulary that contains terms
  135. you want to allow users to use for annotations."),
  136. );
  137. // When this button is clicked, the form will be validated and submitted.
  138. // Therefore, we set custom submit and validate functions to override the
  139. // default form submit. In the validate function we set the form_state
  140. // to rebuild the form so the submit function never actually gets called,
  141. // but we need it or Drupal will run the default validate anyway.
  142. // we also set #limit_validation_errors to empty so fields that
  143. // are required that don't have values won't generate warnings.
  144. $widget['cvterm_class_adder_button'] = array(
  145. '#value' => t('Add Annotation Type'),
  146. '#type' => 'submit',
  147. '#name' => 'cvterm_class_adder_button',
  148. '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  149. '#limit_validation_errors' => array(array($field['field_name'])),
  150. );
  151. }
  152. }
  153. /**
  154. * Callback function for validating the chado_linker__cvterm_adder_widget.
  155. */
  156. function chado_linker__cvterm_adder_widget_validate($element, &$form_state) {
  157. // Add the new field to the entity
  158. if (array_key_exists('triggering_element', $form_state) and
  159. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  160. $form_state['rebuild'] = TRUE;
  161. $field_name = $element['#field_name'];
  162. $entity_type = $element['#entity']->type;
  163. $bundle = $element['#entity']->bundle;
  164. // Get the base table name from the field annotations.
  165. $field = field_info_field($field_name);
  166. $base_table = $field['settings']['base_table'];
  167. // Get the vocabulary.
  168. $cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  169. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  170. if (!$cv) {
  171. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  172. return;
  173. }
  174. $type_field_name = $field_name . '__' . $cv->cv_id;
  175. // The field name is the table name in this case. We want to get the
  176. // primary key as this should be the field that maps th the value.
  177. $schema = chado_get_schema($field_name);
  178. $pkey = $schema['primary key'][0];
  179. // Add the field if it doesn't already exists.
  180. $field = field_info_field('cvterm');
  181. if (!$field) {
  182. $create_info = array(
  183. 'field_name' => 'cvterm',
  184. 'type' => 'tripal_chado_cvterm_widget',
  185. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  186. 'locked' => FALSE,
  187. 'storage' => array(
  188. 'type' => 'field_chado_storage',
  189. ),
  190. 'settings' => array(
  191. 'chado_table' => $field_name,
  192. 'chado_column' => $pkey,
  193. 'base_table' => $base_table,
  194. ),
  195. );
  196. $field = field_create_field($create_info);
  197. }
  198. // Attach the field to the bundle if it isn't already.
  199. if (!$field or !array_key_exists('bundles', $field) or
  200. !array_key_exists('TripalEntity', $field['bundles']) or
  201. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  202. $create_instance_info = array(
  203. 'field_name' => 'cvtmerm',
  204. 'entity_type' => 'TripalEntity',
  205. 'bundle' => $bundle->name,
  206. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  207. 'description' => "Annotations from the $cv->name vocabulary",
  208. 'required' => FALSE,
  209. 'settings' => array(),
  210. 'widget' => array(
  211. 'type' => 'tripal_chado_cvterm_widget',
  212. 'settings' => array(
  213. 'display_label' => 1,
  214. ),
  215. ),
  216. 'display' => array(
  217. 'deafult' => array(
  218. 'label' => 'above',
  219. 'type' => 'tripal_chado_cvterm_formatter',
  220. 'settings' => array(),
  221. ),
  222. ),
  223. );
  224. $instance = field_create_instance($create_instance_info);
  225. }
  226. }
  227. }