tripal_pub.module 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. <?php
  2. //require_once('cron.php');
  3. /**
  4. * @file
  5. * This file contains all the functions which provide functionality to this module.
  6. * This module was developed by Chad N.A. Krilow and Lacey-Anne Sanderson,
  7. * The University of Saskatchewan.
  8. *
  9. *
  10. * The Tripal Publication module allows you to search the PubMed databse for academic articles,
  11. * that relate to user specified tpoic\s. As well, it allows management of publications so that
  12. * a user can enter specified details regarding a desired publication. This allows all of the important
  13. * information that is unique to a Academic Publication to be stored for access.
  14. */
  15. //-----------------------------------------------------------------------------
  16. // SECTION: Main Outline Menu for Tripal Publication
  17. //-----------------------------------------------------------------------------
  18. /**
  19. * Tripal-Publication-Menu
  20. *
  21. * Implemets hook_menu(): Adds menu items for the tripal_pub module menu. This section
  22. * gives the outline for the main menu of the Tripal-Publication module
  23. *
  24. * @return
  25. * An array of menu items that is visible within the Drupal Menu, returned as soon
  26. * as the program is ran
  27. */
  28. function tripal_pub_menu() {
  29. $items = array();
  30. $items[ 'admin/tripal/tripal_pub' ]= array(
  31. 'title' => 'Publications',
  32. 'description' => ('A module for interfacing the GMOD chado database with Drupal,
  33. providing viewing of publications'),
  34. 'page callback' => 'administration_description_page',
  35. 'access arguments' => array('administer site configuration'),
  36. 'type' => MENU_NORMAL_ITEM
  37. );
  38. $items['admin/tripal/tripal_pub/configuration'] = array(
  39. 'title' => 'Configuration',
  40. 'description' => 'Configuration for this module',
  41. 'page callback' => 'drupal_get_form',
  42. 'page arguments' => array('tripal_pub_configuration_form'),
  43. 'access arguments' => array('administer site configuration'),
  44. 'type' => MENU_NORMAL_ITEM
  45. );
  46. // Automatically generate checkboxes.
  47. $items['node/add/tripal_pub/ahah_authors'] = array(
  48. 'title' => 'Add Additional Authors',
  49. 'page callback' => 'drupal_get_form',
  50. 'page arguments' => array('author_addition_fields_ahah_callback'),
  51. 'access callback' => TRUE,
  52. 'type' => MENU_CALLBACK,
  53. 'weight' => 2,
  54. );
  55. $items['tripal_pub/js/%'] = array(
  56. 'page callback' => 'tripal_pub_js',
  57. 'page arguments' => array(2),
  58. 'access arguments' => array('access content'),
  59. 'type ' => MENU_CALLBACK,
  60. );
  61. return $items;
  62. }
  63. /**
  64. * Implements hook_theme(): Register themeing functions for this module
  65. *
  66. *
  67. * @return
  68. * An array of themeing functions to register
  69. *
  70. */
  71. function tripal_pub_theme() {
  72. return array(
  73. 'tripal_pub_author_table' => array(
  74. 'arguments' => array('form' => NULL),
  75. ),
  76. 'publication_author' => array(
  77. 'arguments' => array('element' => NULL)
  78. ),
  79. );
  80. }
  81. /**
  82. * Implement hook_perm().
  83. */
  84. function tripal_pub_perm() {
  85. return array('edit own tripal_pub', 'edit tripal_pub', 'create tripal_pub');
  86. }
  87. /**
  88. * This section uses HTML to output the descriptions of the module, through the Tripal Management
  89. * Administation window.Any installation instructions are given as well as included features
  90. * as well as a overview of the purpose Module.
  91. *
  92. * @return
  93. * Returns '$text'The HTML description of the Module, its instructions, features, and any other
  94. * important aspects. This is returned when the Administration page is selected.
  95. */
  96. function administration_description_page() {
  97. $text = '';
  98. $text = '<br /><h3>Tripal Publication Administrative Tools Quick Links</h3>';
  99. $text .= '<ul>';
  100. $text .= '<li>' . l('Configuration', 'admin/tripal/tripal_pub/configuration') . '</li>';
  101. $text .= '<li>' . l('Create A Publication', 'node/add/publication') . '</li>';
  102. $text .= '</ul>';
  103. $text .= '<h3>Module Description:</h3>';
  104. //================================================================================
  105. $text .= '<p>The Tripal Publication Module provides the functionality for adding, editing, deleting and
  106. accessing academic publications, entered by the user.This module also allows a time limited search,
  107. specified by the user, which searches the PubMed extracts and saves acedemic puplications.
  108. </p>';
  109. $text .= '<h3>Setup Instructions:</h3>';
  110. //================================================================================
  111. $text .= '<ol>';
  112. $text .= '<ol type="i">';
  113. $text .= '</ol>';
  114. $text .= '<li><p><b>Set Permissions</b>: The publication module supports the Drupal user permissions interface for
  115. controlling access to publlication content and functions. These permissions include viewing,
  116. creating, editing or administering of
  117. publication content. The default is that only the original site administrator has these
  118. permissions. You can <a href="' . url('admin/user/roles') . '">add roles</a> for classifying users,
  119. <a href="' . url('admin/user/user') . '">assign users to roles</a> and
  120. <a href="' . url('admin/user/permissions') . '">assign permissions</a> for the publication content to
  121. those roles. For a simple setup, allow anonymous users access to view publication content and
  122. allow the site administrator all other permissions.</p></li>';
  123. $text .= '<li><p><b>Set Publication Type Controlled Vocabulary</b>: The select list for setting the publication '
  124. . 'type is controlled be a controlled vocabulary (cv). '
  125. . '<ul><li>Before you can add any publications you need '
  126. . 'to create/load this cv. There is a limited cv included in this module. To use it, you need to '
  127. . 'load it using the <a href="' . url('admin/tripal/tripal_cv/obo_loader') . '">OBO Loader included with Tripal</a>.</li>'
  128. . '<li>After the controlled vocabulary is loaded you need to set it to be used for the publication '
  129. . 'module. To do this, go to <a href="' . url('admin/tripal/tripal_pub/configuration') . '">Publication->Configuration</a>, select it in the controlled vocabulary '
  130. . 'select list and click save configuration.</p></li></ul>';
  131. $text .= '</ol>';
  132. $text .= '<h3>Features of this Module:</h3>';
  133. //================================================================================
  134. $text .= '<ul>';
  135. $text .= '<li><p><b>Configuration (Search For Academic Publications):</b> The search capability implemented in
  136. this module allows a user to search, by remote connection , the PubMEd database for articles
  137. that relate to key search words, chosen by the user.The "search keys" are used to search through
  138. Publication titles and when one of the key words is matched in a title, the recognized article will
  139. be saved to the database.
  140. <ul>
  141. <li><b>Choose a Controlled Vocabulary:</b>The controlled vocabulary list is a set of terms
  142. <li><b>Set Search Key Words:</b>The search keywords, are the user entered key terms, in which
  143. the publications in the PubMed database can be recognized by. The user may enter any number
  144. of search terms, as by adding more search terms, the search will limit the results to those
  145. in which all of the search terms appear in the publication title.
  146. <li><b>Set a time search interval:</b>The search term interval represents a pre-set ammount
  147. of time for the search. The time search interval must be entered in minutes. This allows
  148. the module to automatically search the PubMed database in a predetermined time interval.
  149. </ul>
  150. </p></li>';
  151. $text .= '<li><b>Creating a Publication:</b>';
  152. $text .= '<p>To <b>Create,update/delete a given property of a publication</b>:When Creating a Publication
  153. it is neccessary to enter the requried fields in the form. These are marked with an astrix and
  154. if they are not entered upon completion a warning will be issued and the user is forced to fill
  155. in these entries. The author field, requires a given/surname/suffix to be entered. To add the
  156. author to the publication, the add author button is to be pushed. The user is able to add as
  157. many authors to the publication as needed.
  158. </p>';
  159. return $text;
  160. }
  161. //-----------------------------------------------------------------------------
  162. // SECTION: Node Capability
  163. //-----------------------------------------------------------------------------
  164. /**
  165. * Implement hook_access().
  166. *
  167. * This hook allows node modules to limit access to the node types they define.
  168. *
  169. * @param $op
  170. * The operation to be performed
  171. *
  172. * @param $node
  173. * The node on which the operation is to be performed, or, if it does not yet exist, the
  174. * type of node to be created
  175. *
  176. * @param $account
  177. * A user object representing the user for whom the operation is to be performed
  178. *
  179. * @return
  180. * TRUE
  181. *
  182. */
  183. function chado_pub_access($op, $node, $account ) {
  184. global $user;
  185. if (empty($account)) {
  186. $account = $user;
  187. }
  188. if ($op != 'create') {
  189. $node = (object)$node;
  190. }
  191. if ($op == 'create') {
  192. if (user_access('create tripal_pub')) {
  193. return TRUE;
  194. }
  195. }
  196. elseif ($op == 'update' || $op == 'delete') {
  197. if (user_access('edit tripal_pub')) {
  198. return TRUE;
  199. }
  200. elseif (user_access('edit own tripal_pub') && $account->uid == $node->uid) {
  201. return TRUE;
  202. }
  203. }
  204. /////////////////The following is taken from the tripal_pub module insert later////////////////////
  205. /*
  206. if ($op == 'create') {
  207. // Only users with permission to do so may create this node type.
  208. <<<<<<< .mine
  209. return user_access('create tripal_pub', $account);
  210. =======
  211. if(!user_access('create tripal_pub', $account)){
  212. return FALSE;
  213. }
  214. >>>>>>> .r25216
  215. }
  216. // Users who create a node may edit or delete it later, assuming they have the
  217. // necessary permissions.
  218. if ($op == 'update' || $op == 'delete') {
  219. if (user_access('edit own tripal_pub',$account) && ($account->uid == $node->uid)) {
  220. return TRUE;
  221. }
  222. }*/
  223. ///////////////////////////////////////////////////////////////////////////////////////////////////
  224. }
  225. /**
  226. * Implementation of hook_tripal_pub_node_info().
  227. *
  228. * This node_info, is a simple node that describes the functionallity of the module.
  229. *
  230. */
  231. function tripal_pub_node_info() {
  232. return array(
  233. 'chado_pub' => array(
  234. 'name' => t('Publication'),
  235. 'module' => 'chado_pub',
  236. 'description' => t('A module for interfacing the GMOD chado database with Drupal, providing viewing of publications'),
  237. 'title_label' => t('Article Title'),
  238. 'body_label' => t('Abstract'),
  239. 'has_title' => TRUE,
  240. 'has_body' => FALSE,
  241. ),
  242. );
  243. }
  244. /**
  245. * Implementation of tripal_pub_form().
  246. *
  247. *
  248. *
  249. * @parm &$node
  250. * The node that is created when the database is initialized
  251. *
  252. * @parm $form_state
  253. * The state of the form, that has the user entered information that is neccessary for, setting
  254. * up the database tables for the publication
  255. *
  256. * @return $form
  257. * The information that was enterd allong with
  258. *
  259. */
  260. function chado_pub_form(&$node, $form_state) {
  261. $type = node_get_types('type', $node);
  262. // Article Title.
  263. $form['title'] = array(
  264. '#type' => 'textfield',
  265. '#title' => check_plain($type->title_label),
  266. '#default_value' => $node->title,
  267. '#required' => TRUE,
  268. '#weight' => 0,
  269. );
  270. // Abstract
  271. $form['abstract'] = array(
  272. '#title' => 'Abstract',
  273. '#type' => 'textarea',
  274. '#default_value' => isset($node->abstract) ? $node->abstract : ''
  275. );
  276. $form['pub_id'] = array(
  277. '#type' => 'hidden',
  278. '#value' => (isset($node->pub_id)) ? $node->pub_id->pub_id : NULL ,
  279. );
  280. $form['uniquename'] = array(
  281. '#type' => 'textfield',
  282. '#title' => t('Unique Name'),
  283. '#required' => TRUE,
  284. '#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.',
  285. '#default_value' => isset($node->pub_id->uniquename) ? $node->pub_id->uniquename : ''
  286. );
  287. $values= array(
  288. 'cv_id' => variable_get('tripal_pub_types_cv', NULL),
  289. );
  290. //population select list with 'cvterm' names
  291. $result = tripal_core_chado_select('cvterm', array('cvterm_id', 'name'), $values);
  292. foreach ($result as $value) {
  293. $newArray[$value->cvterm_id]=$value->name; //options for the select list
  294. }
  295. $form['type_id'] = array(
  296. '#type' => 'select',
  297. '#title' => t('Publication Type'),
  298. '#options' => $newArray,
  299. '#required' => TRUE,
  300. '#default_value' => isset($node->pub_id->type_id) ? $node->pub_id->type_id : ''
  301. );
  302. $form['author_wrapper'] = array(
  303. '#tree' => FALSE,
  304. '#prefix' => '<div class="clear-block" id="author-wrapper">',
  305. '#suffix' => '</div>',
  306. );
  307. // Get number of authors
  308. $author_count = empty($node->authors) ? 0 : count($node->authors);
  309. // If a new author added, add to list and update the author count.
  310. if (isset($form_state['new_author'])) {
  311. if (!isset($node->authors)) {
  312. $node->authors = array();
  313. }
  314. $node->authors = array_merge($node->authors, array($form_state['new_author']));
  315. $author_count++;
  316. }
  317. // If a author removed, remove from list and update the author count.
  318. $remove_delta = -1;
  319. if (!empty($form_state['remove_delta'])) {
  320. $remove_delta = $form_state['remove_delta'] - 1;
  321. unset($node->authors[$remove_delta]);
  322. // Re-number the values.
  323. $node->authors = array_values($node->authors);
  324. $author_count--;
  325. }
  326. // Container to display existing authors.
  327. $form['author_wrapper']['authors'] = array(
  328. '#prefix' => '<div id="publication-authors">',
  329. '#suffix' => '</div>',
  330. '#theme' => 'tripal_pub_author_table',
  331. );
  332. if (!isset($node->authors)) {
  333. if (isset($node->new_author['new_author_name'])) {
  334. $node->authors = array();
  335. $node->authors[]['author_name'] = $node->new_author['new_author_name'];
  336. }
  337. }
  338. //Add the existing authors to the form.
  339. for ($delta = 0; $delta < $author_count; $delta++) {
  340. $author = isset($node->authors[$delta]['author_name']) ? $node->authors[$delta] : array();
  341. $form['author_wrapper']['authors'][$delta] = tripal_pub_author_display_form($delta, $author);
  342. }
  343. /////////////////////Edit authors//////////////////////////////////////////////////////////////////
  344. if (isset($form_state['values']['edit_author'])) {
  345. // Add new authors
  346. $form['author_wrapper']['edit_author'] = array(
  347. '#type' => 'fieldset',
  348. '#title' => t('Current Publication Authors'),
  349. '#tree' => FALSE,
  350. );
  351. // Define the form fields for the new author
  352. $form['author_wrapper']['edit_author']['edit_author'] = array(
  353. '#tree' => TRUE,
  354. //'#default_value'=> '',
  355. //'#theme' => 'author_add_author_form',
  356. );
  357. $form['author_wrapper']['edit_author']['edit_author']['delta'] = array(
  358. '#type' => 'hidden',
  359. '#value' => $form_state['values']['edit_author']['delta']
  360. );
  361. $form['author_wrapper']['edit_author']['edit_author']['edit_author_name'] = array(
  362. '#type' => 'publication_author',
  363. '#title' => t('Contributing Authors'),
  364. '#default_value' => array(
  365. 'givennames' => $form_state['values']['edit_author']['author_name']['givennames'],
  366. 'surname' => $form_state['values']['edit_author']['author_name']['surname'],
  367. 'suffix' => $form_state['values']['edit_author']['author_name']['suffix'],
  368. ),
  369. '#weight' => 1,
  370. );
  371. // We name our button 'author_more' to avoid conflicts with other modules using
  372. // AHAH-enabled buttons with the id 'more'.
  373. $form['author_wrapper']['edit_author']['author_save'] = array(
  374. '#type' => 'submit',
  375. '#value' => t('Save Author'),
  376. '#weight' => 0,
  377. '#submit' => array('tripal_pub_edit_author_submit'),
  378. '#ahah' => array(
  379. 'path' => 'tripal_pub/js/0',
  380. 'wrapper' => 'author-wrapper',
  381. 'method' => 'replace',
  382. 'effect' => 'fade',
  383. ),
  384. );
  385. ////////////////////////////////////////////////////////////////////////////////////
  386. }
  387. else{
  388. // Add new authors
  389. $form['author_wrapper']['add_author'] = array(
  390. '#type' => 'fieldset',
  391. '#title' => t('Publication Authors'),
  392. '#tree' => FALSE,
  393. );
  394. // Define the form fields for the new author
  395. $form['author_wrapper']['add_author']['new_author'] = array(
  396. '#tree' => TRUE,
  397. //'#default_value'=> '',
  398. '#theme' => 'author_add_author_form',
  399. );
  400. $form['author_wrapper']['add_author']['new_author']['new_author_name'] = array(
  401. '#type' => 'publication_author',
  402. '#title' => t('Contributing Authors'),
  403. '#default_value' => '',
  404. '#weight' => 1,
  405. );
  406. // We name our button 'author_more' to avoid conflicts with other modules using
  407. // AHAH-enabled buttons with the id 'more'.
  408. $form['author_wrapper']['add_author']['author_more'] = array(
  409. '#type' => 'submit',
  410. '#value' => t('Add Author'),
  411. '#weight' => 0,
  412. '#submit' => array('tripal_pub_add_author_submit'),
  413. '#ahah' => array(
  414. 'path' => 'tripal_pub/js/0',
  415. 'wrapper' => 'author-wrapper',
  416. 'method' => 'replace',
  417. 'effect' => 'fade',
  418. ),
  419. );
  420. }
  421. ////////////////////////////////////////////////////////////////////////////////////////////////////
  422. $form['volumetitle'] = array(
  423. '#type' => 'textfield',
  424. '#title' => t('Volume Title'),
  425. '#description' => t('Title of part if one of a series.'),
  426. '#default_value' => isset($node->pub_id->volumetitle) ? $node->pub_id->volumetitle : ''
  427. );
  428. $form['volume'] = array(
  429. '#type' => 'textfield',
  430. '#title' => t('Volume'),
  431. '#default_value' => isset($node->pub_id->volume) ? $node->pub_id->volume : ''
  432. );
  433. $form['series_name'] = array(
  434. '#type' => 'textfield',
  435. '#title' => t('Series Name'),
  436. '#default_value' => isset($node->pub_id->series_name) ? $node->pub_id->series_name : ''
  437. );
  438. $form['issue'] = array(
  439. '#type' => 'textfield',
  440. '#title' => t('Issue'),
  441. '#default_value' => isset($node->pub_id->issue) ? $node->pub_id->issue : ''
  442. );
  443. $form['pyear'] = array(
  444. '#type' => 'textfield',
  445. '#title' => t('Publication Year'),
  446. '#default_value' => isset($node->pub_id->pyear) ? $node->pub_id->pyear : ''
  447. );
  448. $form['pages'] = array(
  449. '#type' => 'textfield',
  450. '#title' => t('Pages'),
  451. '#description' => t('Page number range[s], e.g. 457--459, viii + 664pp, lv--lvii.'),
  452. '#default_value' => isset($node->pub_id->pages) ? $node->pub_id->pages : ''
  453. );
  454. $form['miniref'] = array(
  455. '#type' => 'textfield',
  456. '#title' => t('Mini-Ref'),
  457. '#required' => FALSE,
  458. '#default_value' => isset($node->pub_id->miniref) ? $node->pub_id->miniref : ''
  459. );
  460. $form['is_obsolete'] = array(
  461. '#type' => 'checkbox',
  462. '#title' => t('Is Obsolete?(Check for Yes)'),
  463. '#required' => TRUE,
  464. '#default_value' => isset($node->pub_id->is_obsolete) ? $node->pub_id->is_obsolete : FALSE
  465. );
  466. $form['publisher'] = array(
  467. '#type' => 'textfield',
  468. '#title' => t('Publisher Name'),
  469. '#required' => FALSE,
  470. '#default_value' => isset($node->pub_id->publisher) ? $node->pub_id->publisher : ''
  471. );
  472. $form['pubplace'] = array(
  473. '#type' => 'textfield',
  474. '#title' => t('Place of Publication'),
  475. '#required' => FALSE,
  476. '#default_value' => isset($node->pub_id->pubplace) ? $node->pub_id->pubplace : ''
  477. );
  478. return $form;
  479. }
  480. /**
  481. * Implementation of tripal_pub_insert().
  482. *
  483. * This function inserts user entered information pertaining to the Publication instance into the
  484. * 'pubauthor', 'pubprop', 'chado_pub', 'pub' talble of the database.
  485. *
  486. * @parm $node
  487. * Then node which contains the information stored within the node-ID
  488. *
  489. *
  490. */
  491. function chado_pub_insert($node) {
  492. $values = array(
  493. 'title' => $node->title,
  494. 'volumetitle' => $node->volumetitle,
  495. 'volume' => $node->volume,
  496. 'series_name' => $node->series_name,
  497. 'issue' => $node->issue,
  498. 'pyear' => $node->pyear,
  499. 'pages' => $node->pages,
  500. 'miniref' => $node->miniref,
  501. 'type_id' => $node->type_id,
  502. 'is_obsolete' => $node->is_obsolete,
  503. 'publisher' => $node->publisher,
  504. 'pubplace' => $node->pubplace,
  505. 'uniquename' => $node->uniquename,
  506. 'type_id' => $node->type_id
  507. );
  508. //inserts info into chado table
  509. $result = tripal_core_chado_insert('pub', $values);
  510. if (isset($result)) {
  511. //inserts the row of vid,nid,project_id into the chado_pub table
  512. db_query("INSERT INTO {chado_pub} (nid, vid, pub_id) VALUES (%d, %d, %d)",
  513. $node->nid,
  514. $node->vid,
  515. $result['pub_id']
  516. );
  517. //Aquiring information for the abstract
  518. $abstract_info = tripal_core_chado_select('cvterm', array('cvterm_id'),
  519. array('name' => 'abstract', 'cv_id' => array('name' => 'tripal')
  520. )
  521. );
  522. //Extracting the type_id
  523. $type_id = $abstract_info[0]->cvterm_id;
  524. //setting the abstract values
  525. $abstract = array(
  526. 'pub_id' => $result['pub_id'],
  527. 'type_id' => $type_id,
  528. 'value' => $node->abstract,
  529. 'rank' => 1
  530. );
  531. //inserts info into chado pubpro table for abstract
  532. tripal_core_chado_insert('pubprop', $abstract);
  533. //counter for loop
  534. for ($i=0; $i<=sizeof($node->authors); $i++) {
  535. if (isset($node->authors[$i]['author_name'] )) {
  536. $authors = array(
  537. 'pub_id' => $result['pub_id'],
  538. 'rank' => $i,
  539. 'surname' => $node->authors[$i]['author_name']['surname'],
  540. 'givennames' => $node->authors[$i]['author_name']['givennames'],
  541. 'suffix' => $node->authors[$i]['author_name']['suffix'],
  542. );
  543. //inserts info into chado pubpro table for abstract
  544. tripal_core_chado_insert('pubauthor', $authors);
  545. }
  546. }
  547. if (!empty($node->new_author['new_author_name']['surname'])) {
  548. $authors = array(
  549. 'pub_id' => $result['pub_id'],
  550. 'rank' => $i+1,
  551. 'surname' => $node->new_author['new_author_name']['surname'],
  552. 'givennames' => $node->new_author['new_author_name']['givennames'],
  553. 'suffix' => $node->new_author['new_author_name']['suffix'],
  554. );
  555. //inserts info into chado pubpro table for abstract
  556. tripal_core_chado_insert('pubauthor', $authors);
  557. }
  558. }
  559. else{
  560. druapl_set_message('Pub_id was not set, No information has been set.');
  561. }
  562. }
  563. /**
  564. * Implementation of tripal_pub_delete().
  565. *
  566. * This function takes a node and if the delete button has been chosen by the user, the publication
  567. * and it's details will be removed.Following,given the node-ID, the instance will be deleted from
  568. * the 'chado_pub' table.
  569. *
  570. * @parm $node
  571. * Then node which contains the information stored within the node-ID
  572. *
  573. */
  574. function chado_pub_delete(&$node) {
  575. // Matching all revision, by using the node's pub_id.
  576. $values = array(
  577. 'pub_id' => $node->pub_id->pub_id,
  578. );
  579. //deleting row in chado table
  580. tripal_core_chado_delete('pub', $values);
  581. //deleteing in drupal chado_project table
  582. db_query('DELETE FROM {chado_pub} WHERE nid = %d', $node->nid);
  583. }
  584. /*
  585. *
  586. * Implements hook_update
  587. *
  588. * The purpose of the function is to allow the module to take action when an edited node is being
  589. * updated. It updates any name changes to the database tables that werec reated upon registering a Publication.
  590. * As well, the database will be changed, so the user changed information will be saved to the database.
  591. *
  592. * @param $node
  593. * The node being updated
  594. *
  595. */
  596. function chado_pub_update($node) {
  597. $values = array(
  598. 'title' => $node->title,
  599. 'volumetitle' => $node->volumetitle,
  600. 'volume' => $node->volume,
  601. 'series_name' => $node->series_name,
  602. 'issue' => $node->issue,
  603. 'pyear' => $node->pyear,
  604. 'pages' => $node->pages,
  605. 'miniref' => $node->miniref,
  606. 'uniquename' => $node->uniquename,
  607. 'type_id' => $node->type_id,
  608. 'is_obsolete' => $node->is_obsolete,
  609. 'publisher' => $node->publisher,
  610. 'pubplace' => $node->pubplace,
  611. 'type_id' => $node->type_id
  612. );
  613. $result = db_fetch_object(db_query('SELECT pub_id FROM {chado_pub} WHERE nid=%d AND vid=%d ', $node->nid, $node->vid));
  614. //extract pub_id from the chado table for update function
  615. $match = array( 'pub_id' => $result->pub_id );
  616. //$table to be updated, $match is the 'pub_id', $value are the values that are to be updated
  617. $update_result = tripal_core_chado_update('pub', $match, $values);
  618. //Aquiring information for the abstract
  619. $abstract_info = tripal_core_chado_select('cvterm', array('cvterm_id'),
  620. array('name' => 'abstract', 'cv_id' => array('name' => 'tripal')
  621. )
  622. );
  623. //Grabbing the type id of the old abstract
  624. $type_id = $abstract_info[0]->cvterm_id;
  625. //Aquiring information for the abstract
  626. $abstract = array(
  627. 'pub_id' => $result->pub_id,
  628. 'type_id' => $type_id,
  629. 'value' => $node->abstract,
  630. 'rank' => 1
  631. );
  632. //$table to be updated, $match is the 'pub_id', $value are the values that are to be updated
  633. tripal_core_chado_update('pubprop', array('pub_id' => $result->pub_id, 'type_id' => $type_id, 'rank' => 1), $abstract);
  634. //counter for loop
  635. for ($i=0; $i<=sizeof($node->authors); $i++) {
  636. if (isset($node->authors[$i]['author_name'] )) {
  637. //if new insert
  638. if (isset($node->authors[$i]['is_new'])) {
  639. $authors = array(
  640. 'pub_id' => $match['pub_id'],
  641. 'rank' => $i+1,
  642. 'surname' => $node->authors[$i]['author_name']['surname'],
  643. 'givennames' => $node->authors[$i]['author_name']['givennames'],
  644. 'suffix' => $node->authors[$i]['author_name']['suffix'],
  645. );
  646. //inserts info into chado pubpro table for abstract
  647. tripal_core_chado_insert('pubauthor', $authors);
  648. }
  649. else{
  650. //update
  651. //$table=pubauthor to be updated, $match=author_match is the 'pub_id', $value=author are the values that are to be updated
  652. tripal_core_chado_update('pubauthor', array('pub_id' => $match['pub_id'], 'rank' => $node->authors[$i]['author_name']['rank']), $node->authors[$i]['author_name']);
  653. }
  654. }
  655. }
  656. }
  657. /**
  658. * Implementation of tripal_pub_load().
  659. *
  660. *
  661. * @param $node
  662. * The node that is to be accessed from the database
  663. *
  664. * @return $node
  665. * The node with the information to be loaded into the database
  666. *
  667. */
  668. function chado_pub_load($node) {
  669. $result = db_fetch_object(db_query('SELECT * FROM {chado_pub} WHERE nid=%d AND vid=%d ', $node->nid, $node->vid));
  670. $values = array(
  671. 'pub_id' => $result->pub_id,
  672. );
  673. if (empty($result->pub_id)) {
  674. drupal_set_message(t("Unable to find publication"), 'error');
  675. }
  676. else{
  677. $node->pub = tripal_core_generate_chado_var('pub', $values);
  678. // add in authors
  679. $authors = tripal_core_chado_select(
  680. 'pubauthor',
  681. array('rank', 'surname', 'givennames', 'suffix'),
  682. array('pub_id' => $node->pub->pub_id)
  683. );
  684. foreach ($authors as $author) {
  685. $node->pub->authors[$author->rank] = $author;
  686. }
  687. ksort($node->pub->authors);
  688. }
  689. return $node;
  690. }
  691. //-----------------------------------------------------------------------------
  692. // SECTION: Drupal AHAH Implementation & Edit Publication Section
  693. //-----------------------------------------------------------------------------
  694. /**
  695. * Submit handler for 'Add Author' button on node form.
  696. */
  697. function tripal_pub_add_author_submit($form, &$form_state) {
  698. $form_state['remove_delta'] = 0;
  699. // Set the form to rebuild and run submit handlers.
  700. node_form_submit_build_node($form, $form_state);
  701. // Make the changes we want to the form state.
  702. if ($form_state['values']['author_more']) {
  703. $new_author = array();
  704. $new_author['author_name'] = $form_state['values']['new_author']['new_author_name'];
  705. $new_author['is_new'] = TRUE;
  706. $form_state['new_author'] = $new_author;
  707. }
  708. }
  709. /**
  710. * Submit handler for 'Edit' button on node form.
  711. */
  712. function tripal_pub_edit_author_submit($form, &$form_state) {
  713. // remember which author we're editing
  714. $delta = $form_state['values']['edit_author']['delta'];
  715. //add changes author details back
  716. $rank = $form_state['values']['authors'][$delta]['author_name']['rank'];
  717. $form_state['values']['authors'][$delta]['author_name'] = $form_state['values']['edit_author']['edit_author_name'];
  718. $form_state['values']['authors'][$delta]['author_name']['rank'] = $rank;
  719. //ensures that after they save their changes the edit fieldstate goes away
  720. unset($form_state['values']['edit_author']);
  721. // Set the form to rebuild and run submit handlers.
  722. node_form_submit_build_node($form, $form_state);
  723. }
  724. /**
  725. * Submit handler for 'Edit' button on node form.
  726. */
  727. function tripal_pub_set_edit_author_submit($form, &$form_state) {
  728. $form_state['remove_delta'] = 0;
  729. // Make the changes we want to the form state
  730. $edit_author = array();
  731. if (preg_match('/edit_author_(\d+)/', $form_state['clicked_button']['#name'], $matches)) {
  732. $delta = $matches[1];
  733. $form_state['values']['edit_author'] = $form_state['values']['authors'][$delta];
  734. $form_state['values']['edit_author']['delta'] = $delta;
  735. }
  736. // Set the form to rebuild and run submit handlers.
  737. node_form_submit_build_node($form, $form_state);
  738. }
  739. /**
  740. * Submit handler for 'Remove' button on node form.
  741. */
  742. function tripal_pub_remove_row_submit($form, &$form_state) {
  743. if (preg_match('/remove_author_(\d+)/', $form_state['clicked_button']['#name'], $matches)) {
  744. $delta = $matches[1];
  745. $form_state['values']['remove_author'] = $form_state['values']['authors'][$delta];
  746. $form_state['values']['remove_author']['delta'] = $delta;
  747. }
  748. $values = array(
  749. 'pub_id' => $form_state['values']['pub_id'],
  750. 'rank' => $form_state['values']['authors'][$delta]['author_name']['rank']
  751. );
  752. //deleting row in chado table
  753. tripal_core_chado_delete('pubauthor', $values);
  754. // Set the form to rebuild and run submit handlers.
  755. node_form_submit_build_node($form, $form_state);
  756. }
  757. /*
  758. *
  759. *
  760. *
  761. */
  762. function tripal_pub_js($delta = 0) {
  763. $form = tripal_pub_ajax_form_handler($delta);
  764. // Render the new output.
  765. $author_form = $form['author_wrapper']; //was ['author']
  766. // Prevent duplicate wrappers.
  767. unset($author_form['#prefix'], $author_form['#suffix']);
  768. $output = theme('status_messages') . drupal_render($author_form);
  769. // AHAH does not know about the "Remove" button.
  770. // This causes it not to attach AHAH behaviours to it after modifying the form.
  771. // So we need to tell it first.
  772. $javascript = drupal_add_js(NULL, NULL);
  773. if (isset($javascript['setting'])) {
  774. $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings, '. drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) .');</script>';
  775. }
  776. // Final rendering callback.
  777. drupal_json(array('status' => TRUE, 'data' => $output));
  778. }
  779. /**
  780. * AJAX form handler.
  781. */
  782. function tripal_pub_ajax_form_handler($delta=0 ) {
  783. // The form is generated in an include file which we need to include manually.
  784. include_once 'modules/node/node.pages.inc';
  785. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  786. $form_build_id = filter_xss($_POST['form_build_id']);
  787. // Get the form from the cache.
  788. $form = form_get_cache($form_build_id, $form_state);
  789. $args = $form['#parameters'];
  790. $form_id = array_shift($args);
  791. // We need to process the form, prepare for that by setting a few internals.
  792. $form_state['post'] = $form['#post'] = $_POST;
  793. $form['#programmed'] = $form['#redirect'] = FALSE;
  794. // Set up our form state variable, needed for removing authors.
  795. $form_state['remove_delta'] = $delta;
  796. // Build, validate and if possible, submit the form.
  797. drupal_process_form($form_id, $form, $form_state);
  798. // If validation fails, force form submission.
  799. if (form_get_errors()) {
  800. form_execute_handlers('submit', $form, $form_state);
  801. }
  802. // This call recreates the form relying solely on the form_state that the
  803. // drupal_process_form set up.
  804. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  805. return $form;
  806. }
  807. /*
  808. *
  809. *
  810. */
  811. function theme_tripal_pub_author_table($form) {
  812. $rows = array();
  813. $headers = array(
  814. t('Givennames'),
  815. t('Surname'),
  816. t('Suffix'),
  817. '', // Blank header title for the remove link.
  818. );
  819. foreach (element_children($form) as $key) {
  820. // No need to print the field title every time.
  821. unset(
  822. $form[$key]['author_name_text']['#title'],
  823. $form[$key]['author_name_text-2']['#title'],
  824. $form[$key]['author_name_text-3']['#title'],
  825. $form[$key]['remove_author']['#title']
  826. );
  827. // Build the table row.
  828. $row = array(
  829. 'data' => array(
  830. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text']), 'class' => 'author-name'),
  831. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text-2']), 'class' => 'author-name'),
  832. array('data' => drupal_render($form[$key]['author_name']) . drupal_render($form[$key]['author_name_text-3']), 'class' => 'author-name'),
  833. array('data' => drupal_render($form[$key]['edit_author']) . drupal_render($form[$key]['remove_author']), 'class' => 'remove-author'),
  834. ),
  835. );
  836. // Add additional attributes to the row, such as a class for this row.
  837. if (isset($form[$key]['#attributes'])) {
  838. $row = array_merge($row, $form[$key]['#attributes']);
  839. }
  840. $rows[] = $row;
  841. }
  842. //$output = theme('table', $headers, $rows);
  843. $output .= drupal_render($form);
  844. return $output;
  845. }
  846. /**
  847. *
  848. * Helper function to define populated form field elements for publication node form.
  849. *
  850. */
  851. function tripal_pub_author_display_form($delta, $author) {
  852. $form = array(
  853. '#tree' => TRUE,
  854. );
  855. // Author Name
  856. $form['author_name'] = array(
  857. '#type' => 'hidden',
  858. '#value' => $author,
  859. '#parents' => array('authors', $delta),
  860. );
  861. $form['author_name_text'] = array(
  862. '#type' => 'item',
  863. '#title' => t('Givennames'),
  864. '#parents' => array('authors', $delta),
  865. '#value' => $author['author_name']['givennames'],
  866. );
  867. $form['author_name_text-2'] = array(
  868. '#type' => 'item',
  869. '#title' => t('Surname'),
  870. '#parents' => array('authors', $delta),
  871. '#value' => $author['author_name']['surname'],
  872. );
  873. $form['author_name_text-3'] = array(
  874. '#type' => 'item',
  875. '#title' => t('Suffix'),
  876. '#parents' => array('authors', $delta),
  877. '#value' => $author['author_name']['suffix'],
  878. );
  879. // Remove button.
  880. $form['remove_author'] = array(
  881. '#type' => 'submit',
  882. '#name' => 'remove_author_' . $delta,
  883. '#value' => t('Remove'),
  884. '#submit' => array('tripal_pub_remove_row_submit'),
  885. '#parents' => array('authors', $delta, 'remove_author'),
  886. '#ahah' => array(
  887. 'path' => 'tripal_pub/js/0',
  888. 'wrapper' => 'author-wrapper',
  889. 'method' => 'replace',
  890. 'effect' => 'fade',
  891. ),
  892. );
  893. // Edit Author button
  894. $form['edit_author'] = array(
  895. '#type' => 'submit',
  896. '#name' => 'edit_author_' . $delta,
  897. '#value' => t('Edit'),
  898. '#submit' => array('tripal_pub_set_edit_author_submit'),
  899. '#parents' => array('authors', $delta, 'edit_author'),
  900. '#ahah' => array(
  901. 'path' => 'tripal_pub/js/0',
  902. 'wrapper' => 'author-wrapper',
  903. 'method' => 'replace',
  904. 'effect' => 'fade',
  905. ),
  906. );
  907. return $form;
  908. }
  909. /*
  910. * This function executes commands periodically. This is called whenever a cron run occurs. This
  911. * function uses the time interval that the user has entered. This Publication module, requires
  912. * that the program is ran in pre-determined intervals, as desired by the user. By using a time stamp
  913. * and comparing the current time and the time that the last time a cron was ran, this program will
  914. * be ran.
  915. *
  916. *
  917. * @TODO: The cron function is not working correctly, not sure why the cron is not working properly.
  918. *
  919. *
  920. *
  921. */
  922. /**
  923. function tripal_pub_cron(){
  924. global $user; //needed to make the current users details available so access of user id is available
  925. //Aquiring the current time
  926. $current_time = time();
  927. //Aquiring the user entered time interval
  928. $user_interval = variable_get('time_interval',NULL);
  929. //converting the user entered interval into seconds for use with unix time stamp
  930. $converted_interval = ($user_interval*60);
  931. //Accessing database for time stamp from watchdog
  932. $cron = db_result(db_query('select timestamp from {watchdog} where type="cron" order by timestamp desc limit 1'));
  933. //debugging print statement
  934. //print($cron);
  935. //taking variable value & assigning for use
  936. $cron_last = variable_get('cron', time());
  937. $updated_interval = $cron_last - $converted_interval;
  938. if($current_time >= $updated_interval ){
  939. tripal_add_job('Search & Load PubMed Publications', 'tripal_pub', 'tripal_pub_search_load_pubmed_publications', $job_args, $user->uid);
  940. }
  941. }
  942. */
  943. //-----------------------------------------------------------------------------
  944. // SECTION: Configuration form
  945. //-----------------------------------------------------------------------------
  946. /**
  947. * Implementation of tripal_pub_form().
  948. *
  949. * This form takes the following information:A Publication Title,Volume title,Volume,Series Name,
  950. * Issue,Publication Year,Pages where the Article is located, Miniref,Type-Id, if the article is Obsolete,
  951. * Publishing company,Pubplication Place and a Uniquename for the the instance. It then puts the
  952. * infromation into the Chado_project database table based on its 'pub_id'.
  953. *
  954. *
  955. * @return $form
  956. * An array of menu items '$form'
  957. *
  958. */
  959. function tripal_pub_configuration_form() {
  960. $cv_options = tripal_cv_get_cv_options();
  961. //Creating Fieldset for multiple fields in form
  962. $form['node_form'] = array(
  963. '#type' => 'fieldset',
  964. '#title' => t('Create/Edit Publication Settings'),
  965. );
  966. $form['node_form']['tripal_pub_types_cv'] = array(
  967. '#type' => 'select',
  968. '#title' => t('Controlled Vocabularies'),
  969. '#options' => $cv_options,
  970. '#default_value' => variable_get('tripal_pub_types_cv', 0),
  971. '#description' => 'Set the controlled vocabulary to pull publication type options from. Terms in this vocabulary will be available is the Publication Type select box on both the create and edit pages.',
  972. );
  973. $form['pubmed'] = array(
  974. '#type' => 'fieldset',
  975. '#title' => t('Create Nodes via PubMed Search'),
  976. );
  977. $form['pubmed']['description'] = array(
  978. '#type' => 'item',
  979. '#value' => 'Publication nodes are created based on the results of a PubMed publication search using '
  980. .'the keywords entered below. No content is created until the sync is clicked below and the registered tripal job is '
  981. .'run. This script attempts to only load new publications (ones which don\'t already have nodes from a previous search) '
  982. .'by comparing the pub_id thus if a publication is added manually which also appears in the pubmed search it will likely '
  983. .'get added twice.'
  984. );
  985. //define form elements for the node's title and body.
  986. $form['pubmed']['unique_name'] = array(
  987. '#type' => 'textfield',
  988. '#title' => t('Search Keywords'),
  989. '#description' => t('Specific search terms. Must be seperated by a single space.'),
  990. '#required' => FALSE,
  991. '#default_value' => variable_get('unique_name', NULL)
  992. );
  993. //define form elements for the node's title and body.
  994. /**
  995. $form['set']['time_interval'] = array(
  996. '#type' => 'textfield',
  997. '#title' => t('Time Search Interval (Minutes)'),
  998. '#description'=>t(' The “Search Interval” set here determines when a drupal cron job should
  999. schedule a tripal job. As such, in reality the time until publications are sync’d is “Search Interval”
  1000. + time remaining until drupal cron is run + time between drupal cron run and next tripal jobs run'),
  1001. '#required' => FALSE,
  1002. '#default_value' => variable_get('time_interval', NULL)
  1003. );
  1004. */
  1005. $form['pubmed']['sync_info'] = array(
  1006. '#type' => 'submit',
  1007. '#title' => t('Sync Publications Jobs'),
  1008. '#value' => t('Sync'),
  1009. );
  1010. $form['submit'] = array(
  1011. '#type' => 'submit',
  1012. '#weight' => 10,
  1013. '#value' => t('Save Configuration')
  1014. );
  1015. return $form;
  1016. }
  1017. /*
  1018. * Pub Configuration-Form
  1019. * This form submit uses variable_set to set the vocabularies that are used, as well as the
  1020. * unique_name and time interval that was entered by the user. If the user selects the option
  1021. * to Sync Publicatin Jobs, the 'tripal_add_job' function is called, and a tripal job
  1022. * will be added.
  1023. *
  1024. * @param $form
  1025. * -The submitted form containing the user entered infromation
  1026. * @param $form_state
  1027. * -Is the state of the form: i.e what button was pressed, what infromation was entered,etc.
  1028. * The key is the 'values'
  1029. */
  1030. function tripal_pub_configuration_form_submit($form, $form_state) {
  1031. global $user; //needed to make the current users details available so access of user id is available
  1032. if ($form_state['values']['op'] == t('Save Configuration')) {
  1033. variable_set('tripal_pub_types_cv', $form_state['values']['tripal_pub_types_cv']);
  1034. variable_set('unique_name', $form_state['values']['unique_name'] );
  1035. }
  1036. //adding a tripal job if the user selects to Sync the Publications
  1037. if ($form_state['values']['op'] == t('Sync')) {
  1038. variable_set('unique_name', $form_state['values']['unique_name'] );
  1039. $job_args = array($form_state['values']['unique_name']);
  1040. $job_id = tripal_add_job('Search & Load PubMed Publications', 'tripal_pub', 'tripal_pub_search_load_pubmed_publications', $job_args, $user->uid);
  1041. }
  1042. }
  1043. /**
  1044. * Tripal Pub Search Load Pubmed Publications
  1045. *
  1046. * This function takes a search term array and uses it as the parameters for the perl script
  1047. * e_utiliies.pl, which needs to be included and installed in any system that wants full
  1048. * functionality of this module, with respects to searching PubMed for relative articles.
  1049. * When the function ends, and all of the form_state values are set, the drupal_execute()
  1050. * function is called and retrieves a form using a form_id, populates it with $form_values,
  1051. * processes it, and returns any validation errors that came up.
  1052. *
  1053. *
  1054. *
  1055. * @TODO: The loading of this function still needs to be fixed. When running the configuration, there
  1056. * are still errors coming up, UFT8 warnings still come up, in random places. My guess, it is when
  1057. * a article is found that is not in english
  1058. *
  1059. *
  1060. *
  1061. * @param $search_term_array
  1062. * -This arrary contains the search terms that were entered
  1063. *
  1064. *
  1065. */
  1066. function tripal_pub_search_load_pubmed_publications($search_term_array) {
  1067. $key_terms = preg_split('/\s+/', $search_term_array);
  1068. $search_terms= implode("+", $key_terms);
  1069. //Retrieving absolute path of current directory
  1070. $path = drupal_get_path('module', 'tripal_pub');
  1071. $i_path = realpath(".");
  1072. $absolute_path = $i_path . '/' . $path;
  1073. //perl file required for operating the NCBI PubMed database search, uses users working directory
  1074. $command = 'perl ' . $absolute_path . '/e_utilities.pl' . ' ' . $search_terms . ' ' . 'uilist';
  1075. print "\nAccessing PubMed ID's\n";
  1076. exec($command, $output);
  1077. //for loop going through all the pub_med_id, creating a node for each one
  1078. for ($i=0; $i < sizeof($output); $i++) {
  1079. $pmid = $output[$i];
  1080. //aquiring the pubmed id from the pub table based on the uniquename
  1081. $pubmed_id = tripal_core_chado_select( 'pub', array('pub_id'), array( 'uniquename' => $pmid) );
  1082. //Checking that the pub-med id is not set & thus not in the database
  1083. if (!isset($pubmed_id[0]->pub_id)) {
  1084. print "\nRetrieving PubMed ID:" . $pmid . "\n";
  1085. $command = '';
  1086. $xml_report = '';
  1087. $newline_separated = '';
  1088. $xml = '';
  1089. //Uncomment below line to see any syntax errors within the perl script
  1090. //$command = 'perl ' ."-cs" .' ' . $absolute_path .'/e_utilities.pl' .' ' .$pmid.'[uid] ' .'xml';
  1091. $command = 'perl ' . $absolute_path . '/e_utilities.pl' . ' ' . $pmid . '[uid] ' . 'xml';
  1092. exec($command, $xml_report);
  1093. //adding the xml report once the new line characters have been removed
  1094. $newline_separated = implode("\n", $xml_report);
  1095. //relaying the xml output so it can be put in a string variable
  1096. $xml = simplexml_load_string($newline_separated);
  1097. //checking the Article Title exists, if so, procede with setting values
  1098. if (isset($xml->PubmedArticle->MedlineCitation->Article->ArticleTitle)) {
  1099. $form_id ='chado_pub_node_form';
  1100. $form_state = array();
  1101. module_load_include('inc', 'node', 'node.pages');
  1102. $node = array('type' => 'chado_pub');
  1103. $form_state = array();
  1104. $form_state['values']['title'] =
  1105. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->ArticleTitle),
  1106. "UTF-8",
  1107. (string)$xml->PubmedArticle->MedlineCitation->Article->ArticleTitle);
  1108. $form_state['values']['abstract'] =
  1109. iconv(iconv_get_encoding((string)$xml->PubmedArticle),
  1110. "UTF-8",
  1111. (string)$xml->PubmedArticle);
  1112. $form_state['values']['authors'] =
  1113. iconv(iconv_get_encoding((string)$xml->PubmedArticle),
  1114. "UTF-8",
  1115. (string)$xml->PubmedArticle);
  1116. $form_state['values']['volumetitle'] =
  1117. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Journal->Title),
  1118. "UTF-8",
  1119. (string)$xml->PubmedArticle->MedlineCitation->Article->Journal->Title);
  1120. $form_state['values']['series_name'] =
  1121. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Journal->Title),
  1122. "UTF-8",
  1123. (string)$xml->PubmedArticle->MedlineCitation->Article->Journal->Title);
  1124. $form_state['values']['volume'] =
  1125. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Volume),
  1126. "UTF-8",
  1127. (string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Volume);
  1128. $form_state['values']['issue'] =
  1129. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Issue),
  1130. "UTF-8",
  1131. (string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->Issue);
  1132. $form_state['values']['pyear'] =
  1133. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year),
  1134. "UTF-8",
  1135. (string)$xml->PubmedArticle->MedlineCitation->Article->Journal->JournalIssue->PubDate->Year);
  1136. $form_state['values']['pages'] =
  1137. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->Article->Pagination->MedlinePgn),
  1138. "UTF-8",
  1139. (string)$xml->PubmedArticle->MedlineCitation->Article->Pagination->MedlinePgn);
  1140. $form_state['values']['uniquename'] =
  1141. iconv(iconv_get_encoding((string)$xml->PubmedArticle->MedlineCitation->PMID),
  1142. "UTF-8",
  1143. (string)$xml->PubmedArticle->MedlineCitation->PMID);
  1144. $type_id = tripal_core_chado_select( 'cvterm', array('cvterm_id'), array( 'name' => 'Article') );
  1145. $form_state['values']['type_id'] = $type_id[0]->cvterm_id;
  1146. $form_state['values']['is_obsolete']='f';
  1147. $form_state['values']['op'] = t('Save');
  1148. form_set_error(NULL, '', TRUE);
  1149. //If the required values & fields are not set, the drupal_execute function is not called and node is not added
  1150. if (isset($form_state['values']['volumetitle']) || isset($form_state['values']['series_name'])||
  1151. isset($form_state['values']['pages'])||isset($form_state['values']['uniquename'])) {
  1152. drupal_execute('tripal_pub_node_form', $form_state, (object)$node);
  1153. }
  1154. $errors = form_get_errors();
  1155. //if(isset($errors)){
  1156. if ( $errors !=
  1157. 'Warning: pg_query(): Query failed: ERROR: invalid byte sequence for encoding "UTF8": 0xfc
  1158. HINT: This error can also happen if the byte sequence does not match the encoding expected
  1159. by the server, which is controlled by "client_encoding".') {
  1160. print "\nErrors:" . print_r($errors, TRUE) . "\n";
  1161. }
  1162. else{
  1163. print "\nErrors:" . print_r($errors, TRUE) . "\n";
  1164. }
  1165. }
  1166. }
  1167. elseif ($i==sizeof($output)) {
  1168. print "\n Search Completed Successfully \n";
  1169. print "\n Total Nodes Created:" . $i . "\n";
  1170. }
  1171. else{
  1172. print "\nERROR: The following PubMed ID:" . ' ' . $pubmed_id[0]->pub_id . " Exists in database,skipped.\n";
  1173. }
  1174. }
  1175. }
  1176. //-----------------------------------------------------------------------------
  1177. // SECTION: Custom form Elements
  1178. //-----------------------------------------------------------------------------
  1179. /*
  1180. * This fucnction tells the FAPI(Form-API) that this is a element that will carry a value, contains
  1181. * arrays of callback function names. Will declare an element will create a reuseable element type.
  1182. *
  1183. * @return
  1184. * An associative array with the name of each element type as a key and an
  1185. * array of attributes describingthe type as a value
  1186. */
  1187. function tripal_pub_elements() {
  1188. return array(
  1189. 'publication_author' => array(
  1190. '#input' => TRUE,
  1191. '#process' => array('expand_publication_author'),
  1192. '#element_validate' => array('publication_author_validate'),
  1193. ),
  1194. );
  1195. }
  1196. /*
  1197. *
  1198. *
  1199. *
  1200. */
  1201. function expand_publication_author($element) {
  1202. if (empty($element['#value'])) {
  1203. $element['#value'] = array(
  1204. 'givennames' => '',
  1205. 'suffix' => '',
  1206. 'surname' => '',
  1207. );
  1208. }
  1209. $element['#tree'] = TRUE;
  1210. $parents = $element['#parents'];
  1211. $parents[] = 'givennames';
  1212. $element['givennames'] = array(
  1213. '#type' => 'textfield',
  1214. '#size' => 10,
  1215. //'#maxlength' => TRUE,
  1216. //'#default_value'=> $element['#value']['#given'],
  1217. '#prefix' => 'Given Name',
  1218. );
  1219. if ($element['#default_value']) {
  1220. $element['givennames']['#default_value'] = $element['#default_value']['givennames'];
  1221. }
  1222. $parents = $element['#parents'];
  1223. $parents[]= 'surname';
  1224. $element['surname'] = array(
  1225. '#type' => 'textfield',
  1226. '#size' => 10,
  1227. //'#maxlength' => TRUE,
  1228. //'#default_value'=> $element['#value']['#surname'],
  1229. '#prefix' => 'Surname',
  1230. );
  1231. if ($element['#default_value']) {
  1232. $element['surname']['#default_value'] = $element['#default_value']['surname'];
  1233. }
  1234. $parents = $element['#parents'];
  1235. $parents[]= 'suffix';
  1236. $element['suffix'] = array(
  1237. '#type' => 'textfield',
  1238. '#size' => 5,
  1239. //'#maxlength' => TRUE,
  1240. //'#default_value'=> $element['#value']['#suffix'],
  1241. '#prefix' => 'suffix',
  1242. );
  1243. if ($element['#default_value']) {
  1244. $element['suffix']['#default_value'] = $element['#default_value']['suffix'];
  1245. }
  1246. return $element;
  1247. }
  1248. /*
  1249. *
  1250. *
  1251. *
  1252. */
  1253. function theme_publication_author($element) {
  1254. return theme('form_element', $element, '<div class="container-inline">' . $element['#children'] . '</div>');
  1255. }
  1256. /*
  1257. *
  1258. *
  1259. *
  1260. */
  1261. function publication_author_validate($element) {
  1262. if ($element['#required']) {
  1263. if (trim($element['#value']['givennames']) == '' || trim($element['#value']['suffix']) == '' || trim($element['#value']['surname']) == '' ||
  1264. !is_string($element['#value']['givennames']) || !is_string($element['#value']['givennames']) || !is_string($element['#value']['surname'])) {
  1265. form_error($element, t('The Author name is required.'));
  1266. }
  1267. }
  1268. }
  1269. //-----------------------------------------------------------------------------
  1270. // End of Software
  1271. //-----------------------------------------------------------------------------