tripal_chado.unpublish_form.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Unpublish orphaned entities form.
  4. *
  5. * @param $form
  6. * @param $form_state
  7. */
  8. function tripal_chado_unpublish_form($form, &$form_state) {
  9. $bundles = tripal_chado_get_orphaned_bundles();
  10. $form['description'] = [
  11. '#markup' => t('<p>Unpublish entities that have no associated records in
  12. their corresponding chado tables</p>')
  13. ];
  14. if (empty($bundles)) {
  15. $form['message'] = [
  16. '#type' => 'markup',
  17. '#markup' => t("<p>No orphaned entities detected.</p>"),
  18. ];
  19. return $form;
  20. }
  21. $form['bundles'] = [
  22. '#title' => t('Select a bundle'),
  23. '#type' => 'select',
  24. '#options' => $bundles,
  25. '#empty_option' => t('-- Select a Bundle --'),
  26. '#description' => t('Select a bundle to check if it has orphaned entities.'),
  27. '#ajax' => [
  28. 'callback' => 'tripal_chado_unpublish_form_callback',
  29. 'wrapper' => 'bundle_info_fieldset_wrapper',
  30. ],
  31. ];
  32. $form['bundle_info_fieldset'] = [
  33. '#type' => 'fieldset',
  34. '#title' => 'Search Results',
  35. '#states' => [
  36. 'invisible' => [
  37. 'select[name="bundles"]' => ['value' => ''],
  38. ],
  39. ],
  40. '#collapsible' => FALSE,
  41. '#prefix' => '<div id="bundle_info_fieldset_wrapper">',
  42. '#suffix' => '</div>',
  43. ];
  44. $selected_bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  45. if ($selected_bundle_id) {
  46. $count = tripal_chado_get_orphaned_counts_for_bundle($selected_bundle_id);
  47. $name = $bundles[$selected_bundle_id];
  48. $form['bundle_info_fieldset']['message'] = [
  49. '#type' => 'markup',
  50. '#markup' => t('<p><strong>There are ' . $count . ' orphaned entities in the ' . $name . ' bundle.</strong></p>'),
  51. ];
  52. if ($count > 0) {
  53. $form['bundle_info_fieldset']['example_table'] = [
  54. '#type' => 'markup',
  55. '#prefix' => t('<p>The following is an example of the records that will get removed. This table contains a maximum of 10 records.</p>'),
  56. '#markup' => tripal_chado_missing_records_table($selected_bundle_id),
  57. ];
  58. $form['bundle_info_fieldset']['submit'] = [
  59. '#type' => 'submit',
  60. '#value' => 'Permanently Delete Orphaned Entities',
  61. ];
  62. }
  63. }
  64. return $form;
  65. }
  66. /**
  67. * Validate form entries.
  68. *
  69. * @param array $form
  70. * @param array $form_state
  71. */
  72. function tripal_chado_unpublish_form_validate($form, &$form_state) {
  73. $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  74. if (empty($bundle_id) || !is_numeric($bundle_id)) {
  75. form_set_error('bundles', t('Please select a valid bundle.'));
  76. }
  77. }
  78. /**
  79. * Process unpublish form.
  80. *
  81. * @param array $form
  82. * @param array $form_state
  83. */
  84. function tripal_chado_unpublish_form_submit($form, &$form_state) {
  85. global $user;
  86. $bundle_id = isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : NULL;
  87. tripal_add_job('Delete Orphaned Entities', 'tripal_chado',
  88. 'tripal_chado_delete_orphaned_entities', [$bundle_id], $user->uid, 10);
  89. drupal_set_message('Job submitted successfully!');
  90. drupal_set_message('WARNING: It is expected to see a few tripal_chado errors
  91. while the job is executing. The errors can be safely ignored.', 'warning');
  92. }
  93. /**
  94. * Get a list of bundles keyed by bundle id.
  95. *
  96. * @return array
  97. */
  98. function tripal_chado_get_orphaned_bundles() {
  99. $bundles = db_query('SELECT bundle_id, data_table, label
  100. FROM {chado_bundle} CB
  101. INNER JOIN {tripal_bundle} TB ON TB.id = CB.bundle_id')->fetchAll();
  102. $mapped = [];
  103. foreach ($bundles as $bundle) {
  104. $mapped[$bundle->bundle_id] = "{$bundle->label}";
  105. }
  106. return $mapped;
  107. }
  108. /**
  109. * Get the count of orphaned entities per bundle.
  110. *
  111. * @param int $bundle
  112. *
  113. * @return int
  114. */
  115. function tripal_chado_get_orphaned_counts_for_bundle($bundle_id) {
  116. // Get the bundle
  117. $bundle = db_query('SELECT bundle_id, data_table, label
  118. FROM {chado_bundle} CB
  119. INNER JOIN {tripal_bundle} TB ON TB.id = CB.bundle_id
  120. WHERE bundle_id = :id', [':id' => $bundle_id])->fetchObject();
  121. $bundle_table = db_escape_table("chado_bio_data_{$bundle->bundle_id}");
  122. $chado_table = db_escape_table($bundle->data_table);
  123. $schema = chado_get_schema($chado_table);
  124. $primary_key = is_array($schema) ? $schema['primary key'][0] : NULL;
  125. $count = 0;
  126. // Get the count
  127. if ($primary_key) {
  128. $count = db_query('SELECT count(*) FROM {' . $bundle_table . '} BT
  129. LEFT JOIN {chado.' . $chado_table . '} CT ON BT.record_id = CT.' . $primary_key . '
  130. WHERE CT.' . $primary_key . ' IS NULL')->fetchField();
  131. }
  132. return (int) $count;
  133. }
  134. /**
  135. * Ajax callback for this form.
  136. *
  137. * @param array $form
  138. *
  139. * @return array
  140. */
  141. function tripal_chado_unpublish_form_callback($form) {
  142. return $form['bundle_info_fieldset'];
  143. }
  144. /**
  145. * Create a table populated with examples of records that would get deleted.
  146. *
  147. * @param $bundle_id
  148. *
  149. * @return string
  150. * @throws \Exception
  151. */
  152. function tripal_chado_missing_records_table($bundle_id) {
  153. // Get the bundle
  154. $bundle = db_query('SELECT bundle_id, data_table, label
  155. FROM {chado_bundle} CB
  156. INNER JOIN {tripal_bundle} TB ON TB.id = CB.bundle_id
  157. WHERE bundle_id = :id', [':id' => $bundle_id])->fetchObject();
  158. $bundle_table = db_escape_table("chado_bio_data_{$bundle->bundle_id}");
  159. $chado_table = db_escape_table($bundle->data_table);
  160. $schema = chado_get_schema($chado_table);
  161. $primary_key = is_array($schema) ? $schema['primary key'][0] : NULL;
  162. $entities = db_query('SELECT TE.title, TE.id, BT.record_id FROM {' . $bundle_table . '} BT
  163. LEFT JOIN {chado.' . $chado_table . '} CT ON BT.record_id = CT.' . $primary_key . '
  164. LEFT JOIN {tripal_entity} TE ON TE.id = BT.entity_id
  165. WHERE CT.' . $primary_key . ' IS NULL
  166. ORDER BY BT.entity_id ASC
  167. LIMIT 10')->fetchAll();
  168. return theme('table', [
  169. 'header' => [
  170. 'Entity ID',
  171. 'Title',
  172. 'Chado Table',
  173. str_replace('_', ' ', $primary_key),
  174. ],
  175. 'rows' => array_map(function ($entity) use ($chado_table) {
  176. return [
  177. $entity->id,
  178. l($entity->title, 'bio_data/' . $entity->id),
  179. $chado_table,
  180. $entity->record_id,
  181. ];
  182. }, $entities),
  183. ]);
  184. }