tripal_pub.module 26 KB

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