tripal_pub.module 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. * If the permission for the specified operation is not set then return FALSE. If the
  246. * permission is set then return NULL as this allows other modules to disable
  247. * access. The only exception is when the $op == 'create'. We will always
  248. * return TRUE if the permission is set.
  249. *
  250. */
  251. function chado_pub_access($op, $node, $account ) {
  252. if ($op == 'create') {
  253. if (!user_access('create chado_pub content', $account)) {
  254. return FALSE;
  255. }
  256. return TRUE;
  257. }
  258. if ($op == 'update') {
  259. if (!user_access('edit chado_pub content', $account)) {
  260. return FALSE;
  261. }
  262. }
  263. if ($op == 'delete') {
  264. if (!user_access('delete chado_pub content', $account)) {
  265. return FALSE;
  266. }
  267. }
  268. if ($op == 'view') {
  269. if (!user_access('access chado_pub content', $account)) {
  270. return FALSE;
  271. }
  272. }
  273. return NULL;
  274. }
  275. /**
  276. * Implementation of tripal_pub_insert().
  277. *
  278. * This function inserts user entered information pertaining to the Publication instance into the
  279. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  280. *
  281. * @parm $node
  282. * Then node which contains the information stored within the node-ID
  283. *
  284. *
  285. */
  286. function chado_pub_insert($node) {
  287. // if a pub_id already exists for this node then it already exists in Chado and
  288. // we get here because we are syncing the node. Therefore, we can skip the insert
  289. // but we always want to set the URL path alias to be the Chado pub ID
  290. if ($node->pub_id) {
  291. $pub['pub_id'] = $node->pub_id;
  292. }
  293. else {
  294. $properties = array(); // stores all of the properties we need to add
  295. $cross_refs = array(); // stores any cross references for this publication
  296. // get the list of properties for easy lookup (without doing lots of database queries
  297. $properties_list = array();
  298. $sql = "
  299. SELECT CVTS.cvterm_id, CVTS.name, CVTS.definition
  300. FROM {cvtermpath} CVTP
  301. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  302. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  303. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  304. WHERE CV.name = 'tripal_pub' and CVTO.name = 'Publication Details' and
  305. NOT CVTS.is_obsolete = 1
  306. ORDER BY CVTS.name ASC
  307. ";
  308. $prop_types = chado_query($sql);
  309. while ($prop = db_fetch_object($prop_types)) {
  310. $properties_list[$prop->cvterm_id] = $prop->name;
  311. // The 'Citation' term is special because it serves
  312. // both as a property and as the uniquename for the
  313. // pub and we want it stored in both the pub table and the pubprop table
  314. if ($prop->name == 'Citation') {
  315. $properties[$prop->name][0] = $node->uniquename;
  316. }
  317. }
  318. // get the properties that should be added. Properties are in one of two forms:
  319. // 1) prop_value-[type id]-[index]
  320. // 2) new_value-[type id]-[index]
  321. // 3) new_id, new_value
  322. foreach ($node as $name => $value) {
  323. if (preg_match('/^new_value-(\d+)-(\d+)/', $name, $matches)) {
  324. $type_id = $matches[1];
  325. $index = $matches[2];
  326. $name = $properties_list[$type_id];
  327. $properties[$name][$index] = trim($value);
  328. }
  329. }
  330. if ($node->new_id and $node->new_value) {
  331. $type_id = $node->new_id;
  332. $index = count($properties[$name]);
  333. $name = $properties_list[$type_id];
  334. $properties[$name][$index] = trim($node->new_value);
  335. }
  336. // iterate through all of the properties and remove those that really are
  337. // part of the pub table fields
  338. foreach ($properties as $name => $element) {
  339. $value = trim($element[0]);
  340. if ($name == "Volume") {
  341. $volume = $value;
  342. unset($properties[$name]);
  343. }
  344. elseif ($name == "Volume Title") {
  345. $volumetitle = $value;
  346. unset($properties[$name]);
  347. }
  348. elseif ($name == "Issue") {
  349. $issue = $value;
  350. unset($properties[$name]);
  351. }
  352. elseif ($name == "Pages") {
  353. $pages = $value;
  354. unset($properties[$name]);
  355. }
  356. elseif ($name == "Publisher") {
  357. $publisher = $value;
  358. unset($properties[$name]);
  359. }
  360. elseif ($name == "Journal Name" or $name == "Conference Name") {
  361. $node->series_name = $value;
  362. unset($properties[$name]);
  363. }
  364. elseif ($name == "Journal Country" or $name == "Published Location") {
  365. $pubplace = $value;
  366. unset($properties[$name]);
  367. }
  368. elseif ($name == "Publication Dbxref") {
  369. // we will add the cross-references to the pub_dbxref table
  370. // but we also want to keep the property in the pubprop table so don't unset it
  371. $cross_refs[] = $value;
  372. }
  373. }
  374. // insert the pub record
  375. $values = array(
  376. 'title' => trim($node->pubtitle),
  377. 'series_name' => trim($node->series_name),
  378. 'type_id' => trim($node->type_id),
  379. 'pyear' => trim($node->pyear),
  380. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  381. 'uniquename' => trim($node->uniquename),
  382. 'volumetitle' => $volumetitle,
  383. 'volume' => $volume,
  384. 'issue' => $issue,
  385. 'pages' => $pages,
  386. 'miniref' => $miniref,
  387. 'publisher' => $publisher,
  388. 'pubplace' => $pubplace,
  389. );
  390. $pub = tripal_core_chado_insert('pub', $values);
  391. if (!$pub) {
  392. drupal_set_message("Error inserting publication", "error");
  393. watchdog('tripal_pub', "Error inserting publication", array(), WATCHDOG_ERROR);
  394. return;
  395. }
  396. // now add in the properties
  397. foreach ($properties as $property => $elements) {
  398. foreach ($elements as $rank => $value) {
  399. $status = tripal_pub_insert_property($pub['pub_id'], $property, $value, FALSE);
  400. if (!$status) {
  401. drupal_set_message("Error cannot add property: $property", "error");
  402. watchdog('tripal_pub', "Error cannot add property: %prop",
  403. array('%property' => $property), WATCHDOG_ERROR);
  404. }
  405. }
  406. }
  407. // add in any database cross-references
  408. foreach ($cross_refs as $index => $ref) {
  409. $pub_dbxref = tripal_pub_add_pub_dbxref($pub['pub_id'], trim($ref));
  410. if (!$pub_dbxref) {
  411. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  412. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  413. array('%ref' => $ref), WATCHDOG_ERROR);
  414. }
  415. }
  416. } // if ($node->pub_id) {} else
  417. if ($pub) {
  418. // make sure the entry for this feature doesn't already exist in the chado_pub table
  419. // if it doesn't exist then we want to add it.
  420. $pub_id = chado_get_id_for_node('pub', $node) ;
  421. if (!$pub_id) {
  422. // next add the item to the drupal table
  423. $sql = "INSERT INTO {chado_pub} (nid, vid, pub_id) ".
  424. "VALUES (%d, %d, %d)";
  425. db_query($sql, $node->nid, $node->vid, $pub['pub_id']);
  426. }
  427. }
  428. else {
  429. drupal_set_message(t('Unable to add publication.', 'warning'));
  430. watchdog('tripal_pub', 'Insert publication: Unable to create pub where values: %values',
  431. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  432. }
  433. }
  434. /*
  435. *
  436. * Implements hook_update
  437. *
  438. * The purpose of the function is to allow the module to take action when an edited node is being
  439. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  440. * As well, the database will be changed, so the user changed information will be saved to the database.
  441. *
  442. * @param $node
  443. * The node being updated
  444. *
  445. * @ingroup tripal_pub
  446. */
  447. function chado_pub_update($node) {
  448. if ($node->revision) {
  449. // there is no way to handle revisions in Chado but leave
  450. // this here just to make not we've addressed it.
  451. }
  452. // get the publication ID for this publication
  453. $pub_id = chado_get_id_for_node('pub', $node) ;
  454. $properties = array(); // stores all of the properties we need to add
  455. $cross_refs = array(); // stores any cross references for this publication
  456. // get the list of properties for easy lookup (without doing lots of database queries
  457. $properties_list = array();
  458. $sql = "
  459. SELECT DISTINCT CVTS.cvterm_id, CVTS.name, CVTS.definition
  460. FROM {cvtermpath} CVTP
  461. INNER JOIN {cvterm} CVTS ON CVTP.subject_id = CVTS.cvterm_id
  462. INNER JOIN {cvterm} CVTO ON CVTP.object_id = CVTO.cvterm_id
  463. INNER JOIN {cv} ON CVTO.cv_id = CV.cv_id
  464. WHERE CV.name = 'tripal_pub' and
  465. (CVTO.name = 'Publication Details' or CVTS.name = 'Publication Type') and
  466. NOT CVTS.is_obsolete = 1
  467. ORDER BY CVTS.name ASC
  468. ";
  469. $prop_types = chado_query($sql);
  470. while ($prop = db_fetch_object($prop_types)) {
  471. $properties_list[$prop->cvterm_id] = $prop->name;
  472. // The 'Citation' term is special because it serves
  473. // both as a property and as the uniquename for the
  474. // pub and we want it stored in both the pub table and the pubprop table
  475. if ($prop->name == 'Citation') {
  476. $properties[$prop->name][0] = $node->uniquename;
  477. }
  478. }
  479. // get the properties that should be added. Properties are in one of three forms:
  480. // 1) prop_value-[type id]-[index]
  481. // 2) new_value-[type id]-[index]
  482. // 3) new_id, new_value
  483. // dpm($node);
  484. foreach ($node as $key => $value) {
  485. if (preg_match('/^prop_value-(\d+)-(\d+)/', $key, $matches)) {
  486. $type_id = $matches[1];
  487. $index = $matches[2];
  488. $name = $properties_list[$type_id];
  489. $properties[$name][$index] = trim($value);
  490. }
  491. if (preg_match('/^new_value-(\d+)-(\d+)/', $key, $matches)) {
  492. $type_id = $matches[1];
  493. $index = $matches[2];
  494. $name = $properties_list[$type_id];
  495. $properties[$name][$index] = trim($value);
  496. }
  497. }
  498. if ($node->new_id and $node->new_value) {
  499. $type_id = $node->new_id;
  500. $name = $properties_list[$type_id];
  501. $index = count($properties[$name]);
  502. $properties[$name][$index] = trim($node->new_value);
  503. }
  504. // iterate through all of the properties and remove those that really are
  505. // part of the pub table fields
  506. foreach ($properties as $name => $element) {
  507. foreach ($element as $index => $value) {
  508. if ($name == "Volume") {
  509. $volume = $value;
  510. unset($properties[$name]);
  511. }
  512. elseif ($name == "Volume Title") {
  513. $volumetitle = $value;
  514. unset($properties[$name]);
  515. }
  516. elseif ($name == "Issue") {
  517. $issue = $value;
  518. unset($properties[$name]);
  519. }
  520. elseif ($name == "Pages") {
  521. $pages = $value;
  522. unset($properties[$name]);
  523. }
  524. elseif ($name == "Publisher") {
  525. $publisher = $value;
  526. unset($properties[$name]);
  527. }
  528. elseif ($name == "Journal Name" or $name == "Conference Name") {
  529. $node->series_name = $value;
  530. unset($properties[$name]);
  531. }
  532. elseif ($name == "Journal Country" or $name == "Published Location") {
  533. $pubplace = $value;
  534. unset($properties[$name]);
  535. }
  536. elseif ($name == "Publication Dbxref") {
  537. // we will add the cross-references to the pub_dbxref table
  538. // but we also want to keep the property in the pubprop table so don't unset it
  539. $cross_refs[] = $value;
  540. }
  541. }
  542. }
  543. // update the pub record
  544. $match = array(
  545. 'pub_id' => $pub_id,
  546. );
  547. $values = array(
  548. 'title' => trim($node->pubtitle),
  549. 'type_id' => trim($node->type_id),
  550. 'pyear' => trim($node->pyear),
  551. 'is_obsolete' => $node->is_obsolete ? 'true' : 'false',
  552. 'uniquename' => trim($node->uniquename),
  553. 'series_name' => trim($node->series_name),
  554. 'volumetitle' => $volumetitle,
  555. 'volume' => $volume,
  556. 'issue' => $issue,
  557. 'pages' => $pages,
  558. 'miniref' => $miniref,
  559. 'publisher' => $publisher,
  560. 'pubplace' => $pubplace,
  561. );
  562. $status = tripal_core_chado_update('pub', $match, $values);
  563. if (!$status) {
  564. drupal_set_message("Error updating publication", "error");
  565. watchdog('tripal_pub', "Error updating publication", array(), WATCHDOG_ERROR);
  566. return;
  567. }
  568. // now add in the properties by first removing any the publication
  569. // already has and adding the ones we have
  570. tripal_core_chado_delete('pubprop', array('pub_id' => $pub_id));
  571. foreach ($properties as $property => $elements) {
  572. foreach ($elements as $rank => $value) {
  573. $status = tripal_pub_insert_property($pub_id, $property, $value, FALSE);
  574. if (!$status) {
  575. drupal_set_message("Error cannot add property: '$property'", "error");
  576. watchdog('tripal_pub', "Error cannot add property: '%prop'",
  577. array('%prop' => $property), WATCHDOG_ERROR);
  578. }
  579. }
  580. }
  581. // add in any database cross-references after first removing
  582. tripal_core_chado_delete('pub_dbxref', array('pub_id' => $pub_id));
  583. foreach ($cross_refs as $index => $ref) {
  584. $pub_dbxref = tripal_pub_add_pub_dbxref($pub_id, trim($ref));
  585. if (!$pub_dbxref) {
  586. drupal_set_message("Error cannot add publication cross reference: $ref", "error");
  587. watchdog('tripal_pub', "Error cannot add publication cross reference: %ref",
  588. array('%ref' => $ref), WATCHDOG_ERROR);
  589. }
  590. }
  591. }
  592. /**
  593. * Implementation of tripal_pub_load().
  594. *
  595. *
  596. * @param $node
  597. * The node that is to be accessed from the database
  598. *
  599. * @return $node
  600. * The node with the information to be loaded into the database
  601. *
  602. */
  603. function chado_pub_load($node) {
  604. // get the feature details from chado
  605. $pub_id = chado_get_id_for_node('pub', $node);
  606. $values = array('pub_id' => $pub_id);
  607. $pub = tripal_core_generate_chado_var('pub', $values);
  608. // expand the 'text' fields as those aren't included by default
  609. // and they really shouldn't be so large to cause problems
  610. if (is_array($pub) or is_object($pub)) {
  611. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.title');
  612. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.volumetitle');
  613. $pub = tripal_core_expand_chado_vars($pub, 'field', 'pub.uniquename');
  614. }
  615. // set the URL path
  616. $path = "pub/$pub_id";
  617. $additions = new stdClass();
  618. $additions->pub = $pub;
  619. return $additions;
  620. }
  621. /**
  622. * Implementation of tripal_pub_delete().
  623. *
  624. * This function takes a node and if the delete button has been chosen by the user, the publication
  625. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  626. * the 'chado_pub' table.
  627. *
  628. * @parm $node
  629. * Then node which contains the information stored within the node-ID
  630. *
  631. */
  632. function chado_pub_delete(&$node) {
  633. $pub_id = chado_get_id_for_node('pub', $node);
  634. // if we don't have a pub id for this node then this isn't a node of
  635. // type chado_pub or the entry in the chado_pub table was lost.
  636. if (!$pub_id) {
  637. return;
  638. }
  639. // Remove data from {chado_pub}, {node} and {node_revisions} tables of
  640. // drupal database
  641. $sql_del = "DELETE FROM {chado_pub} ".
  642. "WHERE nid = %d ".
  643. "AND vid = %d";
  644. db_query($sql_del, $node->nid, $node->vid);
  645. $sql_del = "DELETE FROM {node_revisions} ".
  646. "WHERE nid = %d ".
  647. "AND vid = %d";
  648. db_query($sql_del, $node->nid, $node->vid);
  649. $sql_del = "DELETE FROM {node} ".
  650. "WHERE nid = %d ".
  651. "AND vid = %d";
  652. db_query($sql_del, $node->nid, $node->vid);
  653. // Remove data from pub and pubprop tables of chado database as well
  654. chado_query("DELETE FROM {pubprop} WHERE pub_id = %d", $pub_id);
  655. chado_query("DELETE FROM {pub} WHERE pub_id = %d", $pub_id);
  656. }
  657. /**
  658. * Because we are using AJAX with a node form we need to provide a callback
  659. * for the chado_pub node form. This callback is different from the
  660. * default 'chado_pub_form' callback
  661. */
  662. /*
  663. function tripal_pub_forms($form_id, $args) {
  664. $forms = array();
  665. if($form_id == 'chado_pub_node_form') {
  666. $forms[$form_id] = array(
  667. 'callback' => 'chado_pub_node_form',
  668. 'callback arguments' => array($args)
  669. );
  670. }
  671. return $forms;
  672. }*/
  673. /*
  674. *
  675. */
  676. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  677. if ($form_id == "tripal_pub_importer_setup_form") {
  678. // updating the form through the ahah callback sets the action of
  679. // the form to the ahah callback URL. We need to set it back
  680. // to the normal form URL
  681. if ($form_state['values']['action'] == 'edit') {
  682. $form['#action'] = url("admin/tripal/tripal_pub/import/edit/" . $form_state['values']['pub_import_id']);
  683. }
  684. if ($form_state['values']['action'] == 'new') {
  685. $form['#action'] = url("admin/tripal/tripal_pub/import/new");
  686. }
  687. }
  688. if ($form_id == "tripal_pub_search_form") {
  689. $form['#action'] = url("find/publications");
  690. }
  691. if ($form_id == "chado_pub_node_form") {
  692. }
  693. }
  694. /**
  695. *
  696. *
  697. * @ingroup tripal_pub
  698. */
  699. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  700. // we want to provide a new variable that contains the matched pubs.
  701. $pub = $variables['node']->pub;
  702. // normally we would use tripal_core_expand_chado_vars to expand our
  703. // organism object and add in the relationships, however whan a large
  704. // number of relationships are present this significantly slows the
  705. // query, therefore we will manually perform the query
  706. $sql = "
  707. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  708. FROM pub_relationship PR
  709. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  710. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  711. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  712. WHERE PR.subject_id = %d
  713. ";
  714. $as_subject = chado_query($sql, $pub->pub_id);
  715. $sql = "
  716. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  717. FROM pub_relationship PR
  718. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  719. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  720. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  721. WHERE PR.object_id = %d
  722. ";
  723. $as_object = chado_query($sql, $pub->pub_id);
  724. // combine both object and subject relationshisp into a single array
  725. $relationships = array();
  726. $relationships['object'] = array();
  727. $relationships['subject'] = array();
  728. // iterate through the object relationships
  729. while ($relationship = db_fetch_object($as_object)) {
  730. // get the relationship and child types
  731. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  732. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  733. if (!array_key_exists($rel_type, $relationships['object'])) {
  734. $relationships['object'][$rel_type] = array();
  735. }
  736. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  737. $relationships['object'][$rel_type][$sub_type] = array();
  738. }
  739. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  740. }
  741. // now add in the subject relationships
  742. while ($relationship = db_fetch_object($as_subject)) {
  743. // get the relationship and child types
  744. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  745. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  746. if (!array_key_exists($rel_type, $relationships['subject'])) {
  747. $relationships['subject'][$rel_type] = array();
  748. }
  749. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  750. $relationships['subject'][$rel_type][$obj_type] = array();
  751. }
  752. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  753. }
  754. $pub->all_relationships = $relationships;
  755. }
  756. /**
  757. *
  758. */
  759. function tripal_pub_mail($key, &$message, $params) {
  760. $language = $message['language'];
  761. $variables = user_mail_tokens($params['account'], $language);
  762. switch($key) {
  763. case 'import_report':
  764. $headers = array(
  765. 'MIME-Version' => '1.0',
  766. 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
  767. 'Content-Transfer-Encoding' => '8Bit',
  768. 'X-Mailer' => 'Drupal'
  769. );
  770. foreach ($headers as $key => $value) {
  771. $message['headers'][$key] = $value;
  772. }
  773. $message['subject'] = t('Publication import from !site', $variables, $language->language);
  774. $message['body'][] = $params['message'];
  775. break;
  776. }
  777. }
  778. /*
  779. *
  780. */
  781. function tripal_pub_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  782. // we want the publications to always have a URL of http://[base url]/pub/[pub id]
  783. // where [pub id] is the Chado publication ID. This will allow for easy linking
  784. // into the publication without needing to know the node. Of course if you know the
  785. // node that will still work too (e.g. http://[base url]/node/[node id]
  786. // so the nodeapi function ensures that the URL path is set after insert or update
  787. // of the node and when the node is loaded if it hasn't yet been set.
  788. if ($node->type == 'chado_pub') {
  789. switch ($op) {
  790. case 'presave':
  791. break;
  792. case 'insert':
  793. $pub_id = chado_get_id_for_node('pub', $node);
  794. tripal_pub_set_pub_url($node, $pub_id);
  795. break;
  796. case 'load':
  797. if (!$node->path) {
  798. $pub_id = chado_get_id_for_node('pub', $node);
  799. $path = tripal_pub_set_pub_url($node, $pub_id);
  800. }
  801. break;
  802. case 'update':
  803. $pub_id = chado_get_id_for_node('pub', $node);
  804. tripal_pub_set_pub_url($node, $pub_id);
  805. break;
  806. case 'view':
  807. break;
  808. }
  809. }
  810. }