chado_linker__prop_adder.inc 9.5 KB

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