tripal_pub.module 36 KB

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