tripal_pub.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. // but we always want to set the URL path alias to be the Chado pub ID
  270. if ($node->pub_id) {
  271. $pub['pub_id'] = $node->pub_id;
  272. }
  273. else {
  274. $properties = array(); // stores all of the properties we need to add
  275. $cross_refs = array(); // stores any cross references for this publication
  276. // get the list of properties for easy lookup (without doing lots of database queries
  277. $properties_list = array();
  278. $sql = "
  279. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  280. FROM {cvtermpath} CVTP
  281. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  282. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  283. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  284. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  285. NOT CVTS.is_obsolete = 1
  286. ORDER BY CVTS.name ASC
  287. ";
  288. $prop_types = chado_query($sql);
  289. while ($prop = db_fetch_object($prop_types)) {
  290. $properties_list[$prop->cvterm_id] = $prop->name;
  291. // The 'Citation' term is special because it serves
  292. // both as a property and as the uniquename for the
  293. // pub and we want it stored in both the pub table and the pubprop table
  294. if ($prop->name == 'Citation') {
  295. $properties[$prop->name][0] = $node->uniquename;
  296. }
  297. }
  298. // get the properties that should be added. Properties are in one of two forms:
  299. // 1) prop_value-[type id]-[index]
  300. // 2) new_value-[type id]-[index]
  301. // 3) new_id, new_value
  302. foreach ($node as $name => $value) {
  303. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  304. $type_id = $matches[1];
  305. $index = $matches[2];
  306. $name = $properties_list[$type_id];
  307. $properties[$name][$index] = trim($value);
  308. }
  309. }
  310. if ($node->new_id and $node->new_value) {
  311. $type_id = $node->new_id;
  312. $index = count($properties[$name]);
  313. $name = $properties_list[$type_id];
  314. $properties[$name][$index] = trim($node->new_value);
  315. }
  316. // iterate through all of the properties and remove those that really are
  317. // part of the pub table fields
  318. foreach ($properties as $name => $element) {
  319. $value = trim($element[0]);
  320. if ($name == "Volume") {
  321. $volume = $value;
  322. unset($properties[$name]);
  323. }
  324. elseif ($name == "Volume Title") {
  325. $volumetitle = $value;
  326. unset($properties[$name]);
  327. }
  328. elseif ($name == "Issue") {
  329. $issue = $value;
  330. unset($properties[$name]);
  331. }
  332. elseif ($name == "Pages") {
  333. $pages = $value;
  334. unset($properties[$name]);
  335. }
  336. elseif ($name == "Publisher") {
  337. $publisher = $value;
  338. unset($properties[$name]);
  339. }
  340. elseif ($name == "Journal Country" or $name == "Published Location") {
  341. $pubplace = $value;
  342. unset($properties[$name]);
  343. }
  344. elseif ($name == "Publication Dbxref") {
  345. // we will add the cross-references to the pub_dbxref table
  346. // but we also want to keep the property in the pubprop table so don't unset it
  347. $cross_refs[] = $value;
  348. }
  349. }
  350. // insert the pub record
  351. $values = array(
  352. 'title' => trim($node->title),
  353. 'series_name' => trim($node->series_name),
  354. 'type_id' => trim($node->type_id),
  355. 'pyear' => trim($node->pyear),
  356. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  357. 'uniquename' => trim($node->uniquename),
  358. 'volumetitle' => $volumetitle,
  359. 'volume' => $volume,
  360. 'issue' => $issue,
  361. 'pages' => $pages,
  362. 'miniref' => $miniref,
  363. 'publisher' => $publisher,
  364. 'pubplace' => $pubplace,
  365. );
  366. $pub = tripal_core_chado_insert('pub', $values);
  367. if (!$pub) {
  368. drupal_set_message("Error inserting publication", "error");
  369. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  370. return;
  371. }
  372. // now add in the properties
  373. foreach ($properties as $property => $elements) {
  374. foreach ($elements as $rank => $value) {
  375. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  376. if (!$status) {
  377. drupal_set_message("Error cannot add property: $property", "error");
  378. watchdog('tripal_pub', "Error cannot add property: %prop",
  379. array('%property' => $property), WATCHDOG_ERROR);
  380. }
  381. }
  382. }
  383. // add in any database cross-references
  384. foreach ($cross_refs as $index => $ref) {
  385. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  386. if (!$pub_dbxref) {
  387. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  388. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  389. array('%ref' => $ref), WATCHDOG_ERROR);
  390. }
  391. }
  392. } // if ($node->pub_id) {} else
  393. if ($pub) {
  394. // make sure the entry for this feature doesn't already exist in the chado_pub table
  395. // if it doesn't exist then we want to add it.
  396. $pub_id = chado_get_id_for_node('pub', $node) ;
  397. if (!$pub_id) {
  398. // next add the item to the drupal table
  399. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  400. "VALUES (%d, %d, %d)";
  401. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  402. }
  403. }
  404. else {
  405. drupal_set_message(t('Unable to add publication.', 'warning'));
  406. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  407. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  408. }
  409. }
  410. /*
  411. *
  412. * Implements hook_update
  413. *
  414. * The purpose of the function is to allow the module to take action when an edited node is being
  415. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  416. * As well, the database will be changed, so the user changed information will be saved to the database.
  417. *
  418. * @param $node
  419. * The node being updated
  420. *
  421. * @ingroup tripal_pub
  422. */
  423. function chado_pub_update($node) {
  424. if ($node->revision) {
  425. // there is no way to handle revisions in Chado but leave
  426. // this here just to make not we've addressed it.
  427. }
  428. // get the publication ID for this publication
  429. $pub_id = chado_get_id_for_node('pub', $node) ;
  430. $properties = array(); // stores all of the properties we need to add
  431. $cross_refs = array(); // stores any cross references for this publication
  432. // get the list of properties for easy lookup (without doing lots of database queries
  433. $properties_list = array();
  434. $sql = "
  435. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  436. FROM {cvtermpath} CVTP
  437. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  438. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  439. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  440. WHERE CV.name = 'tripal_pub' and
  441. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  442. NOT CVTS.is_obsolete = 1
  443. ORDER BY CVTS.name ASC
  444. ";
  445. $prop_types = chado_query($sql);
  446. while ($prop = db_fetch_object($prop_types)) {
  447. $properties_list[$prop->cvterm_id] = $prop->name;
  448. // The 'Citation' term is special because it serves
  449. // both as a property and as the uniquename for the
  450. // pub and we want it stored in both the pub table and the pubprop table
  451. if ($prop->name == 'Citation') {
  452. $properties[$prop->name][0] = $node->uniquename;
  453. }
  454. }
  455. // get the properties that should be added. Properties are in one of three forms:
  456. // 1) prop_value-[type id]-[index]
  457. // 2) new_value-[type id]-[index]
  458. // 3) new_id, new_value
  459. // dpm($node);
  460. foreach ($node as $key => $value) {
  461. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  462. $type_id = $matches[1];
  463. $index = $matches[2];
  464. $name = $properties_list[$type_id];
  465. $properties[$name][$index] = trim($value);
  466. }
  467. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  468. $type_id = $matches[1];
  469. $index = $matches[2];
  470. $name = $properties_list[$type_id];
  471. $properties[$name][$index] = trim($value);
  472. }
  473. }
  474. if ($node->new_id and $node->new_value) {
  475. $type_id = $node->new_id;
  476. $name = $properties_list[$type_id];
  477. $index = count($properties[$name]);
  478. $properties[$name][$index] = trim($node->new_value);
  479. }
  480. // iterate through all of the properties and remove those that really are
  481. // part of the pub table fields
  482. foreach ($properties as $name => $element) {
  483. foreach ($element as $index => $value) {
  484. if ($name == "Volume") {
  485. $volume = $value;
  486. unset($properties[$name]);
  487. }
  488. elseif ($name == "Volume Title") {
  489. $volumetitle = $value;
  490. unset($properties[$name]);
  491. }
  492. elseif ($name == "Issue") {
  493. $issue = $value;
  494. unset($properties[$name]);
  495. }
  496. elseif ($name == "Pages") {
  497. $pages = $value;
  498. unset($properties[$name]);
  499. }
  500. elseif ($name == "Publisher") {
  501. $publisher = $value;
  502. unset($properties[$name]);
  503. }
  504. elseif ($name == "Journal Name") {
  505. $series_name = $value;
  506. unset($properties[$name]);
  507. }
  508. elseif ($name == "Journal Country" or $name == "Published Location") {
  509. $pubplace = $value;
  510. unset($properties[$name]);
  511. }
  512. elseif ($name == "Publication Dbxref") {
  513. // we will add the cross-references to the pub_dbxref table
  514. // but we also want to keep the property in the pubprop table so don't unset it
  515. $cross_refs[] = $value;
  516. }
  517. }
  518. }
  519. // update the pub record
  520. $match = array(
  521. 'pub_id' => $pub_id,
  522. );
  523. $values = array(
  524. 'title' => trim($node->title),
  525. 'type_id' => trim($node->type_id),
  526. 'pyear' => trim($node->pyear),
  527. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  528. 'uniquename' => trim($node->uniquename),
  529. 'series_name' => $series_name,
  530. 'volumetitle' => $volumetitle,
  531. 'volume' => $volume,
  532. 'issue' => $issue,
  533. 'pages' => $pages,
  534. 'miniref' => $miniref,
  535. 'publisher' => $publisher,
  536. 'pubplace' => $pubplace,
  537. );
  538. $status = tripal_core_chado_update('pub', $match, $values);
  539. if (!$status) {
  540. drupal_set_message("Error updating publication", "error");
  541. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  542. return;
  543. }
  544. // now add in the properties by first removing any the publication
  545. // already has and adding the ones we have
  546. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  547. foreach ($properties as $property => $elements) {
  548. foreach ($elements as $rank => $value) {
  549. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  550. if (!$status) {
  551. drupal_set_message("Error cannot add property: '$property'", "error");
  552. watchdog('tripal_pub', "Error cannot add property: '%prop'",
  553. array('%prop' => $property), WATCHDOG_ERROR);
  554. }
  555. }
  556. }
  557. // add in any database cross-references after first removing
  558. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  559. foreach ($cross_refs as $index => $ref) {
  560. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  561. if (!$pub_dbxref) {
  562. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  563. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  564. array('%ref' => $ref), WATCHDOG_ERROR);
  565. }
  566. }
  567. }
  568. /**
  569. * Implementation of tripal_pub_load().
  570. *
  571. *
  572. * @param $node
  573. * The node that is to be accessed from the database
  574. *
  575. * @return $node
  576. * The node with the information to be loaded into the database
  577. *
  578. */
  579. function chado_pub_load($node) {
  580. // get the feature details from chado
  581. $pub_id = chado_get_id_for_node('pub', $node);
  582. $values = array('pub_id' => $pub_id);
  583. $pub = tripal_core_generate_chado_var('pub', $values);
  584. // expand the 'text' fields as those aren't included by default
  585. // and they really shouldn't be so large to cause problems
  586. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  587. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  588. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');;
  589. // set the URL path
  590. $path = "pub/$pub_id";
  591. $additions = new stdClass();
  592. $additions->pub = $pub;
  593. return $additions;
  594. }
  595. /**
  596. * Implementation of tripal_pub_delete().
  597. *
  598. * This function takes a node and if the delete button has been chosen by the user, the publication
  599. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  600. * the 'chado_pub' table.
  601. *
  602. * @parm $node
  603. * Then node which contains the information stored within the node-ID
  604. *
  605. */
  606. function chado_pub_delete(&$node) {
  607. $pub_id = chado_get_id_for_node('pub', $node);
  608. // if we don't have a pub id for this node then this isn't a node of
  609. // type chado_pub or the entry in the chado_pub table was lost.
  610. if (!$pub_id) {
  611. return;
  612. }
  613. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  614. // drupal database
  615. $sql_del = "DELETE FROM {chado_pub} ".
  616. "WHERE nid = %d ".
  617. "AND vid = %d";
  618. db_query($sql_del, $node->nid, $node->vid);
  619. $sql_del = "DELETE FROM {node_revisions} ".
  620. "WHERE nid = %d ".
  621. "AND vid = %d";
  622. db_query($sql_del, $node->nid, $node->vid);
  623. $sql_del = "DELETE FROM {node} ".
  624. "WHERE nid = %d ".
  625. "AND vid = %d";
  626. db_query($sql_del, $node->nid, $node->vid);
  627. // Remove data from pub and pubprop tables of chado database as well
  628. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  629. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  630. }
  631. /**
  632. * Because we are using AJAX with a node form we need to provide a callback
  633. * for the chado_pub node form. This callback is different from the
  634. * default 'chado_pub_form' callback
  635. */
  636. /*
  637. function tripal_pub_forms($form_id, $args) {
  638. $forms = array();
  639. if($form_id == 'chado_pub_node_form') {
  640. $forms[$form_id] = array(
  641. 'callback' => 'chado_pub_node_form',
  642. 'callback arguments' => array($args)
  643. );
  644. }
  645. return $forms;
  646. }*/
  647. /*
  648. *
  649. */
  650. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  651. if ($form_id == "tripal_pub_importer_setup_form") {
  652. // updating the form through the ahah callback sets the action of
  653. // the form to the ahah callback URL. We need to set it back
  654. // to the normal form URL
  655. if ($form_state['values']['action'] == 'edit') {
  656. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form_state['values']['pub_import_id']);
  657. }
  658. if ($form_state['values']['action'] == 'new') {
  659. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  660. }
  661. }
  662. if ($form_id == "tripal_pub_search_form") {
  663. $form['#action'] = url("find/publications");
  664. }
  665. if ($form_id == "chado_pub_node_form") {
  666. }
  667. }
  668. /**
  669. *
  670. *
  671. * @ingroup tripal_pub
  672. */
  673. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  674. // we want to provide a new variable that contains the matched pubs.
  675. $pub = $variables['node']->pub;
  676. // normally we would use tripal_core_expand_chado_vars to expand our
  677. // organism object and add in the relationships, however whan a large
  678. // number of relationships are present this significantly slows the
  679. // query, therefore we will manually perform the query
  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.object_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.subject_id = %d
  687. ";
  688. $as_subject = chado_query($sql, $pub->pub_id);
  689. $sql = "
  690. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  691. FROM pub_relationship PR
  692. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  693. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  694. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  695. WHERE PR.object_id = %d
  696. ";
  697. $as_object = chado_query($sql, $pub->pub_id);
  698. // combine both object and subject relationshisp into a single array
  699. $relationships = array();
  700. $relationships['object'] = array();
  701. $relationships['subject'] = array();
  702. // iterate through the object relationships
  703. while ($relationship = db_fetch_object($as_object)) {
  704. // get the relationship and child types
  705. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  706. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  707. if (!array_key_exists($rel_type, $relationships['object'])) {
  708. $relationships['object'][$rel_type] = array();
  709. }
  710. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  711. $relationships['object'][$rel_type][$sub_type] = array();
  712. }
  713. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  714. }
  715. // now add in the subject relationships
  716. while ($relationship = db_fetch_object($as_subject)) {
  717. // get the relationship and child types
  718. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  719. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  720. if (!array_key_exists($rel_type, $relationships['subject'])) {
  721. $relationships['subject'][$rel_type] = array();
  722. }
  723. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  724. $relationships['subject'][$rel_type][$obj_type] = array();
  725. }
  726. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  727. }
  728. $pub->all_relationships = $relationships;
  729. }
  730. /**
  731. *
  732. */
  733. function tripal_pub_mail($key, &$message, $params) {
  734. $language = $message['language'];
  735. $variables = user_mail_tokens($params['account'], $language);
  736. switch($key) {
  737. case 'import_report':
  738. $headers = array(
  739. 'MIME-Version' => '1.0',
  740. 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  741. 'Content-Transfer-Encoding' => '8Bit',
  742. 'X-Mailer' => 'Drupal'
  743. );
  744. foreach ($headers as $key => $value) {
  745. $message['headers'][$key] = $value;
  746. }
  747. $message['subject'] = t('Publication import from !site', $variables, $language->language);
  748. $message['body'][] = $params['message'];
  749. break;
  750. }
  751. }
  752. /*
  753. *
  754. */
  755. function tripal_pub_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  756. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  757. // where [pub id] is the Chado publication ID. This will allow for easy linking
  758. // into the publication without needing to know the node. Of course if you know the
  759. // node that will still work too (e.g. http://[base url]/node/[node id]
  760. // so the nodeapi function ensures that the URL path is set after insert or update
  761. // of the node and when the node is loaded if it hasn't yet been set.
  762. if ($node->type == 'chado_pub') {
  763. switch ($op) {
  764. case 'presave':
  765. break;
  766. case 'insert':
  767. $pub_id = chado_get_id_for_node('pub', $node);
  768. tripal_pub_set_pub_url($node, $pub_id);
  769. break;
  770. case 'load':
  771. if (!$node->path) {
  772. $pub_id = chado_get_id_for_node('pub', $node);
  773. $path = tripal_pub_set_pub_url($node, $pub_id);
  774. }
  775. break;
  776. case 'update':
  777. $pub_id = chado_get_id_for_node('pub', $node);
  778. tripal_pub_set_pub_url($node, $pub_id);
  779. break;
  780. case 'view':
  781. break;
  782. }
  783. }
  784. }