tripal_chado.publish.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. $element = [];
  100. if (function_exists($function)) {
  101. $element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  102. }
  103. // None of these fields are required, so turn off that setting.
  104. $element['#required'] = FALSE;
  105. $form['filters'][$field_name] = [
  106. '#type' => 'container',
  107. '#attributes' => [
  108. 'class' => [
  109. 'field-type-' . drupal_html_class($field['type']),
  110. 'field-name-' . drupal_html_class($field_name),
  111. 'field-widget-' . drupal_html_class($instance['widget']['type']),
  112. ],
  113. ],
  114. '#weight' => $instance['widget']['weight'],
  115. '#tree' => TRUE,
  116. $langcode => [
  117. 0 => $element,
  118. ],
  119. ];
  120. }
  121. }
  122. $form['publish_btn'] = [
  123. '#type' => 'submit',
  124. '#name' => 'publish_btn',
  125. '#value' => 'Publish',
  126. ];
  127. }
  128. $form['#prefix'] = '<div id="tripal-chado-publish-form">';
  129. $form['#suffix'] = '</div>';
  130. return $form;
  131. }
  132. /**
  133. * Validate handler for the tripal_chado_publish_form form.
  134. */
  135. function tripal_chado_publish_form_validate($form, &$form_state) {
  136. }
  137. /**
  138. * Submit handler for the tripal_chado_publish_form form.
  139. */
  140. function tripal_chado_publish_form_submit($form, &$form_state) {
  141. if ($form_state['clicked_button']['#name'] == 'publish_btn') {
  142. global $user;
  143. $langcode = 'und';
  144. $bundle_name = $form_state['values']['bundle_name'];
  145. $bundle = tripal_load_bundle_entity(['name' => $bundle_name]);
  146. // Iterate through any filters and add those to the arguments
  147. $filters = [];
  148. $fields = field_info_field_map();
  149. foreach ($fields as $field_name => $details) {
  150. if (array_key_exists('TripalEntity', $details['bundles']) and
  151. in_array($bundle_name, $details['bundles']['TripalEntity']) and
  152. array_key_exists($field_name, $form_state['values']) and
  153. array_key_exists('value', $form_state['values'][$field_name][$langcode][0])) {
  154. $value = $form_state['values'][$field_name][$langcode][0]['value'];
  155. if ($value) {
  156. $filters[$field_name] = $value;
  157. }
  158. }
  159. }
  160. $args = [
  161. [
  162. 'bundle_name' => $bundle_name,
  163. 'filters' => $filters,
  164. ],
  165. ];
  166. $includes = [
  167. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.publish'),
  168. ];
  169. return tripal_add_job("Publish " . $bundle->label . " records.",
  170. 'tripal_chado', 'chado_publish_records', $args,
  171. $user->uid, 10, $includes);
  172. }
  173. }
  174. /**
  175. *
  176. */
  177. function tripal_chado_publish_form_ajax_callback($form, $form_state) {
  178. return $form;
  179. }