chado_linker__prop_adder.inc 8.8 KB

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