tripal_pub.module 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. require_once "api/tripal_pub.api.inc";
  3. require_once "includes/tripal_pub.admin.inc";
  4. require_once "includes/pub_sync.inc";
  5. require_once "includes/pub_form.inc";
  6. require_once "includes/pub_importers.inc";
  7. require_once "includes/pubmed.inc";
  8. require_once "includes/agricola.inc";
  9. /**
  10. * @file
  11. *
  12. * The Tripal Publication module allows you to search the PubMed databse for academic articles,
  13. * that relate to user specified tpoic\s. As well, it allows management of publications so that
  14. * a user can enter specified details regarding a desired publication. This allows all of the important
  15. * information that is unique to a Academic Publication to be stored for access.
  16. */
  17. /**
  18. *
  19. * @ingroup tripal_pub
  20. */
  21. function tripal_pub_init() {
  22. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_pub.js');
  23. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_pub.css');
  24. // this variable indicates which databases are supported by the Tripal pub module
  25. // for importing
  26. variable_set('tripal_pub_supported_dbs', array('PMID'));
  27. }
  28. /**
  29. * Implementation of hook_tripal_pub_node_info().
  30. *
  31. * This node_info, is a simple node that describes the functionallity of the module.
  32. *
  33. */
  34. function tripal_pub_node_info() {
  35. return array(
  36. 'chado_pub' => array(
  37. 'name' => t('Publication'),
  38. 'module' => 'chado_pub',
  39. 'description' => t('A publication from the Chado database'),
  40. 'title_label' => t('Article Title'),
  41. 'body_label' => t('Abstract'),
  42. 'has_title' => TRUE,
  43. 'has_body' => FALSE,
  44. ),
  45. );
  46. }
  47. /**
  48. * Tripal-Publication-Menu
  49. *
  50. * Implemets hook_menu(): Adds menu items for the tripal_pub module menu. This section
  51. * gives the outline for the main menu of the Tripal-Publication module
  52. *
  53. * @return
  54. * An array of menu items that is visible within the Drupal Menu, returned as soon
  55. * as the program is ran
  56. */
  57. function tripal_pub_menu() {
  58. $items = array();
  59. $items['admin/tripal/tripal_pub' ]= array(
  60. 'title' => 'Publications',
  61. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  62. 'page callback' => 'theme',
  63. 'page arguments' => array('tripal_pub_admin'),
  64. 'access arguments' => array('administer tripal pubs'),
  65. 'type' => MENU_NORMAL_ITEM
  66. );
  67. $items['admin/tripal/tripal_pub/configuration'] = array(
  68. 'title' => 'Configuration',
  69. 'description' => 'Configure the Tripal publication module.',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('tripal_pub_admin'),
  72. 'access arguments' => array('administer tripal pubs'),
  73. 'type' => MENU_NORMAL_ITEM,
  74. );
  75. $items['admin/tripal/tripal_pub/sync'] = array(
  76. 'title' => ' Sync Publications',
  77. 'description' => 'Sync publications in Chado with Drupal',
  78. 'page callback' => 'drupal_get_form',
  79. 'page arguments' => array('tripal_pub_sync_form'),
  80. 'access arguments' => array('administer tripal pubs'),
  81. 'type' => MENU_NORMAL_ITEM,
  82. );
  83. $items['admin/tripal/tripal_pub/import_list'] = array(
  84. 'title' => t('Importers List'),
  85. 'description' => t('List all publication importers'),
  86. 'page callback' => 'tripal_pub_importers_list',
  87. 'access arguments' => array('administer tripal pubs'),
  88. 'type ' => MENU_CALLBACK,
  89. );
  90. $items['admin/tripal/tripal_pub/import/new'] = array(
  91. 'title' => t('Add an Importer'),
  92. 'description' => t('Add a new publication importer.'),
  93. 'page callback' => 'tripal_pub_importer_setup',
  94. 'page arguments' => array(4, NULL),
  95. 'access arguments' => array('administer tripal pubs'),
  96. 'type ' => MENU_CALLBACK,
  97. );
  98. $items['admin/tripal/tripal_pub/import/edit/%'] = array(
  99. 'page callback' => 'tripal_pub_importer_setup',
  100. 'page arguments' => array(4, 5),
  101. 'access arguments' => array('administer tripal pubs'),
  102. 'type ' => MENU_CALLBACK,
  103. );
  104. $items['admin/tripal/tripal_pub/import/delete/%'] = array(
  105. 'page callback' => 'tripal_pub_importer_delete',
  106. 'page arguments' => array(5),
  107. 'access arguments' => array('administer tripal pubs'),
  108. 'type ' => MENU_CALLBACK,
  109. );
  110. $items['admin/tripal/tripal_pub/import/criteria/%/%'] = array(
  111. 'page callback' => 'tripal_pub_importer_setup_page_update_criteria',
  112. 'page arguments' => array(5, 6),
  113. 'access arguments' => array('administer tripal pubs'),
  114. 'type ' => MENU_CALLBACK,
  115. );
  116. $items['tripal_pub/properties/add'] = array(
  117. 'page callback' => 'tripal_pub_property_add',
  118. 'access arguments' => array('edit chado_pub content'),
  119. 'type ' => MENU_CALLBACK,
  120. );
  121. $items['tripal_pub/properties/description'] = array(
  122. 'page callback' => 'tripal_pub_property_get_description',
  123. 'access arguments' => array('edit chado_pub content'),
  124. 'type ' => MENU_CALLBACK,
  125. );
  126. $items['tripal_pub/properties/minus/%/%'] = array(
  127. 'page callback' => 'tripal_pub_property_delete',
  128. 'page arguments' => array(3, 4),
  129. 'access arguments' => array('edit chado_pub content'),
  130. 'type ' => MENU_CALLBACK,
  131. );
  132. return $items;
  133. }
  134. /**
  135. * Implements hook_theme(): Register themeing functions for this module
  136. *
  137. *
  138. * @return
  139. * An array of themeing functions to register
  140. *
  141. */
  142. function tripal_pub_theme() {
  143. return array(
  144. 'tripal_pub_base' => array(
  145. 'arguments' => array('node' => NULL),
  146. ),
  147. 'tripal_pub_properties' => array(
  148. 'arguments' => array('node' => NULL)
  149. ),
  150. 'tripal_pub_authors' => array(
  151. 'arguments' => array('node' => NULL)
  152. ),
  153. 'tripal_pub_references' => array(
  154. 'arguments' => array('node' => NULL)
  155. ),
  156. 'tripal_pub_relationships' => array(
  157. 'arguments' => array('node' => NULL)
  158. ),
  159. 'tripal_pub_importer_setup_form' => array(
  160. 'arguments' => array('form'),
  161. ),
  162. 'chado_pub_node_form' => array(
  163. 'arguments' => array('form'),
  164. ),
  165. 'tripal_pub_admin' => array(
  166. 'template' => 'tripal_pub_admin',
  167. 'arguments' => array(NULL),
  168. 'path' => drupal_get_path('module', 'tripal_pub') . '/theme'
  169. ),
  170. );
  171. }
  172. /**
  173. * Implement hook_perm().
  174. */
  175. function tripal_pub_perm() {
  176. return array(
  177. 'access chado_pub content',
  178. 'create chado_pub content',
  179. 'delete chado_pub content',
  180. 'edit chado_pub content',
  181. 'administer tripal pubs',
  182. );
  183. }
  184. /**
  185. * Implement hook_access().
  186. *
  187. * This hook allows node modules to limit access to the node types they define.
  188. *
  189. * @param $op
  190. * The operation to be performed
  191. *
  192. * @param $node
  193. * The node on which the operation is to be performed, or, if it does not yet exist, the
  194. * type of node to be created
  195. *
  196. * @param $account
  197. * A user object representing the user for whom the operation is to be performed
  198. *
  199. * @return
  200. * TRUE
  201. *
  202. */
  203. function chado_pub_access($op, $node, $account ) {
  204. if ($op == 'create') {
  205. if (!user_access('create chado_pub content', $account)) {
  206. return FALSE;
  207. }
  208. }
  209. if ($op == 'update') {
  210. if (!user_access('edit chado_pub content', $account)) {
  211. return FALSE;
  212. }
  213. }
  214. if ($op == 'delete') {
  215. if (!user_access('delete chado_pub content', $account)) {
  216. return FALSE;
  217. }
  218. }
  219. if ($op == 'view') {
  220. if (!user_access('access chado_pub content', $account)) {
  221. return FALSE;
  222. }
  223. }
  224. return NULL;
  225. }
  226. /**
  227. * Implementation of tripal_pub_insert().
  228. *
  229. * This function inserts user entered information pertaining to the Publication instance into the
  230. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  231. *
  232. * @parm $node
  233. * Then node which contains the information stored within the node-ID
  234. *
  235. *
  236. */
  237. function chado_pub_insert($node) {
  238. // if a pub_id already exists for this node then it already exists in Chado and
  239. // we get here because we are syncing the node. Therefore, we can skip the insert
  240. if ($node->pub_id) {
  241. $pub['pub_id'] = $node->pub_id;
  242. }
  243. else {
  244. $properties = array(); // stores all of the properties we need to add
  245. $cross_refs = array(); // stores any cross references for this publication
  246. // get the list of properties for easy lookup (without doing lots of database queries
  247. $properties_list = array();
  248. $sql = "
  249. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  250. FROM {cvtermpath} CVTP
  251. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  252. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  253. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  254. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  255. NOT CVTS.is_obsolete = 1
  256. ORDER BY CVTS.name ASC
  257. ";
  258. $prop_types = chado_query($sql);
  259. while ($prop = db_fetch_object($prop_types)) {
  260. $properties_list[$prop->cvterm_id] = $prop->name;
  261. // The 'Citation' term is special because it serves
  262. // both as a property and as the uniquename for the
  263. // pub and we want it stored in both the pub table and the pubprop table
  264. if ($prop->name == 'Citation') {
  265. $properties[$prop->name][0] = $node->uniquename;
  266. }
  267. }
  268. // get the properties that should be added. Properties are in one of two forms:
  269. // 1) prop_value-[type id]-[index]
  270. // 2) new_value-[type id]-[index]
  271. // 3) new_id, new_value
  272. foreach ($node as $name => $value) {
  273. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  274. $type_id = $matches[1];
  275. $index = $matches[2];
  276. $name = $properties_list[$type_id];
  277. $properties[$name][$index] = $value;
  278. }
  279. }
  280. if ($node->new_id and $node->new_value) {
  281. $type_id = $node->new_id;
  282. $index = count($properties[$name]);
  283. $name = $properties_list[$type_id];
  284. $properties[$name][$index] = $node->new_value;
  285. }
  286. // iterate through all of the properties and remove those that really are
  287. // part of the pub table fields
  288. foreach ($properties as $name => $element) {
  289. $value = $element[0];
  290. if ($name == "Volume") {
  291. $volume = $value;
  292. unset($properties[$name]);
  293. }
  294. elseif ($name == "Volume Title") {
  295. $volumetitle = $value;
  296. unset($properties[$name]);
  297. }
  298. elseif ($name == "Issue") {
  299. $issue = $value;
  300. unset($properties[$name]);
  301. }
  302. elseif ($name == "Pages") {
  303. $pages = $value;
  304. unset($properties[$name]);
  305. }
  306. elseif ($name == "Publisher") {
  307. $publisher = $value;
  308. unset($properties[$name]);
  309. }
  310. elseif ($name == "Journal Country" or $name == "Published Location") {
  311. $pubplace = $value;
  312. unset($properties[$name]);
  313. }
  314. elseif ($name == "Publication Dbxref") {
  315. // we will add the cross-references to the pub_dbxref table
  316. // but we also want to keep the property in the pubprop table so don't unset it
  317. $cross_refs[] = $value;
  318. }
  319. }
  320. // insert the pub record
  321. $values = array(
  322. 'title' => $node->title,
  323. 'series_name' => $node->series_name,
  324. 'type_id' => $node->type_id,
  325. 'pyear' => $node->pyear,
  326. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  327. 'uniquename' => $node->uniquename,
  328. 'volumetitle' => $volumetitle,
  329. 'volume' => $volume,
  330. 'issue' => $issue,
  331. 'pages' => $pages,
  332. 'miniref' => $miniref,
  333. 'publisher' => $publisher,
  334. 'pubplace' => $pubplace,
  335. );
  336. $pub = tripal_core_chado_insert('pub', $values);
  337. if (!$pub) {
  338. drupal_set_message("Error inserting publication", "error");
  339. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  340. return;
  341. }
  342. // now add in the properties
  343. foreach ($properties as $property => $elements) {
  344. foreach ($elements as $rank => $value) {
  345. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  346. if (!$status) {
  347. drupal_set_message("Error cannot add property: $property", "error");
  348. watchdog('tripal_pub', "Error cannot add property: %prop",
  349. array('%property' => $property), WATCHDOG_ERROR);
  350. }
  351. }
  352. }
  353. // add in any database cross-references
  354. foreach ($cross_refs as $index => $ref) {
  355. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], $ref);
  356. if (!$pub_dbxref) {
  357. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  358. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  359. array('%ref' => $ref), WATCHDOG_ERROR);
  360. }
  361. }
  362. } // if ($node->pub_id) {} else
  363. if ($pub) {
  364. // make sure the entry for this feature doesn't already exist in the chado_pub table
  365. // if it doesn't exist then we want to add it.
  366. $pub_id = chado_get_id_for_node('pub', $node) ;
  367. if (!$pub_id) {
  368. // next add the item to the drupal table
  369. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  370. "VALUES (%d, %d, %d)";
  371. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  372. }
  373. }
  374. else {
  375. drupal_set_message(t('Unable to add publication.', 'warning'));
  376. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  377. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  378. }
  379. }
  380. /*
  381. *
  382. * Implements hook_update
  383. *
  384. * The purpose of the function is to allow the module to take action when an edited node is being
  385. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  386. * As well, the database will be changed, so the user changed information will be saved to the database.
  387. *
  388. * @param $node
  389. * The node being updated
  390. *
  391. * @ingroup tripal_pub
  392. */
  393. function chado_pub_update($node) {
  394. if ($node->revision) {
  395. // there is no way to handle revisions in Chado but leave
  396. // this here just to make not we've addressed it.
  397. }
  398. // get the publication ID for this publication
  399. $pub_id = chado_get_id_for_node('pub', $node) ;
  400. $properties = array(); // stores all of the properties we need to add
  401. $cross_refs = array(); // stores any cross references for this publication
  402. // get the list of properties for easy lookup (without doing lots of database queries
  403. $properties_list = array();
  404. $sql = "
  405. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  406. FROM {cvtermpath} CVTP
  407. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  408. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  409. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  410. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  411. NOT CVTS.is_obsolete = 1
  412. ORDER BY CVTS.name ASC
  413. ";
  414. $prop_types = chado_query($sql);
  415. while ($prop = db_fetch_object($prop_types)) {
  416. $properties_list[$prop->cvterm_id] = $prop->name;
  417. // The 'Citation' term is special because it serves
  418. // both as a property and as the uniquename for the
  419. // pub and we want it stored in both the pub table and the pubprop table
  420. if ($prop->name == 'Citation') {
  421. $properties[$prop->name][0] = $node->uniquename;
  422. }
  423. }
  424. // get the properties that should be added. Properties are in one of three forms:
  425. // 1) prop_value-[type id]-[index]
  426. // 2) new_value-[type id]-[index]
  427. // 3) new_id, new_value
  428. // dpm($node);
  429. foreach ($node as $key => $value) {
  430. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  431. $type_id = $matches[1];
  432. $index = $matches[2];
  433. $name = $properties_list[$type_id];
  434. $properties[$name][$index] = $value;
  435. }
  436. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  437. $type_id = $matches[1];
  438. $index = $matches[2];
  439. $name = $properties_list[$type_id];
  440. $properties[$name][$index] = $value;
  441. }
  442. }
  443. if ($node->new_id and $node->new_value) {
  444. $type_id = $node->new_id;
  445. $name = $properties_list[$type_id];
  446. $index = count($properties[$name]);
  447. $properties[$name][$index] = $node->new_value;
  448. }
  449. // iterate through all of the properties and remove those that really are
  450. // part of the pub table fields
  451. foreach ($properties as $name => $element) {
  452. foreach ($element as $index => $value) {
  453. if ($name == "Volume") {
  454. $volume = $value;
  455. unset($properties[$name]);
  456. }
  457. elseif ($name == "Volume Title") {
  458. $volumetitle = $value;
  459. unset($properties[$name]);
  460. }
  461. elseif ($name == "Issue") {
  462. $issue = $value;
  463. unset($properties[$name]);
  464. }
  465. elseif ($name == "Pages") {
  466. $pages = $value;
  467. unset($properties[$name]);
  468. }
  469. elseif ($name == "Publisher") {
  470. $publisher = $value;
  471. unset($properties[$name]);
  472. }
  473. elseif ($name == "Journal Country" or $name == "Published Location") {
  474. $pubplace = $value;
  475. unset($properties[$name]);
  476. }
  477. elseif ($name == "Publication Dbxref") {
  478. // we will add the cross-references to the pub_dbxref table
  479. // but we also want to keep the property in the pubprop table so don't unset it
  480. $cross_refs[] = $value;
  481. }
  482. }
  483. }
  484. // update the pub record
  485. $match = array(
  486. 'pub_id' => $pub_id,
  487. );
  488. $values = array(
  489. 'title' => $node->title,
  490. 'series_name' => $node->series_name,
  491. 'type_id' => $node->type_id,
  492. 'pyear' => $node->pyear,
  493. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  494. 'uniquename' => $node->uniquename,
  495. 'volumetitle' => $volumetitle,
  496. 'volume' => $volume,
  497. 'issue' => $issue,
  498. 'pages' => $pages,
  499. 'miniref' => $miniref,
  500. 'publisher' => $publisher,
  501. 'pubplace' => $pubplace,
  502. );
  503. $status = tripal_core_chado_update('pub', $match, $values);
  504. if (!$status) {
  505. drupal_set_message("Error updating publication", "error");
  506. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  507. return;
  508. }
  509. // now add in the properties by first removing any the publication
  510. // already has and adding the ones we have
  511. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  512. foreach ($properties as $property => $elements) {
  513. foreach ($elements as $rank => $value) {
  514. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  515. if (!$status) {
  516. drupal_set_message("Error cannot add property: $property", "error");
  517. watchdog('tripal_pub', "Error cannot add property: %prop",
  518. array('%property' => $property), WATCHDOG_ERROR);
  519. }
  520. }
  521. }
  522. // add in any database cross-references after first removing
  523. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  524. foreach ($cross_refs as $index => $ref) {
  525. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, $ref);
  526. if (!$pub_dbxref) {
  527. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  528. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  529. array('%ref' => $ref), WATCHDOG_ERROR);
  530. }
  531. }
  532. }
  533. /**
  534. * Implementation of tripal_pub_load().
  535. *
  536. *
  537. * @param $node
  538. * The node that is to be accessed from the database
  539. *
  540. * @return $node
  541. * The node with the information to be loaded into the database
  542. *
  543. */
  544. function chado_pub_load($node) {
  545. // get the feature details from chado
  546. $pub_id = chado_get_id_for_node('pub', $node);
  547. $values = array('pub_id' => $pub_id);
  548. $pub = tripal_core_generate_chado_var('pub', $values);
  549. // expand the 'text' fields as those aren't included by default
  550. // and they really shouldn't be so large to cause problems
  551. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  552. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  553. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');;
  554. $additions = new stdClass();
  555. $additions->pub = $pub;
  556. return $additions;
  557. }
  558. /**
  559. * Implementation of tripal_pub_delete().
  560. *
  561. * This function takes a node and if the delete button has been chosen by the user, the publication
  562. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  563. * the 'chado_pub' table.
  564. *
  565. * @parm $node
  566. * Then node which contains the information stored within the node-ID
  567. *
  568. */
  569. function chado_pub_delete(&$node) {
  570. $pub_id = chado_get_id_for_node('pub', $node);
  571. // if we don't have a pub id for this node then this isn't a node of
  572. // type chado_pub or the entry in the chado_pub table was lost.
  573. if (!$pub_id) {
  574. return;
  575. }
  576. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  577. // drupal database
  578. $sql_del = "DELETE FROM {chado_pub} ".
  579. "WHERE nid = %d ".
  580. "AND vid = %d";
  581. db_query($sql_del, $node->nid, $node->vid);
  582. $sql_del = "DELETE FROM {node_revisions} ".
  583. "WHERE nid = %d ".
  584. "AND vid = %d";
  585. db_query($sql_del, $node->nid, $node->vid);
  586. $sql_del = "DELETE FROM {node} ".
  587. "WHERE nid = %d ".
  588. "AND vid = %d";
  589. db_query($sql_del, $node->nid, $node->vid);
  590. // Remove data from pub and pubprop tables of chado database as well
  591. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  592. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  593. }
  594. /**
  595. * Because we are using AJAX with a node form we need to provide a callback
  596. * for the chado_pub node form. This callback is different from the
  597. * default 'chado_pub_form' callback
  598. */
  599. /*
  600. function tripal_pub_forms($form_id, $args) {
  601. $forms = array();
  602. if($form_id == 'chado_pub_node_form') {
  603. $forms[$form_id] = array(
  604. 'callback' => 'chado_pub_node_form',
  605. 'callback arguments' => array($args)
  606. );
  607. }
  608. return $forms;
  609. }*/
  610. /*
  611. *
  612. */
  613. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  614. if ($form_id == "tripal_pub_importer_setup_form") {
  615. // updating the form through the ahah callback sets the action of
  616. // the form to the ahah callback URL. We need to set it back
  617. // to the normal form URL
  618. if ($form_state['values']['action'] == 'edit') {
  619. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form['pub_import_id']);
  620. }
  621. if ($form_state['values']['action'] == 'new') {
  622. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  623. }
  624. }
  625. if ($form_id == "chado_pub_node_form") {
  626. }
  627. }
  628. /**
  629. *
  630. *
  631. * @ingroup tripal_pub
  632. */
  633. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  634. // we want to provide a new variable that contains the matched pubs.
  635. $pub = $variables['node']->pub;
  636. // normally we would use tripal_core_expand_chado_vars to expand our
  637. // organism object and add in the relationships, however whan a large
  638. // number of relationships are present this significantly slows the
  639. // query, therefore we will manually perform the query
  640. $sql = "
  641. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  642. FROM pub_relationship PR
  643. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  644. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  645. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  646. WHERE PR.subject_id = %d
  647. ";
  648. $as_subject = chado_query($sql, $pub->pub_id);
  649. $sql = "
  650. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  651. FROM pub_relationship PR
  652. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  653. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  654. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  655. WHERE PR.object_id = %d
  656. ";
  657. $as_object = chado_query($sql, $pub->pub_id);
  658. // combine both object and subject relationshisp into a single array
  659. $relationships = array();
  660. $relationships['object'] = array();
  661. $relationships['subject'] = array();
  662. // iterate through the object relationships
  663. while ($relationship = db_fetch_object($as_object)) {
  664. // get the relationship and child types
  665. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  666. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  667. if (!array_key_exists($rel_type, $relationships['object'])) {
  668. $relationships['object'][$rel_type] = array();
  669. }
  670. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  671. $relationships['object'][$rel_type][$sub_type] = array();
  672. }
  673. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  674. }
  675. // now add in the subject relationships
  676. while ($relationship = db_fetch_object($as_subject)) {
  677. // get the relationship and child types
  678. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  679. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  680. if (!array_key_exists($rel_type, $relationships['subject'])) {
  681. $relationships['subject'][$rel_type] = array();
  682. }
  683. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  684. $relationships['subject'][$rel_type][$obj_type] = array();
  685. }
  686. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  687. }
  688. $pub->all_relationships = $relationships;
  689. }