tripal_pub.module 26 KB

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