tripal_pub.module 29 KB

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