tripal_pub.module 26 KB

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