tripal_pub.module 18 KB

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