chado_linker__cvterm_adder.inc 8.2 KB

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