tripal_featuremap.admin.inc 14 KB

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