tripal_pub.module 35 KB

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