tripal_pub.module 34 KB

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