tripal_pub.module 34 KB

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