tripal_pub.module 34 KB

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