tripal_pub.module 36 KB

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