chado_linker__prop_adder.inc 8.8 KB

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