tripal_pub.module 34 KB

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