schema__alternate_name_widget.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. class schema__alternate_name_widget extends TripalFieldWidget {
  3. // The default lable for this field.
  4. public static $label = 'Synonyms';
  5. // The list of field types for which this formatter is appropriate.
  6. public static $field_types = array('schema__alternate_name');
  7. /**
  8. *
  9. * @see TripalFieldWidget::form()
  10. */
  11. public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  12. parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
  13. $entity = $form['#entity'];
  14. $field_name = $this->field['field_name'];
  15. // Get the FK column that links to the base table.
  16. $table_name = $this->field['settings']['chado_table'];
  17. $base_table = $this->field['settings']['base_table'];
  18. $schema = chado_get_schema($table_name);
  19. $pkey = $schema['primary key'][0];
  20. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  21. $fkey = $fkeys[0];
  22. // Get the field defaults.
  23. $record_id = '';
  24. $fkey_value = $element['#entity']->chado_record_id;
  25. $synonym_id = '';
  26. $pub_id = '';
  27. $is_current = TRUE;
  28. $is_internal = FALSE;
  29. $syn_name = '';
  30. $syn_type = '';
  31. // If the field already has a value then it will come through the $items
  32. // array. This happens when editing an existing record.
  33. if (array_key_exists($delta, $items)) {
  34. $record_id = $items[$delta]['chado-' . $table_name . '__' . $pkey];
  35. $synonym_id = $items[$delta]['chado-' . $table_name . '__synonym_id'];
  36. $pub_id = $items[$delta]['chado-' . $table_name . '__pub_id'];
  37. $is_current = $items[$delta]['chado-' . $table_name . '__is_current'];
  38. $is_internal = $items[$delta]['chado-' . $table_name . '__is_internal'];
  39. $syn_name = $items[$delta]['name'];
  40. $syn_type = $items[$delta]['type_id'];
  41. }
  42. // Check $form_state['values'] to see if an AJAX call set the values.
  43. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  44. $record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__' . $pkey];
  45. $synonym_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__synonym_id'];
  46. $pub_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__pub_id'];
  47. $is_current = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_current'];
  48. $is_internal = $form_state['values'][$field_name]['und'][$delta]['chado-' . $table_name . '__is_internal'];
  49. $syn_name = $form_state['values'][$field_name]['und'][$delta]['name'];
  50. $syn_type = $form_state['values'][$field_name]['und'][$delta]['type_id'];
  51. }
  52. $options = array();
  53. $value = array('cv_id' => array('name' => 'synonym_type'));
  54. $op = array('return_array' => 1);
  55. $types = chado_generate_var('cvterm', $value, $op);
  56. if ($types) {
  57. foreach($types AS $type) {
  58. $options[$type->cvterm_id] = $type->name;
  59. }
  60. }
  61. // Get the schema for the synonym table so we can make sure we limit the
  62. // size of the name field to the proper size.
  63. $schema = chado_get_schema('synonym');
  64. $widget['#table_name'] = $table_name;
  65. $widget['#fkey_field'] = $fkey;
  66. $widget['#theme'] = 'schema__alternate_name_widget';
  67. $widget['#prefix'] = "<span id='$table_name-$delta'>";
  68. $widget['#suffix'] = "</span>";
  69. $widget['value'] = array(
  70. '#type' => 'value',
  71. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  72. );
  73. $widget['chado-' . $table_name . '__' . $pkey] = array(
  74. '#type' => 'value',
  75. '#default_value' => $record_id,
  76. );
  77. $widget['chado-' . $table_name . '__synonym_id'] = array(
  78. '#type' => 'value',
  79. '#default_value' => $synonym_id,
  80. );
  81. $widget['chado-' . $table_name . '__' . $fkey] = array(
  82. '#type' => 'value',
  83. '#default_value' => $fkey_value,
  84. );
  85. // TODO: add a widget for selecting a publication.
  86. $widget['chado-' . $table_name . '__pub_id'] = array(
  87. '#type' => 'value',
  88. '#default_value' => $pub_id,
  89. );
  90. $widget['type_id'] = array(
  91. '#type' => 'select',
  92. '#title' => t('Type'),
  93. '#options' => $options,
  94. '#default_value' => $syn_type,
  95. );
  96. $widget['name'] = array(
  97. '#type' => 'textfield',
  98. '#title' => t('Synonym Name'),
  99. '#default_value' => $syn_name,
  100. '#size' => 25,
  101. );
  102. $widget['chado-' . $table_name . '__is_current'] = array(
  103. '#type' => 'checkbox',
  104. '#title' => t('Is Current'),
  105. '#default_value' => $is_current,
  106. '#required' => $element['#required'],
  107. );
  108. $widget['chado-' . $table_name . '__is_internal'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Is Internal'),
  111. '#default_value' => $is_internal,
  112. '#required' => $element['#required'],
  113. );
  114. }
  115. /**
  116. * Performs validation of the widgetForm.
  117. *
  118. * Use this validate to ensure that form values are entered correctly. Note
  119. * this is different from the validate() function which ensures that the
  120. * field data meets expectations.
  121. *
  122. * @param $form
  123. * @param $form_state
  124. */
  125. public function validate($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  126. }
  127. /**
  128. *
  129. * @see TripalFieldWidget::submit()
  130. */
  131. public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
  132. $field_name = $this->field['field_name'];
  133. $field_type = $this->field['type'];
  134. $table_name = $this->field['settings']['chado_table'];
  135. $field_table = $this->field['settings']['chado_table'];
  136. $field_column = $this->field['settings']['chado_column'];
  137. $base_table = $this->field['settings']['base_table'];
  138. $schema = chado_get_schema($table_name);
  139. $pkey = $schema['primary key'][0];
  140. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  141. $fkey = $fkeys[0];
  142. $record_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $pkey] : '';
  143. $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] : '';
  144. $synonym_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] : '';
  145. $pub_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] : '';
  146. $is_current = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current'] : '';
  147. $is_internal = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal'] : '';
  148. $syn_name = isset($form_state['values'][$field_name][$langcode][$delta]['name']) ? $form_state['values'][$field_name][$langcode][$delta]['name'] : '';
  149. $syn_type = isset($form_state['values'][$field_name][$langcode][$delta]['type_id']) ? $form_state['values'][$field_name][$langcode][$delta]['type_id'] : '';
  150. // If the user provided a $syn_name and a $syn_type then we want to set
  151. // the foreign key value to be the chado_record_id.
  152. if ($syn_name and $syn_type) {
  153. // Get the synonym. If one with the same name and type is already present
  154. // then use that. Otherwise, insert a new one.
  155. if (!$synonym_id) {
  156. $synonym = chado_generate_var('synonym', array('name' => $syn_name, 'type_id' => $syn_type));
  157. if (!$synonym) {
  158. $synonym = chado_insert_record('synonym', array(
  159. 'name' => $syn_name,
  160. 'type_id' => $syn_type,
  161. 'synonym_sgml' => '',
  162. ));
  163. $synonym = (object) $synonym;
  164. }
  165. // Set the synonym_id and FK value
  166. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] = $synonym->synonym_id;
  167. }
  168. if (!$pub_id) {
  169. $pub = chado_generate_var('pub', array('uniquename' => 'null'));
  170. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
  171. }
  172. }
  173. else {
  174. // If the $syn_name is not set, then remove the linker FK value to the base table.
  175. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
  176. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__synonym_id'] = '';
  177. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_internal'] = '';
  178. $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__is_current'] = '';
  179. }
  180. }
  181. }
  182. /**
  183. * Theme function for the synonym widget.
  184. *
  185. * @param $variables
  186. */
  187. function theme_schema__alternate_name_widget($variables) {
  188. $element = $variables['element'];
  189. // These two fields were added to the widget to help identify the fields
  190. // for layout.
  191. $table_name = $element['#table_name'];
  192. $fkey = $element['#fkey_field'];
  193. $layout = "
  194. <div class=\"synonym-widget\">
  195. <div class=\"synonym-widget-item\">" .
  196. drupal_render($element['name']) . "
  197. </div>
  198. <div>" .
  199. drupal_render($element['type_id']) . "
  200. </div>
  201. <div class=\"synonym-widget-item\">" .
  202. drupal_render($element['chado-' . $table_name . '__is_internal']) . "
  203. </div>
  204. <div>" .
  205. drupal_render($element['chado-' . $table_name . '__is_current']) . "
  206. </div>
  207. </div>
  208. ";
  209. return $layout;
  210. }
  211. /**
  212. * An Ajax callback for the synonym widget.
  213. */
  214. function schema__alternate_name_widget_form_ajax_callback($form, $form_state) {
  215. $field_name = $form_state['triggering_element']['#parents'][0];
  216. $delta = $form_state['triggering_element']['#parents'][2];
  217. return $form[$field_name]['und'][$delta];
  218. }