tripal_pub.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. require_once "api/tripal_pub.api.inc";
  3. require_once "includes/tripal_pub.admin.inc";
  4. require_once "includes/pubmed.inc";
  5. require_once "includes/pub_sync.inc";
  6. /**
  7. * @file
  8. *
  9. * The Tripal Publication module allows you to search the PubMed databse for academic articles,
  10. * that relate to user specified tpoic\s. As well, it allows management of publications so that
  11. * a user can enter specified details regarding a desired publication. This allows all of the important
  12. * information that is unique to a Academic Publication to be stored for access.
  13. */
  14. /**
  15. *
  16. * @ingroup tripal_pub
  17. */
  18. function tripal_pub_init() {
  19. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_pub.js');
  20. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_pub.css');
  21. }
  22. /**
  23. * Implementation of hook_tripal_pub_node_info().
  24. *
  25. * This node_info, is a simple node that describes the functionallity of the module.
  26. *
  27. */
  28. function tripal_pub_node_info() {
  29. return array(
  30. 'chado_pub' => array(
  31. 'name' => t('Publication'),
  32. 'module' => 'chado_pub',
  33. 'description' => t('A publication from the Chado database'),
  34. 'title_label' => t('Article Title'),
  35. 'body_label' => t('Abstract'),
  36. 'has_title' => TRUE,
  37. 'has_body' => FALSE,
  38. ),
  39. );
  40. }
  41. /**
  42. * Tripal-Publication-Menu
  43. *
  44. * Implemets hook_menu(): Adds menu items for the tripal_pub module menu. This section
  45. * gives the outline for the main menu of the Tripal-Publication module
  46. *
  47. * @return
  48. * An array of menu items that is visible within the Drupal Menu, returned as soon
  49. * as the program is ran
  50. */
  51. function tripal_pub_menu() {
  52. $items = array();
  53. $items[ 'admin/tripal/tripal_pub' ]= array(
  54. 'title' => 'Publications',
  55. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  56. 'page callback' => 'theme',
  57. 'page arguments' => array('tripal_pub_admin'),
  58. 'access arguments' => array('administer tripal pubs'),
  59. 'type' => MENU_NORMAL_ITEM
  60. );
  61. $items['admin/tripal/tripal_pub/sync'] = array(
  62. 'title' => ' Sync Publications',
  63. 'description' => 'Sync publications in Chado with Drupal',
  64. 'page callback' => 'drupal_get_form',
  65. 'page arguments' => array('tripal_pub_sync_form'),
  66. 'access arguments' => array('administer tripal pubs'),
  67. 'type' => MENU_NORMAL_ITEM,
  68. );
  69. $items['admin/tripal/tripal_pub/import_list'] = array(
  70. 'title' => t('Importers List'),
  71. 'description' => t('List all publication importers'),
  72. 'page callback' => 'tripal_pub_importers_list',
  73. 'access arguments' => array('administer tripal pubs'),
  74. 'type ' => MENU_CALLBACK,
  75. );
  76. $items['admin/tripal/tripal_pub/import/new'] = array(
  77. 'title' => t('Add an Importer'),
  78. 'description' => t('Add a new publication importer.'),
  79. 'page callback' => 'tripal_pub_importer_setup',
  80. 'page arguments' => array(4, NULL),
  81. 'access arguments' => array('administer tripal pubs'),
  82. 'type ' => MENU_CALLBACK,
  83. );
  84. $items['admin/tripal/tripal_pub/import/edit/%'] = array(
  85. 'page callback' => 'tripal_pub_importer_setup',
  86. 'page arguments' => array(4, 5),
  87. 'access arguments' => array('administer tripal pubs'),
  88. 'type ' => MENU_CALLBACK,
  89. );
  90. $items['admin/tripal/tripal_pub/import/delete/%'] = array(
  91. 'page callback' => 'tripal_pub_importer_delete',
  92. 'page arguments' => array(5),
  93. 'access arguments' => array('administer tripal pubs'),
  94. 'type ' => MENU_CALLBACK,
  95. );
  96. $items['admin/tripal/tripal_pub/import/criteria/%/%'] = array(
  97. 'page callback' => 'tripal_pub_importer_setup_page_update_criteria',
  98. 'page arguments' => array(5, 6),
  99. 'access arguments' => array('administer tripal pubs'),
  100. 'type ' => MENU_CALLBACK,
  101. );
  102. return $items;
  103. }
  104. /**
  105. * Implements hook_theme(): Register themeing functions for this module
  106. *
  107. *
  108. * @return
  109. * An array of themeing functions to register
  110. *
  111. */
  112. function tripal_pub_theme() {
  113. return array(
  114. 'tripal_pub_base' => array(
  115. 'arguments' => array('node' => NULL),
  116. ),
  117. 'tripal_pub_properties' => array(
  118. 'arguments' => array('node' => NULL)
  119. ),
  120. 'tripal_pub_authors' => array(
  121. 'arguments' => array('node' => NULL)
  122. ),
  123. 'tripal_pub_references' => array(
  124. 'arguments' => array('node' => NULL)
  125. ),
  126. 'tripal_pub_relationships' => array(
  127. 'arguments' => array('node' => NULL)
  128. ),
  129. 'tripal_pub_importer_setup_form' => array(
  130. 'arguments' => array('form'),
  131. ),
  132. 'tripal_pub_admin' => array(
  133. 'template' => 'tripal_pub_admin',
  134. 'arguments' => array(NULL),
  135. 'path' => drupal_get_path('module', 'tripal_pub') . '/theme'
  136. ),
  137. );
  138. }
  139. /**
  140. * Implement hook_perm().
  141. */
  142. function tripal_pub_perm() {
  143. return array(
  144. 'access chado_pub content',
  145. 'create chado_pub content',
  146. 'delete chado_pub content',
  147. 'edit chado_pub content',
  148. 'administer tripal pubs',
  149. );
  150. }
  151. /**
  152. * Implement hook_access().
  153. *
  154. * This hook allows node modules to limit access to the node types they define.
  155. *
  156. * @param $op
  157. * The operation to be performed
  158. *
  159. * @param $node
  160. * The node on which the operation is to be performed, or, if it does not yet exist, the
  161. * type of node to be created
  162. *
  163. * @param $account
  164. * A user object representing the user for whom the operation is to be performed
  165. *
  166. * @return
  167. * TRUE
  168. *
  169. */
  170. function chado_pub_access($op, $node, $account ) {
  171. if ($op == 'create') {
  172. if (!user_access('create chado_pub content', $account)) {
  173. return FALSE;
  174. }
  175. }
  176. if ($op == 'update') {
  177. if (!user_access('edit chado_pub content', $account)) {
  178. return FALSE;
  179. }
  180. }
  181. if ($op == 'delete') {
  182. if (!user_access('delete chado_pub content', $account)) {
  183. return FALSE;
  184. }
  185. }
  186. if ($op == 'view') {
  187. if (!user_access('access chado_pub content', $account)) {
  188. return FALSE;
  189. }
  190. }
  191. return NULL;
  192. }
  193. /**
  194. * Implementation of tripal_pub_form().
  195. *
  196. *
  197. *
  198. * @parm &$node
  199. * The node that is created when the database is initialized
  200. *
  201. * @parm $form_state
  202. * The state of the form, that has the user entered information that is neccessary for, setting
  203. * up the database tables for the publication
  204. *
  205. * @return $form
  206. * The information that was enterd allong with
  207. *
  208. */
  209. function chado_pub_form(&$node, $form_state) {
  210. $type = node_get_types('type', $node);
  211. $form['pub_id'] = array(
  212. '#type' => 'hidden',
  213. '#value' => (isset($node->pub_id)) ? $node->pub_id->pub_id : NULL ,
  214. );
  215. $sql = "
  216. SELECT CVTS.cvterm_id, CVTS.name
  217. FROM {cvtermpath} CVTP
  218. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  219. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  220. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  221. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Type'
  222. ORDER BY CVTS.name ASC
  223. ";
  224. $results = chado_query($sql);
  225. $pub_types = array();
  226. $default_type = '';
  227. while ($pub_type = db_fetch_object($results)) {
  228. $pub_types[$pub_type->cvterm_id] = $pub_type->name;
  229. if (strcmp($pub_type->name,"Journal Article") == 0) {
  230. $default_type = $pub_type->cvterm_id;
  231. }
  232. }
  233. $form['type_id'] = array(
  234. '#type' => 'select',
  235. '#title' => t('Publication Type'),
  236. '#options' => $pub_types,
  237. '#required' => TRUE,
  238. '#default_value' => isset($node->pub_id->type_id) ? $node->pub_id->type_id : $default_type,
  239. );
  240. // Article Title.
  241. $form['title'] = array(
  242. '#type' => 'textfield',
  243. '#title' => check_plain($type->title_label),
  244. '#default_value' => $node->title,
  245. '#required' => TRUE,
  246. );
  247. $form['volume'] = array(
  248. '#type' => 'textfield',
  249. '#title' => t('Volume'),
  250. '#default_value' => isset($node->pub_id->volume) ? $node->pub_id->volume : ''
  251. );
  252. $form['volumetitle'] = array(
  253. '#type' => 'textfield',
  254. '#title' => t('Volume Title'),
  255. '#description' => t('Title of part if one of a series.'),
  256. '#default_value' => isset($node->pub_id->volumetitle) ? $node->pub_id->volumetitle : ''
  257. );
  258. $form['series_name'] = array(
  259. '#type' => 'textfield',
  260. '#title' => t('Series Name'),
  261. '#description' => t('Full name of (journal) series.'),
  262. '#default_value' => isset($node->pub_id->series_name) ? $node->pub_id->series_name : ''
  263. );
  264. $form['issue'] = array(
  265. '#type' => 'textfield',
  266. '#title' => t('Issue'),
  267. '#default_value' => isset($node->pub_id->issue) ? $node->pub_id->issue : ''
  268. );
  269. $form['pyear'] = array(
  270. '#type' => 'textfield',
  271. '#title' => t('Publication Year'),
  272. '#default_value' => isset($node->pub_id->pyear) ? $node->pub_id->pyear : ''
  273. );
  274. $form['pages'] = array(
  275. '#type' => 'textfield',
  276. '#title' => t('Pages'),
  277. '#description' => t('Page number range[s], e.g. 457--459, viii + 664pp, lv--lvii.'),
  278. '#default_value' => isset($node->pub_id->pages) ? $node->pub_id->pages : ''
  279. );
  280. $form['miniref'] = array(
  281. '#type' => 'textfield',
  282. '#title' => t('Mini-Ref'),
  283. '#required' => FALSE,
  284. '#default_value' => isset($node->pub_id->miniref) ? $node->pub_id->miniref : ''
  285. );
  286. $form['publisher'] = array(
  287. '#type' => 'textfield',
  288. '#title' => t('Publisher Name'),
  289. '#required' => FALSE,
  290. '#default_value' => isset($node->pub_id->publisher) ? $node->pub_id->publisher : ''
  291. );
  292. $form['pubplace'] = array(
  293. '#type' => 'textfield',
  294. '#title' => t('Place of Publication'),
  295. '#required' => FALSE,
  296. '#default_value' => isset($node->pub_id->pubplace) ? $node->pub_id->pubplace : ''
  297. );
  298. $form['is_obsolete'] = array(
  299. '#type' => 'checkbox',
  300. '#title' => t('Is Obsolete? (Check for Yes)'),
  301. '#required' => TRUE,
  302. '#default_value' => isset($node->pub_id->is_obsolete) ? $node->pub_id->is_obsolete : FALSE
  303. );
  304. return $form;
  305. }
  306. /**
  307. * Implementation of tripal_pub_insert().
  308. *
  309. * This function inserts user entered information pertaining to the Publication instance into the
  310. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  311. *
  312. * @parm $node
  313. * Then node which contains the information stored within the node-ID
  314. *
  315. *
  316. */
  317. function chado_pub_insert($node) {
  318. // if a pub_id already exists for this node then it already exists in Chado and
  319. // we get here because we are syncing the node. Therefore, we can skip the insert
  320. if ($node->pub_id) {
  321. $pub['pub_id'] = $node->pub_id;
  322. }
  323. else {
  324. $values = array(
  325. 'title' => $node->title,
  326. 'volumetitle' => $node->volumetitle,
  327. 'volume' => $node->volume,
  328. 'series_name' => $node->series_name,
  329. 'issue' => $node->issue,
  330. 'pyear' => $node->pyear,
  331. 'pages' => $node->pages,
  332. 'miniref' => $node->miniref,
  333. 'type_id' => $node->type_id,
  334. 'is_obsolete' => $node->is_obsolete,
  335. 'publisher' => $node->publisher,
  336. 'pubplace' => $node->pubplace,
  337. 'uniquename' => $node->uniquename,
  338. 'type_id' => $node->type_id
  339. );
  340. $pub = tripal_core_chado_insert('pub', $values);
  341. }
  342. if ($pub) {
  343. // make sure the entry for this feature doesn't already exist in the chado_pub table
  344. // if it doesn't exist then we want to add it.
  345. $pub_id = chado_get_id_for_node('pub', $node) ;
  346. if (!$pub_id) {
  347. // next add the item to the drupal table
  348. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  349. "VALUES (%d, %d, %d)";
  350. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  351. }
  352. }
  353. else {
  354. drupal_set_message(t('Unable to add publication.', 'warning'));
  355. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  356. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  357. }
  358. }
  359. /*
  360. *
  361. * Implements hook_update
  362. *
  363. * The purpose of the function is to allow the module to take action when an edited node is being
  364. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  365. * As well, the database will be changed, so the user changed information will be saved to the database.
  366. *
  367. * @param $node
  368. * The node being updated
  369. *
  370. * @ingroup tripal_pub
  371. */
  372. function chado_pub_update($node) {
  373. if ($node->revision) {
  374. // there is no way to handle revisions in Chado but leave
  375. // this here just to make not we've addressed it.
  376. }
  377. $pub_id = chado_get_id_for_node('pub', $node) ;
  378. // update the pub record
  379. $match = array(
  380. 'pub_id' => $pub_id,
  381. );
  382. $values = array(
  383. 'title' => $node->title,
  384. 'volumetitle' => $node->volumetitle,
  385. 'volume' => $node->volume,
  386. 'series_name' => $node->series_name,
  387. 'issue' => $node->issue,
  388. 'pyear' => $node->pyear,
  389. 'pages' => $node->pages,
  390. 'miniref' => $node->miniref,
  391. 'uniquename' => $node->uniquename,
  392. 'type_id' => $node->type_id,
  393. 'is_obsolete' => $node->is_obsolete,
  394. 'publisher' => $node->publisher,
  395. 'pubplace' => $node->pubplace,
  396. 'type_id' => $node->type_id
  397. );
  398. $status = tripal_core_chado_update('pub', $match, $values);
  399. }
  400. /**
  401. * Implementation of tripal_pub_load().
  402. *
  403. *
  404. * @param $node
  405. * The node that is to be accessed from the database
  406. *
  407. * @return $node
  408. * The node with the information to be loaded into the database
  409. *
  410. */
  411. function chado_pub_load($node) {
  412. // get the feature details from chado
  413. $pub_id = chado_get_id_for_node('pub', $node);
  414. $values = array('pub_id' => $pub_id);
  415. $pub = tripal_core_generate_chado_var('pub', $values);
  416. $additions = new stdClass();
  417. $additions->pub = $pub;
  418. return $additions;
  419. }
  420. /**
  421. * Implementation of tripal_pub_delete().
  422. *
  423. * This function takes a node and if the delete button has been chosen by the user, the publication
  424. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  425. * the 'chado_pub' table.
  426. *
  427. * @parm $node
  428. * Then node which contains the information stored within the node-ID
  429. *
  430. */
  431. function chado_pub_delete(&$node) {
  432. $pub_id = chado_get_id_for_node('pub', $node);
  433. // if we don't have a pub id for this node then this isn't a node of
  434. // type chado_pub or the entry in the chado_pub table was lost.
  435. if (!$pub_id) {
  436. return;
  437. }
  438. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  439. // drupal database
  440. $sql_del = "DELETE FROM {chado_pub} ".
  441. "WHERE nid = %d ".
  442. "AND vid = %d";
  443. db_query($sql_del, $node->nid, $node->vid);
  444. $sql_del = "DELETE FROM {node_revisions} ".
  445. "WHERE nid = %d ".
  446. "AND vid = %d";
  447. db_query($sql_del, $node->nid, $node->vid);
  448. $sql_del = "DELETE FROM {node} ".
  449. "WHERE nid = %d ".
  450. "AND vid = %d";
  451. db_query($sql_del, $node->nid, $node->vid);
  452. // Remove data from pub and pubprop tables of chado database as well
  453. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  454. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  455. }
  456. /*
  457. *
  458. */
  459. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  460. if ($form_id == "tripal_pub_importer_setup_form") {
  461. // updating the form through the ahah callback sets the action of
  462. // the form to the ahah callback URL. We need to set it back
  463. // to the normal form URL
  464. if ($form_state['values']['action'] == 'edit') {
  465. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form['pub_import_id']);
  466. }
  467. if ($form_state['values']['action'] == 'new') {
  468. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  469. }
  470. }
  471. }
  472. /**
  473. *
  474. *
  475. * @ingroup tripal_pub
  476. */
  477. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  478. // we want to provide a new variable that contains the matched pubs.
  479. $pub = $variables['node']->pub;
  480. // normally we would use tripal_core_expand_chado_vars to expand our
  481. // organism object and add in the relationships, however whan a large
  482. // number of relationships are present this significantly slows the
  483. // query, therefore we will manually perform the query
  484. $sql = "
  485. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  486. FROM pub_relationship PR
  487. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  488. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  489. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  490. WHERE PR.subject_id = %d
  491. ";
  492. $as_subject = chado_query($sql, $pub->pub_id);
  493. $sql = "
  494. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  495. FROM pub_relationship PR
  496. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  497. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  498. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  499. WHERE PR.object_id = %d
  500. ";
  501. $as_object = chado_query($sql, $pub->pub_id);
  502. // combine both object and subject relationshisp into a single array
  503. $relationships = array();
  504. $relationships['object'] = array();
  505. $relationships['subject'] = array();
  506. // iterate through the object relationships
  507. while ($relationship = db_fetch_object($as_object)) {
  508. // get the relationship and child types
  509. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  510. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  511. if (!array_key_exists($rel_type, $relationships['object'])) {
  512. $relationships['object'][$rel_type] = array();
  513. }
  514. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  515. $relationships['object'][$rel_type][$sub_type] = array();
  516. }
  517. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  518. }
  519. // now add in the subject relationships
  520. while ($relationship = db_fetch_object($as_subject)) {
  521. // get the relationship and child types
  522. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  523. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  524. if (!array_key_exists($rel_type, $relationships['subject'])) {
  525. $relationships['subject'][$rel_type] = array();
  526. }
  527. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  528. $relationships['subject'][$rel_type][$obj_type] = array();
  529. }
  530. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  531. }
  532. $pub->all_relationships = $relationships;
  533. }