tripal_pub.module 32 KB

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