chado_linker__prop_adder.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. $field_name = $widget['#field_name'];
  34. $widget['#type'] = 'fieldset';
  35. $widget['#title'] = $element['#title'];
  36. $widget['#description'] = $element['#description'];
  37. $widget['#group'] = 'entity_form_vtabs';
  38. /* $widget['kvproperty_instructions'] = array(
  39. '#type' => 'item',
  40. '#markup' => t('You may add additional properties to this form by
  41. providing a property name (from a vocabulary) in the field below
  42. and clicking the "Add Property" button. This will add a
  43. new field to the form above for the property you entered.
  44. In the future, this field will be present for all records
  45. of this type.'),
  46. ); */
  47. $term_name = array_key_exists('values', $form_state) ? $form_state['values'][$field_name]['und'][0]['wrapper']['term_name'] : '';
  48. $widget['wrapper'] = array(
  49. '#prefix' => "<span id='$field_name-lookup-form'>",
  50. '#suffix' => '</span>',
  51. );
  52. // If no term has been selected yet then provide the auto complete field.
  53. $widget['wrapper']['term_name'] = array(
  54. '#title' => t('Term'),
  55. '#type' => 'textfield',
  56. '#description' => t("The content type must be the name of a term in
  57. a controlled vocabulary and the controlled vocabulary should
  58. already be loaded into Tripal. For example, to create a content
  59. type for storing 'genes', use the 'gene' term from the
  60. Sequence Ontology (SO)."),
  61. '#required' => TRUE,
  62. '#default_value' => $term_name,
  63. '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/",
  64. );
  65. $widget['wrapper']['select_button'] = array(
  66. '#type' => 'button',
  67. '#value' => t('Lookup Term'),
  68. '#name' => 'select_cvterm',
  69. '#ajax' => array(
  70. 'callback' => "tripal_chado_prop_adder_form_ajax_callback",
  71. 'wrapper' => "$field_name-lookup-form",
  72. 'effect' => 'fade',
  73. 'method' => 'replace'
  74. ),
  75. );
  76. if ($term_name) {
  77. $widget['wrapper']['terms_list'] = array(
  78. '#type' => 'fieldset',
  79. '#title' => t('Matching Terms'),
  80. '#description' => t('Please select the term the best matches the
  81. content type you want to create. If the same term exists in
  82. multiple vocabularies you will see more than one option below.')
  83. );
  84. $match = array(
  85. 'name' => $term_name,
  86. );
  87. $terms = chado_generate_var('cvterm', $match, array('return_array' => TRUE));
  88. $terms = chado_expand_var($terms, 'field', 'cvterm.definition');
  89. $num_terms = 0;
  90. foreach ($terms as $term) {
  91. // Save the user a click by setting the default value as 1 if there's
  92. // only one matching term.
  93. $default = FALSE;
  94. $attrs = array();
  95. if ($num_terms == 0 and count($terms) == 1) {
  96. $default = TRUE;
  97. $attrs = array('checked' => 'checked');
  98. }
  99. $widget['wrapper']['terms_list']['term-' . $term->cvterm_id] = array(
  100. '#type' => 'checkbox',
  101. '#title' => $term->name,
  102. '#default_value' => $default,
  103. '#attributes' => $attrs,
  104. '#description' => '<b>Vocabulary:</b> ' . $term->cv_id->name . ' (' . $term->dbxref_id->db_id->name . ') ' . $term->cv_id->definition .
  105. '<br><b>Term: </b> ' . $term->dbxref_id->db_id->name . ':' . $term->dbxref_id->accession . '. ' .
  106. '<br><b>Definition:</b> ' . $term->definition,
  107. );
  108. $num_terms++;
  109. }
  110. if ($num_terms == 0) {
  111. $widget['wrapper']['terms_list']['none'] = array(
  112. '#type' => 'item',
  113. '#markup' => '<i>' . t('There is no term that matches the entered text.') . '</i>'
  114. );
  115. }
  116. // Add in the button for the cases of no terms or too many.
  117. $widget['wrapper']['submit_button'] = array(
  118. '#type' => 'submit',
  119. '#value' => t('Use this term'),
  120. '#name' => 'kvproperty_adder_button'
  121. );
  122. }
  123. }
  124. /**
  125. * @see TripalField::widgetFormValidate
  126. */
  127. public function widgetFormValidate($entity_type, $entity, $langcode, $items, &$errors) {
  128. // We will never have more than one item for this field at a time, so
  129. // delta is always zero.
  130. $delta = 0;
  131. // Make sure the cvterm for this property is uniquely identified.
  132. $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
  133. $cvterms = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
  134. dpm($kvproperty);
  135. if (trim($kvproperty) && count($cvterms) == 1) {
  136. // Get the table and base table.
  137. $base_table = $this->field['settings']['base_table'];
  138. // Generate the name for the property table and the field name that we'll
  139. // be creating.
  140. $prop_table = $base_table . 'prop';
  141. $field_name = $prop_table . '__' . $cvterms[0]->cvterm_id;
  142. // Create an instance of the field.
  143. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  144. if ($instance) {
  145. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  146. 'error' => 'chado_linker__prop_adder',
  147. 'message' => t("The property already exists."),
  148. );
  149. }
  150. }
  151. if (trim($kvproperty) && count($cvterms) > 1) {
  152. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  153. 'error' => 'chado_linker__prop_adder',
  154. 'message' => t("This term is present in multiple vocabularies. Please select the appropriate one."),
  155. );
  156. }
  157. if (trim($kvproperty) && count($cvterms) == 0) {
  158. $errors[$this->field['field_name']][$langcode][$delta][] = array(
  159. 'error' => 'chado_linker__prop_adder',
  160. 'message' => t("Please provide a property type to add."),
  161. );
  162. }
  163. }
  164. /**
  165. * @see TripalField::widgetFormSubmit()
  166. */
  167. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {dpm($form);return;
  168. // We will never have more than one item for this field at a time, so
  169. // delta is always zero.
  170. $delta = 0;
  171. // Add the new field to the entity but only if the property adder button
  172. // was clicked
  173. if (!array_key_exists('triggering_element', $form_state) or
  174. $form_state['triggering_element']['#name'] != 'kvproperty_adder_button') {
  175. return;
  176. }
  177. // Because we're going to add a new property we want to rebuild the form
  178. // rather than have it fully submit.
  179. $form_state['rebuild'] = TRUE;
  180. // Get the table and base table.
  181. $base_table = $this->field['settings']['base_table'];
  182. // Get the term for the property
  183. $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
  184. $cvterm = chado_generate_var('cvterm', array('name' => $kvproperty));
  185. // Generate the name for the property table and the field name that we'll
  186. // be creating.
  187. $prop_table = $base_table . 'prop';
  188. $field_name = $prop_table . '__' . $cvterm->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($prop_table);
  192. $pkey = $schema['primary key'][0];
  193. // Add the field if it doesn't already exists.
  194. $field = field_info_field($field_name);
  195. if (!$field) {
  196. $field = field_create_field(array(
  197. 'field_name' => $field_name,
  198. 'type' => 'chado_linker__prop',
  199. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  200. 'locked' => FALSE,
  201. 'storage' => array(
  202. 'type' => 'field_chado_storage',
  203. ),
  204. 'settings' => array(
  205. 'chado_table' => $prop_table,
  206. 'chado_column' => $pkey,
  207. 'base_table' => $base_table,
  208. 'semantic_web' => $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession,
  209. ),
  210. ));
  211. }
  212. // Create an instance of the field.
  213. $instance = field_info_instance($entity_type, $field_name, $entity->bundle);
  214. if (!$instance) {
  215. $instance = field_create_instance(array(
  216. 'field_name' => $field_name,
  217. 'entity_type' => 'TripalEntity',
  218. 'bundle' => $entity->bundle,
  219. 'label' => ucfirst(preg_replace('/_/', ' ', $cvterm->name)),
  220. 'description' => $cvterm->definition ? $cvterm->definition : '',
  221. 'required' => FALSE,
  222. 'settings' => array(),
  223. 'widget' => array(
  224. 'type' => $field_name . '_widget',
  225. 'settings' => array(
  226. 'display_label' => 1,
  227. ),
  228. ),
  229. 'display' => array(
  230. 'default' => array(
  231. 'label' => 'inline',
  232. 'type' => $field_name . '_formatter',
  233. 'settings' => array(),
  234. ),
  235. ),
  236. ));
  237. }
  238. }
  239. }
  240. /**
  241. *
  242. */
  243. function tripal_chado_prop_adder_form_ajax_callback($form, $form_state) {
  244. $field_name = $form_state['triggering_element']['#parents'][0];
  245. return $form[$field_name]['und'][0]['wrapper'];
  246. }