tripal_analysis.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /**
  3. * @file
  4. * Contains all the main hook implementations for the tripal_analysis module
  5. *
  6. * @defgroup tripal_analysis Analysis Module
  7. * @ingroup tripal_modules
  8. * @{
  9. * Provides functions for managing chado analysis' including creating details pages for each one
  10. *
  11. * @}
  12. *
  13. *
  14. */
  15. require('api/tripal_analysis.api.inc');
  16. require('includes/tripal_analysis_privacy.inc');
  17. require('includes/tripal_analysis.admin.inc');
  18. require('includes/tripal_analysis.fofrm.inc');
  19. /**
  20. * Add tripal javascript to page headers
  21. *
  22. * @ingroup tripal_analysis
  23. */
  24. function tripal_analysis_init() {
  25. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_analysis.js');
  26. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_analysis.css');
  27. }
  28. /**
  29. * Implementation of hook_menu().
  30. * Entry points and paths of the module
  31. *
  32. * @ingroup tripal_analysis
  33. */
  34. function tripal_analysis_menu() {
  35. //Sync analysis
  36. $items['chado_sync_analyses'] = array(
  37. 'title' => 'Sync Data',
  38. 'page callback' => 'tripal_analysis_sync_analyses',
  39. 'access arguments' => array('administer tripal analyses'),
  40. 'type' => MENU_CALLBACK
  41. );
  42. // Tripal Analysis administrative settings
  43. $items['admin/tripal/tripal_analysis'] = array(
  44. 'title' => 'Analyses',
  45. 'description' => 'Basic Description of Tripal Analysis Module Functionality.',
  46. 'page callback' => 'theme',
  47. 'page arguments' => array('tripal_analysis_admin'),
  48. 'access arguments' => array('administer tripal analyses'),
  49. 'type' => MENU_NORMAL_ITEM,
  50. 'file' => 'includes/tripal_analysis.admin.inc',
  51. );
  52. $items['admin/tripal/tripal_analysis/configuration'] = array(
  53. 'title' => 'Configuration',
  54. 'description' => 'Settings for the displays of analysis results.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('tripal_analysis_admin'),
  57. 'access arguments' => array('administer tripal analyses'),
  58. 'type' => MENU_NORMAL_ITEM,
  59. 'file' => 'includes/tripal_analysis.admin.inc',
  60. );
  61. // AJAX calls for adding/removing properties to a contact
  62. $items['tripal_analysis/properties/add'] = array(
  63. 'page callback' => 'tripal_analysis_property_add',
  64. 'access arguments' => array('edit chado_analysis content'),
  65. 'type ' => MENU_CALLBACK,
  66. );
  67. $items['tripal_analysis/properties/description'] = array(
  68. 'page callback' => 'tripal_analysis_property_get_description',
  69. 'access arguments' => array('edit chado_analysis content'),
  70. 'type ' => MENU_CALLBACK,
  71. );
  72. $items['tripal_analysis/properties/minus/%/%'] = array(
  73. 'page callback' => 'tripal_analysis_property_delete',
  74. 'page arguments' => array(3, 4),
  75. 'access arguments' => array('edit chado_analysis content'),
  76. 'type ' => MENU_CALLBACK,
  77. );
  78. return $items;
  79. }
  80. /**
  81. * Provide information to drupal about the node types that we're creating
  82. * in this module
  83. *
  84. * @ingroup tripal_analysis
  85. */
  86. function tripal_analysis_node_info() {
  87. $nodes = array();
  88. $nodes['chado_analysis'] = array(
  89. 'name' => t('Analysis'),
  90. 'module' => 'chado_analysis',
  91. 'description' => t('An analysis from the chado database'),
  92. 'has_title' => FALSE,
  93. 'title_label' => t('Analysis'),
  94. 'has_body' => FALSE,
  95. 'body_label' => t('Analysis Description'),
  96. 'locked' => TRUE
  97. );
  98. return $nodes;
  99. }
  100. /**
  101. * When a new chado_analysis node is created we also need to add information
  102. * to our chado_analysis table. This function is called on insert of a new
  103. * node of type 'chado_analysis' and inserts the necessary information.
  104. *
  105. * @ingroup tripal_analysis
  106. */
  107. function chado_analysis_insert($node) {
  108. global $user;
  109. // Create a timestamp so we can insert it into the chado database
  110. $time = $node->timeexecuted;
  111. $month = $time['month'];
  112. $day = $time['day'];
  113. $year = $time['year'];
  114. $timestamp = $month . '/' . $day . '/' . $year;
  115. // If this analysis already exists then don't recreate it in chado
  116. $analysis_id = $node->analysis_id;
  117. if ($analysis_id) {
  118. $values = array('analysis_id' => $node->analysis_id);
  119. $result = tripal_core_chado_select('analysis', array('analysis_id'), $values);
  120. if($result and count($result) > 0) {
  121. $analysis = $result[0];
  122. }
  123. }
  124. // If the analysis doesn't exist then let's create it in chado.
  125. if (!$analysis) {
  126. // insert and then get the newly inserted analysis record
  127. $values = array(
  128. 'name' => $node->analysisname,
  129. 'description' => $node->description,
  130. 'program' => $node->program,
  131. 'programversion' => $node->programversion,
  132. 'algorithm' => $node->algorithm,
  133. 'sourcename' => $node->sourcename,
  134. 'sourceversion' => $node->sourceversion,
  135. 'sourceuri' => $node->sourceuri,
  136. 'timeexecuted' => $timestamp
  137. );
  138. if (tripal_core_chado_insert('analysis', $values)) {
  139. $analysis = tripal_core_chado_select('analysis', array('*'), $values);
  140. $analysis_id = $analysis[0]->analysis_id;
  141. }
  142. }
  143. // Make sure the entry for this analysis doesn't already exist in the
  144. // chado_analysis table if it doesn't exist then we want to add it.
  145. $node_check_sql = "SELECT * FROM {chado_analysis} ".
  146. "WHERE analysis_id = %d";
  147. $node_check = db_fetch_object(db_query($node_check_sql, $analysis_id));
  148. if (!$node_check) {
  149. // next add the item to the drupal table
  150. $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
  151. "VALUES (%d, %d, %d)";
  152. db_query($sql, $node->nid, $node->vid, $analysis_id);
  153. // Create a title for the analysis node using the unique keys so when the
  154. // node is saved, it will have a title
  155. $record = new stdClass();
  156. // If the analysis has a name, use it as the node title. If not, construct
  157. // the title using program, programversion, and sourcename
  158. if ($node->analysisname) {
  159. $record->title = $node->analysisname;
  160. }
  161. else {
  162. //Construct node title as "program (version)
  163. $record->title = "$node->program ($node->programversion)";
  164. }
  165. $record->nid = $node->nid;
  166. drupal_write_record('node', $record, 'nid');
  167. drupal_write_record('node_revisions', $record, 'nid');
  168. }
  169. // add the analysis to the node object for
  170. // use by other analysis modules that may be using this function
  171. $node->analysis = $analysis;
  172. $node->analysis_id = $analysis_id; // we need to set this for children
  173. }
  174. /**
  175. * Removes analysis from the chado database
  176. *
  177. * @param $node
  178. * The node object specifying which chado record to delete
  179. *
  180. * @ingroup tripal_analysis
  181. */
  182. function chado_analysis_delete($node) {
  183. $analysis_id = chado_get_id_for_node('analysis', $node);
  184. // if we don't have an organism id for this node then this isn't a node of
  185. // type chado_organism or the entry in the chado_organism table was lost.
  186. if (!$analysis_id) {
  187. return;
  188. }
  189. // Remove data from the {chado_analysis}, {node}, and {node_revisions} tables
  190. $sql_del = "DELETE FROM {chado_analysis} ".
  191. "WHERE nid = %d ".
  192. "AND vid = %d";
  193. db_query($sql_del, $node->nid, $node->vid);
  194. $sql_del = "DELETE FROM {node} ".
  195. "WHERE nid = %d ".
  196. "AND vid = %d";
  197. db_query($sql_del, $node->nid, $node->vid);
  198. $sql_del = "DELETE FROM {node_revisions} ".
  199. "WHERE nid = %d ".
  200. "AND vid = %d";
  201. db_query($sql_del, $node->nid, $node->vid);
  202. //Remove from analysis and analysisprop tables of chado database as well
  203. chado_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
  204. }
  205. /**
  206. * Update analyses
  207. *
  208. * @param $node
  209. * The updated node object
  210. *
  211. * @ingroup tripal_analysis
  212. */
  213. function chado_analysis_update($node) {
  214. global $user;
  215. if ($node->revision) {
  216. // TODO -- decide what to do about revisions
  217. }
  218. // Create a timestamp so we can insert it into the chado database
  219. $time = $node->timeexecuted;
  220. $month = $time['month'];
  221. $day = $time['day'];
  222. $year = $time['year'];
  223. $timestamp = $month . '/' . $day . '/' . $year;
  224. // get the analysis_id for this node:
  225. $sql = "SELECT analysis_id ".
  226. "FROM {chado_analysis} ".
  227. "WHERE nid = %d";
  228. $analysis_id = db_fetch_object(db_query($sql, $node->nid))->analysis_id;
  229. $sql = "UPDATE {analysis} ".
  230. "SET name = '%s', ".
  231. " description = '%s', ".
  232. " program = '%s', ".
  233. " programversion = '%s', ".
  234. " algorithm = '%s', ".
  235. " sourcename = '%s', ".
  236. " sourceversion = '%s', ".
  237. " sourceuri = '%s', ".
  238. " timeexecuted = '%s' ".
  239. "WHERE analysis_id = %d ";
  240. chado_query($sql, $node->analysisname, $node->description, $node->program,
  241. $node->programversion, $node->algorithm, $node->sourcename,
  242. $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
  243. // Create a title for the analysis node using the unique keys so when the
  244. // node is saved, it will have a title
  245. $record = new stdClass();
  246. // If the analysis has a name, use it as the node title. If not, construct
  247. // the title using program, programversion, and sourcename
  248. if ($node->analysisname) {
  249. $record->title = $node->analysisname;
  250. }
  251. else {
  252. //Construct node title as "program (version)
  253. $record->title = "$node->program ($node->programversion)";
  254. }
  255. $record->nid = $node->nid;
  256. drupal_write_record('node', $record, 'nid');
  257. drupal_write_record('node_revisions', $record, 'nid');
  258. }
  259. /**
  260. * When a node is requested by the user this function is called to allow us
  261. * to add auxiliary data to the node object.
  262. *
  263. * @ingroup tripal_analysis
  264. */
  265. function chado_analysis_load($node) {
  266. // get the feature details from chado
  267. $analysis_id = chado_get_id_for_node('analysis', $node);
  268. $values = array('analysis_id' => $analysis_id);
  269. $analysis = tripal_core_generate_chado_var('analysis', $values);
  270. $additions = new stdClass();
  271. $additions->analysis = $analysis;
  272. return $additions;
  273. }
  274. /**
  275. * This function customizes the view of the chado_analysis node. It allows
  276. * us to generate the markup.
  277. *
  278. * @ingroup tripal_analysis
  279. */
  280. function chado_analysis_view($node, $teaser = FALSE, $page = FALSE) {
  281. // use drupal's default node view:
  282. if (!$teaser) {
  283. $node = node_prepare($node, $teaser);
  284. // When previewing a node submitting form, it shows 'Array' instead of
  285. // correct date format. We need to format the date here
  286. $time = $node->timeexecuted;
  287. if (is_array($time)) {
  288. $month = $time['month'];
  289. $day = $time['day'];
  290. $year = $time['year'];
  291. $timestamp = $year . '-' . $month . '-' . $day;
  292. $node->timeexecuted = $timestamp;
  293. }
  294. }
  295. return $node;
  296. }
  297. /**
  298. * Display help and module information
  299. * @param path which path of the site we're displaying help
  300. * @param arg array that holds the current path as would be returned from arg()
  301. * function
  302. * @return help text for the path
  303. *
  304. * @ingroup tripal_analysis
  305. */
  306. function tripal_analysis_help($path, $arg) {
  307. $output = '';
  308. switch ($path) {
  309. case "admin/help#tripal_analysis":
  310. $output = '<p>' .
  311. t("Displays links to nodes created on this date") .
  312. '</p>';
  313. break;
  314. }
  315. return $output;
  316. }
  317. /**
  318. * The following function proves access control for users trying to
  319. * perform actions on data managed by this module
  320. *
  321. * @ingroup tripal_analysis
  322. */
  323. function chado_analysis_access($op, $node, $account) {
  324. if ($op == 'create') {
  325. if (!user_access('create chado_analysis content', $account)) {
  326. return FALSE;
  327. }
  328. }
  329. if ($op == 'update') {
  330. if (!user_access('edit chado_analysis content', $account)) {
  331. return FALSE;
  332. }
  333. }
  334. if ($op == 'delete') {
  335. if (!user_access('delete chado_analysis content', $account)) {
  336. return FALSE;
  337. }
  338. }
  339. if ($op == 'view') {
  340. if (!user_access('access chado_analysis content', $account)) {
  341. return FALSE;
  342. }
  343. }
  344. return NULL;
  345. }
  346. /**
  347. * Set the permission types that the chado module uses. Essentially we
  348. * want permissionis that protect creation, editing and deleting of chado
  349. * data objects
  350. *
  351. * @ingroup tripal_analysis
  352. */
  353. function tripal_analysis_perm() {
  354. return array(
  355. 'access chado_analysis content',
  356. 'create chado_analysis content',
  357. 'delete chado_analysis content',
  358. 'edit chado_analysis content',
  359. 'administer tripal analyses',
  360. );
  361. }
  362. /**
  363. * We need to let drupal know about our theme functions and their arguments.
  364. * We create theme functions to allow users of the module to customize the
  365. * look and feel of the output generated in this module
  366. *
  367. * @ingroup tripal_analysis
  368. */
  369. function tripal_analysis_theme() {
  370. return array(
  371. 'tripal_analysis_base' => array(
  372. 'arguments' => array('node' => NULL),
  373. 'template' => 'tripal_analysis_base',
  374. ),
  375. 'tripal_feature_analyses' => array(
  376. 'template' => 'tripal_feature_analyses',
  377. 'arguments' => array('node' => NULL),
  378. ),
  379. 'tripal_analysis_admin' => array(
  380. 'template' => 'tripal_analysis_admin',
  381. 'arguments' => array(NULL),
  382. 'path' => drupal_get_path('module', 'tripal_analysis') . '/theme',
  383. ),
  384. // Themed Forms
  385. 'chado_analysis_node_form' => array(
  386. 'arguments' => array('form'),
  387. ),
  388. );
  389. }
  390. /**
  391. *
  392. *
  393. * @ingroup tripal_feature
  394. */
  395. function tripal_analysis_block($op = 'list', $delta = 0, $edit=array()) {
  396. switch ($op) {
  397. case 'list':
  398. $blocks['base']['info'] = t('Tripal Analysis Details');
  399. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  400. $blocks['featureblast']['info'] = t('Tripal Feature Analyses');
  401. $blocks['featureblast']['cache'] = BLOCK_NO_CACHE;
  402. return $blocks;
  403. case 'view':
  404. if (user_access('access chado_analysis content') and arg(0) == 'node' and is_numeric(arg(1))) {
  405. $nid = arg(1);
  406. $node = node_load($nid);
  407. $block = array();
  408. switch ($delta) {
  409. case 'base':
  410. $block['subject'] = t('Analysis Details');
  411. $block['content'] = theme('tripal_analysis_base', $node);
  412. break;
  413. case 'tripal_feature_analyses':
  414. $block['subject'] = t('Feature Analyses');
  415. $block['content'] = theme('tripal_feature_analyses', $node);
  416. break;
  417. default :
  418. }
  419. return $block;
  420. }
  421. }
  422. }
  423. /*******************************************************************************
  424. * tripal_analysis_nodeapi()
  425. * HOOK: Implementation of hook_nodeapi()
  426. * Display blast results for allowed node types
  427. */
  428. function tripal_analysis_nodeapi(&$node, $op, $teaser, $page) {
  429. switch ($op) {
  430. case 'view':
  431. if ($teaser) {
  432. return '';
  433. }
  434. // Abort if this node is not one of the types we should show.
  435. if (strcmp($node->type, 'chado_feature') == 0) {
  436. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  437. // return results for searching
  438. }
  439. else {
  440. // return normal results
  441. $node->content['tripal_feature_analyses'] = array(
  442. '#value' => theme('tripal_feature_analyses', $node),
  443. '#weight' => 8
  444. );
  445. }
  446. }
  447. break;
  448. }
  449. }
  450. /**
  451. * Implements hook_views_api()
  452. * Purpose: Essentially this hook tells drupal that there is views support for
  453. * for this module which then includes tripal_analysis.views.inc where all the
  454. * views integration code is
  455. *
  456. * @ingroup tripal_analysis
  457. */
  458. function tripal_analysis_views_api() {
  459. return array(
  460. 'api' => 2.0,
  461. );
  462. }
  463. /*
  464. *
  465. */
  466. function tripal_analysis_form_alter(&$form, &$form_state, $form_id) {
  467. if ($form_id == "chado_analysis_node_form") {
  468. }
  469. }