tripal_pub.module 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. require_once "includes/pub_form.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. $items['tripal_pub/properties/add'] = array(
  104. 'page callback' => 'tripal_pub_property_add',
  105. 'access arguments' => array('edit chado_pub content'),
  106. 'type ' => MENU_CALLBACK,
  107. );
  108. $items['tripal_pub/properties/minus/%/%'] = array(
  109. 'page callback' => 'tripal_pub_property_delete',
  110. 'page arguments' => array(3, 4),
  111. 'access arguments' => array('edit chado_pub content'),
  112. 'type ' => MENU_CALLBACK,
  113. );
  114. return $items;
  115. }
  116. /**
  117. * Implements hook_theme(): Register themeing functions for this module
  118. *
  119. *
  120. * @return
  121. * An array of themeing functions to register
  122. *
  123. */
  124. function tripal_pub_theme() {
  125. return array(
  126. 'tripal_pub_base' => array(
  127. 'arguments' => array('node' => NULL),
  128. ),
  129. 'tripal_pub_properties' => array(
  130. 'arguments' => array('node' => NULL)
  131. ),
  132. 'tripal_pub_authors' => array(
  133. 'arguments' => array('node' => NULL)
  134. ),
  135. 'tripal_pub_references' => array(
  136. 'arguments' => array('node' => NULL)
  137. ),
  138. 'tripal_pub_relationships' => array(
  139. 'arguments' => array('node' => NULL)
  140. ),
  141. 'tripal_pub_importer_setup_form' => array(
  142. 'arguments' => array('form'),
  143. ),
  144. 'chado_pub_node_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_insert().
  210. *
  211. * This function inserts user entered information pertaining to the Publication instance into the
  212. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  213. *
  214. * @parm $node
  215. * Then node which contains the information stored within the node-ID
  216. *
  217. *
  218. */
  219. function chado_pub_insert($node) {
  220. // if a pub_id already exists for this node then it already exists in Chado and
  221. // we get here because we are syncing the node. Therefore, we can skip the insert
  222. if ($node->pub_id) {
  223. $pub['pub_id'] = $node->pub_id;
  224. }
  225. else {
  226. // get the list of properties for easy lookup (without doing lots of database queries
  227. $properties_list = array();
  228. $sql = "
  229. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  230. FROM {cvtermpath} CVTP
  231. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  232. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  233. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  234. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  235. NOT CVTS.is_obsolete = 1
  236. ORDER BY CVTS.name ASC
  237. ";
  238. $prop_types = chado_query($sql);
  239. $properties = array();
  240. while ($prop = db_fetch_object($prop_types)) {
  241. $properties_list[$prop->cvterm_id] = $prop->name;
  242. // The 'Citation' term is special because it serves
  243. // both as a property and as the uniquename for the
  244. // pub and we want it stored in both the pub table and the pubprop table
  245. if ($prop->name == 'Citation') {
  246. $properties[$prop->name][0] = $node->uniquename;
  247. }
  248. }
  249. // get the properties that should be added. Properties are in one of two forms:
  250. // 1) prop_value-[type id]-[index]
  251. // 2) new_value-[type id]-[index]
  252. // 3) new_id, new_value
  253. foreach ($node as $name => $value) {
  254. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  255. $type_id = $matches[1];
  256. $index = $matches[2];
  257. $name = $properties_list[$type_id];
  258. $properties[$name][$index] = $value;
  259. }
  260. }
  261. if ($node->new_id and $node->new_value) {
  262. $type_id = $node->new_id;
  263. $index = count($properties[$name]);
  264. $name = $properties_list[$type_id];
  265. $properties[$name][$index] = $node->new_value;
  266. }
  267. // iterate through all of the properties and remove those that really are
  268. // part of the pub table fields
  269. foreach ($properties as $name => $element) {
  270. $value = $element[0];
  271. if ($name == "Volume") {
  272. $volume = $value;
  273. unset($properties[$name]);
  274. }
  275. elseif ($name == "Volume Title") {
  276. $volumetitle = $value;
  277. unset($properties[$name]);
  278. }
  279. elseif ($name == "Issue") {
  280. $issue = $value;
  281. unset($properties[$name]);
  282. }
  283. elseif ($name == "Pages") {
  284. $pages = $value;
  285. unset($properties[$name]);
  286. }
  287. elseif ($name == "Publisher") {
  288. $publisher = $value;
  289. unset($properties[$name]);
  290. }
  291. elseif ($name == "Journal Country" or $name == "Published Location") {
  292. $pubplace = $value;
  293. unset($properties[$name]);
  294. }
  295. }
  296. // insert the pub record
  297. $values = array(
  298. 'title' => $node->title,
  299. 'series_name' => $node->series_name,
  300. 'type_id' => $node->type_id,
  301. 'pyear' => $node->pyear,
  302. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  303. 'uniquename' => $node->uniquename,
  304. 'volumetitle' => $volumetitle,
  305. 'volume' => $volume,
  306. 'issue' => $issue,
  307. 'pages' => $pages,
  308. 'miniref' => $miniref,
  309. 'publisher' => $publisher,
  310. 'pubplace' => $pubplace,
  311. );
  312. $pub = tripal_core_chado_insert('pub', $values);
  313. if (!$pub) {
  314. drupal_set_message("Error inserting publication", "error");
  315. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  316. return;
  317. }
  318. // now add in the properties
  319. foreach ($properties as $property => $elements) {
  320. foreach ($elements as $rank => $value) {
  321. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  322. if (!$status) {
  323. drupal_set_message("Error cannot add property: $property", "error");
  324. watchdog('tripal_pub', "Error cannot add property: %prop",
  325. array('%property' => $property), WATCHDOG_ERROR);
  326. }
  327. }
  328. }
  329. } // if ($node->pub_id) {} else
  330. if ($pub) {
  331. // make sure the entry for this feature doesn't already exist in the chado_pub table
  332. // if it doesn't exist then we want to add it.
  333. $pub_id = chado_get_id_for_node('pub', $node) ;
  334. if (!$pub_id) {
  335. // next add the item to the drupal table
  336. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  337. "VALUES (%d, %d, %d)";
  338. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  339. }
  340. }
  341. else {
  342. drupal_set_message(t('Unable to add publication.', 'warning'));
  343. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  344. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  345. }
  346. }
  347. /*
  348. *
  349. * Implements hook_update
  350. *
  351. * The purpose of the function is to allow the module to take action when an edited node is being
  352. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  353. * As well, the database will be changed, so the user changed information will be saved to the database.
  354. *
  355. * @param $node
  356. * The node being updated
  357. *
  358. * @ingroup tripal_pub
  359. */
  360. function chado_pub_update($node) {
  361. if ($node->revision) {
  362. // there is no way to handle revisions in Chado but leave
  363. // this here just to make not we've addressed it.
  364. }
  365. // get the publication ID for this publication
  366. $pub_id = chado_get_id_for_node('pub', $node) ;
  367. // get the list of properties for easy lookup (without doing lots of database queries
  368. $properties_list = array();
  369. $sql = "
  370. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  371. FROM {cvtermpath} CVTP
  372. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  373. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  374. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  375. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  376. NOT CVTS.is_obsolete = 1
  377. ORDER BY CVTS.name ASC
  378. ";
  379. $prop_types = chado_query($sql);
  380. $properties = array();
  381. while ($prop = db_fetch_object($prop_types)) {
  382. $properties_list[$prop->cvterm_id] = $prop->name;
  383. // The 'Citation' term is special because it serves
  384. // both as a property and as the uniquename for the
  385. // pub and we want it stored in both the pub table and the pubprop table
  386. if ($prop->name == 'Citation') {
  387. $properties[$prop->name][0] = $node->uniquename;
  388. }
  389. }
  390. // get the properties that should be added. Properties are in one of three forms:
  391. // 1) prop_value-[type id]-[index]
  392. // 2) new_value-[type id]-[index]
  393. // 3) new_id, new_value
  394. foreach ($node as $name => $value) {
  395. if (preg_match('/^prop_value-(\d+)-(\d+)/', $name, $matches)) {
  396. $type_id = $matches[1];
  397. $index = $matches[2];
  398. $name = $properties_list[$type_id];
  399. $properties[$name][$index] = $value;
  400. }
  401. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  402. $type_id = $matches[1];
  403. $index = $matches[2];
  404. $name = $properties_list[$type_id];
  405. $properties[$name][$index] = $value;
  406. }
  407. }
  408. if ($node->new_id and $node->new_value) {
  409. $type_id = $node->new_id;
  410. $index = count($properties[$name]);
  411. $name = $properties_list[$type_id];
  412. $properties[$name][$index] = $node->new_value;
  413. }
  414. // iterate through all of the properties and remove those that really are
  415. // part of the pub table fields
  416. foreach ($properties as $name => $element) {
  417. $value = $element[0];
  418. if ($name == "Volume") {
  419. $volume = $value;
  420. unset($properties[$name]);
  421. }
  422. elseif ($name == "Volume Title") {
  423. $volumetitle = $value;
  424. unset($properties[$name]);
  425. }
  426. elseif ($name == "Issue") {
  427. $issue = $value;
  428. unset($properties[$name]);
  429. }
  430. elseif ($name == "Pages") {
  431. $pages = $value;
  432. unset($properties[$name]);
  433. }
  434. elseif ($name == "Publisher") {
  435. $publisher = $value;
  436. unset($properties[$name]);
  437. }
  438. elseif ($name == "Journal Country" or $name == "Published Location") {
  439. $pubplace = $value;
  440. unset($properties[$name]);
  441. }
  442. }
  443. // update the pub record
  444. $match = array(
  445. 'pub_id' => $pub_id,
  446. );
  447. $values = array(
  448. 'title' => $node->title,
  449. 'series_name' => $node->series_name,
  450. 'type_id' => $node->type_id,
  451. 'pyear' => $node->pyear,
  452. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  453. 'uniquename' => $node->uniquename,
  454. 'volumetitle' => $volumetitle,
  455. 'volume' => $volume,
  456. 'issue' => $issue,
  457. 'pages' => $pages,
  458. 'miniref' => $miniref,
  459. 'publisher' => $publisher,
  460. 'pubplace' => $pubplace,
  461. );
  462. $status = tripal_core_chado_update('pub', $match, $values);
  463. if (!$status) {
  464. drupal_set_message("Error updating publication", "error");
  465. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  466. return;
  467. }
  468. // now add in the properties by first removing any the publication
  469. // already has and adding the ones we have
  470. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  471. foreach ($properties as $property => $elements) {
  472. foreach ($elements as $rank => $value) {
  473. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  474. if (!$status) {
  475. drupal_set_message("Error cannot add property: $property", "error");
  476. watchdog('tripal_pub', "Error cannot add property: %prop",
  477. array('%property' => $property), WATCHDOG_ERROR);
  478. }
  479. }
  480. }
  481. }
  482. /**
  483. * Implementation of tripal_pub_load().
  484. *
  485. *
  486. * @param $node
  487. * The node that is to be accessed from the database
  488. *
  489. * @return $node
  490. * The node with the information to be loaded into the database
  491. *
  492. */
  493. function chado_pub_load($node) {
  494. // get the feature details from chado
  495. $pub_id = chado_get_id_for_node('pub', $node);
  496. $values = array('pub_id' => $pub_id);
  497. $pub = tripal_core_generate_chado_var('pub', $values);
  498. // expand the 'text' fields as those aren't included by default
  499. // and they really shouldn't be so large to cause problems
  500. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  501. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  502. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');;
  503. $additions = new stdClass();
  504. $additions->pub = $pub;
  505. return $additions;
  506. }
  507. /**
  508. * Implementation of tripal_pub_delete().
  509. *
  510. * This function takes a node and if the delete button has been chosen by the user, the publication
  511. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  512. * the 'chado_pub' table.
  513. *
  514. * @parm $node
  515. * Then node which contains the information stored within the node-ID
  516. *
  517. */
  518. function chado_pub_delete(&$node) {
  519. $pub_id = chado_get_id_for_node('pub', $node);
  520. // if we don't have a pub id for this node then this isn't a node of
  521. // type chado_pub or the entry in the chado_pub table was lost.
  522. if (!$pub_id) {
  523. return;
  524. }
  525. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  526. // drupal database
  527. $sql_del = "DELETE FROM {chado_pub} ".
  528. "WHERE nid = %d ".
  529. "AND vid = %d";
  530. db_query($sql_del, $node->nid, $node->vid);
  531. $sql_del = "DELETE FROM {node_revisions} ".
  532. "WHERE nid = %d ".
  533. "AND vid = %d";
  534. db_query($sql_del, $node->nid, $node->vid);
  535. $sql_del = "DELETE FROM {node} ".
  536. "WHERE nid = %d ".
  537. "AND vid = %d";
  538. db_query($sql_del, $node->nid, $node->vid);
  539. // Remove data from pub and pubprop tables of chado database as well
  540. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  541. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  542. }
  543. /**
  544. * Because we are using AJAX with a node form we need to provide a callback
  545. * for the chado_pub node form. This callback is different from the
  546. * default 'chado_pub_form' callback
  547. */
  548. /*
  549. function tripal_pub_forms($form_id, $args) {
  550. $forms = array();
  551. if($form_id == 'chado_pub_node_form') {
  552. $forms[$form_id] = array(
  553. 'callback' => 'chado_pub_node_form',
  554. 'callback arguments' => array($args)
  555. );
  556. }
  557. return $forms;
  558. }*/
  559. /*
  560. *
  561. */
  562. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  563. if ($form_id == "tripal_pub_importer_setup_form") {
  564. // updating the form through the ahah callback sets the action of
  565. // the form to the ahah callback URL. We need to set it back
  566. // to the normal form URL
  567. if ($form_state['values']['action'] == 'edit') {
  568. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form['pub_import_id']);
  569. }
  570. if ($form_state['values']['action'] == 'new') {
  571. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  572. }
  573. }
  574. if ($form_id == "chado_pub_node_form") {
  575. }
  576. }
  577. /**
  578. *
  579. *
  580. * @ingroup tripal_pub
  581. */
  582. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  583. // we want to provide a new variable that contains the matched pubs.
  584. $pub = $variables['node']->pub;
  585. // normally we would use tripal_core_expand_chado_vars to expand our
  586. // organism object and add in the relationships, however whan a large
  587. // number of relationships are present this significantly slows the
  588. // query, therefore we will manually perform the query
  589. $sql = "
  590. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  591. FROM pub_relationship PR
  592. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  593. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  594. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  595. WHERE PR.subject_id = %d
  596. ";
  597. $as_subject = chado_query($sql, $pub->pub_id);
  598. $sql = "
  599. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  600. FROM pub_relationship PR
  601. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  602. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  603. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  604. WHERE PR.object_id = %d
  605. ";
  606. $as_object = chado_query($sql, $pub->pub_id);
  607. // combine both object and subject relationshisp into a single array
  608. $relationships = array();
  609. $relationships['object'] = array();
  610. $relationships['subject'] = array();
  611. // iterate through the object relationships
  612. while ($relationship = db_fetch_object($as_object)) {
  613. // get the relationship and child types
  614. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  615. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  616. if (!array_key_exists($rel_type, $relationships['object'])) {
  617. $relationships['object'][$rel_type] = array();
  618. }
  619. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  620. $relationships['object'][$rel_type][$sub_type] = array();
  621. }
  622. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  623. }
  624. // now add in the subject relationships
  625. while ($relationship = db_fetch_object($as_subject)) {
  626. // get the relationship and child types
  627. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  628. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  629. if (!array_key_exists($rel_type, $relationships['subject'])) {
  630. $relationships['subject'][$rel_type] = array();
  631. }
  632. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  633. $relationships['subject'][$rel_type][$obj_type] = array();
  634. }
  635. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  636. }
  637. $pub->all_relationships = $relationships;
  638. }