chado_linker__prop_adder.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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::can_attach()
  24. */
  25. protected function can_attach($entity_type, $bundle, $details) {
  26. $table_name = $details['chado_table'];
  27. $type_table = $details['chado_type_table'];
  28. $type_field = $details['chado_type_column'];
  29. $cv_id = $details['chado_cv_id'];
  30. $cvterm_id = $details['chado_cvterm_id'];
  31. // If the linker table does not exists then we don't want to add attach.
  32. $prop_table = $table_name . 'prop';
  33. if (chado_table_exists($prop_table)) {
  34. return TRUE;
  35. }
  36. return FALSE;
  37. }
  38. /**
  39. * @see TripalField::create_info()
  40. */
  41. function create_info($entity_type, $bundle, $details) {
  42. if (!$this->can_attach($entity_type, $bundle, $details)) {
  43. return;
  44. }
  45. $table_name = $details['chado_table'];
  46. $type_table = $details['chado_type_table'];
  47. $type_field = $details['chado_type_column'];
  48. $cv_id = $details['chado_cv_id'];
  49. $cvterm_id = $details['chado_cvterm_id'];
  50. $prop_table = $table_name . 'prop';
  51. return array(
  52. 'field_name' => $prop_table,
  53. 'type' => 'chado_linker__prop_adder',
  54. 'cardinality' => 1,
  55. 'locked' => FALSE,
  56. 'storage' => array(
  57. 'type' => 'field_chado_storage',
  58. ),
  59. 'settings' => array(
  60. ),
  61. );
  62. }
  63. /**
  64. * @see TripalField::create_instance_info()
  65. */
  66. function create_instance_info($entity_type, $bundle, $details) {
  67. if (!$this->can_attach($entity_type, $bundle, $details)) {
  68. return;
  69. }
  70. $table_name = $details['chado_table'];
  71. $type_table = $details['chado_type_table'];
  72. $type_field = $details['chado_type_column'];
  73. $cv_id = $details['chado_cv_id'];
  74. $cvterm_id = $details['chado_cvterm_id'];
  75. $prop_table = $table_name . 'prop';
  76. return array(
  77. 'field_name' => $prop_table,
  78. 'entity_type' => $entity_type,
  79. 'bundle' => $bundle->name,
  80. 'label' => 'Add Properties',
  81. 'description' => 'Add additional property types to this record.',
  82. 'required' => FALSE,
  83. 'settings' => array(),
  84. 'widget' => array(
  85. 'type' => 'chado_linker__prop_adder_widget',
  86. 'settings' => array(
  87. 'display_label' => 1,
  88. ),
  89. ),
  90. 'display' => array(
  91. 'deafult' => array(
  92. 'label' => 'above',
  93. 'type' => 'chado_linker__prop_adder_formatter',
  94. 'settings' => array(),
  95. ),
  96. ),
  97. );
  98. }
  99. /**
  100. * @see TripalField::widget_info()
  101. */
  102. function widget_info() {
  103. return array(
  104. 'label' => t('Add a Property'),
  105. 'field types' => array('chado_linker__prop_adder'),
  106. );
  107. }
  108. /**
  109. * @see TripalField::widget_form()
  110. */
  111. function widget_form(&$widget, &$form, &$form_state,
  112. $field, $instance, $langcode, $items, $delta, $element) {
  113. // This field has no value field. Just a fieldset for adding new properties.
  114. $widget['#element_validate'] = array('chado_linker__prop_adder_widget_validate');
  115. $widget['#type'] = 'fieldset';
  116. $widget['#title'] = $element['#title'];
  117. $widget['#description'] = $element['#description'];
  118. $widget['#group'] = 'entity_form_vtabs';
  119. $widget['kvproperty_instructions'] = array(
  120. '#type' => 'item',
  121. '#markup' => t('You may add additional properties to this form by
  122. providing a property name (from a vocabulary) in the field below
  123. and clicking the "Add Property" button. This will add a
  124. new field to the form above for the property you entered.
  125. In the future, this field will be present for all records
  126. of this type.'),
  127. );
  128. $widget['value'] = array(
  129. '#title' => t('Property Type'),
  130. '#type' => 'textfield',
  131. '#description' => t("Please enter the type of property that you want to
  132. add. As you type, suggestions will be provided."),
  133. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  134. );
  135. $widget['kvproperty_adder_link'] = array(
  136. '#type' => 'item',
  137. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  138. );
  139. // When this button is clicked, the form will be validated and submitted.
  140. // Therefore, we set custom submit and validate functions to override the
  141. // default form submit. In the validate function we set the form_state
  142. // to rebuild the form so the submit function never actually gets called,
  143. // but we need it or Drupal will run the default validate anyway.
  144. // we also set #limit_validation_errors to empty so fields that
  145. // are required that don't have values won't generate warnings.
  146. $widget['kvproperty_adder_button'] = array(
  147. '#value' => t('Add Property'),
  148. '#type' => 'submit',
  149. '#name' => 'kvproperty_adder_button',
  150. '#limit_validation_errors' => array(array($field['field_name'])),
  151. );
  152. }
  153. }
  154. /**
  155. * Callback function for validating the chado_linker__prop_adder_widget.
  156. */
  157. function chado_linker__prop_adder_widget_validate($element, &$form_state) {
  158. // Add the new field to the entity
  159. if (array_key_exists('triggering_element', $form_state) and
  160. $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
  161. $form_state['rebuild'] = TRUE;
  162. $field_name = $element['#field_name'];
  163. $entity_type = $element['#entity']->type;
  164. $bundle = $element['#entity']->bundle;
  165. // Get the base table name from the field properties.
  166. $field = field_info_field($field_name);
  167. $base_table = $field['settings']['base_table'];
  168. // Get the term for the property
  169. $kvproperty = tripal_chado_get_field_form_values($field_name, $form_state);
  170. $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  171. if (count($term) == 1) {
  172. $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
  173. // The field name is the table name in this case. We want to get the
  174. // primary key as this should be the field that maps th the value.
  175. $schema = chado_get_schema($field_name);
  176. $pkey = $schema['primary key'][0];
  177. // Add the field if it doesn't already exists.
  178. $field = field_info_field('cvterm');
  179. if (!$field) {
  180. $create_info = array(
  181. 'field_name' => 'property-' . $term[0]->cvterm_id,
  182. 'type' => 'tripal_chado_kvproperty_widget',
  183. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  184. 'locked' => FALSE,
  185. 'storage' => array(
  186. 'type' => 'field_chado_storage',
  187. ),
  188. 'settings' => array(
  189. 'chado_table' => $field_name,
  190. 'chado_column' => $pkey,
  191. 'base_table' => $base_table,
  192. 'semantic_web' => '',
  193. ),
  194. );
  195. $field = field_create_field($create_info);
  196. }
  197. // Attach the field to the bundle if it isn't already.
  198. if (!$field and array_key_exists('bundles', $field) or
  199. !array_key_exists('TripalEntity', $field['bundles']) or
  200. !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
  201. $create_instance_info = array(
  202. 'field_name' => 'property-' . $term[0]->cvterm_id,
  203. 'entity_type' => 'TripalEntity',
  204. 'bundle' => $bundle->name,
  205. 'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
  206. 'description' => $term[0]->definition ? $term[0]->definition : '',
  207. 'required' => FALSE,
  208. 'settings' => array(),
  209. 'widget' => array(
  210. 'type' => 'tripal_chado_kvproperty_widget',
  211. 'settings' => array(
  212. 'display_label' => 1,
  213. ),
  214. ),
  215. 'display' => array(
  216. 'deafult' => array(
  217. 'label' => 'above',
  218. 'type' => 'tripal_chado_kvproperty_formatter',
  219. 'settings' => array(),
  220. ),
  221. ),
  222. );
  223. $instance = field_create_instance($create_instance_info);
  224. }
  225. }
  226. else if (count($term) > 1) {
  227. form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
  228. }
  229. else {
  230. form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
  231. }
  232. }
  233. }