tripal_chado.publish.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $base_table = $instance['settings']['base_table'];
  54. $chado_table = $instance['settings']['chado_table'];
  55. $chado_field = $instance['settings']['chado_field'];
  56. // For now, only handle filtering by fields that are mapped directly
  57. // to the base table.
  58. if ($base_table != $chado_table) {
  59. continue;
  60. }
  61. // Create a default element for this field. This code
  62. // is adapted from the field_multiple-value_form() function.
  63. $title = check_plain($instance['label']);
  64. $description = field_filter_xss($instance['description']);
  65. $parents = $form['#parents'];
  66. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  67. $element = array(
  68. '#entity_type' => $instance['entity_type'],
  69. '#entity' => NULL,
  70. '#bundle' => $instance['bundle'],
  71. '#field_name' => $field_name,
  72. '#language' => $langcode,
  73. '#field_parents' => $parents,
  74. '#columns' => array_keys($field['columns']),
  75. '#title' => $title,
  76. '#description' => $description,
  77. '#required' => FALSE,
  78. '#delta' => 0,
  79. );
  80. // Now call the widget callback function to let the module adjust the
  81. // element as needed.
  82. $function = $instance['widget']['module'] . '_field_widget_form';
  83. $items = array();
  84. $delta = 0;
  85. $element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  86. // None of these fields are required, so turn off that setting.
  87. $element['#required'] = FALSE;
  88. $form['filters'][$field_name] = array(
  89. '#type' => 'container',
  90. '#attributes' => array(
  91. 'class' => array(
  92. 'field-type-' . drupal_html_class($field['type']),
  93. 'field-name-' . drupal_html_class($field_name),
  94. 'field-widget-' . drupal_html_class($instance['widget']['type']),
  95. ),
  96. ),
  97. '#weight' => $instance['widget']['weight'],
  98. '#tree' => TRUE,
  99. $langcode => array(
  100. 0 => $element
  101. )
  102. );
  103. }
  104. }
  105. $form['publish_btn'] = array(
  106. '#type' => 'submit',
  107. '#name' => 'publish_btn',
  108. '#value' => 'Publish',
  109. );
  110. }
  111. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  112. $form['#suffix'] = '</div>';
  113. return $form;
  114. }
  115. /**
  116. * Validate handler for the tripal_chado_publish_form form.
  117. */
  118. function tripal_chado_publish_form_validate($form, &$form_state) {
  119. }
  120. /**
  121. * Submit handler for the tripal_chado_publish_form form.
  122. */
  123. function tripal_chado_publish_form_submit($form, &$form_state) {
  124. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  125. global $user;
  126. global $language;
  127. $langcode = $language->language;
  128. $bundle_name = $form_state['values']['bundle_name'];
  129. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  130. // Iterate through any filters and add those to the arguments
  131. $filters = array();
  132. $fields = field_info_field_map();
  133. foreach ($fields as $field_name => $details) {
  134. if (array_key_exists('TripalEntity', $details['bundles']) and
  135. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  136. array_key_exists($field_name, $form_state['values']) and
  137. array_key_exists('value', $form_state['values'][$field_name][$langcode][0])) {
  138. $value = $form_state['values'][$field_name][$langcode][0]['value'];
  139. if ($value) {
  140. $filters[$field_name] = $value;
  141. }
  142. }
  143. }
  144. $args = array(
  145. array(
  146. 'bundle_name' => $bundle_name,
  147. 'filters' => $filters,
  148. ),
  149. );
  150. $includes = array(
  151. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  152. );
  153. return tripal_add_job("Publish " . $bundle->label . " records.",
  154. 'tripal_chado', 'tripal_chado_publish_records', $args,
  155. $user->uid, 10, $includes);
  156. }
  157. }
  158. /**
  159. *
  160. */
  161. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  162. return $form;
  163. }