tripal_pub.module 37 KB

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