tripal_pub.module 31 KB

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