tripal_pub.module 30 KB

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