tripal_featuremap.module 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. <?php
  2. /**
  3. * @defgroup tripal_featuremap Map
  4. * @{
  5. * Provides functions for managing chado maps including creating details pages for each map
  6. * @}
  7. * @ingroup tripal_modules
  8. */
  9. require('api/tripal_featuremap.api.inc');
  10. /**
  11. * Display help and module information
  12. * @param path which path of the site we're displaying help
  13. * @param arg array that holds the current path as would be returned from arg()
  14. * function
  15. * @return help text for the path
  16. *
  17. * @ingroup tripal_featuremap
  18. */
  19. function tripal_featuremap_help($path, $arg) {
  20. $output = '';
  21. switch ($path) {
  22. case "admin/help#tripal_featuremap":
  23. $output = '<p>'.
  24. t("Displays links to nodes created on this date") .
  25. '</p>';
  26. break;
  27. }
  28. return $output;
  29. }
  30. /**
  31. * Provide information to drupal about the node types that we're creating
  32. * in this module
  33. *
  34. * @ingroup tripal_featuremap
  35. */
  36. function tripal_featuremap_node_info() {
  37. $nodes = array();
  38. $nodes['chado_featuremap'] = array(
  39. 'name' => t('Map'),
  40. 'module' => 'chado_featuremap',
  41. 'description' => t('A feature map from the chado database (e.g. genetic map)'),
  42. 'has_title' => FALSE,
  43. 'title_label' => t('Feature Map'),
  44. 'has_body' => FALSE,
  45. 'body_label' => t('Feature Map Description'),
  46. 'locked' => TRUE
  47. );
  48. return $nodes;
  49. }
  50. /**
  51. * Set the permission types that the chado module uses. Essentially we
  52. * want permissionis that protect creation, editing and deleting of chado
  53. * data objects
  54. *
  55. * @ingroup tripal_featuremap
  56. */
  57. function tripal_featuremap_perm() {
  58. return array(
  59. 'access chado_featuremap content',
  60. 'create chado_featuremap content',
  61. 'delete chado_featuremap content',
  62. 'edit chado_featuremap content',
  63. );
  64. }
  65. /**
  66. * Set the permission types that the module uses.
  67. *
  68. * @ingroup tripal_featuremap
  69. */
  70. function chado_featuremap_access($op, $node, $account) {
  71. if ($op == 'create') {
  72. if (!user_access('create chado_featuremap content', $account)) {
  73. return FALSE;
  74. }
  75. }
  76. if ($op == 'update') {
  77. if (!user_access('edit chado_featuremap content', $account)) {
  78. return FALSE;
  79. }
  80. }
  81. if ($op == 'delete') {
  82. if (!user_access('delete chado_featuremap content', $account)) {
  83. return FALSE;
  84. }
  85. }
  86. if ($op == 'view') {
  87. if (!user_access('access chado_featuremap content', $account)) {
  88. return FALSE;
  89. }
  90. }
  91. return NULL;
  92. }
  93. /**
  94. * Menu items are automatically added for the new node types created
  95. * by this module to the 'Create Content' Navigation menu item. This function
  96. * adds more menu items needed for this module.
  97. *
  98. * @ingroup tripal_featuremap
  99. */
  100. function tripal_featuremap_menu() {
  101. $items = array();
  102. // The administative settings menu
  103. $items['admin/tripal/tripal_featuremap'] = array(
  104. 'title' => 'Maps',
  105. 'description' => 'Basic Description of Tripal Map Module Functionality',
  106. 'page callback' => 'tripal_featuremap_module_description_page',
  107. 'access arguments' => array('access administration pages'),
  108. 'type' => MENU_NORMAL_ITEM,
  109. );
  110. $items['admin/tripal/tripal_featuremap/configuration'] = array(
  111. 'title' => 'Configuration',
  112. 'description' => 'Manage integration of Chado maps including associated features.',
  113. 'page callback' => 'drupal_get_form',
  114. 'page arguments' => array('tripal_featuremap_admin'),
  115. 'access arguments' => array('access administration pages'),
  116. 'type' => MENU_NORMAL_ITEM,
  117. );
  118. // Synchronizing maps from Chado to Drupal
  119. $items['chado_sync_featuremaps'] = array(
  120. 'title' => 'Sync Data',
  121. 'page callback' => 'tripal_featuremap_sync_featuremaps',
  122. 'access arguments' => array('access administration pages'),
  123. 'type' => MENU_CALLBACK
  124. );
  125. // Displaying maps
  126. $items['featuremaps'] = array(
  127. 'menu_name' => ('primary-links'), //Enable the 'Map' primary link
  128. 'title' => 'Feature Maps',
  129. 'page callback' => 'tripal_featuremap_show_maps',
  130. 'access arguments' => array('access chado_featuremap content'),
  131. 'type' => MENU_NORMAL_ITEM
  132. );
  133. return $items;
  134. }
  135. /**
  136. * Implements hook_views_api()
  137. * Purpose: Essentially this hook tells drupal that there is views support for
  138. * for this module which then includes tripal_db.views.inc where all the
  139. * views integration code is
  140. *
  141. * @ingroup tripal_featuremap
  142. */
  143. function tripal_featuremap_views_api() {
  144. return array(
  145. 'api' => 2.0,
  146. );
  147. }
  148. /**
  149. * Purpose: Provide Guidance to new Tripal Admin
  150. *
  151. * @return HTML Formatted text
  152. *
  153. * @ingroup tripal_featuremap
  154. */
  155. function tripal_featuremap_module_description_page() {
  156. $text = '';
  157. $text .= '<h3>Tripal Feature Map Administrative Tools Quick Links:</h3>';
  158. $text .= "<ul>
  159. <li><a href=\"" . url("admin/tripal/tripal_featuremap/configuration") . "\">Map Configuration</a></li>
  160. </ul>";
  161. $text .= '<h3>Module Description:</h3>';
  162. $text .= '<p>The Tripal Map module is an interface for the Chado Map module which groups features (sequences) into genetic maps.
  163. This module provides support for visualization of "map" pages, editing and updating.</p>';
  164. $text .= '<h3>Setup Instructions:</h3>';
  165. $text .= '<ol>';
  166. $text .= '<li><p><b>Set Permissions</b>: The map module supports the Drupal user permissions interface for
  167. controlling access to map content and functions. These permissions include viewing,
  168. creating, editing or administering of
  169. map content. The default is that only the original site administrator has these
  170. permissions. You can <a href="' . url('admin/user/roles') . '">add roles</a> for classifying users,
  171. <a href="' . url('admin/user/user') . '">assign users to roles</a> and
  172. <a href="' . url('admin/user/permissions') . '">assign permissions</a> for the map content to
  173. those roles. For a simple setup, allow anonymous users access to view organism content and
  174. allow the site administrator all other permissions.</p></li>';
  175. $text .= '<li><p><b>Sync any Existing Maps</b>: Near the top of the ' . l('Map Configuration page', 'admin/tripal/tripal_featuremap/configuration') . ' there is
  176. a Sync Maps section which provides list of maps currently in chado which can be sync\'d.
  177. Simply select the maps you would like to create Drupal/Tripal pages for and click Sync Maps.</p></li>';
  178. $text .= '</ol>';
  179. $text .= '<h3>Features of this Module:</h3>';
  180. $text .= '<ul>';
  181. $text .= '<li><b>Add/Edit/Delete Maps</b>: Maps with no associated features can be created ' . l('here', 'node/add/chado-map') . ' but it is
  182. recommended to create the map using the feature loader. For example, when you load FASTA files using the Tripal loader you are
  183. given the option of specifying a map for all created features. Existing Maps (regardless of the method used to create them) can be
  184. edited or deleted by clicking the Edit tab at the top of the Map Page.</li>';
  185. $text .= '<li><p><b>Integration with Drupal Views</b>: <a href="http://drupal.org/project/views">Drupal Views</a> is
  186. a powerful tool that allows the site administrator to create lists or basic searching forms of Chado content.
  187. It provides a graphical interface within Drupal to allow the site admin to directly query the Chado database
  188. and create custom lists without PHP programming or customization of Tripal source code. Views can also
  189. be created to filter content that has not yet been synced with Druapl in order to protect access to non
  190. published data (only works if Chado was installed using Tripal). You can see a list of available pre-existing
  191. Views <a href="' . url('admin/build/views/') . '">here</a>, as well as create your own. </p></li>';
  192. $text .= '<li><b>Basic Listing</b>: This module provides a basic <a href="' . url('maps') . '">map display
  193. tool</a> for finding or listing maps in Chado. It does not require indexing for Drupal searching but relies
  194. on Drupal Views. <a href="http://drupal.org/project/views">Drupal Views</a> must be installed.</li>';
  195. $text .= '</ul>';
  196. $text .= '<h3>Page Customizations</h3>';
  197. $text .= '<p>There are several ways to customize the look-and-feel for the way Chado data is presented through Tripal.
  198. Below is a description of several methods. These methods may be used in conjunction with one another to
  199. provide fine-grained control.
  200. <ul>
  201. <li><p><b>Integration with Drupal Panels</b>: <a href="http://drupal.org/project/views">Drupal Panels</a>
  202. allows for customization of a page layout if you don\'t want to do PHP/Javascript/CSS programming. Tripal comes with pre-set layouts for map pages. However,
  203. Panels become useful if you prefer a layout that is different from the pre-set layouts. Chado content
  204. is provided to Panels in the form of Drupal "blocks" which you can then place anywhere on a page using the
  205. Panel\'s GUI.</p></li>
  206. <li><p><b>Drupal\'s Content Construction Kit (CCK)</b>: the
  207. <a href="http://drupal.org/project/cck">Content Construction Kit (CCK) </a> is a powerful way to add non-Chado content
  208. to any page without need to edit template files or knowing PHP. You must first download and install CCK.
  209. With CCK, the site administartor can create a new field to appear on the page. For example, currently,
  210. the Chado publication module is not yet supported by Tripal. Therefore, the site administrator can add a text
  211. field to the map pages. This content is not stored in Chado, but will appear on the map page. A field
  212. added by CCK will also appear in the form when editing a map to allow users to manually enter the appropriate
  213. text. If the default pre-set layout and themeing for Tripal is used, it is better to create the CCK element,
  214. indicate that it is not to be shown (using the CCK interface), then manually add the new content type
  215. where desired by editing the templates (as described below). If using Panels, the CCK field can be added to the
  216. location desired using the Panels interface.</p></li>
  217. <li><p><b>Drupal Node Templates</b>: The Tripal packages comes with a "theme_tripal" directory that contains the
  218. themeing for Chado content. The map module has a template file for map "nodes" (Tripal map pages). This file
  219. is named "node-chado_featuremap.tpl.php", and provides javascript, HTML and PHP code for display of the map
  220. pages. You can edit this file to control which types of information (or which map "blocks") are displayed for maps. Be sure to
  221. copy these template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
  222. future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
  223. for instructions on how to access variables and other Chado content within the template file.</p></li>
  224. <li><p><b>Map "Block" Templates</b>: In the "theme_tripal" directory is a subdirectory named "tripal_featuremap".
  225. Inside this directory is a set of templates that control distinct types of information for maps. For example,
  226. there is a "base" template for displaying of data directly from the Chado map table. These templates are used both by Drupal blocks
  227. for use in Drupal Panels (as described above) or for use in the default pre-set layout that the node template
  228. provides (also desribed above). You can customize this template as you desire. Be sure to copy the
  229. template to your primary theme directory for editing. Do not edit them in the "theme_tripal" directory as
  230. future Tripal updates may overwrite your customizations. See the <a href="http://tripal.sourceforge.net/">Tripal website </a>
  231. for instructions on how to access variables and other Chado content within the template files.</p></li>
  232. </li>
  233. <li><p><b>Adding Links to the "Resources" Sidebar</b>: If you use the pre-set default Tripal layout for theming, you
  234. will see a "Resources" sidebar on each page. The links that appear on the sidebar are automatically generated
  235. using Javascript for all of the map "Blocks" that appear on the page. If you want to add additional links
  236. (e.g. a link to a views table showing all features of the current map) and you want that link to appear in the
  237. "Resources" sidebar, simply edit the Drupal Node Template (as described above) and add the link to the
  238. section at the bottom of the template file where the resources section is found.</p></li>
  239. </ul>
  240. </p>';
  241. return $text;
  242. }
  243. /**
  244. * Administrative settings form
  245. *
  246. * @ingroup tripal_featuremap
  247. */
  248. function tripal_featuremap_admin() {
  249. $form = array();
  250. // before proceeding check to see if we have any
  251. // currently processing jobs. If so, we don't want
  252. // to give the opportunity to sync maps
  253. $active_jobs = FALSE;
  254. if (tripal_get_module_active_jobs('tripal_featuremap')) {
  255. $active_jobs = TRUE;
  256. }
  257. // add the field set for syncing maps
  258. if (!$active_jobs) {
  259. get_tripal_featuremap_admin_form_sync_set($form);
  260. get_tripal_featuremap_admin_form_cleanup_set($form);
  261. get_tripal_featuremap_admin_form_reindex_set($form);
  262. get_tripal_featuremap_admin_form_taxonomy_set($form);
  263. get_tripal_featuremap_admin_form_cleanup_set($form);
  264. }
  265. else {
  266. $form['notice'] = array(
  267. '#type' => 'fieldset',
  268. '#title' => t('Feature Map Management Temporarily Unavailable')
  269. );
  270. $form['notice']['message'] = array(
  271. '#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.'),
  272. );
  273. }
  274. return system_settings_form($form);
  275. }
  276. /**
  277. * Implementation of hook_nodeapi().
  278. * Display map information for associated features or organisms
  279. * This function also provides contents for indexing
  280. *
  281. * @ingroup tripal_featuremap
  282. */
  283. function tripal_featuremap_nodeapi(&$node, $op, $teaser, $page) {
  284. switch ($op) {
  285. // Note that this function only adds map view to an organism/feature
  286. // node.
  287. case 'view':
  288. // add the map to the organism/feature search indexing
  289. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  290. $node->content['tripal_featuremap_index_version'] = array(
  291. '#value' => theme('tripal_featuremap_search_index', $node),
  292. );
  293. }
  294. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  295. $node->content['tripal_featuremap_index_version'] = array(
  296. '#value' => theme('tripal_featuremap_search_result', $node),
  297. );
  298. }
  299. }
  300. }
  301. /**
  302. * We need to let drupal know about our theme functions and their arguments.
  303. * We create theme functions to allow users of the module to customize the
  304. * look and feel of the output generated in this module
  305. *
  306. * @ingroup tripal_featuremap
  307. */
  308. function tripal_featuremap_theme() {
  309. return array(
  310. 'tripal_featuremap_search_index' => array(
  311. 'arguments' => array('node'),
  312. ),
  313. 'tripal_featuremap_search_result' => array(
  314. 'arguments' => array('node'),
  315. ),
  316. 'tripal_featuremap_base' => array(
  317. 'arguments' => array('node' => NULL),
  318. 'template' => 'tripal_featuremap_base',
  319. ),
  320. 'tripal_featuremap_properties' => array(
  321. 'arguments' => array('node' => NULL),
  322. 'template' => 'tripal_featuremap_properties',
  323. ),
  324. );
  325. }
  326. /**
  327. * This function is an extension of the chado_feature_view and
  328. * chado_organism_view by providing the markup for the map object
  329. * THAT WILL BE INDEXED.
  330. *
  331. * @ingroup tripal_featuremap
  332. */
  333. function theme_tripal_featuremap_search_index($node) {
  334. }
  335. /**
  336. *
  337. *
  338. * @ingroup tripal_featuremap
  339. */
  340. function get_tripal_featuremap_admin_form_cleanup_set(&$form) {
  341. $form['cleanup'] = array(
  342. '#type' => 'fieldset',
  343. '#title' => t('Clean Up')
  344. );
  345. $form['cleanup']['description'] = array(
  346. '#type' => 'item',
  347. '#value' => t("With Drupal and chado residing in different databases ".
  348. "it is possible that nodes in Drupal and maps in Chado become ".
  349. "\"orphaned\". This can occur if an map node in Drupal is ".
  350. "deleted but the corresponding chado map is not and/or vice ".
  351. "versa. Click the button below to resolve these discrepancies."),
  352. '#weight' => 1,
  353. );
  354. $form['cleanup']['button'] = array(
  355. '#type' => 'submit',
  356. '#value' => t('Clean up orphaned maps'),
  357. '#weight' => 2,
  358. );
  359. }
  360. /**
  361. *
  362. *
  363. * @ingroup tripal_featuremap
  364. */
  365. function get_tripal_featuremap_admin_form_taxonomy_set(&$form) {
  366. $form['taxonify'] = array(
  367. '#type' => 'fieldset',
  368. '#title' => t('Assign Drupal Taxonomy to Map Features')
  369. );
  370. // get the list of maps
  371. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  372. $previous_db = tripal_db_set_active('chado'); // use chado database
  373. $lib_rset = db_query($sql);
  374. tripal_db_set_active($previous_db); // now use drupal database
  375. // iterate through all of the maps
  376. $lib_boxes = array();
  377. while ($featuremap = db_fetch_object($lib_rset)) {
  378. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  379. }
  380. $form['taxonify']['description'] = array(
  381. '#type' => 'item',
  382. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  383. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  384. "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."),
  385. '#weight' => 1,
  386. );
  387. $form['taxonify']['tx-maps'] = array(
  388. '#title' => t('Maps'),
  389. '#type' => t('checkboxes'),
  390. '#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."),
  391. '#required' => FALSE,
  392. '#prefix' => '<div id="lib_boxes">',
  393. '#suffix' => '</div>',
  394. '#options' => $lib_boxes,
  395. '#weight' => 2
  396. );
  397. $form['taxonify']['tx-button'] = array(
  398. '#type' => 'submit',
  399. '#value' => t('Set Feature Taxonomy'),
  400. '#weight' => 3
  401. );
  402. }
  403. /**
  404. *
  405. * @ingroup tripal_featuremap
  406. */
  407. function get_tripal_featuremap_admin_form_reindex_set(&$form) {
  408. // define the fieldsets
  409. $form['reindex'] = array(
  410. '#type' => 'fieldset',
  411. '#title' => t('Reindex Map Features')
  412. );
  413. // get the list of maps
  414. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  415. $previous_db = tripal_db_set_active('chado'); // use chado database
  416. $lib_rset = db_query($sql);
  417. tripal_db_set_active($previous_db); // now use drupal database
  418. // iterate through all of the maps
  419. $lib_boxes = array();
  420. while ($featuremap = db_fetch_object($lib_rset)) {
  421. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  422. }
  423. $form['reindex']['description'] = array(
  424. '#type' => 'item',
  425. '#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."),
  426. '#weight' => 1,
  427. );
  428. $form['reindex']['re-maps'] = array(
  429. '#title' => t('Maps'),
  430. '#type' => t('checkboxes'),
  431. '#description' => t("Check the maps whoee features you want to reindex. Note: this list contains all maps, even those that may not be synced."),
  432. '#required' => FALSE,
  433. '#prefix' => '<div id="lib_boxes">',
  434. '#suffix' => '</div>',
  435. '#options' => $lib_boxes,
  436. '#weight' => 2,
  437. );
  438. $form['reindex']['re-button'] = array(
  439. '#type' => 'submit',
  440. '#value' => t('Reindex Features'),
  441. '#weight' => 3,
  442. );
  443. }
  444. /**
  445. *
  446. * @ingroup tripal_featuremap
  447. */
  448. function get_tripal_featuremap_admin_form_sync_set(&$form) {
  449. // define the fieldsets
  450. $form['sync'] = array(
  451. '#type' => 'fieldset',
  452. '#title' => t('Sync Maps')
  453. );
  454. // get the list of maps
  455. $sql = "SELECT * FROM {featuremap} ORDER BY name";
  456. $previous_db = tripal_db_set_active('chado'); // use chado database
  457. $lib_rset = db_query($sql);
  458. tripal_db_set_active($previous_db); // now use drupal database
  459. // if we've added any maps to the list that can be synced
  460. // then we want to build the form components to allow the user
  461. // to select one or all of them. Otherwise, just present
  462. // a message stating that all maps are currently synced.
  463. $lib_boxes = array();
  464. $added = 0;
  465. while ($featuremap = db_fetch_object($lib_rset)) {
  466. // check to see if the map is already present as a node in drupal.
  467. // if so, then skip it.
  468. $sql = "SELECT * FROM {chado_featuremap} WHERE featuremap_id = %d";
  469. if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
  470. $lib_boxes[$featuremap->featuremap_id] = "$featuremap->name";
  471. $added++;
  472. }
  473. }
  474. // if we have maps we need to add to the checkbox then
  475. // build that form element
  476. if ($added > 0) {
  477. $lib_boxes['all'] = "All Maps";
  478. $form['reindex']['description'] = array(
  479. '#type' => 'item',
  480. '#value' => t("This option allows for the creation of Drupal content for maps in chado. Only the selected maps will be synced."),
  481. '#weight' => 1,
  482. );
  483. $form['sync']['featuremaps'] = array(
  484. '#title' => t('Available Maps'),
  485. '#type' => t('checkboxes'),
  486. '#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."),
  487. '#required' => FALSE,
  488. '#prefix' => '<div id="lib_boxes">',
  489. '#suffix' => '</div>',
  490. '#options' => $lib_boxes,
  491. '#weight' => 2,
  492. );
  493. $form['sync']['button'] = array(
  494. '#type' => 'submit',
  495. '#value' => t('Sync Maps'),
  496. '#weight' => 3,
  497. );
  498. }
  499. // we don't have any maps to select from
  500. else {
  501. $form['sync']['value'] = array(
  502. '#value' => t('All maps in Chado are currently synced with Drupal.')
  503. );
  504. }
  505. }
  506. /**
  507. *
  508. * @ingroup tripal_featuremap
  509. */
  510. function tripal_featuremap_admin_validate($form, &$form_state) {
  511. global $user; // we need access to the user info
  512. $job_args = array();
  513. // Submit the Sync Job if selected
  514. if ($form_state['values']['op'] == t('Sync Maps')) {
  515. // check to see if the user wants to sync chado and drupal. If
  516. // so then we need to register a job to do so with tripal
  517. $featuremaps = $form_state['values']['featuremaps'];
  518. $do_all = FALSE;
  519. $to_sync = array();
  520. foreach ($featuremaps as $featuremap_id) {
  521. if (preg_match("/^all$/i", $featuremap_id)) {
  522. $do_all = TRUE;
  523. }
  524. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  525. // get the map info
  526. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  527. $previous_db = tripal_db_set_active('chado'); // use chado database
  528. $featuremap = db_fetch_object(db_query($sql, $featuremap_id));
  529. tripal_db_set_active($previous_db); // now use drupal database
  530. $to_sync[$featuremap_id] = $featuremap->name;
  531. }
  532. }
  533. // submit the job to the tripal job manager
  534. if ($do_all) {
  535. tripal_add_job('Sync all maps', 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
  536. }
  537. else{
  538. foreach ($to_sync as $featuremap_id => $name) {
  539. $job_args[0] = $featuremap_id;
  540. tripal_add_job("Sync map: $name", 'tripal_featuremap', 'tripal_featuremap_sync_featuremaps', $job_args, $user->uid);
  541. }
  542. }
  543. }
  544. // -------------------------------------
  545. // Submit the Reindex Job if selected
  546. if ($form_state['values']['op'] == t('Reindex Features')) {
  547. $featuremaps = $form_state['values']['re-maps'];
  548. foreach ($featuremaps as $featuremap_id) {
  549. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  550. // get the map info
  551. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  552. $previous_db = tripal_db_set_active('chado'); // use chado database
  553. $featuremap = db_fetch_object(db_query($sql, $featuremap_id));
  554. tripal_db_set_active($previous_db); // now use drupal database
  555. $job_args[0] = $featuremap_id;
  556. tripal_add_job("Reindex features for map: $featuremap->name", 'tripal_featuremap',
  557. 'tripal_featuremap_reindex_features', $job_args, $user->uid);
  558. }
  559. }
  560. }
  561. // -------------------------------------
  562. // Submit the Taxonomy Job if selected
  563. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  564. $featuremaps = $form_state['values']['tx-maps'];
  565. foreach ($featuremaps as $featuremap_id) {
  566. if ($featuremap_id and preg_match("/^\d+$/i", $featuremap_id)) {
  567. // get the map info
  568. $sql = "SELECT * FROM {featuremap} WHERE featuremap_id = %d";
  569. $previous_db = tripal_db_set_active('chado'); // use chado database
  570. $featuremap = db_fetch_object(db_query($sql, $featuremap_id));
  571. tripal_db_set_active($previous_db); // now use drupal database
  572. $job_args[0] = $featuremap_id;
  573. tripal_add_job("Set taxonomy for features in map: $featuremap->name", 'tripal_featuremap',
  574. 'tripal_featuremap_taxonify_features', $job_args, $user->uid);
  575. }
  576. }
  577. }
  578. // -------------------------------------
  579. // Submit the Cleanup Job if selected
  580. if ($form_state['values']['op'] == t('Clean up orphaned maps')) {
  581. tripal_add_job('Cleanup orphaned maps', 'tripal_featuremap',
  582. 'tripal_featuremap_cleanup', $job_args, $user->uid);
  583. }
  584. }
  585. /**
  586. *
  587. * @ingroup tripal_featuremap
  588. */
  589. function tripal_featuremap_cron() {
  590. }
  591. /**
  592. *
  593. * CHADO_LIBRARY NODE FUNCTIONS
  594. *
  595. * The following function proves access control for users trying to
  596. * perform actions on data managed by this module
  597. *
  598. * @ingroup tripal_featuremap
  599. */
  600. function tripal_featuremap_map_access($op, $node, $account) {
  601. if ($op == 'create') {
  602. if (!user_access('create chado_featuremap content', $account)) {
  603. return FALSE;
  604. }
  605. }
  606. if ($op == 'update') {
  607. if (!user_access('edit any chado_featuremap content', $account) &&
  608. !user_access('edit own chado_featuremap content', $account)) {
  609. return FALSE;
  610. }
  611. if (user_access('edit own chado_featuremap content', $account) &&
  612. $account->uid != $node->uid) {
  613. return FALSE;
  614. }
  615. }
  616. if ($op == 'delete') {
  617. if (!user_access('delete any chado_featuremap content', $account) &&
  618. !user_access('delete own chado_featuremap content', $account)) {
  619. return FALSE;
  620. }
  621. if (user_access('delete own chado_featuremap content', $account) &&
  622. $account->uid != $node->uid) {
  623. return FALSE;
  624. }
  625. }
  626. return NULL;
  627. }
  628. /**
  629. * validates submission of form when adding or updating a map node
  630. *
  631. * @ingroup tripal_featuremap
  632. */
  633. function chado_featuremap_validate($node) {
  634. $lib = 0;
  635. // check to make sure the unique name on the map is unique
  636. // before we try to insert into chado.
  637. if ($node->featuremap_id) {
  638. $sql = "SELECT * FROM ".
  639. "{featuremap} WHERE ".
  640. "name = '%s' ".
  641. "AND NOT featuremap_id = %d";
  642. $previous_db = tripal_db_set_active('chado');
  643. $lib = db_fetch_object(db_query($sql, $node->name,
  644. $node->featuremap_id));
  645. tripal_db_set_active($previous_db);
  646. }
  647. else {
  648. $sql = "SELECT * FROM ".
  649. "{featuremap} ".
  650. "WHERE name = '%s'";
  651. $previous_db = tripal_db_set_active('chado');
  652. $lib = db_fetch_object(db_query($sql, $node->name));
  653. tripal_db_set_active($previous_db);
  654. }
  655. if ($lib) {
  656. form_set_error('name', t('The unique map name already exists. '.
  657. 'Please choose another'));
  658. }
  659. }
  660. /**
  661. * When a new chado_featuremap node is created we also need to add information
  662. * to our chado_featuremap table. This function is called on insert of a new node
  663. * of type 'chado_featuremap' and inserts the necessary information.
  664. *
  665. * @ingroup tripal_featuremap
  666. */
  667. function chado_featuremap_insert($node) {
  668. if ($node->featuremap_id) {
  669. $featuremap['featuremap_id'] = $node->featuremap_id;
  670. }
  671. else {
  672. $values = array(
  673. 'name' => $node->title,
  674. 'description' => $node->description,
  675. );
  676. $featuremap = tripal_core_chado_insert('featuremap', $values);
  677. }
  678. if ($featuremap) {
  679. // make sure the entry for this feature doesn't already exist in the chado_featuremap table
  680. // if it doesn't exist then we want to add it.
  681. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  682. if (!$featuremap_id) {
  683. // next add the item to the drupal table
  684. $sql = "INSERT INTO {chado_featuremap} (nid, vid, featuremap_id) ".
  685. "VALUES (%d, %d, %d)";
  686. db_query($sql, $node->nid, $node->vid, $featuremap['featuremap_id']);
  687. }
  688. }
  689. else {
  690. drupal_set_message(t('Unable to add featuremap.', 'warning'));
  691. watchdog('tripal_featuremap',
  692. 'Insert feature: Unable to create featuremap where values: %values',
  693. array('%values' => print_r($values, TRUE)),
  694. WATCHDOG_WARNING
  695. );
  696. }
  697. }
  698. /**
  699. * Update nodes
  700. *
  701. * @ingroup tripal_featuremap
  702. */
  703. function chado_featuremap_update($node) {
  704. if ($node->revision) {
  705. // TODO -- decide what to do about revisions
  706. }
  707. else {
  708. $featuremap_id = chado_get_id_for_node('featuremap', $node) ;
  709. // update the map record
  710. $match = array(
  711. 'featuremap_id' => $featuremap_id,
  712. );
  713. $values = array(
  714. 'name' => $node->title,
  715. );
  716. $status = tripal_core_chado_update('featuremap', $match, $values);
  717. }
  718. }
  719. /**
  720. * Add the map as a taxonomy term for associating with map_features
  721. *
  722. * @ingroup tripal_featuremap
  723. */
  724. function tripal_featuremap_add_taxonomy($node, $featuremap_id) {
  725. }
  726. /**
  727. * When editing or creating a new node of type 'chado_featuremap' we need
  728. * a form. This function creates the form that will be used for this.
  729. *
  730. * @ingroup tripal_featuremap
  731. */
  732. function chado_featuremap_form($node) {
  733. $form = array();
  734. $featuremap = $node->featuremap;
  735. // get the default values
  736. $name = $node->name;
  737. if (!$name) {
  738. $name = $featuremap->name;
  739. }
  740. // keep track of the map id if we have. If we do have one then
  741. // this is an update as opposed to an insert.
  742. $form['featuremap_id'] = array(
  743. '#type' => 'value',
  744. '#value' => $featuremap->featuremap_id,
  745. );
  746. $form['title']= array(
  747. '#type' => 'textfield',
  748. '#title' => t('Map Title'),
  749. '#description' => t('Please enter the title for this map. '.
  750. 'This appears at the top of the map page.'),
  751. '#required' => TRUE,
  752. '#default_value' => $node->title,
  753. '#weight' => 1
  754. );
  755. $form['name']= array(
  756. '#type' => 'textfield',
  757. '#title' => t('Map Name'),
  758. '#description' => t('Please enter a name for this map'),
  759. '#required' => TRUE,
  760. '#default_value' => $name,
  761. '#weight' => 2
  762. );
  763. $form['description']= array(
  764. '#type' => 'textarea',
  765. '#title' => t('Map Description'),
  766. '#description' => t('A brief description of the map'),
  767. '#required' => TRUE,
  768. '#default_value' => $description,
  769. '#weight' => 5
  770. );
  771. return $form;
  772. }
  773. /**
  774. *
  775. *
  776. * @ingroup tripal_featuremap
  777. */
  778. function tripal_featuremap_sync_featuremaps($featuremap_id = NULL, $job_id = NULL) {
  779. global $user;
  780. $page_content = '';
  781. // get the list of featuremaps and create new nodes
  782. if (!$featuremap_id) {
  783. $sql = "SELECT * FROM {featuremap} L";
  784. $previous_db = tripal_db_set_active('chado'); // use chado database
  785. $results = db_query($sql);
  786. tripal_db_set_active($previous_db); // now use drupal database
  787. }
  788. else {
  789. $sql = "SELECT * FROM {featuremap} L WHERE featuremap_id = %d";
  790. $previous_db = tripal_db_set_active('chado'); // use chado database
  791. $results = db_query($sql, $featuremap_id);
  792. tripal_db_set_active($previous_db); // now use drupal database
  793. }
  794. // We'll use the following SQL statement for checking if the map
  795. // already exists as a drupal node.
  796. $sql = "SELECT * FROM {chado_featuremap} ".
  797. "WHERE featuremap_id = %d";
  798. while ($featuremap = db_fetch_object($results)) {
  799. // check if this map already exists in the drupal database. if it
  800. // does then skip this map and go to the next one.
  801. if (!db_fetch_object(db_query($sql, $featuremap->featuremap_id))) {
  802. $new_node = new stdClass();
  803. $new_node->type = 'chado_featuremap';
  804. $new_node->uid = $user->uid;
  805. $new_node->title = "$featuremap->name";
  806. $new_node->featuremap_id = $featuremap->featuremap_id;
  807. node_validate($new_node);
  808. $errors = form_get_errors();
  809. if (!$errors) {
  810. $node = node_submit($new_node);
  811. node_save($node);
  812. if ($node->nid) {
  813. print "Added " . $featuremap->name . "\n";
  814. }
  815. else {
  816. print "ERROR: Unable to create " . $featuremap->name . "\n";
  817. }
  818. }
  819. else {
  820. print "ERROR: Unable to create " . $featuremap->name . "\n" . print_r($errors, TRUE) . "\n";
  821. }
  822. }
  823. else {
  824. print "Skipped " . $featuremap->name . "\n";
  825. }
  826. }
  827. return $page_content;
  828. }
  829. /**
  830. * When a node is requested by the user this function is called to allow us
  831. * to add auxiliary data to the node object.
  832. *
  833. * @ingroup tripal_featuremap
  834. */
  835. function chado_featuremap_load($node) {
  836. // get the feature details from chado
  837. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  838. $values = array('featuremap_id' => $featuremap_id);
  839. $featuremap = tripal_core_generate_chado_var('featuremap', $values);
  840. $additions = new stdClass();
  841. $additions->featuremap = $featuremap;
  842. return $additions;
  843. }
  844. /**
  845. * This function customizes the view of the chado_featuremap node. It allows
  846. * us to generate the markup. This function is required for node [Preview]
  847. *
  848. * @ingroup tripal_featuremap
  849. */
  850. function chado_featuremap_view($node, $teaser = FALSE, $page = FALSE) {
  851. // use drupal's default node view:
  852. if (!$teaser) {
  853. $node = node_prepare($node, $teaser);
  854. }
  855. return $node;
  856. }
  857. /**
  858. * Delete data from drupal and chado databases when a node is deleted
  859. * @ingroup tripal_featuremap
  860. */
  861. function chado_featuremap_delete(&$node) {
  862. $featuremap_id = chado_get_id_for_node('featuremap', $node);
  863. // if we don't have a map id for this node then this isn't a node of
  864. // type chado_featuremap or the entry in the chado_featuremap table was lost.
  865. if (!$featuremap_id) {
  866. return;
  867. }
  868. // Remove data from {chado_featuremap}, {node} and {node_revisions} tables of
  869. // drupal database
  870. $sql_del = "DELETE FROM {chado_featuremap} ".
  871. "WHERE nid = %d ".
  872. "AND vid = %d";
  873. db_query($sql_del, $node->nid, $node->vid);
  874. $sql_del = "DELETE FROM {node} ".
  875. "WHERE nid = %d ".
  876. "AND vid = %d";
  877. db_query($sql_del, $node->nid, $node->vid);
  878. $sql_del = "DELETE FROM {node_revisions} ".
  879. "WHERE nid = %d ".
  880. "AND vid = %d";
  881. db_query($sql_del, $node->nid, $node->vid);
  882. // Remove data from map and mapprop tables of chado database as well
  883. $previous_db = tripal_db_set_active('chado');
  884. db_query("DELETE FROM {featuremap} WHERE featuremap_id = %d", $featuremap_id);
  885. db_query("DELETE FROM {featuremapprop} WHERE featuremap_id = %d", $featuremap_id);
  886. tripal_db_set_active($previous_db);
  887. }
  888. /**
  889. * Remove orphaned drupal nodes
  890. *
  891. * @param $dummy
  892. * Not Used -kept for backwards compatibility
  893. * @param $job_id
  894. * The id of the tripal job executing this function
  895. *
  896. * @ingroup tripal_featuremap
  897. */
  898. function tripal_featuremap_cleanup($dummy = NULL, $job_id = NULL) {
  899. return tripal_core_clean_orphaned_nodes('featuremap', $job_id);
  900. }
  901. /************************************************************************
  902. */
  903. function theme_tripal_featuremap_search_result($node) {
  904. }