tripal_pub.module 28 KB

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