tripal_pub.module 18 KB

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