chado_linker__prop_adder.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. class chado_linker__prop_adder extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. function field_info() {
  7. return array(
  8. 'label' => t('Add a Property Type'),
  9. 'description' => t('This record may have any number of properties. Use
  10. this field to first add the type.'),
  11. 'default_widget' => 'chado_linker__prop_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::attach_info()
  24. */
  25. function attach_info($entity_type, $bundle, $target) {
  26. $field_info = array();
  27. $table_name = $target['data_table'];
  28. $type_table = $target['type_table'];
  29. $type_field = $target['field'];
  30. $cv_id = $target['cv_id'];
  31. $cvterm_id = $target['cvterm_id'];
  32. // If the linker table does not exists then we don't want to add attach.
  33. $prop_table = $table_name . 'prop';
  34. if (!chado_table_exists($prop_table)) {
  35. return $field_info;
  36. }
  37. // Initialize the field array.
  38. $field_info = array(
  39. 'field_name' => $prop_table,
  40. 'field_type' => 'chado_linker__prop_adder',
  41. 'widget_type' => 'chado_linker__prop_adder_widget',
  42. 'field_settings' => array(
  43. 'base_table' => $table_name,
  44. ),
  45. 'cardinality' => 1,
  46. 'storage' => 'field_chado_storage',
  47. 'widget_settings' => array('display_label' => 1),
  48. 'description' => '',
  49. 'label' => 'Additional Properties',
  50. 'is_required' => 0,
  51. // This feld is never visible so there are no field settings for
  52. // Chado nor the semantic web.
  53. );
  54. return $field_info;
  55. }
  56. /**
  57. * @see TripalField::widget_info()
  58. */
  59. function widget_info() {
  60. return array(
  61. 'label' => t('Add a Property'),
  62. 'field types' => array('chado_linker__prop_adder'),
  63. );
  64. }
  65. /**
  66. * @see TripalField::widget_form()
  67. */
  68. function widget_form(&$widget, &$form, &$form_state,
  69. $field, $instance, $langcode, $items, $delta, $element) {
  70. // This field has no value field. Just a fieldset for adding new properties.
  71. $widget['#element_validate'] = array('chado_linker__prop_adder_widget_validate');
  72. $widget['#type'] = 'fieldset';
  73. $widget['#title'] = $element['#title'];
  74. $widget['#description'] = $element['#description'];
  75. $widget['#group'] = 'entity_form_vtabs';
  76. $widget['kvproperty_instructions'] = array(
  77. '#type' => 'item',
  78. '#markup' => t('You may add additional properties to this form by
  79. providing a property name (from a vocabulary) in the field below
  80. and clicking the "Add Property" button. This will add a
  81. new field to the form above for the property you entered.
  82. In the future, this field will be present for all records
  83. of this type.'),
  84. );
  85. $widget['value'] = array(
  86. '#title' => t('Property Type'),
  87. '#type' => 'textfield',
  88. '#description' => t("Please enter the type of property that you want to
  89. add. As you type, suggestions will be provided."),
  90. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  91. );
  92. $widget['kvproperty_adder_link'] = array(
  93. '#type' => 'item',
  94. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  95. );
  96. // When this button is clicked, the form will be validated and submitted.
  97. // Therefore, we set custom submit and validate functions to override the
  98. // default form submit. In the validate function we set the form_state
  99. // to rebuild the form so the submit function never actually gets called,
  100. // but we need it or Drupal will run the default validate anyway.
  101. // we also set #limit_validation_errors to empty so fields that
  102. // are required that don't have values won't generate warnings.
  103. $widget['kvproperty_adder_button'] = array(
  104. '#value' => t('Add Property'),
  105. '#type' => 'submit',
  106. '#name' => 'kvproperty_adder_button',
  107. '#limit_validation_errors' => array(array($field['field_name'])),
  108. );
  109. }
  110. }
  111. /**
  112. * Callback function for validating the chado_linker__prop_adder_widget.
  113. */
  114. function chado_linker__prop_adder_widget_validate($element, &$form_state) {
  115. // Add the new field to the entity
  116. if (array_key_exists('triggering_element', $form_state) and
  117. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  118. $form_state['rebuild'] = TRUE;
  119. $field_name = $element['#field_name'];
  120. $entity_type = $element['#entity']->type;
  121. $bundle = $element['#entity']->bundle;
  122. // Get the base table name from the field properties.
  123. $field = field_info_field($field_name);
  124. $base_table = $field['settings']['base_table'];
  125. // Get the term for the property
  126. $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  127. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  128. if (count($term) == 1) {
  129. $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
  130. // The field name is the table name in this case. We want to get the
  131. // primary key as this should be the field that maps th the value.
  132. $schema = chado_get_schema($field_name);
  133. $pkey = $schema['primary key'][0];
  134. $field_info = array(
  135. 'field_type' => 'kvproperty',
  136. 'widget_type' => 'tripal_chado_kvproperty_widget',
  137. 'field_settings' => array(
  138. 'chado_table' => $field_name,
  139. 'chado_column' => $pkey,
  140. 'base_table' => $base_table,
  141. ),
  142. 'storage' => 'field_chado_storage',
  143. 'widget_settings' => array(),
  144. 'description' => $term[0]->definition ? $term[0]->definition : '',
  145. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  146. 'is_required' => FALSE,
  147. // All properties are unlimited.
  148. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  149. );
  150. tripal_add_bundle_field($prop_field_name, $field_info, $entity_type, $bundle);
  151. }
  152. else if (count($term) > 1) {
  153. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  154. }
  155. else {
  156. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  157. }
  158. }
  159. }