tripal_pub.module 52 KB

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