tripal_chado.publish.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 = [];
  14. $bundle_ids[] = 'Select a Content Type';
  15. while ($bundle = $bundles->fetchObject()) {
  16. $bundle_ids[$bundle->name] = $bundle->label;
  17. }
  18. $form['bundle_name'] = [
  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' => [
  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'] = [
  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. // Exclude data fields from the publish form.
  53. // We do this because the lack of default causes errors.
  54. if ($field['type'] == 'datetime'){
  55. continue;
  56. }
  57. // Get the Chado mapping information.
  58. $base_table = array_key_exists('base_table', $instance['settings']) ? $instance['settings']['base_table'] : '';
  59. $chado_table = array_key_exists('chado_table', $instance['settings']) ? $instance['settings']['chado_table'] : '';
  60. $chado_field = array_key_exists('chado_field', $instance['settings']) ? $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 or $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 = array_key_exists('#parents', $form) ? $form['#parents'] : '';
  71. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  72. $element = [
  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. if (!isset($instance['widget']['module'])) {
  88. tripal_report_error(
  89. 'tripal_chado',
  90. TRIPAL_ERROR,
  91. "No module set for field :name widget.",
  92. [':name' => $instance['field_name']]
  93. );
  94. $instance['widget']['module'] = 'tripal';
  95. }
  96. $function = $instance['widget']['module'] . '_field_widget_form';
  97. $items = [];
  98. $delta = 0;
  99. if (function_exists($function)) {
  100. $element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  101. }
  102. // None of these fields are required, so turn off that setting.
  103. $element['#required'] = FALSE;
  104. $form['filters'][$field_name] = [
  105. '#type' => 'container',
  106. '#attributes' => [
  107. 'class' => [
  108. 'field-type-' . drupal_html_class($field['type']),
  109. 'field-name-' . drupal_html_class($field_name),
  110. 'field-widget-' . drupal_html_class($instance['widget']['type']),
  111. ],
  112. ],
  113. '#weight' => $instance['widget']['weight'],
  114. '#tree' => TRUE,
  115. $langcode => [
  116. 0 => $element,
  117. ],
  118. ];
  119. }
  120. }
  121. $form['publish_btn'] = [
  122. '#type' => 'submit',
  123. '#name' => 'publish_btn',
  124. '#value' => 'Publish',
  125. ];
  126. }
  127. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  128. $form['#suffix'] = '</div>';
  129. return $form;
  130. }
  131. /**
  132. * Validate handler for the tripal_chado_publish_form form.
  133. */
  134. function tripal_chado_publish_form_validate($form, &$form_state) {
  135. }
  136. /**
  137. * Submit handler for the tripal_chado_publish_form form.
  138. */
  139. function tripal_chado_publish_form_submit($form, &$form_state) {
  140. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  141. global $user;
  142. $langcode = 'und';
  143. $bundle_name = $form_state['values']['bundle_name'];
  144. $bundle = tripal_load_bundle_entity(['name' => $bundle_name]);
  145. // Iterate through any filters and add those to the arguments
  146. $filters = [];
  147. $fields = field_info_field_map();
  148. foreach ($fields as $field_name => $details) {
  149. if (array_key_exists('TripalEntity', $details['bundles']) and
  150. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  151. array_key_exists($field_name, $form_state['values']) and
  152. array_key_exists('value', $form_state['values'][$field_name][$langcode][0])) {
  153. $value = $form_state['values'][$field_name][$langcode][0]['value'];
  154. if ($value) {
  155. $filters[$field_name] = $value;
  156. }
  157. }
  158. }
  159. $args = [
  160. [
  161. 'bundle_name' => $bundle_name,
  162. 'filters' => $filters,
  163. ],
  164. ];
  165. $includes = [
  166. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  167. ];
  168. return tripal_add_job("Publish " . $bundle->label . " records.",
  169. 'tripal_chado', 'chado_publish_records', $args,
  170. $user->uid, 10, $includes);
  171. }
  172. }
  173. /**
  174. *
  175. */
  176. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  177. return $form;
  178. }