chado_linker__prop_adder.inc 9.1 KB

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