tripal_pub.module 26 KB

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