tripal_pub.module 34 KB

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