tripal_chado.publish.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. function tripal_chado_publish_form($form, &$form_state) {
  3. global $language;
  4. $langcode = $language->language;
  5. $bundle_name = '';
  6. if (array_key_exists('values', $form_state)) {
  7. $bundle_name = $form_state['values']['bundle_name'];
  8. }
  9. // Build the list of available TripalEntity content types.
  10. $bundles = db_select('tripal_bundle', 'tb')
  11. ->fields('tb')
  12. ->orderBy('label', 'ASC')
  13. ->execute();
  14. $bundle_ids = array();
  15. $bundle_ids[] = 'Select a Content Type';
  16. while ($bundle = $bundles->fetchObject()) {
  17. $bundle_ids[$bundle->name] = $bundle->label;
  18. }
  19. $form['bundle_name'] = array(
  20. '#type' => 'select',
  21. '#title' => 'Content Type',
  22. '#description' => t('Select a content type to publish. Only data that
  23. is mapped to the selected vocabulary term will be published.'),
  24. '#options' => $bundle_ids,
  25. '#default_value' => $bundle_name,
  26. '#ajax' => array(
  27. 'callback' => "tripal_chado_publish_form_ajax_callback",
  28. 'wrapper' => "tripal-chado-publish-form",
  29. 'effect' => 'fade',
  30. 'method' => 'replace'
  31. ),
  32. );
  33. // If the user has selected a content type, then we need to
  34. // show some filters.
  35. if ($bundle_name) {
  36. $form['filters'] = array(
  37. '#type' => 'fieldset',
  38. '#title' => 'Filters',
  39. '#description' => t('Please provide any filters for limiting
  40. the records. Only those that match the filters specified
  41. below will be published. To publish all records of this
  42. type, leave all filters blank.'),
  43. '#collapsed' => TRUE,
  44. '#collapsible' => TRUE,
  45. );
  46. // Get the list of fields and their widgets
  47. $fields = field_info_field_map();
  48. foreach ($fields as $field_name => $details) {
  49. if (array_key_exists('TripalEntity', $details['bundles']) and
  50. in_array($bundle_name, $details['bundles']['TripalEntity'])) {
  51. $field = field_info_field($field_name);
  52. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  53. // Skip fields that aren't Chado fields.
  54. if (!array_key_exists('base_table', $instance['settings'])) {
  55. continue;
  56. }
  57. // Get the Chado mapping information.
  58. $base_table = $instance['settings']['base_table'];
  59. $chado_table = $instance['settings']['chado_table'];
  60. $chado_field = $instance['settings']['chado_field'];
  61. // For now, only handle filtering by fields that are mapped directly
  62. // to the base table.
  63. if ($base_table != $chado_table) {
  64. continue;
  65. }
  66. // Create a default element for this field. This code
  67. // is adapted from the field_multiple-value_form() function.
  68. $title = check_plain($instance['label']);
  69. $description = field_filter_xss($instance['description']);
  70. $parents = $form['#parents'];
  71. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  72. $element = array(
  73. '#entity_type' => $instance['entity_type'],
  74. '#entity' => NULL,
  75. '#bundle' => $instance['bundle'],
  76. '#field_name' => $field_name,
  77. '#language' => $langcode,
  78. '#field_parents' => $parents,
  79. '#columns' => array_keys($field['columns']),
  80. '#title' => $title,
  81. '#description' => $description,
  82. '#required' => FALSE,
  83. '#delta' => 0,
  84. );
  85. // Now call the widget callback function to let the module adjust the
  86. // element as needed.
  87. $function = $instance['widget']['module'] . '_field_widget_form';
  88. $items = array();
  89. $delta = 0;
  90. $element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  91. // None of these fields are required, so turn off that setting.
  92. $element['#required'] = FALSE;
  93. $form['filters'][$field_name] = array(
  94. '#type' => 'container',
  95. '#attributes' => array(
  96. 'class' => array(
  97. 'field-type-' . drupal_html_class($field['type']),
  98. 'field-name-' . drupal_html_class($field_name),
  99. 'field-widget-' . drupal_html_class($instance['widget']['type']),
  100. ),
  101. ),
  102. '#weight' => $instance['widget']['weight'],
  103. '#tree' => TRUE,
  104. $langcode => array(
  105. 0 => $element
  106. )
  107. );
  108. }
  109. }
  110. $form['publish_btn'] = array(
  111. '#type' => 'submit',
  112. '#name' => 'publish_btn',
  113. '#value' => 'Publish',
  114. );
  115. }
  116. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  117. $form['#suffix'] = '</div>';
  118. return $form;
  119. }
  120. /**
  121. * Validate handler for the tripal_chado_publish_form form.
  122. */
  123. function tripal_chado_publish_form_validate($form, &$form_state) {
  124. }
  125. /**
  126. * Submit handler for the tripal_chado_publish_form form.
  127. */
  128. function tripal_chado_publish_form_submit($form, &$form_state) {
  129. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  130. global $user;
  131. global $language;
  132. $langcode = $language->language;
  133. $bundle_name = $form_state['values']['bundle_name'];
  134. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  135. // Iterate through any filters and add those to the arguments
  136. $filters = array();
  137. $fields = field_info_field_map();
  138. foreach ($fields as $field_name => $details) {
  139. if (array_key_exists('TripalEntity', $details['bundles']) and
  140. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  141. array_key_exists($field_name, $form_state['values']) and
  142. array_key_exists('value', $form_state['values'][$field_name][$langcode][0])) {
  143. $value = $form_state['values'][$field_name][$langcode][0]['value'];
  144. if ($value) {
  145. $filters[$field_name] = $value;
  146. }
  147. }
  148. }
  149. $args = array(
  150. array(
  151. 'bundle_name' => $bundle_name,
  152. 'filters' => $filters,
  153. ),
  154. );
  155. $includes = array(
  156. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  157. );
  158. return tripal_add_job("Publish " . $bundle->label . " records.",
  159. 'tripal_chado', 'tripal_chado_publish_records', $args,
  160. $user->uid, 10, $includes);
  161. }
  162. }
  163. /**
  164. *
  165. */
  166. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  167. return $form;
  168. }