chado_linker__cvterm_adder.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. class chado_linker__cvterm_adder 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 globalSettingsForm()
  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 function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  97. // This field has no value field. Just a fieldset for adding new annotation types.
  98. $widget['#type'] = 'fieldset';
  99. $widget['#title'] = $element['#title'];
  100. $widget['#description'] = $element['#description'];
  101. $widget['#group'] = 'entity_form_vtabs';
  102. $widget['cvterm_class_adder_instructions'] = array(
  103. '#type' => 'item',
  104. '#markup' => t('You may add annotation types to this form by
  105. providing a vocabulary name in the field below
  106. and clicking the "Add Annotation Type" button. This will add a
  107. new field to the form above for the vocabulary you entered which
  108. will allow users to associate terms from that vocabulary to
  109. this record.'),
  110. );
  111. $options = tripal_get_cv_select_options();
  112. $widget['value'] = array(
  113. '#type' => 'select',
  114. '#title' => t('Vocabulary'),
  115. '#options' => $options,
  116. '#description' => t("Please enter the vocabulary that contains terms
  117. you want to allow users to use for annotations."),
  118. );
  119. // $widget['#element_validate'] = array('chado_linker__cvterm_adder_widget_validate');
  120. // When this button is clicked, the form will be validated and submitted.
  121. // Therefore, we set custom submit and validate functions to override the
  122. // default form submit. In the validate function we set the form_state
  123. // to rebuild the form so the submit function never actually gets called,
  124. // but we need it or Drupal will run the default validate anyway.
  125. // we also set #limit_validation_errors to empty so fields that
  126. // are required that don't have values won't generate warnings.
  127. $widget['cvterm_class_adder_button'] = array(
  128. '#value' => t('Add Annotation Type'),
  129. '#type' => 'submit',
  130. '#name' => 'cvterm_class_adder_button',
  131. // '#submit' => array('chado_linker__cvterm_adder_widget_submit'),
  132. '#limit_validation_errors' => array(array($this->field['field_name'])),
  133. );
  134. }
  135. }
  136. /**
  137. * Callback function for validating the chado_linker__cvterm_adder_widget.
  138. */
  139. function chado_linker__cvterm_adder_widget_validate($element, &$form_state) {
  140. // Add the new field to the entity
  141. if (array_key_exists('triggering_element', $form_state) and
  142. $form_state['triggering_element']['#name'] == 'cvterm_class_adder_button') {
  143. $form_state['rebuild'] = TRUE;
  144. $field_name = $element['#field_name'];
  145. $entity_type = $element['#entity']->type;
  146. $bundle = $element['#entity']->bundle;
  147. // Get the base table name from the field annotations.
  148. $field = field_info_field($field_name);
  149. $base_table = $field['settings']['base_table'];
  150. // Get the vocabulary.
  151. //$cvterm_class_adder = tripal_chado_get_field_form_values($field_name, $form_state);
  152. $cv = chado_generate_var('cv', array('cv_id' => $cvterm_class_adder));
  153. if (!$cv) {
  154. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please select a vocabulary."));
  155. return;
  156. }
  157. $type_field_name = $field_name . '__' . $cv->cv_id;
  158. // The field name is the table name in this case. We want to get the
  159. // primary key as this should be the field that maps th the value.
  160. $schema = chado_get_schema($field_name);
  161. $pkey = $schema['primary key'][0];
  162. // Add the field if it doesn't already exists.
  163. $field = field_info_field('cvterm');
  164. if (!$field) {
  165. $create_info = array(
  166. 'field_name' => 'cvterm',
  167. 'type' => 'tripal_chado_cvterm_widget',
  168. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  169. 'locked' => FALSE,
  170. 'storage' => array(
  171. 'type' => 'field_chado_storage',
  172. ),
  173. 'settings' => array(
  174. 'chado_table' => $field_name,
  175. 'chado_column' => $pkey,
  176. 'base_table' => $base_table,
  177. ),
  178. );
  179. $field = field_create_field($create_info);
  180. }
  181. // Attach the field to the bundle if it isn't already.
  182. if (!$field or !array_key_exists('bundles', $field) or
  183. !array_key_exists('TripalEntity', $field['bundles']) or
  184. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  185. $createInstanceInfo = array(
  186. 'field_name' => 'cvtmerm',
  187. 'entity_type' => 'TripalEntity',
  188. 'bundle' => $this->bundle->name,
  189. 'label' => ucfirst(preg_replace('/_/', ' ', $cv->name)),
  190. 'description' => "Annotations from the $cv->name vocabulary",
  191. 'required' => FALSE,
  192. 'settings' => array(),
  193. 'widget' => array(
  194. 'type' => 'tripal_chado_cvterm_widget',
  195. 'settings' => array(
  196. 'display_label' => 1,
  197. ),
  198. ),
  199. 'display' => array(
  200. 'default' => array(
  201. 'label' => 'above',
  202. 'type' => 'tripal_chado_cvterm_formatter',
  203. 'settings' => array(),
  204. ),
  205. ),
  206. );
  207. $instance = field_create_instance($createInstanceInfo);
  208. }
  209. }
  210. }