tripal_chado.publish.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. function tripal_chado_publish_form($form, &$form_state) {
  3. $langcode = 'und';
  4. $bundle_name = '';
  5. if (array_key_exists('values', $form_state)) {
  6. $bundle_name = $form_state['values']['bundle_name'];
  7. }
  8. // Build the list of available TripalEntity content types.
  9. $bundles = db_select('tripal_bundle', 'tb')
  10. ->fields('tb')
  11. ->orderBy('label', 'ASC')
  12. ->execute();
  13. $bundle_ids = array();
  14. $bundle_ids[] = 'Select a Content Type';
  15. while ($bundle = $bundles->fetchObject()) {
  16. $bundle_ids[$bundle->name] = $bundle->label;
  17. }
  18. $form['bundle_name'] = array(
  19. '#type' => 'select',
  20. '#title' => 'Content Type',
  21. '#description' => t('Select a content type to publish. Only data that
  22. is mapped to the selected vocabulary term will be published.'),
  23. '#options' => $bundle_ids,
  24. '#default_value' => $bundle_name,
  25. '#ajax' => array(
  26. 'callback' => "tripal_chado_publish_form_ajax_callback",
  27. 'wrapper' => "tripal-chado-publish-form",
  28. 'effect' => 'fade',
  29. 'method' => 'replace'
  30. ),
  31. );
  32. // If the user has selected a content type, then we need to
  33. // show some filters.
  34. if ($bundle_name) {
  35. $form['filters'] = array(
  36. '#type' => 'fieldset',
  37. '#title' => 'Filters',
  38. '#description' => t('Please provide any filters for limiting
  39. the records. Only those that match the filters specified
  40. below will be published. To publish all records of this
  41. type, leave all filters blank.'),
  42. '#collapsed' => TRUE,
  43. '#collapsible' => TRUE,
  44. );
  45. // Get the list of fields and their widgets
  46. $fields = field_info_field_map();
  47. foreach ($fields as $field_name => $details) {
  48. if (array_key_exists('TripalEntity', $details['bundles']) and
  49. in_array($bundle_name, $details['bundles']['TripalEntity'])) {
  50. $field = field_info_field($field_name);
  51. $instance = field_info_instance('TripalEntity', $field_name, $bundle_name);
  52. // Get the Chado mapping information.
  53. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  54. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  55. $chado_field = array_key_exists('chado_field', $instance['settings']) ? $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 or $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 = array_key_exists('#parents', $form) ? $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. $langcode = 'und';
  127. $bundle_name = $form_state['values']['bundle_name'];
  128. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  129. // Iterate through any filters and add those to the arguments
  130. $filters = array();
  131. $fields = field_info_field_map();
  132. foreach ($fields as $field_name => $details) {
  133. if (array_key_exists('TripalEntity', $details['bundles']) and
  134. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  135. array_key_exists($field_name, $form_state['values']) and
  136. array_key_exists('value', $form_state['values'][$field_name][$langcode][0])) {
  137. $value = $form_state['values'][$field_name][$langcode][0]['value'];
  138. if ($value) {
  139. $filters[$field_name] = $value;
  140. }
  141. }
  142. }
  143. $args = array(
  144. array(
  145. 'bundle_name' => $bundle_name,
  146. 'filters' => $filters,
  147. ),
  148. );
  149. $includes = array(
  150. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  151. );
  152. return tripal_add_job("Publish " . $bundle->label . " records.",
  153. 'tripal_chado', 'chado_publish_records', $args,
  154. $user->uid, 10, $includes);
  155. }
  156. }
  157. /**
  158. *
  159. */
  160. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  161. return $form;
  162. }