tripal_pub.module 33 KB

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