chado_linker__prop_adder.inc 9.1 KB

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