tripal_pub.module 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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/importers/PMID.inc";
  9. require_once "includes/importers/AGL.inc";
  10. /**
  11. * @file
  12. *
  13. * The Tripal Publication module allows you to search the PubMed databse for academic articles,
  14. * that relate to user specified tpoic\s. As well, it allows management of publications so that
  15. * a user can enter specified details regarding a desired publication. This allows all of the important
  16. * information that is unique to a Academic Publication to be stored for access.
  17. */
  18. /**
  19. *
  20. * @ingroup tripal_pub
  21. */
  22. function tripal_pub_init() {
  23. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_pub.js');
  24. drupal_add_css(drupal_get_path('theme', 'tripal') . '/css/tripal_pub.css');
  25. }
  26. /**
  27. * Implementation of hook_tripal_pub_node_info().
  28. *
  29. * This node_info, is a simple node that describes the functionallity of the module.
  30. *
  31. */
  32. function tripal_pub_node_info() {
  33. return array(
  34. 'chado_pub' => array(
  35. 'name' => t('Publication'),
  36. 'module' => 'chado_pub',
  37. 'description' => t('A publication from the Chado database'),
  38. 'title_label' => t('Article Title'),
  39. 'body_label' => t('Abstract'),
  40. 'has_title' => TRUE,
  41. 'has_body' => FALSE,
  42. ),
  43. );
  44. }
  45. /**
  46. * Tripal-Publication-Menu
  47. *
  48. * Implemets hook_menu(): Adds menu items for the tripal_pub module menu. This section
  49. * gives the outline for the main menu of the Tripal-Publication module
  50. *
  51. * @return
  52. * An array of menu items that is visible within the Drupal Menu, returned as soon
  53. * as the program is ran
  54. */
  55. function tripal_pub_menu() {
  56. $items = array();
  57. $items['find/publications' ]= array(
  58. 'title' => 'Publication Search',
  59. 'description' => ('Search for publications'),
  60. 'page callback' => 'tripal_pub_search_page',
  61. 'access arguments' => array('access chado_pub content'),
  62. 'type' => MENU_CALLBACK
  63. );
  64. $items['find/publications/criteria/%/%'] = array(
  65. 'page callback' => 'tripal_pub_search_page_update_criteria',
  66. 'page arguments' => array(5, 6),
  67. 'access arguments' => array('access chado_pub content'),
  68. 'type ' => MENU_CALLBACK,
  69. );
  70. $items['admin/tripal/tripal_pub']= array(
  71. 'title' => 'Publications',
  72. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  73. 'page callback' => 'theme',
  74. 'page arguments' => array('tripal_pub_admin'),
  75. 'access arguments' => array('administer tripal pubs'),
  76. 'type' => MENU_NORMAL_ITEM
  77. );
  78. $items['admin/tripal/tripal_pub/configuration'] = array(
  79. 'title' => 'Configuration',
  80. 'description' => 'Configure the Tripal publication module.',
  81. 'page callback' => 'drupal_get_form',
  82. 'page arguments' => array('tripal_pub_admin'),
  83. 'access arguments' => array('administer tripal pubs'),
  84. 'type' => MENU_NORMAL_ITEM,
  85. );
  86. $items['admin/tripal/tripal_pub/sync'] = array(
  87. 'title' => ' Sync Publications',
  88. 'description' => 'Sync publications in Chado with Drupal',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('tripal_pub_sync_form'),
  91. 'access arguments' => array('administer tripal pubs'),
  92. 'type' => MENU_NORMAL_ITEM,
  93. );
  94. $items['admin/tripal/tripal_pub/import_list'] = array(
  95. 'title' => t('Importers List'),
  96. 'description' => t('List all publication importers'),
  97. 'page callback' => 'tripal_pub_importers_list',
  98. 'access arguments' => array('administer tripal pubs'),
  99. 'type ' => MENU_CALLBACK,
  100. );
  101. $items['admin/tripal/tripal_pub/import/new'] = array(
  102. 'title' => t('Add an Importer'),
  103. 'description' => t('Add a new publication importer.'),
  104. 'page callback' => 'tripal_pub_importer_setup',
  105. 'page arguments' => array(4, NULL),
  106. 'access arguments' => array('administer tripal pubs'),
  107. 'type ' => MENU_CALLBACK,
  108. );
  109. $items['admin/tripal/tripal_pub/import/raw/%'] = array(
  110. 'page callback' => 'tripal_pub_get_raw_data',
  111. 'page arguments' => array(5),
  112. 'access arguments' => array('administer tripal pubs'),
  113. 'type ' => MENU_CALLBACK,
  114. );
  115. $items['admin/tripal/tripal_pub/import/edit/%'] = array(
  116. 'page callback' => 'tripal_pub_importer_setup',
  117. 'page arguments' => array(4, 5),
  118. 'access arguments' => array('administer tripal pubs'),
  119. 'type ' => MENU_CALLBACK,
  120. );
  121. $items['admin/tripal/tripal_pub/import/delete/%'] = array(
  122. 'page callback' => 'tripal_pub_importer_delete',
  123. 'page arguments' => array(5),
  124. 'access arguments' => array('administer tripal pubs'),
  125. 'type ' => MENU_CALLBACK,
  126. );
  127. $items['admin/tripal/tripal_pub/import/changedb'] = array(
  128. 'page callback' => 'tripal_pub_importer_setup_page_update_remotedb',
  129. 'page arguments' => array(),
  130. 'access arguments' => array('administer tripal pubs'),
  131. 'type ' => MENU_CALLBACK,
  132. );
  133. $items['admin/tripal/tripal_pub/import/criteria/%/%'] = array(
  134. 'page callback' => 'tripal_pub_importer_setup_page_update_criteria',
  135. 'page arguments' => array(5, 6),
  136. 'access arguments' => array('administer tripal pubs'),
  137. 'type ' => MENU_CALLBACK,
  138. );
  139. $items['tripal_pub/properties/add'] = array(
  140. 'page callback' => 'tripal_pub_property_add',
  141. 'access arguments' => array('edit chado_pub content'),
  142. 'type ' => MENU_CALLBACK,
  143. );
  144. $items['tripal_pub/properties/description'] = array(
  145. 'page callback' => 'tripal_pub_property_get_description',
  146. 'access arguments' => array('edit chado_pub content'),
  147. 'type ' => MENU_CALLBACK,
  148. );
  149. $items['tripal_pub/properties/minus/%/%'] = array(
  150. 'page callback' => 'tripal_pub_property_delete',
  151. 'page arguments' => array(3, 4),
  152. 'access arguments' => array('edit chado_pub content'),
  153. 'type ' => MENU_CALLBACK,
  154. );
  155. return $items;
  156. }
  157. /**
  158. * Implements hook_theme(): Register themeing functions for this module
  159. *
  160. *
  161. * @return
  162. * An array of themeing functions to register
  163. *
  164. */
  165. function tripal_pub_theme() {
  166. $themes = array(
  167. // node templates
  168. 'tripal_pub_base' => array(
  169. 'arguments' => array('node' => NULL),
  170. ),
  171. 'tripal_pub_properties' => array(
  172. 'arguments' => array('node' => NULL)
  173. ),
  174. 'tripal_pub_authors' => array(
  175. 'arguments' => array('node' => NULL)
  176. ),
  177. 'tripal_pub_references' => array(
  178. 'arguments' => array('node' => NULL)
  179. ),
  180. 'tripal_pub_relationships' => array(
  181. 'arguments' => array('node' => NULL)
  182. ),
  183. 'tripal_pub_featuremaps' => array(
  184. 'arguments' => array('node' => NULL)
  185. ),
  186. 'tripal_pub_features' => array(
  187. 'arguments' => array('node' => NULL)
  188. ),
  189. 'tripal_pub_libraries' => array(
  190. 'arguments' => array('node' => NULL)
  191. ),
  192. 'tripal_pub_projects' => array(
  193. 'arguments' => array('node' => NULL)
  194. ),
  195. 'tripal_pub_stocks' => array(
  196. 'arguments' => array('node' => NULL)
  197. ),
  198. // instructions page for the pub module
  199. 'tripal_pub_admin' => array(
  200. 'template' => 'tripal_pub_admin',
  201. 'arguments' => array(NULL),
  202. 'path' => drupal_get_path('module', 'tripal_pub') . '/theme'
  203. ),
  204. // themed forms
  205. 'tripal_pub_importer_setup_form' => array(
  206. 'arguments' => array('form'),
  207. ),
  208. 'tripal_pub_search_form' => array(
  209. 'arguments' => array('form'),
  210. ),
  211. 'chado_pub_node_form' => array(
  212. 'arguments' => array('form'),
  213. ),
  214. );
  215. return $themes;
  216. }
  217. /**
  218. * Implement hook_perm().
  219. */
  220. function tripal_pub_perm() {
  221. return array(
  222. 'access chado_pub content',
  223. 'create chado_pub content',
  224. 'delete chado_pub content',
  225. 'edit chado_pub content',
  226. 'administer tripal pubs',
  227. );
  228. }
  229. /**
  230. * Implement hook_access().
  231. *
  232. * This hook allows node modules to limit access to the node types they define.
  233. *
  234. * @param $op
  235. * The operation to be performed
  236. *
  237. * @param $node
  238. * The node on which the operation is to be performed, or, if it does not yet exist, the
  239. * type of node to be created
  240. *
  241. * @param $account
  242. * A user object representing the user for whom the operation is to be performed
  243. *
  244. * @return
  245. * TRUE
  246. *
  247. */
  248. function chado_pub_access($op, $node, $account ) {
  249. if ($op == 'create') {
  250. if (!user_access('create chado_pub content', $account)) {
  251. return FALSE;
  252. }
  253. }
  254. if ($op == 'update') {
  255. if (!user_access('edit chado_pub content', $account)) {
  256. return FALSE;
  257. }
  258. }
  259. if ($op == 'delete') {
  260. if (!user_access('delete chado_pub content', $account)) {
  261. return FALSE;
  262. }
  263. }
  264. if ($op == 'view') {
  265. if (!user_access('access chado_pub content', $account)) {
  266. return FALSE;
  267. }
  268. }
  269. return NULL;
  270. }
  271. /**
  272. * Implementation of tripal_pub_insert().
  273. *
  274. * This function inserts user entered information pertaining to the Publication instance into the
  275. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  276. *
  277. * @parm $node
  278. * Then node which contains the information stored within the node-ID
  279. *
  280. *
  281. */
  282. function chado_pub_insert($node) {
  283. // if a pub_id already exists for this node then it already exists in Chado and
  284. // we get here because we are syncing the node. Therefore, we can skip the insert
  285. // but we always want to set the URL path alias to be the Chado pub ID
  286. if ($node->pub_id) {
  287. $pub['pub_id'] = $node->pub_id;
  288. }
  289. else {
  290. $properties = array(); // stores all of the properties we need to add
  291. $cross_refs = array(); // stores any cross references for this publication
  292. // get the list of properties for easy lookup (without doing lots of database queries
  293. $properties_list = array();
  294. $sql = "
  295. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  296. FROM {cvtermpath} CVTP
  297. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  298. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  299. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  300. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  301. NOT CVTS.is_obsolete = 1
  302. ORDER BY CVTS.name ASC
  303. ";
  304. $prop_types = chado_query($sql);
  305. while ($prop = db_fetch_object($prop_types)) {
  306. $properties_list[$prop->cvterm_id] = $prop->name;
  307. // The 'Citation' term is special because it serves
  308. // both as a property and as the uniquename for the
  309. // pub and we want it stored in both the pub table and the pubprop table
  310. if ($prop->name == 'Citation') {
  311. $properties[$prop->name][0] = $node->uniquename;
  312. }
  313. }
  314. // get the properties that should be added. Properties are in one of two forms:
  315. // 1) prop_value-[type id]-[index]
  316. // 2) new_value-[type id]-[index]
  317. // 3) new_id, new_value
  318. foreach ($node as $name => $value) {
  319. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  320. $type_id = $matches[1];
  321. $index = $matches[2];
  322. $name = $properties_list[$type_id];
  323. $properties[$name][$index] = trim($value);
  324. }
  325. }
  326. if ($node->new_id and $node->new_value) {
  327. $type_id = $node->new_id;
  328. $index = count($properties[$name]);
  329. $name = $properties_list[$type_id];
  330. $properties[$name][$index] = trim($node->new_value);
  331. }
  332. // iterate through all of the properties and remove those that really are
  333. // part of the pub table fields
  334. foreach ($properties as $name => $element) {
  335. $value = trim($element[0]);
  336. if ($name == "Volume") {
  337. $volume = $value;
  338. unset($properties[$name]);
  339. }
  340. elseif ($name == "Volume Title") {
  341. $volumetitle = $value;
  342. unset($properties[$name]);
  343. }
  344. elseif ($name == "Issue") {
  345. $issue = $value;
  346. unset($properties[$name]);
  347. }
  348. elseif ($name == "Pages") {
  349. $pages = $value;
  350. unset($properties[$name]);
  351. }
  352. elseif ($name == "Publisher") {
  353. $publisher = $value;
  354. unset($properties[$name]);
  355. }
  356. elseif ($name == "Journal Name" or $name == "Conference Name") {
  357. $node->series_name = $value;
  358. unset($properties[$name]);
  359. }
  360. elseif ($name == "Journal Country" or $name == "Published Location") {
  361. $pubplace = $value;
  362. unset($properties[$name]);
  363. }
  364. elseif ($name == "Publication Dbxref") {
  365. // we will add the cross-references to the pub_dbxref table
  366. // but we also want to keep the property in the pubprop table so don't unset it
  367. $cross_refs[] = $value;
  368. }
  369. }
  370. // insert the pub record
  371. $values = array(
  372. 'title' => trim($node->pubtitle),
  373. 'series_name' => trim($node->series_name),
  374. 'type_id' => trim($node->type_id),
  375. 'pyear' => trim($node->pyear),
  376. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  377. 'uniquename' => trim($node->uniquename),
  378. 'volumetitle' => $volumetitle,
  379. 'volume' => $volume,
  380. 'issue' => $issue,
  381. 'pages' => $pages,
  382. 'miniref' => $miniref,
  383. 'publisher' => $publisher,
  384. 'pubplace' => $pubplace,
  385. );
  386. $pub = tripal_core_chado_insert('pub', $values);
  387. if (!$pub) {
  388. drupal_set_message("Error inserting publication", "error");
  389. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  390. return;
  391. }
  392. // now add in the properties
  393. foreach ($properties as $property => $elements) {
  394. foreach ($elements as $rank => $value) {
  395. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  396. if (!$status) {
  397. drupal_set_message("Error cannot add property: $property", "error");
  398. watchdog('tripal_pub', "Error cannot add property: %prop",
  399. array('%property' => $property), WATCHDOG_ERROR);
  400. }
  401. }
  402. }
  403. // add in any database cross-references
  404. foreach ($cross_refs as $index => $ref) {
  405. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  406. if (!$pub_dbxref) {
  407. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  408. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  409. array('%ref' => $ref), WATCHDOG_ERROR);
  410. }
  411. }
  412. } // if ($node->pub_id) {} else
  413. if ($pub) {
  414. // make sure the entry for this feature doesn't already exist in the chado_pub table
  415. // if it doesn't exist then we want to add it.
  416. $pub_id = chado_get_id_for_node('pub', $node) ;
  417. if (!$pub_id) {
  418. // next add the item to the drupal table
  419. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  420. "VALUES (%d, %d, %d)";
  421. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  422. }
  423. }
  424. else {
  425. drupal_set_message(t('Unable to add publication.', 'warning'));
  426. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  427. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  428. }
  429. }
  430. /*
  431. *
  432. * Implements hook_update
  433. *
  434. * The purpose of the function is to allow the module to take action when an edited node is being
  435. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  436. * As well, the database will be changed, so the user changed information will be saved to the database.
  437. *
  438. * @param $node
  439. * The node being updated
  440. *
  441. * @ingroup tripal_pub
  442. */
  443. function chado_pub_update($node) {
  444. if ($node->revision) {
  445. // there is no way to handle revisions in Chado but leave
  446. // this here just to make not we've addressed it.
  447. }
  448. // get the publication ID for this publication
  449. $pub_id = chado_get_id_for_node('pub', $node) ;
  450. $properties = array(); // stores all of the properties we need to add
  451. $cross_refs = array(); // stores any cross references for this publication
  452. // get the list of properties for easy lookup (without doing lots of database queries
  453. $properties_list = array();
  454. $sql = "
  455. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  456. FROM {cvtermpath} CVTP
  457. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  458. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  459. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  460. WHERE CV.name = 'tripal_pub' and
  461. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  462. NOT CVTS.is_obsolete = 1
  463. ORDER BY CVTS.name ASC
  464. ";
  465. $prop_types = chado_query($sql);
  466. while ($prop = db_fetch_object($prop_types)) {
  467. $properties_list[$prop->cvterm_id] = $prop->name;
  468. // The 'Citation' term is special because it serves
  469. // both as a property and as the uniquename for the
  470. // pub and we want it stored in both the pub table and the pubprop table
  471. if ($prop->name == 'Citation') {
  472. $properties[$prop->name][0] = $node->uniquename;
  473. }
  474. }
  475. // get the properties that should be added. Properties are in one of three forms:
  476. // 1) prop_value-[type id]-[index]
  477. // 2) new_value-[type id]-[index]
  478. // 3) new_id, new_value
  479. // dpm($node);
  480. foreach ($node as $key => $value) {
  481. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  482. $type_id = $matches[1];
  483. $index = $matches[2];
  484. $name = $properties_list[$type_id];
  485. $properties[$name][$index] = trim($value);
  486. }
  487. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  488. $type_id = $matches[1];
  489. $index = $matches[2];
  490. $name = $properties_list[$type_id];
  491. $properties[$name][$index] = trim($value);
  492. }
  493. }
  494. if ($node->new_id and $node->new_value) {
  495. $type_id = $node->new_id;
  496. $name = $properties_list[$type_id];
  497. $index = count($properties[$name]);
  498. $properties[$name][$index] = trim($node->new_value);
  499. }
  500. // iterate through all of the properties and remove those that really are
  501. // part of the pub table fields
  502. foreach ($properties as $name => $element) {
  503. foreach ($element as $index => $value) {
  504. if ($name == "Volume") {
  505. $volume = $value;
  506. unset($properties[$name]);
  507. }
  508. elseif ($name == "Volume Title") {
  509. $volumetitle = $value;
  510. unset($properties[$name]);
  511. }
  512. elseif ($name == "Issue") {
  513. $issue = $value;
  514. unset($properties[$name]);
  515. }
  516. elseif ($name == "Pages") {
  517. $pages = $value;
  518. unset($properties[$name]);
  519. }
  520. elseif ($name == "Publisher") {
  521. $publisher = $value;
  522. unset($properties[$name]);
  523. }
  524. elseif ($name == "Journal Name" or $name == "Conference Name") {
  525. $node->series_name = $value;
  526. unset($properties[$name]);
  527. }
  528. elseif ($name == "Journal Country" or $name == "Published Location") {
  529. $pubplace = $value;
  530. unset($properties[$name]);
  531. }
  532. elseif ($name == "Publication Dbxref") {
  533. // we will add the cross-references to the pub_dbxref table
  534. // but we also want to keep the property in the pubprop table so don't unset it
  535. $cross_refs[] = $value;
  536. }
  537. }
  538. }
  539. // update the pub record
  540. $match = array(
  541. 'pub_id' => $pub_id,
  542. );
  543. $values = array(
  544. 'title' => trim($node->pubtitle),
  545. 'type_id' => trim($node->type_id),
  546. 'pyear' => trim($node->pyear),
  547. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  548. 'uniquename' => trim($node->uniquename),
  549. 'series_name' => trim($node->series_name),
  550. 'volumetitle' => $volumetitle,
  551. 'volume' => $volume,
  552. 'issue' => $issue,
  553. 'pages' => $pages,
  554. 'miniref' => $miniref,
  555. 'publisher' => $publisher,
  556. 'pubplace' => $pubplace,
  557. );
  558. $status = tripal_core_chado_update('pub', $match, $values);
  559. if (!$status) {
  560. drupal_set_message("Error updating publication", "error");
  561. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  562. return;
  563. }
  564. // now add in the properties by first removing any the publication
  565. // already has and adding the ones we have
  566. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  567. foreach ($properties as $property => $elements) {
  568. foreach ($elements as $rank => $value) {
  569. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  570. if (!$status) {
  571. drupal_set_message("Error cannot add property: '$property'", "error");
  572. watchdog('tripal_pub', "Error cannot add property: '%prop'",
  573. array('%prop' => $property), WATCHDOG_ERROR);
  574. }
  575. }
  576. }
  577. // add in any database cross-references after first removing
  578. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  579. foreach ($cross_refs as $index => $ref) {
  580. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  581. if (!$pub_dbxref) {
  582. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  583. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  584. array('%ref' => $ref), WATCHDOG_ERROR);
  585. }
  586. }
  587. }
  588. /**
  589. * Implementation of tripal_pub_load().
  590. *
  591. *
  592. * @param $node
  593. * The node that is to be accessed from the database
  594. *
  595. * @return $node
  596. * The node with the information to be loaded into the database
  597. *
  598. */
  599. function chado_pub_load($node) {
  600. // get the feature details from chado
  601. $pub_id = chado_get_id_for_node('pub', $node);
  602. $values = array('pub_id' => $pub_id);
  603. $pub = tripal_core_generate_chado_var('pub', $values);
  604. // expand the 'text' fields as those aren't included by default
  605. // and they really shouldn't be so large to cause problems
  606. if (is_array($pub) or is_object($pub)) {
  607. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  608. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  609. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  610. }
  611. // set the URL path
  612. $path = "pub/$pub_id";
  613. $additions = new stdClass();
  614. $additions->pub = $pub;
  615. return $additions;
  616. }
  617. /**
  618. * Implementation of tripal_pub_delete().
  619. *
  620. * This function takes a node and if the delete button has been chosen by the user, the publication
  621. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  622. * the 'chado_pub' table.
  623. *
  624. * @parm $node
  625. * Then node which contains the information stored within the node-ID
  626. *
  627. */
  628. function chado_pub_delete(&$node) {
  629. $pub_id = chado_get_id_for_node('pub', $node);
  630. // if we don't have a pub id for this node then this isn't a node of
  631. // type chado_pub or the entry in the chado_pub table was lost.
  632. if (!$pub_id) {
  633. return;
  634. }
  635. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  636. // drupal database
  637. $sql_del = "DELETE FROM {chado_pub} ".
  638. "WHERE nid = %d ".
  639. "AND vid = %d";
  640. db_query($sql_del, $node->nid, $node->vid);
  641. $sql_del = "DELETE FROM {node_revisions} ".
  642. "WHERE nid = %d ".
  643. "AND vid = %d";
  644. db_query($sql_del, $node->nid, $node->vid);
  645. $sql_del = "DELETE FROM {node} ".
  646. "WHERE nid = %d ".
  647. "AND vid = %d";
  648. db_query($sql_del, $node->nid, $node->vid);
  649. // Remove data from pub and pubprop tables of chado database as well
  650. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  651. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  652. }
  653. /**
  654. * Because we are using AJAX with a node form we need to provide a callback
  655. * for the chado_pub node form. This callback is different from the
  656. * default 'chado_pub_form' callback
  657. */
  658. /*
  659. function tripal_pub_forms($form_id, $args) {
  660. $forms = array();
  661. if($form_id == 'chado_pub_node_form') {
  662. $forms[$form_id] = array(
  663. 'callback' => 'chado_pub_node_form',
  664. 'callback arguments' => array($args)
  665. );
  666. }
  667. return $forms;
  668. }*/
  669. /*
  670. *
  671. */
  672. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  673. if ($form_id == "tripal_pub_importer_setup_form") {
  674. // updating the form through the ahah callback sets the action of
  675. // the form to the ahah callback URL. We need to set it back
  676. // to the normal form URL
  677. if ($form_state['values']['action'] == 'edit') {
  678. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form_state['values']['pub_import_id']);
  679. }
  680. if ($form_state['values']['action'] == 'new') {
  681. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  682. }
  683. }
  684. if ($form_id == "tripal_pub_search_form") {
  685. $form['#action'] = url("find/publications");
  686. }
  687. if ($form_id == "chado_pub_node_form") {
  688. }
  689. }
  690. /**
  691. *
  692. *
  693. * @ingroup tripal_pub
  694. */
  695. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  696. // we want to provide a new variable that contains the matched pubs.
  697. $pub = $variables['node']->pub;
  698. // normally we would use tripal_core_expand_chado_vars to expand our
  699. // organism object and add in the relationships, however whan a large
  700. // number of relationships are present this significantly slows the
  701. // query, therefore we will manually perform the query
  702. $sql = "
  703. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  704. FROM pub_relationship PR
  705. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  706. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  707. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  708. WHERE PR.subject_id = %d
  709. ";
  710. $as_subject = chado_query($sql, $pub->pub_id);
  711. $sql = "
  712. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  713. FROM pub_relationship PR
  714. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  715. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  716. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  717. WHERE PR.object_id = %d
  718. ";
  719. $as_object = chado_query($sql, $pub->pub_id);
  720. // combine both object and subject relationshisp into a single array
  721. $relationships = array();
  722. $relationships['object'] = array();
  723. $relationships['subject'] = array();
  724. // iterate through the object relationships
  725. while ($relationship = db_fetch_object($as_object)) {
  726. // get the relationship and child types
  727. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  728. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  729. if (!array_key_exists($rel_type, $relationships['object'])) {
  730. $relationships['object'][$rel_type] = array();
  731. }
  732. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  733. $relationships['object'][$rel_type][$sub_type] = array();
  734. }
  735. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  736. }
  737. // now add in the subject relationships
  738. while ($relationship = db_fetch_object($as_subject)) {
  739. // get the relationship and child types
  740. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  741. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  742. if (!array_key_exists($rel_type, $relationships['subject'])) {
  743. $relationships['subject'][$rel_type] = array();
  744. }
  745. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  746. $relationships['subject'][$rel_type][$obj_type] = array();
  747. }
  748. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  749. }
  750. $pub->all_relationships = $relationships;
  751. }
  752. /**
  753. *
  754. */
  755. function tripal_pub_mail($key, &$message, $params) {
  756. $language = $message['language'];
  757. $variables = user_mail_tokens($params['account'], $language);
  758. switch($key) {
  759. case 'import_report':
  760. $headers = array(
  761. 'MIME-Version' => '1.0',
  762. 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  763. 'Content-Transfer-Encoding' => '8Bit',
  764. 'X-Mailer' => 'Drupal'
  765. );
  766. foreach ($headers as $key => $value) {
  767. $message['headers'][$key] = $value;
  768. }
  769. $message['subject'] = t('Publication import from !site', $variables, $language->language);
  770. $message['body'][] = $params['message'];
  771. break;
  772. }
  773. }
  774. /*
  775. *
  776. */
  777. function tripal_pub_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  778. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  779. // where [pub id] is the Chado publication ID. This will allow for easy linking
  780. // into the publication without needing to know the node. Of course if you know the
  781. // node that will still work too (e.g. http://[base url]/node/[node id]
  782. // so the nodeapi function ensures that the URL path is set after insert or update
  783. // of the node and when the node is loaded if it hasn't yet been set.
  784. if ($node->type == 'chado_pub') {
  785. switch ($op) {
  786. case 'presave':
  787. break;
  788. case 'insert':
  789. $pub_id = chado_get_id_for_node('pub', $node);
  790. tripal_pub_set_pub_url($node, $pub_id);
  791. break;
  792. case 'load':
  793. if (!$node->path) {
  794. $pub_id = chado_get_id_for_node('pub', $node);
  795. $path = tripal_pub_set_pub_url($node, $pub_id);
  796. }
  797. break;
  798. case 'update':
  799. $pub_id = chado_get_id_for_node('pub', $node);
  800. tripal_pub_set_pub_url($node, $pub_id);
  801. break;
  802. case 'view':
  803. break;
  804. }
  805. }
  806. }