tripal_featuremap.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * Administrative settings form
  4. *
  5. * @ingroup tripal_featuremap
  6. */
  7. function tripal_featuremap_admin() {
  8. $form = array();
  9. // before proceeding check to see if we have any
  10. // currently processing jobs. If so, we don't want
  11. // to give the opportunity to sync maps
  12. $active_jobs = FALSE;
  13. if (tripal_get_module_active_jobs('tripal_featuremap')) {
  14. $active_jobs = TRUE;
  15. }
  16. // add the field set for syncing maps
  17. if (!$active_jobs) {
  18. get_tripal_featuremap_admin_form_sync_set($form);
  19. get_tripal_featuremap_admin_form_cleanup_set($form);
  20. // TODO: complete coding of indexing and taxonomy assignment to features.
  21. // get_tripal_featuremap_admin_form_reindex_set($form);
  22. // get_tripal_featuremap_admin_form_taxonomy_set($form);
  23. }
  24. else {
  25. $form['notice'] = array(
  26. '#type' => 'fieldset',
  27. '#title' => t('Feature Map Management Temporarily Unavailable')
  28. );
  29. $form['notice']['message'] = array(
  30. '#value' => t('Currently, feature map management jobs are waiting or are running. Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
  31. );
  32. }
  33. return system_settings_form($form);
  34. }
  35. /**
  36. *
  37. *
  38. * @ingroup tripal_featuremap
  39. */
  40. function get_tripal_featuremap_admin_form_cleanup_set(&$form) {
  41. $form['cleanup'] = array(
  42. '#type' => 'fieldset',
  43. '#title' => t('Clean Up')
  44. );
  45. $form['cleanup']['description'] = array(
  46. '#type' => 'item',
  47. '#value' => t("With Drupal and chado residing in different databases ".
  48. "it is possible that nodes in Drupal and maps in Chado become ".
  49. "\"orphaned\". This can occur if an map node in Drupal is ".
  50. "deleted but the corresponding chado map is not and/or vice ".
  51. "versa. Click the button below to resolve these discrepancies."),
  52. '#weight' => 1,
  53. );
  54. $form['cleanup']['button'] = array(
  55. '#type' => 'submit',
  56. '#value' => t('Clean up orphaned maps'),
  57. '#weight' => 2,
  58. );
  59. }
  60. /**
  61. *
  62. *
  63. * @ingroup tripal_featuremap
  64. */
  65. function get_tripal_featuremap_admin_form_taxonomy_set(&$form) {
  66. $form['taxonify'] = array(
  67. '#type' => 'fieldset',
  68. '#title' => t('Assign Drupal Taxonomy to Map Features')
  69. );
  70. // get the list of maps
  71. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  72. $lib_rset = chado_query($sql);
  73. // iterate through all of the maps
  74. $lib_boxes = array();
  75. while ($featuremap = db_fetch_object($lib_rset)) {
  76. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  77. }
  78. $form['taxonify']['description'] = array(
  79. '#type' => 'item',
  80. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  81. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  82. "for setting taxonomy only for features that belong to the selected maps below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
  83. '#weight' => 1,
  84. );
  85. $form['taxonify']['tx-maps'] = array(
  86. '#title' => t('Maps'),
  87. '#type' => t('checkboxes'),
  88. '#description' => t("Check the maps whose features you want to reset taxonomy. Note: this list contains all maps, even those that may not be synced."),
  89. '#required' => FALSE,
  90. '#prefix' => '<div id="lib_boxes">',
  91. '#suffix' => '</div>',
  92. '#options' => $lib_boxes,
  93. '#weight' => 2
  94. );
  95. $form['taxonify']['tx-button'] = array(
  96. '#type' => 'submit',
  97. '#value' => t('Set Feature Taxonomy'),
  98. '#weight' => 3
  99. );
  100. }
  101. /**
  102. *
  103. * @ingroup tripal_featuremap
  104. */
  105. function get_tripal_featuremap_admin_form_reindex_set(&$form) {
  106. // define the fieldsets
  107. $form['reindex'] = array(
  108. '#type' => 'fieldset',
  109. '#title' => t('Reindex Map Features')
  110. );
  111. // get the list of maps
  112. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  113. $lib_rset = chado_query($sql);
  114. // iterate through all of the maps
  115. $lib_boxes = array();
  116. while ($featuremap = db_fetch_object($lib_rset)) {
  117. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  118. }
  119. $form['reindex']['description'] = array(
  120. '#type' => 'item',
  121. '#value' => t("This option allows for reindexing of only those features that belong to the selected maps below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
  122. '#weight' => 1,
  123. );
  124. $form['reindex']['re-maps'] = array(
  125. '#title' => t('Maps'),
  126. '#type' => t('checkboxes'),
  127. '#description' => t("Check the maps whoee features you want to reindex. Note: this list contains all maps, even those that may not be synced."),
  128. '#required' => FALSE,
  129. '#prefix' => '<div id="lib_boxes">',
  130. '#suffix' => '</div>',
  131. '#options' => $lib_boxes,
  132. '#weight' => 2,
  133. );
  134. $form['reindex']['re-button'] = array(
  135. '#type' => 'submit',
  136. '#value' => t('Reindex Features'),
  137. '#weight' => 3,
  138. );
  139. }
  140. /**
  141. *
  142. * @ingroup tripal_featuremap
  143. */
  144. function get_tripal_featuremap_admin_form_sync_set(&$form) {
  145. // define the fieldsets
  146. $form['sync'] = array(
  147. '#type' => 'fieldset',
  148. '#title' => t('Sync Maps')
  149. );
  150. // get the list of maps
  151. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  152. $lib_rset = chado_query($sql);
  153. // if we've added any maps to the list that can be synced
  154. // then we want to build the form components to allow the user
  155. // to select one or all of them. Otherwise, just present
  156. // a message stating that all maps are currently synced.
  157. $lib_boxes = array();
  158. $added = 0;
  159. while ($featuremap = db_fetch_object($lib_rset)) {
  160. // check to see if the map is already present as a node in drupal.
  161. // if so, then skip it.
  162. $sql = "SELECT * FROM {chado_featuremap} WHERE featuremap_id = %d";
  163. if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
  164. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  165. $added++;
  166. }
  167. }
  168. // if we have maps we need to add to the checkbox then
  169. // build that form element
  170. if ($added > 0) {
  171. $lib_boxes['all'] = "All Maps";
  172. $form['reindex']['description'] = array(
  173. '#type' => 'item',
  174. '#value' => t("This option allows for the creation of Drupal content for maps in chado. Only the selected maps will be synced."),
  175. '#weight' => 1,
  176. );
  177. $form['sync']['featuremaps'] = array(
  178. '#title' => t('Available Maps'),
  179. '#type' => t('checkboxes'),
  180. '#description' => t("Check the maps you want to sync. Drupal content will be created for each of the maps listed above. Select 'All Maps' to sync all of them."),
  181. '#required' => FALSE,
  182. '#prefix' => '<div id="lib_boxes">',
  183. '#suffix' => '</div>',
  184. '#options' => $lib_boxes,
  185. '#weight' => 2,
  186. );
  187. $form['sync']['button'] = array(
  188. '#type' => 'submit',
  189. '#value' => t('Sync Maps'),
  190. '#weight' => 3,
  191. );
  192. }
  193. // we don't have any maps to select from
  194. else {
  195. $form['sync']['value'] = array(
  196. '#value' => t('All maps in Chado are currently synced with Drupal.')
  197. );
  198. }
  199. }
  200. /**
  201. *
  202. * @ingroup tripal_featuremap
  203. */
  204. function tripal_featuremap_admin_validate($form, &$form_state) {
  205. global $user; // we need access to the user info
  206. $job_args = array();
  207. // Submit the Sync Job if selected
  208. if ($form_state['values']['op'] == t('Sync Maps')) {
  209. // check to see if the user wants to sync chado and drupal. If
  210. // so then we need to register a job to do so with tripal
  211. $featuremaps = $form_state['values']['featuremaps'];
  212. $do_all = FALSE;
  213. $to_sync = array();
  214. foreach ($featuremaps as $featuremap_id) {
  215. if (preg_match("/^all$/i", $featuremap_id)) {
  216. $do_all = TRUE;
  217. }
  218. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  219. // get the map info
  220. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  221. $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
  222. $to_sync[$featuremap_id] = $featuremap->name;
  223. }
  224. }
  225. // submit the job to the tripal job manager
  226. if ($do_all) {
  227. tripal_add_job('Sync all maps', 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
  228. }
  229. else{
  230. foreach ($to_sync as $featuremap_id => $name) {
  231. $job_args[0] = $featuremap_id;
  232. tripal_add_job("Sync map: $name", 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
  233. }
  234. }
  235. }
  236. // -------------------------------------
  237. // Submit the Reindex Job if selected
  238. if ($form_state['values']['op'] == t('Reindex Features')) {
  239. $featuremaps = $form_state['values']['re-maps'];
  240. foreach ($featuremaps as $featuremap_id) {
  241. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  242. // get the map info
  243. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  244. $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
  245. $job_args[0] = $featuremap_id;
  246. tripal_add_job("Reindex features for map: $featuremap->name", 'tripal_featuremap',
  247. 'tripal_featuremap_reindex_features', $job_args, $user->uid);
  248. }
  249. }
  250. }
  251. // -------------------------------------
  252. // Submit the Taxonomy Job if selected
  253. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  254. $featuremaps = $form_state['values']['tx-maps'];
  255. foreach ($featuremaps as $featuremap_id) {
  256. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  257. // get the map info
  258. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  259. $featuremap = db_fetch_object(chado_query($sql, $featuremap_id));
  260. $job_args[0] = $featuremap_id;
  261. tripal_add_job("Set taxonomy for features in map: $featuremap->name", 'tripal_featuremap',
  262. 'tripal_featuremap_taxonify_features', $job_args, $user->uid);
  263. }
  264. }
  265. }
  266. // -------------------------------------
  267. // Submit the Cleanup Job if selected
  268. if ($form_state['values']['op'] == t('Clean up orphaned maps')) {
  269. tripal_add_job('Cleanup orphaned maps', 'tripal_featuremap',
  270. 'tripal_featuremap_cleanup', $job_args, $user->uid);
  271. }
  272. }
  273. /**
  274. *
  275. *
  276. * @ingroup tripal_featuremap
  277. */
  278. function tripal_featuremap_sync_featuremaps($featuremap_id = NULL, $job_id = NULL) {
  279. global $user;
  280. $page_content = '';
  281. // get the list of featuremaps and create new nodes
  282. if (!$featuremap_id) {
  283. $sql = "SELECT * FROM {featuremap} L";
  284. $results = chado_query($sql);
  285. }
  286. else {
  287. $sql = "SELECT * FROM {featuremap} L WHERE featuremap_id = %d";
  288. $results = chado_query($sql, $featuremap_id);
  289. }
  290. // We'll use the following SQL statement for checking if the map
  291. // already exists as a drupal node.
  292. $sql = "SELECT * FROM {chado_featuremap} ".
  293. "WHERE featuremap_id = %d";
  294. while ($featuremap = db_fetch_object($results)) {
  295. // check if this map already exists in the drupal database. if it
  296. // does then skip this map and go to the next one.
  297. if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
  298. $new_node = new stdClass();
  299. $new_node->type = 'chado_featuremap';
  300. $new_node->uid = $user->uid;
  301. $new_node->title = "$featuremap->name";
  302. $new_node->featuremap_id = $featuremap->featuremap_id;
  303. node_validate($new_node);
  304. $errors = form_get_errors();
  305. if (!$errors) {
  306. $node = node_submit($new_node);
  307. node_save($node);
  308. if ($node->nid) {
  309. print "Added " . $featuremap->name . "\n";
  310. }
  311. else {
  312. print "ERROR: Unable to create " . $featuremap->name . "\n";
  313. }
  314. }
  315. else {
  316. print "ERROR: Unable to create " . $featuremap->name . "\n" . print_r($errors, TRUE) . "\n";
  317. }
  318. }
  319. else {
  320. print "Skipped " . $featuremap->name . "\n";
  321. }
  322. }
  323. return $page_content;
  324. }
  325. /**
  326. * Remove orphaned drupal nodes
  327. *
  328. * @param $dummy
  329. * Not Used -kept for backwards compatibility
  330. * @param $job_id
  331. * The id of the tripal job executing this function
  332. *
  333. * @ingroup tripal_featuremap
  334. */
  335. function tripal_featuremap_cleanup($dummy = NULL, $job_id = NULL) {
  336. return tripal_core_clean_orphaned_nodes('featuremap', $job_id);
  337. }
  338. /**
  339. * Add the map as a taxonomy term for associating with map_features
  340. *
  341. * @ingroup tripal_featuremap
  342. */
  343. function tripal_featuremap_add_taxonomy($node, $featuremap_id) {
  344. }