tripal.unpublish_orphans.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Unpublish orphaned entities form.
  4. *
  5. * @param $form
  6. * @param $form_state
  7. */
  8. function tripal_unpublish_orphans_form($form, &$form_state) {
  9. // Get the list of bundles.
  10. $bundles = [];
  11. $query = '
  12. SELECT bundle_id, data_table, label
  13. FROM {chado_bundle} CB
  14. INNER JOIN {tripal_bundle} TB ON TB.id = CB.bundle_id
  15. ORDER BY label
  16. ';
  17. $results = db_select('tripal_bundle', 'tb')
  18. ->fields('tb')
  19. ->orderBy('label')
  20. ->execute();
  21. while ($bundle = $results->fetchObject()) {
  22. $bundles[$bundle->id] = $bundle->label;
  23. }
  24. drupal_set_title('Unpublish Orphaned Content');
  25. $form['description'] = [
  26. '#type' => 'markup',
  27. '#markup' => t('Sometimes published content can become orphaned. This can
  28. occur if someone deletes records directly from the underlying data store
  29. yet Tripal is not aware of it. Here, you can unpublish orphaned content
  30. '),
  31. ];
  32. if (empty($bundles)) {
  33. $form['message'] = [
  34. '#type' => 'markup',
  35. '#markup' => t("No orphaned content detected."),
  36. ];
  37. return $form;
  38. }
  39. $form['bundles'] = [
  40. '#title' => t('Content type'),
  41. '#type' => 'select',
  42. '#options' => $bundles,
  43. '#empty_option' => t('-- Select a Content Type --'),
  44. '#description' => t('Select a Tripal content type to find orphaned content.'),
  45. '#ajax' => [
  46. 'callback' => 'tripal_unpublish_orphans_form_callback',
  47. 'wrapper' => 'bundle_info_fieldset_wrapper',
  48. ],
  49. ];
  50. $form['bundle_info_fieldset'] = [
  51. '#type' => 'fieldset',
  52. '#title' => 'Search Results',
  53. '#states' => [
  54. 'invisible' => [
  55. 'select[name="bundles"]' => ['value' => ''],
  56. ],
  57. ],
  58. '#collapsible' => FALSE,
  59. '#prefix' => '<div id="bundle_info_fieldset_wrapper">',
  60. '#suffix' => '</div>',
  61. ];
  62. $selected_bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  63. if ($selected_bundle_id) {
  64. $bundlec = entity_get_controller('TripalBundle');
  65. $count = $bundlec->findOrphans($selected_bundle_id, TRUE);
  66. $name = $bundles[$selected_bundle_id];
  67. $form['bundle_info_fieldset']['message'] = [
  68. '#type' => 'markup',
  69. '#markup' => t('<p><strong>There are ' . $count . ' orphaned entities in the ' . $name . ' bundle.</strong></p>'),
  70. ];
  71. if ($count > 0) {
  72. $form['bundle_info_fieldset']['example_table'] = [
  73. '#type' => 'markup',
  74. '#prefix' => t('The following is a subset of the records that will be unpublished. Only a maximum of 10 records are shown.'),
  75. '#markup' => tripal_chado_missing_records_table($bundlec, $selected_bundle_id),
  76. ];
  77. $form['bundle_info_fieldset']['submit'] = [
  78. '#type' => 'submit',
  79. '#value' => 'Unpublish Orphaned Entities',
  80. ];
  81. }
  82. }
  83. return $form;
  84. }
  85. /**
  86. * Validate form entries.
  87. *
  88. * @param array $form
  89. * @param array $form_state
  90. */
  91. function tripal_unpublish_orphans_form_validate($form, &$form_state) {
  92. $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  93. if (empty($bundle_id) || !is_numeric($bundle_id)) {
  94. form_set_error('bundles', t('Please select a valid bundle.'));
  95. }
  96. }
  97. /**
  98. * Process unpublish form.
  99. *
  100. * @param array $form
  101. * @param array $form_state
  102. */
  103. function tripal_unpublish_orphans_form_submit($form, &$form_state) {
  104. global $user;
  105. $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  106. tripal_add_job('Delete Orphaned Entities', 'tripal_chado',
  107. 'tripal_unpublish_orphans', [$bundle_id], $user->uid, 10, []);
  108. drupal_set_message('Job submitted');
  109. }
  110. /**
  111. * Ajax callback for this form.
  112. *
  113. * @param array $form
  114. *
  115. * @return array
  116. */
  117. function tripal_unpublish_orphans_form_callback($form) {
  118. return $form['bundle_info_fieldset'];
  119. }
  120. /**
  121. * Create a table populated with examples of records that would get deleted.
  122. *
  123. * @param TripalBundleController $bundlec
  124. * @param int $selected_bundle_id
  125. *
  126. * @return string
  127. */
  128. function tripal_chado_missing_records_table(TripalBundleController $bundlec, int $selected_bundle_id) {
  129. $ids = $bundlec->findOrphans($selected_bundle_id, FALSE, 0, 10);
  130. $entities = tripal_load_entity('TripalEntity', $ids);
  131. return theme('table', [
  132. 'header' => [
  133. 'Entity ID',
  134. 'Title'
  135. ],
  136. 'rows' => array_map(function ($entity) {
  137. return [
  138. $entity->id,
  139. l($entity->title, 'bio_data/' . $entity->id),
  140. ];
  141. }, $entities),
  142. ]);
  143. }