chado_linker__cvterm_adder.inc 7.9 KB

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