tripal_pub.module 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. <?php
  2. //require_once('cron.php');
  3. require_once "includes/tripal_pub.admin.inc";
  4. require_once "includes/pubmed.inc";
  5. require_once "includes/remote_search.inc";
  6. /**
  7. * @file
  8. *
  9. * The Tripal Publication module allows you to search the PubMed databse for academic articles,
  10. * that relate to user specified tpoic\s. As well, it allows management of publications so that
  11. * a user can enter specified details regarding a desired publication. This allows all of the important
  12. * information that is unique to a Academic Publication to be stored for access.
  13. */
  14. /**
  15. *
  16. * @ingroup tripal_pub
  17. */
  18. function tripal_pub_init() {
  19. drupal_add_js(drupal_get_path('theme', 'tripal') . '/js/tripal_pub.js');
  20. }
  21. /**
  22. * Implementation of hook_tripal_pub_node_info().
  23. *
  24. * This node_info, is a simple node that describes the functionallity of the module.
  25. *
  26. */
  27. function tripal_pub_node_info() {
  28. return array(
  29. 'chado_pub' => array(
  30. 'name' => t('Publication'),
  31. 'module' => 'chado_pub',
  32. 'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  33. 'title_label' => t('Article Title'),
  34. 'body_label' => t('Abstract'),
  35. 'has_title' => TRUE,
  36. 'has_body' => FALSE,
  37. ),
  38. );
  39. }
  40. /**
  41. * Tripal-Publication-Menu
  42. *
  43. * Implemets hook_menu(): Adds menu items for the tripal_pub module menu. This section
  44. * gives the outline for the main menu of the Tripal-Publication module
  45. *
  46. * @return
  47. * An array of menu items that is visible within the Drupal Menu, returned as soon
  48. * as the program is ran
  49. */
  50. function tripal_pub_menu() {
  51. $items = array();
  52. $items[ 'admin/tripal/tripal_pub' ]= array(
  53. 'title' => 'Publications',
  54. 'description' => ('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  55. 'page callback' => 'theme',
  56. 'page arguments' => array('tripal_pub_admin'),
  57. 'access arguments' => array('administer tripal pubs'),
  58. 'type' => MENU_NORMAL_ITEM
  59. );
  60. $items['admin/tripal/tripal_pub/configuration'] = array(
  61. 'title' => 'Configuration',
  62. 'description' => 'Configuration for this module',
  63. 'page callback' => 'drupal_get_form',
  64. 'page arguments' => array('tripal_pub_configuration_form'),
  65. 'access arguments' => array('administer tripal pubs'),
  66. 'type' => MENU_NORMAL_ITEM
  67. );
  68. $items['admin/tripal/tripal_pub/import_setup'] = array(
  69. 'title' => t('Pub Loader Setup'),
  70. 'description' => t('Finds publications using remote publication databases (e.g. PubMed).'),
  71. 'page callback' => 'tripal_pub_remote_search_page',
  72. 'access arguments' => array('administer tripal pubs'),
  73. 'type ' => MENU_CALLBACK,
  74. );
  75. $items['admin/tripal/tripal_pub/import_setup/criteria/%/%'] = array(
  76. 'page callback' => 'tripal_pub_remote_search_page_update_criteria',
  77. 'page arguments' => array(5, 6),
  78. 'access arguments' => array('administer tripal pubs'),
  79. 'type ' => MENU_CALLBACK,
  80. );
  81. // Automatically generate checkboxes.
  82. $items['node/add/tripal_pub/ahah_authors'] = array(
  83. 'title' => 'Add Additional Authors',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('author_addition_fields_ahah_callback'),
  86. 'access callback' => TRUE,
  87. 'type' => MENU_CALLBACK,
  88. 'weight' => 2,
  89. );
  90. $items['tripal_pub/js/%'] = array(
  91. 'page callback' => 'tripal_pub_js',
  92. 'page arguments' => array(2),
  93. 'access arguments' => array('access content'),
  94. 'type ' => MENU_CALLBACK,
  95. );
  96. return $items;
  97. }
  98. /**
  99. * Implements hook_theme(): Register themeing functions for this module
  100. *
  101. *
  102. * @return
  103. * An array of themeing functions to register
  104. *
  105. */
  106. function tripal_pub_theme() {
  107. return array(
  108. 'tripal_pub_author_table' => array(
  109. 'arguments' => array('form' => NULL),
  110. ),
  111. 'publication_author' => array(
  112. 'arguments' => array('element' => NULL)
  113. ),
  114. 'publication_author' => array(
  115. 'arguments' => array('element' => NULL)
  116. ),
  117. 'tripal_pub_remote_search_form' => array(
  118. 'arguments' => array('form'),
  119. ),
  120. 'tripal_pub_admin' => array(
  121. 'template' => 'tripal_pub_admin',
  122. 'arguments' => array(NULL),
  123. 'path' => drupal_get_path('module', 'tripal_pub') . '/theme'
  124. ),
  125. );
  126. }
  127. /**
  128. * Implement hook_perm().
  129. */
  130. function tripal_pub_perm() {
  131. return array(
  132. 'access chado_pub content',
  133. 'create chado_pub content',
  134. 'delete chado_pub content',
  135. 'edit chado_pub content',
  136. 'administer tripal pubs',
  137. );
  138. }
  139. /**
  140. * Implement hook_access().
  141. *
  142. * This hook allows node modules to limit access to the node types they define.
  143. *
  144. * @param $op
  145. * The operation to be performed
  146. *
  147. * @param $node
  148. * The node on which the operation is to be performed, or, if it does not yet exist, the
  149. * type of node to be created
  150. *
  151. * @param $account
  152. * A user object representing the user for whom the operation is to be performed
  153. *
  154. * @return
  155. * TRUE
  156. *
  157. */
  158. function chado_pub_access($op, $node, $account ) {
  159. if ($op == 'create') {
  160. if (!user_access('create chado_pub content', $account)) {
  161. return FALSE;
  162. }
  163. }
  164. if ($op == 'update') {
  165. if (!user_access('edit chado_pub content', $account)) {
  166. return FALSE;
  167. }
  168. }
  169. if ($op == 'delete') {
  170. if (!user_access('delete chado_pub content', $account)) {
  171. return FALSE;
  172. }
  173. }
  174. if ($op == 'view') {
  175. if (!user_access('access chado_pub content', $account)) {
  176. return FALSE;
  177. }
  178. }
  179. return NULL;
  180. }
  181. /**
  182. * Implementation of tripal_pub_form().
  183. *
  184. *
  185. *
  186. * @parm &$node
  187. * The node that is created when the database is initialized
  188. *
  189. * @parm $form_state
  190. * The state of the form, that has the user entered information that is neccessary for, setting
  191. * up the database tables for the publication
  192. *
  193. * @return $form
  194. * The information that was enterd allong with
  195. *
  196. */
  197. function chado_pub_form(&$node, $form_state) {
  198. $type = node_get_types('type', $node);
  199. // Article Title.
  200. $form['title'] = array(
  201. '#type' => 'textfield',
  202. '#title' => check_plain($type->title_label),
  203. '#default_value' => $node->title,
  204. '#required' => TRUE,
  205. '#weight' => 0,
  206. );
  207. // Abstract
  208. $form['abstract'] = array(
  209. '#title' => 'Abstract',
  210. '#type' => 'textarea',
  211. '#default_value' => isset($node->abstract) ? $node->abstract : ''
  212. );
  213. $form['pub_id'] = array(
  214. '#type' => 'hidden',
  215. '#value' => (isset($node->pub_id)) ? $node->pub_id->pub_id : NULL ,
  216. );
  217. $form['uniquename'] = array(
  218. '#type' => 'textfield',
  219. '#title' => t('Unique Name'),
  220. '#required' => TRUE,
  221. '#description' => 'A unique name/identifier for this publication. If this article exists in pubmed, entering the pubmed ID here will ensure duplicate publication pages are not created.',
  222. '#default_value' => isset($node->pub_id->uniquename) ? $node->pub_id->uniquename : ''
  223. );
  224. $values= array(
  225. 'cv_id' => variable_get('tripal_pub_types_cv', NULL),
  226. );
  227. //population select list with 'cvterm' names
  228. $result = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), $values);
  229. foreach ($result as $value) {
  230. $newArray[$value->cvterm_id]=$value->name; //options for the select list
  231. }
  232. $form['type_id'] = array(
  233. '#type' => 'select',
  234. '#title' => t('Publication Type'),
  235. '#options' => $newArray,
  236. '#required' => TRUE,
  237. '#default_value' => isset($node->pub_id->type_id) ? $node->pub_id->type_id : ''
  238. );
  239. $form['author_wrapper'] = array(
  240. '#tree' => FALSE,
  241. '#prefix' => '<div class="clear-block" id="author-wrapper">',
  242. '#suffix' => '</div>',
  243. );
  244. // Get number of authors
  245. $author_count = empty($node->authors) ? 0 : count($node->authors);
  246. // If a new author added, add to list and update the author count.
  247. if (isset($form_state['new_author'])) {
  248. if (!isset($node->authors)) {
  249. $node->authors = array();
  250. }
  251. $node->authors = array_merge($node->authors, array($form_state['new_author']));
  252. $author_count++;
  253. }
  254. // If a author removed, remove from list and update the author count.
  255. $remove_delta = -1;
  256. if (!empty($form_state['remove_delta'])) {
  257. $remove_delta = $form_state['remove_delta'] - 1;
  258. unset($node->authors[$remove_delta]);
  259. // Re-number the values.
  260. $node->authors = array_values($node->authors);
  261. $author_count--;
  262. }
  263. // Container to display existing authors.
  264. $form['author_wrapper']['authors'] = array(
  265. '#prefix' => '<div id="publication-authors">',
  266. '#suffix' => '</div>',
  267. '#theme' => 'tripal_pub_author_table',
  268. );
  269. if (!isset($node->authors)) {
  270. if (isset($node->new_author['new_author_name'])) {
  271. $node->authors = array();
  272. $node->authors[]['author_name'] = $node->new_author['new_author_name'];
  273. }
  274. }
  275. //Add the existing authors to the form.
  276. for ($delta = 0; $delta < $author_count; $delta++) {
  277. $author = isset($node->authors[$delta]['author_name']) ? $node->authors[$delta] : array();
  278. $form['author_wrapper']['authors'][$delta] = tripal_pub_author_display_form($delta, $author);
  279. }
  280. if (isset($form_state['values']['edit_author'])) {
  281. // Add new authors
  282. $form['author_wrapper']['edit_author'] = array(
  283. '#type' => 'fieldset',
  284. '#title' => t('Current Publication Authors'),
  285. '#tree' => FALSE,
  286. );
  287. // Define the form fields for the new author
  288. $form['author_wrapper']['edit_author']['edit_author'] = array(
  289. '#tree' => TRUE,
  290. //'#default_value'=> '',
  291. //'#theme' => 'author_add_author_form',
  292. );
  293. $form['author_wrapper']['edit_author']['edit_author']['delta'] = array(
  294. '#type' => 'hidden',
  295. '#value' => $form_state['values']['edit_author']['delta']
  296. );
  297. $form['author_wrapper']['edit_author']['edit_author']['edit_author_name'] = array(
  298. '#type' => 'publication_author',
  299. '#title' => t('Contributing Authors'),
  300. '#default_value' => array(
  301. 'givennames' => $form_state['values']['edit_author']['author_name']['givennames'],
  302. 'surname' => $form_state['values']['edit_author']['author_name']['surname'],
  303. 'suffix' => $form_state['values']['edit_author']['author_name']['suffix'],
  304. ),
  305. '#weight' => 1,
  306. );
  307. // We name our button 'author_more' to avoid conflicts with other modules using
  308. // AHAH-enabled buttons with the id 'more'.
  309. $form['author_wrapper']['edit_author']['author_save'] = array(
  310. '#type' => 'submit',
  311. '#value' => t('Save Author'),
  312. '#weight' => 0,
  313. '#submit' => array('tripal_pub_edit_author_submit'),
  314. '#ahah' => array(
  315. 'path' => 'tripal_pub/js/0',
  316. 'wrapper' => 'author-wrapper',
  317. 'method' => 'replace',
  318. 'effect' => 'fade',
  319. ),
  320. );
  321. }
  322. else{
  323. // Add new authors
  324. $form['author_wrapper']['add_author'] = array(
  325. '#type' => 'fieldset',
  326. '#title' => t('Publication Authors'),
  327. '#tree' => FALSE,
  328. );
  329. // Define the form fields for the new author
  330. $form['author_wrapper']['add_author']['new_author'] = array(
  331. '#tree' => TRUE,
  332. //'#default_value'=> '',
  333. '#theme' => 'author_add_author_form',
  334. );
  335. $form['author_wrapper']['add_author']['new_author']['new_author_name'] = array(
  336. '#type' => 'publication_author',
  337. '#title' => t('Contributing Authors'),
  338. '#default_value' => '',
  339. '#weight' => 1,
  340. );
  341. // We name our button 'author_more' to avoid conflicts with other modules using
  342. // AHAH-enabled buttons with the id 'more'.
  343. $form['author_wrapper']['add_author']['author_more'] = array(
  344. '#type' => 'submit',
  345. '#value' => t('Add Author'),
  346. '#weight' => 0,
  347. '#submit' => array('tripal_pub_add_author_submit'),
  348. '#ahah' => array(
  349. 'path' => 'tripal_pub/js/0',
  350. 'wrapper' => 'author-wrapper',
  351. 'method' => 'replace',
  352. 'effect' => 'fade',
  353. ),
  354. );
  355. }
  356. $form['volumetitle'] = array(
  357. '#type' => 'textfield',
  358. '#title' => t('Volume Title'),
  359. '#description' => t('Title of part if one of a series.'),
  360. '#default_value' => isset($node->pub_id->volumetitle) ? $node->pub_id->volumetitle : ''
  361. );
  362. $form['volume'] = array(
  363. '#type' => 'textfield',
  364. '#title' => t('Volume'),
  365. '#default_value' => isset($node->pub_id->volume) ? $node->pub_id->volume : ''
  366. );
  367. $form['series_name'] = array(
  368. '#type' => 'textfield',
  369. '#title' => t('Series Name'),
  370. '#default_value' => isset($node->pub_id->series_name) ? $node->pub_id->series_name : ''
  371. );
  372. $form['issue'] = array(
  373. '#type' => 'textfield',
  374. '#title' => t('Issue'),
  375. '#default_value' => isset($node->pub_id->issue) ? $node->pub_id->issue : ''
  376. );
  377. $form['pyear'] = array(
  378. '#type' => 'textfield',
  379. '#title' => t('Publication Year'),
  380. '#default_value' => isset($node->pub_id->pyear) ? $node->pub_id->pyear : ''
  381. );
  382. $form['pages'] = array(
  383. '#type' => 'textfield',
  384. '#title' => t('Pages'),
  385. '#description' => t('Page number range[s], e.g. 457--459, viii + 664pp, lv--lvii.'),
  386. '#default_value' => isset($node->pub_id->pages) ? $node->pub_id->pages : ''
  387. );
  388. $form['miniref'] = array(
  389. '#type' => 'textfield',
  390. '#title' => t('Mini-Ref'),
  391. '#required' => FALSE,
  392. '#default_value' => isset($node->pub_id->miniref) ? $node->pub_id->miniref : ''
  393. );
  394. $form['is_obsolete'] = array(
  395. '#type' => 'checkbox',
  396. '#title' => t('Is Obsolete?(Check for Yes)'),
  397. '#required' => TRUE,
  398. '#default_value' => isset($node->pub_id->is_obsolete) ? $node->pub_id->is_obsolete : FALSE
  399. );
  400. $form['publisher'] = array(
  401. '#type' => 'textfield',
  402. '#title' => t('Publisher Name'),
  403. '#required' => FALSE,
  404. '#default_value' => isset($node->pub_id->publisher) ? $node->pub_id->publisher : ''
  405. );
  406. $form['pubplace'] = array(
  407. '#type' => 'textfield',
  408. '#title' => t('Place of Publication'),
  409. '#required' => FALSE,
  410. '#default_value' => isset($node->pub_id->pubplace) ? $node->pub_id->pubplace : ''
  411. );
  412. return $form;
  413. }
  414. /**
  415. * Implementation of tripal_pub_insert().
  416. *
  417. * This function inserts user entered information pertaining to the Publication instance into the
  418. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  419. *
  420. * @parm $node
  421. * Then node which contains the information stored within the node-ID
  422. *
  423. *
  424. */
  425. function chado_pub_insert($node) {
  426. $values = array(
  427. 'title' => $node->title,
  428. 'volumetitle' => $node->volumetitle,
  429. 'volume' => $node->volume,
  430. 'series_name' => $node->series_name,
  431. 'issue' => $node->issue,
  432. 'pyear' => $node->pyear,
  433. 'pages' => $node->pages,
  434. 'miniref' => $node->miniref,
  435. 'type_id' => $node->type_id,
  436. 'is_obsolete' => $node->is_obsolete,
  437. 'publisher' => $node->publisher,
  438. 'pubplace' => $node->pubplace,
  439. 'uniquename' => $node->uniquename,
  440. 'type_id' => $node->type_id
  441. );
  442. //inserts info into chado table
  443. $result = tripal_core_chado_insert('pub', $values);
  444. if (isset($result)) {
  445. //inserts the row of vid,nid,project_id into the chado_pub table
  446. db_query("INSERT INTO {chado_pub} (nid, vid, pub_id) VALUES (%d, %d, %d)",
  447. $node->nid,
  448. $node->vid,
  449. $result['pub_id']
  450. );
  451. //Aquiring information for the abstract
  452. $abstract_info = tripal_core_chado_select('cvterm', array('cvterm_id'),
  453. array('name' => 'abstract', 'cv_id' => array('name' => 'tripal')
  454. )
  455. );
  456. //Extracting the type_id
  457. $type_id = $abstract_info[0]->cvterm_id;
  458. //setting the abstract values
  459. $abstract = array(
  460. 'pub_id' => $result['pub_id'],
  461. 'type_id' => $type_id,
  462. 'value' => $node->abstract,
  463. 'rank' => 1
  464. );
  465. //inserts info into chado pubpro table for abstract
  466. tripal_core_chado_insert('pubprop', $abstract);
  467. //counter for loop
  468. for ($i=0; $i<=sizeof($node->authors); $i++) {
  469. if (isset($node->authors[$i]['author_name'] )) {
  470. $authors = array(
  471. 'pub_id' => $result['pub_id'],
  472. 'rank' => $i,
  473. 'surname' => $node->authors[$i]['author_name']['surname'],
  474. 'givennames' => $node->authors[$i]['author_name']['givennames'],
  475. 'suffix' => $node->authors[$i]['author_name']['suffix'],
  476. );
  477. //inserts info into chado pubpro table for abstract
  478. tripal_core_chado_insert('pubauthor', $authors);
  479. }
  480. }
  481. if (!empty($node->new_author['new_author_name']['surname'])) {
  482. $authors = array(
  483. 'pub_id' => $result['pub_id'],
  484. 'rank' => $i+1,
  485. 'surname' => $node->new_author['new_author_name']['surname'],
  486. 'givennames' => $node->new_author['new_author_name']['givennames'],
  487. 'suffix' => $node->new_author['new_author_name']['suffix'],
  488. );
  489. //inserts info into chado pubpro table for abstract
  490. tripal_core_chado_insert('pubauthor', $authors);
  491. }
  492. }
  493. else{
  494. druapl_set_message('Pub_id was not set, No information has been set.');
  495. }
  496. }
  497. /**
  498. * Implementation of tripal_pub_delete().
  499. *
  500. * This function takes a node and if the delete button has been chosen by the user, the publication
  501. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  502. * the 'chado_pub' table.
  503. *
  504. * @parm $node
  505. * Then node which contains the information stored within the node-ID
  506. *
  507. */
  508. function chado_pub_delete(&$node) {
  509. // Matching all revision, by using the node's pub_id.
  510. $values = array(
  511. 'pub_id' => $node->pub_id->pub_id,
  512. );
  513. //deleting row in chado table
  514. tripal_core_chado_delete('pub', $values);
  515. //deleteing in drupal chado_project table
  516. db_query('DELETE FROM {chado_pub} WHERE nid = %d', $node->nid);
  517. }
  518. /*
  519. *
  520. * Implements hook_update
  521. *
  522. * The purpose of the function is to allow the module to take action when an edited node is being
  523. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  524. * As well, the database will be changed, so the user changed information will be saved to the database.
  525. *
  526. * @param $node
  527. * The node being updated
  528. *
  529. */
  530. function chado_pub_update($node) {
  531. $values = array(
  532. 'title' => $node->title,
  533. 'volumetitle' => $node->volumetitle,
  534. 'volume' => $node->volume,
  535. 'series_name' => $node->series_name,
  536. 'issue' => $node->issue,
  537. 'pyear' => $node->pyear,
  538. 'pages' => $node->pages,
  539. 'miniref' => $node->miniref,
  540. 'uniquename' => $node->uniquename,
  541. 'type_id' => $node->type_id,
  542. 'is_obsolete' => $node->is_obsolete,
  543. 'publisher' => $node->publisher,
  544. 'pubplace' => $node->pubplace,
  545. 'type_id' => $node->type_id
  546. );
  547. $result = db_fetch_object(db_query('SELECT pub_id FROM {chado_pub} WHERE nid=%d AND vid=%d ', $node->nid, $node->vid));
  548. //extract pub_id from the chado table for update function
  549. $match = array( 'pub_id' => $result->pub_id );
  550. //$table to be updated, $match is the 'pub_id', $value are the values that are to be updated
  551. $update_result = tripal_core_chado_update('pub', $match, $values);
  552. //Aquiring information for the abstract
  553. $abstract_info = tripal_core_chado_select('cvterm', array('cvterm_id'),
  554. array('name' => 'abstract', 'cv_id' => array('name' => 'tripal')
  555. )
  556. );
  557. //Grabbing the type id of the old abstract
  558. $type_id = $abstract_info[0]->cvterm_id;
  559. //Aquiring information for the abstract
  560. $abstract = array(
  561. 'pub_id' => $result->pub_id,
  562. 'type_id' => $type_id,
  563. 'value' => $node->abstract,
  564. 'rank' => 1
  565. );
  566. //$table to be updated, $match is the 'pub_id', $value are the values that are to be updated
  567. tripal_core_chado_update('pubprop', array('pub_id' => $result->pub_id, 'type_id' => $type_id, 'rank' => 1), $abstract);
  568. //counter for loop
  569. for ($i=0; $i<=sizeof($node->authors); $i++) {
  570. if (isset($node->authors[$i]['author_name'] )) {
  571. //if new insert
  572. if (isset($node->authors[$i]['is_new'])) {
  573. $authors = array(
  574. 'pub_id' => $match['pub_id'],
  575. 'rank' => $i+1,
  576. 'surname' => $node->authors[$i]['author_name']['surname'],
  577. 'givennames' => $node->authors[$i]['author_name']['givennames'],
  578. 'suffix' => $node->authors[$i]['author_name']['suffix'],
  579. );
  580. //inserts info into chado pubpro table for abstract
  581. tripal_core_chado_insert('pubauthor', $authors);
  582. }
  583. else{
  584. //update
  585. //$table=pubauthor to be updated, $match=author_match is the 'pub_id', $value=author are the values that are to be updated
  586. tripal_core_chado_update('pubauthor', array('pub_id' => $match['pub_id'], 'rank' => $node->authors[$i]['author_name']['rank']), $node->authors[$i]['author_name']);
  587. }
  588. }
  589. }
  590. }
  591. /**
  592. * Implementation of tripal_pub_load().
  593. *
  594. *
  595. * @param $node
  596. * The node that is to be accessed from the database
  597. *
  598. * @return $node
  599. * The node with the information to be loaded into the database
  600. *
  601. */
  602. function chado_pub_load($node) {
  603. $result = db_fetch_object(db_query('SELECT * FROM {chado_pub} WHERE nid=%d AND vid=%d ', $node->nid, $node->vid));
  604. $values = array(
  605. 'pub_id' => $result->pub_id,
  606. );
  607. if (empty($result->pub_id)) {
  608. drupal_set_message(t("Unable to find publication"), 'error');
  609. }
  610. else{
  611. $node->pub = tripal_core_generate_chado_var('pub', $values);
  612. // add in authors
  613. $authors = tripal_core_chado_select(
  614. 'pubauthor',
  615. array('rank', 'surname', 'givennames', 'suffix'),
  616. array('pub_id' => $node->pub->pub_id)
  617. );
  618. foreach ($authors as $author) {
  619. $node->pub->authors[$author->rank] = $author;
  620. }
  621. ksort($node->pub->authors);
  622. }
  623. return $node;
  624. }
  625. /**
  626. * Submit handler for 'Add Author' button on node form.
  627. */
  628. function tripal_pub_add_author_submit($form, &$form_state) {
  629. $form_state['remove_delta'] = 0;
  630. // Set the form to rebuild and run submit handlers.
  631. node_form_submit_build_node($form, $form_state);
  632. // Make the changes we want to the form state.
  633. if ($form_state['values']['author_more']) {
  634. $new_author = array();
  635. $new_author['author_name'] = $form_state['values']['new_author']['new_author_name'];
  636. $new_author['is_new'] = TRUE;
  637. $form_state['new_author'] = $new_author;
  638. }
  639. }
  640. /**
  641. * Submit handler for 'Edit' button on node form.
  642. */
  643. function tripal_pub_edit_author_submit($form, &$form_state) {
  644. // remember which author we're editing
  645. $delta = $form_state['values']['edit_author']['delta'];
  646. //add changes author details back
  647. $rank = $form_state['values']['authors'][$delta]['author_name']['rank'];
  648. $form_state['values']['authors'][$delta]['author_name'] = $form_state['values']['edit_author']['edit_author_name'];
  649. $form_state['values']['authors'][$delta]['author_name']['rank'] = $rank;
  650. //ensures that after they save their changes the edit fieldstate goes away
  651. unset($form_state['values']['edit_author']);
  652. // Set the form to rebuild and run submit handlers.
  653. node_form_submit_build_node($form, $form_state);
  654. }
  655. /**
  656. * Submit handler for 'Edit' button on node form.
  657. */
  658. function tripal_pub_set_edit_author_submit($form, &$form_state) {
  659. $form_state['remove_delta'] = 0;
  660. // Make the changes we want to the form state
  661. $edit_author = array();
  662. if (preg_match('/edit_author_(\d+)/', $form_state['clicked_button']['#name'], $matches)) {
  663. $delta = $matches[1];
  664. $form_state['values']['edit_author'] = $form_state['values']['authors'][$delta];
  665. $form_state['values']['edit_author']['delta'] = $delta;
  666. }
  667. // Set the form to rebuild and run submit handlers.
  668. node_form_submit_build_node($form, $form_state);
  669. }
  670. /**
  671. * Submit handler for 'Remove' button on node form.
  672. */
  673. function tripal_pub_remove_row_submit($form, &$form_state) {
  674. if (preg_match('/remove_author_(\d+)/', $form_state['clicked_button']['#name'], $matches)) {
  675. $delta = $matches[1];
  676. $form_state['values']['remove_author'] = $form_state['values']['authors'][$delta];
  677. $form_state['values']['remove_author']['delta'] = $delta;
  678. }
  679. $values = array(
  680. 'pub_id' => $form_state['values']['pub_id'],
  681. 'rank' => $form_state['values']['authors'][$delta]['author_name']['rank']
  682. );
  683. //deleting row in chado table
  684. tripal_core_chado_delete('pubauthor', $values);
  685. // Set the form to rebuild and run submit handlers.
  686. node_form_submit_build_node($form, $form_state);
  687. }
  688. /*
  689. *
  690. *
  691. *
  692. */
  693. function tripal_pub_js($delta = 0) {
  694. $form = tripal_pub_ajax_form_handler($delta);
  695. // Render the new output.
  696. $author_form = $form['author_wrapper']; //was ['author']
  697. // Prevent duplicate wrappers.
  698. unset($author_form['#prefix'], $author_form['#suffix']);
  699. $output = theme('status_messages') . drupal_render($author_form);
  700. // AHAH does not know about the "Remove" button.
  701. // This causes it not to attach AHAH behaviours to it after modifying the form.
  702. // So we need to tell it first.
  703. $javascript = drupal_add_js(NULL, NULL);
  704. if (isset($javascript['setting'])) {
  705. $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings, '. drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) .');</script>';
  706. }
  707. // Final rendering callback.
  708. drupal_json(array('status' => TRUE, 'data' => $output));
  709. }
  710. /**
  711. * AJAX form handler.
  712. */
  713. function tripal_pub_ajax_form_handler($delta=0 ) {
  714. // The form is generated in an include file which we need to include manually.
  715. include_once 'modules/node/node.pages.inc';
  716. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  717. $form_build_id = filter_xss($_POST['form_build_id']);
  718. // Get the form from the cache.
  719. $form = form_get_cache($form_build_id, $form_state);
  720. $args = $form['#parameters'];
  721. $form_id = array_shift($args);
  722. // We need to process the form, prepare for that by setting a few internals.
  723. $form_state['post'] = $form['#post'] = $_POST;
  724. $form['#programmed'] = $form['#redirect'] = FALSE;
  725. // Set up our form state variable, needed for removing authors.
  726. $form_state['remove_delta'] = $delta;
  727. // Build, validate and if possible, submit the form.
  728. drupal_process_form($form_id, $form, $form_state);
  729. // If validation fails, force form submission.
  730. if (form_get_errors()) {
  731. form_execute_handlers('submit', $form, $form_state);
  732. }
  733. // This call recreates the form relying solely on the form_state that the
  734. // drupal_process_form set up.
  735. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  736. return $form;
  737. }
  738. /*
  739. *
  740. *
  741. */
  742. function theme_tripal_pub_author_table($form) {
  743. $rows = array();
  744. $headers = array(
  745. t('Givennames'),
  746. t('Surname'),
  747. t('Suffix'),
  748. '', // Blank header title for the remove link.
  749. );
  750. foreach (element_children($form) as $key) {
  751. // No need to print the field title every time.
  752. unset(
  753. $form[$key]['author_name_text']['#title'],
  754. $form[$key]['author_name_text-2']['#title'],
  755. $form[$key]['author_name_text-3']['#title'],
  756. $form[$key]['remove_author']['#title']
  757. );
  758. // Build the table row.
  759. $row = array(
  760. 'data' => array(
  761. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text']), 'class' => 'author-name'),
  762. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text-2']), 'class' => 'author-name'),
  763. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text-3']), 'class' => 'author-name'),
  764. array('data' => drupal_render($form[$key]['edit_author']) . drupal_render($form[$key]['remove_author']), 'class' => 'remove-author'),
  765. ),
  766. );
  767. // Add additional attributes to the row, such as a class for this row.
  768. if (isset($form[$key]['#attributes'])) {
  769. $row = array_merge($row, $form[$key]['#attributes']);
  770. }
  771. $rows[] = $row;
  772. }
  773. //$output = theme('table', $headers, $rows);
  774. $output .= drupal_render($form);
  775. return $output;
  776. }
  777. /**
  778. *
  779. * Helper function to define populated form field elements for publication node form.
  780. *
  781. */
  782. function tripal_pub_author_display_form($delta, $author) {
  783. $form = array(
  784. '#tree' => TRUE,
  785. );
  786. // Author Name
  787. $form['author_name'] = array(
  788. '#type' => 'hidden',
  789. '#value' => $author,
  790. '#parents' => array('authors', $delta),
  791. );
  792. $form['author_name_text'] = array(
  793. '#type' => 'item',
  794. '#title' => t('Givennames'),
  795. '#parents' => array('authors', $delta),
  796. '#value' => $author['author_name']['givennames'],
  797. );
  798. $form['author_name_text-2'] = array(
  799. '#type' => 'item',
  800. '#title' => t('Surname'),
  801. '#parents' => array('authors', $delta),
  802. '#value' => $author['author_name']['surname'],
  803. );
  804. $form['author_name_text-3'] = array(
  805. '#type' => 'item',
  806. '#title' => t('Suffix'),
  807. '#parents' => array('authors', $delta),
  808. '#value' => $author['author_name']['suffix'],
  809. );
  810. // Remove button.
  811. $form['remove_author'] = array(
  812. '#type' => 'submit',
  813. '#name' => 'remove_author_' . $delta,
  814. '#value' => t('Remove'),
  815. '#submit' => array('tripal_pub_remove_row_submit'),
  816. '#parents' => array('authors', $delta, 'remove_author'),
  817. '#ahah' => array(
  818. 'path' => 'tripal_pub/js/0',
  819. 'wrapper' => 'author-wrapper',
  820. 'method' => 'replace',
  821. 'effect' => 'fade',
  822. ),
  823. );
  824. // Edit Author button
  825. $form['edit_author'] = array(
  826. '#type' => 'submit',
  827. '#name' => 'edit_author_' . $delta,
  828. '#value' => t('Edit'),
  829. '#submit' => array('tripal_pub_set_edit_author_submit'),
  830. '#parents' => array('authors', $delta, 'edit_author'),
  831. '#ahah' => array(
  832. 'path' => 'tripal_pub/js/0',
  833. 'wrapper' => 'author-wrapper',
  834. 'method' => 'replace',
  835. 'effect' => 'fade',
  836. ),
  837. );
  838. return $form;
  839. }
  840. /*
  841. * This function executes commands periodically. This is called whenever a cron run occurs. This
  842. * function uses the time interval that the user has entered. This Publication module, requires
  843. * that the program is ran in pre-determined intervals, as desired by the user. By using a time stamp
  844. * and comparing the current time and the time that the last time a cron was ran, this program will
  845. * be ran.
  846. *
  847. *
  848. * @TODO: The cron function is not working correctly, not sure why the cron is not working properly.
  849. *
  850. *
  851. *
  852. */
  853. /**
  854. function tripal_pub_cron(){
  855. global $user; //needed to make the current users details available so access of user id is available
  856. //Aquiring the current time
  857. $current_time = time();
  858. //Aquiring the user entered time interval
  859. $user_interval = variable_get('time_interval',NULL);
  860. //converting the user entered interval into seconds for use with unix time stamp
  861. $converted_interval = ($user_interval*60);
  862. //Accessing database for time stamp from watchdog
  863. $cron = db_result(db_query('select timestamp from {watchdog} where type="cron" order by timestamp desc limit 1'));
  864. //debugging print statement
  865. //print($cron);
  866. //taking variable value & assigning for use
  867. $cron_last = variable_get('cron', time());
  868. $updated_interval = $cron_last - $converted_interval;
  869. if($current_time >= $updated_interval ){
  870. tripal_add_job('Search & Load PubMed Publications', 'tripal_pub', 'tripal_pub_search_load_pubmed_publications', $job_args, $user->uid);
  871. }
  872. }
  873. */
  874. //-----------------------------------------------------------------------------
  875. // SECTION: Custom form Elements
  876. //-----------------------------------------------------------------------------
  877. /*
  878. * This fucnction tells the FAPI(Form-API) that this is a element that will carry a value, contains
  879. * arrays of callback function names. Will declare an element will create a reuseable element type.
  880. *
  881. * @return
  882. * An associative array with the name of each element type as a key and an
  883. * array of attributes describingthe type as a value
  884. */
  885. function tripal_pub_elements() {
  886. return array(
  887. 'publication_author' => array(
  888. '#input' => TRUE,
  889. '#process' => array('expand_publication_author'),
  890. '#element_validate' => array('publication_author_validate'),
  891. ),
  892. );
  893. }
  894. /*
  895. *
  896. *
  897. *
  898. */
  899. function expand_publication_author($element) {
  900. if (empty($element['#value'])) {
  901. $element['#value'] = array(
  902. 'givennames' => '',
  903. 'suffix' => '',
  904. 'surname' => '',
  905. );
  906. }
  907. $element['#tree'] = TRUE;
  908. $parents = $element['#parents'];
  909. $parents[] = 'givennames';
  910. $element['givennames'] = array(
  911. '#type' => 'textfield',
  912. '#size' => 10,
  913. //'#maxlength' => TRUE,
  914. //'#default_value'=> $element['#value']['#given'],
  915. '#prefix' => 'Given Name',
  916. );
  917. if ($element['#default_value']) {
  918. $element['givennames']['#default_value'] = $element['#default_value']['givennames'];
  919. }
  920. $parents = $element['#parents'];
  921. $parents[]= 'surname';
  922. $element['surname'] = array(
  923. '#type' => 'textfield',
  924. '#size' => 10,
  925. //'#maxlength' => TRUE,
  926. //'#default_value'=> $element['#value']['#surname'],
  927. '#prefix' => 'Surname',
  928. );
  929. if ($element['#default_value']) {
  930. $element['surname']['#default_value'] = $element['#default_value']['surname'];
  931. }
  932. $parents = $element['#parents'];
  933. $parents[]= 'suffix';
  934. $element['suffix'] = array(
  935. '#type' => 'textfield',
  936. '#size' => 5,
  937. //'#maxlength' => TRUE,
  938. //'#default_value'=> $element['#value']['#suffix'],
  939. '#prefix' => 'suffix',
  940. );
  941. if ($element['#default_value']) {
  942. $element['suffix']['#default_value'] = $element['#default_value']['suffix'];
  943. }
  944. return $element;
  945. }
  946. /*
  947. *
  948. *
  949. *
  950. */
  951. function theme_publication_author($element) {
  952. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  953. }
  954. /*
  955. *
  956. *
  957. *
  958. */
  959. function publication_author_validate($element) {
  960. if ($element['#required']) {
  961. if (trim($element['#value']['givennames']) == '' || trim($element['#value']['suffix']) == '' || trim($element['#value']['surname']) == '' ||
  962. !is_string($element['#value']['givennames']) || !is_string($element['#value']['givennames']) || !is_string($element['#value']['surname'])) {
  963. form_error($element, t('The Author name is required.'));
  964. }
  965. }
  966. }
  967. /*
  968. *
  969. */
  970. function tripal_pub_form_alter(&$form, &$form_state, $form_id) {
  971. if ($form_id == "tripal_pub_remote_search_form") {
  972. // updating the form through the ahah callback sets the action of
  973. // the form to the ahah callback URL. We need to set it back
  974. // to the normal form URL
  975. $form['#action'] = url("admin/tripal/tripal_pub/import_setup");
  976. }
  977. }