chado_linker__prop_adder.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. // This field should never be viewed. It's to help add new properties
  26. // when editing an entity. So return nothing.
  27. return '';
  28. }
  29. /**
  30. * @see TripalField::widgetForm()
  31. */
  32. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  33. $widget['#type'] = 'fieldset';
  34. $widget['#title'] = $element['#title'];
  35. $widget['#description'] = $element['#description'];
  36. $widget['#group'] = 'entity_form_vtabs';
  37. $widget['kvproperty_instructions'] = array(
  38. '#type' => 'item',
  39. '#markup' => t('You may add additional properties to this form by
  40. providing a property name (from a vocabulary) in the field below
  41. and clicking the "Add Property" button. This will add a
  42. new field to the form above for the property you entered.
  43. In the future, this field will be present for all records
  44. of this type.'),
  45. );
  46. $widget['value'] = array(
  47. '#title' => t('Property Type'),
  48. '#type' => 'textfield',
  49. '#description' => t("Please enter the type of property that you want to
  50. add. As you type, suggestions will be provided."),
  51. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  52. );
  53. $widget['kvproperty_adder_link'] = array(
  54. '#type' => 'item',
  55. '#markup' => '<span class="kvproperty-adder-link">' . l('Add a term', 'admin/tripal/vocab/cvterm/add', array('attributes' => array('target' => '_blank'))) . '</span>',
  56. );
  57. // When this button is clicked, the form will be validated and submitted.
  58. // Therefore, we set custom submit and validate functions to override the
  59. // default form submit. In the validate function we set the form_state
  60. // to rebuild the form so the submit function never actually gets called,
  61. // but we need it or Drupal will run the default validate anyway.
  62. // we also set #limit_validation_errors to empty so fields that
  63. // are required that don't have values won't generate warnings.
  64. $widget['kvproperty_adder_button'] = array(
  65. '#value' => t('Add Property'),
  66. '#type' => 'submit',
  67. '#name' => 'kvproperty_adder_button',
  68. '#limit_validation_errors' => array(array($this->field['field_name'])),
  69. );
  70. }
  71. /**
  72. * @see TripalField::widgetFormValidate
  73. */
  74. public function widgetFormValidate($entity_type, $entity, $langcode, $items, &$errors) {
  75. // We will never have more than one item for this field at a time, so
  76. // delta is always zero.
  77. $delta = 0;
  78. // Make sure the cvterm for this property is uniquely identified.
  79. $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
  80. $cvterms = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  81. if (trim($kvproperty) && count($cvterms) == 1) {
  82. // Get the table and base table.
  83. $base_table = $this->field['settings']['base_table'];
  84. // Generate the name for the property table and the field name that we'll
  85. // be creating.
  86. $prop_table = $base_table . 'prop';
  87. $field_name = $prop_table . '__' . $cvterms[0]->cvterm_id;
  88. // Create an instance of the field.
  89. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  90. if ($instance) {
  91. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  92. 'error' => 'chado_linker__prop_adder',
  93. 'message' => t("The property already exists."),
  94. );
  95. }
  96. }
  97. if (trim($kvproperty) && count($cvterms) > 1) {
  98. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  99. 'error' => 'chado_linker__prop_adder',
  100. 'message' => t("This term is present in multiple vocabularies. Please select the appropriate one."),
  101. );
  102. }
  103. if (trim($kvproperty) && count($cvterms) == 0) {
  104. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  105. 'error' => 'chado_linker__prop_adder',
  106. 'message' => t("Please provide a property type to add."),
  107. );
  108. }
  109. }
  110. /**
  111. * @see TripalField::widgetFormSubmit()
  112. */
  113. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  114. // We will never have more than one item for this field at a time, so
  115. // delta is always zero.
  116. $delta = 0;
  117. // Add the new field to the entity but only if the property adder button
  118. // was clicked
  119. if (!array_key_exists('triggering_element', $form_state) or
  120. $form_state['triggering_element']['#name'] != 'kvproperty_adder_button') {
  121. return;
  122. }
  123. // Because we're going to add a new property we want to rebuild the form
  124. // rather than have it fully submit.
  125. $form_state['rebuild'] = TRUE;
  126. // Get the table and base table.
  127. $base_table = $this->field['settings']['base_table'];
  128. // Get the term for the property
  129. $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
  130. $cvterm = chado_generate_var('cvterm', array('name' => $kvproperty));
  131. // Generate the name for the property table and the field name that we'll
  132. // be creating.
  133. $prop_table = $base_table . 'prop';
  134. $field_name = $prop_table . '__' . $cvterm->cvterm_id;
  135. // The field name is the table name in this case. We want to get the
  136. // primary key as this should be the field that maps th the value.
  137. $schema = chado_get_schema($prop_table);
  138. $pkey = $schema['primary key'][0];
  139. // Add the field if it doesn't already exists.
  140. $field = field_info_field($field_name);
  141. if (!$field) {
  142. $field = field_create_field(array(
  143. 'field_name' => $field_name,
  144. 'type' => 'chado_linker__prop',
  145. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  146. 'locked' => FALSE,
  147. 'storage' => array(
  148. 'type' => 'field_chado_storage',
  149. ),
  150. 'settings' => array(
  151. 'chado_table' => $prop_table,
  152. 'chado_column' => $pkey,
  153. 'base_table' => $base_table,
  154. 'semantic_web' => $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession,
  155. ),
  156. ));
  157. }
  158. // Create an instance of the field.
  159. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  160. if (!$instance) {
  161. $instance = field_create_instance(array(
  162. 'field_name' => $field_name,
  163. 'entity_type' => 'TripalEntity',
  164. 'bundle' => $entity->bundle,
  165. 'label' => ucfirst(preg_replace('/_/', ' ', $cvterm->name)),
  166. 'description' => $cvterm->definition ? $cvterm->definition : '',
  167. 'required' => FALSE,
  168. 'settings' => array(),
  169. 'widget' => array(
  170. 'type' => $field_name . '_widget',
  171. 'settings' => array(
  172. 'display_label' => 1,
  173. ),
  174. ),
  175. 'display' => array(
  176. 'default' => array(
  177. 'label' => 'inline',
  178. 'type' => $field_name . '_formatter',
  179. 'settings' => array(),
  180. ),
  181. ),
  182. ));
  183. }
  184. }
  185. }