schema__alternate_name_widget.inc

File

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