chado_linker__cvterm_adder.inc 8.6 KB

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