PhpLabWare version 0.3 
/plugins/ -> pdfs_plugin.php

1  <?php
2 
3  // plugin_inc.php - skeleton file for plugin codes
4  // plugin_inc.php - author: Nico Stuurman
5 
6  /*
7  Copyright 2002, Nico Stuurman
8 
9  This is a skeleton file to code your own plugins.
10  To use it, rename this file to something meaningfull,
11  add the path and name to this file (relative to the phplabware root)
12  in the column 'plugin_code' of 'tableoftables', and code away.
13  And, when you make something nice, please send us a copy!
14 
15  This program is free software: you can redistribute it and/ormodify it under
16  the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
17 
18  */
19 
20 
21  ////
22  // ! outputs to a file a reference plus link to the newly added pdf
23  function plugin_add ($db,$tableid,$id)
24  {
25     global $PHP_SELF,$system_settings;
26     $table_desc=get_cell($db,"tableoftables","table_desc_name","id",$tableid);
27     $tablename=get_cell($db,"tableoftables","tablename","id",$tableid);
28     $real_tablename=get_cell($db,"tableoftables","real_tablename","id",$tableid);
29     $journaltable=get_cell($db,$table_desc,"associated_table","columnname","journal");
30 
31     $r=$db->Execute("SELECT ownerid,title,journal,pubyear,volume,fpage,lpage,author FROM $real_tablename WHERE id=$id");
32     $fid=@fopen($system_settings["pdfs_file"],w);
33     if ($fid) {
34        $link= $system_settings["baseURL"].getenv("SCRIPT_NAME")."?tablename=$tablename&showid=$id";
35        $journal=get_cell($db,$journaltable,"type","id",$r->fields["journal"]);
36        $submitter=get_person_link($db,$r->fields["ownerid"]);
37        $text="<a href='$link'><b>".$r->fields["title"];
38        $text.="</b></a> $journal (".$r->fields["pubyear"]."), <b>".$r->fields["volume"];
39        $text.="</b>:".$r->fields["fpage"]."-".$r->fields["lpage"];
40        $text.= ". ".$r->fields["author"]." Submitted by $submitter.";
41        fwrite($fid,$text);
42        fclose($fid);
43     }
44  }
45 
46 
47  ////
48  // !Change/calculate/check values just before they are added/modified
49  // $fieldvalues is an array with the column names as key.
50  // Any changes you make in the values of $fieldvalues will result in
51  // changes in the database.
52  // You could, for instance, calculate a value of a field based on other fields
53  function plugin_check_data($db,&$field_values,$table_desc,$modify=false)
54  {
55     global $HTTP_POST_FILES;
56     // we need some info from the database
57     $pdftable=get_cell($db,"tableoftables","real_tablename","table_desc_name",$table_desc);
58     $pdftablelabel=get_cell($db,"tableoftables","tablename","table_desc_name",$table_desc);
59     $journaltable=get_cell($db,$table_desc,"associated_table","columnname","journal");
60 
61     // some browsers do not send a mime type??
62     if (is_readable($HTTP_POST_FILES["file"]["tmp_name"][0])) {
63        if (!$HTTP_POST_FILES["file"]["type"][0]) {
64           // we simply force it to be a pdf risking users making a mess
65           $HTTP_POST_FILES["file"]["type"][0]="application/pdf";
66        }
67     }
68     // avoid problems with spaces and the like
69     $field_values["pmid"]=trim($field_values["pmid"]);
70 
71     // no fun without a pmid
72     if (!$field_values["pmid"]) {
73        if ($modify)
74           return true;
75        else {
76           echo "<h3 align='center'>Please enter the Pubmed ID of the PDF reprint.</h3>";
77           return false;
78        }
79     }
80 
81     // rename file to pmid.pdf
82     if ($HTTP_POST_FILES["file"]["name"][0]) {
83        $HTTP_POST_FILES["file"]["name"][0]=$field_values["pmid"].".pdf";
84     }
85     
86     // check whether we had this one already
87     if (!$modify) {
88        $existing_id=get_cell($db,$pdftable,"id","pmid",$field_values["pmid"]);
89        if ($existing_id) {
90           echo "<h3 align='center'><a href='general.php?tablename=$pdftablelabel&showid=$existing_id'>That paper </a>is already in the database.</h3>\n";
91           return false;
92        }
93     }
94 
95     // this will protect quotes in the imported data
96     set_magic_quotes_runtime(1);
97 
98     // data from pubmed and parse
99     $pmid=$field_values["pmid"];
100     $pubmedinfo=@file("http://www.ncbi.nlm.nih.gov/entrez/utils/pmfetch.fcgi?db=PubMed&id=$pmid&report=abstract&report=abstract&mode=text");
101     if ($pubmedinfo) {
102        // lines appear to be broken randomly, but parts are separated by empty lines
103        // get them into array $line
104        for ($i=0; $i<sizeof($pubmedinfo);$i++) {
105           $line[$lc].=str_replace("\n"," ",$pubmedinfo[$i]);
106           if ($pubmedinfo[$i]=="\n")
107           $lc++;
108        }
109        // parse the first line. 1: journal date;Vol:fp-lp
110        $jstart=strpos($line[1],": ");
111        $jend=strpos($line[1],". ")-1;
112        $journal=trim(substr($line[1],$jstart+1,$jend-$jstart));
113        $dend=strpos($line[1],";");
114        $date=trim(substr($line[1],$jend+2,$dend-$jend-1));
115        $year=$field_values["pubyear"]=strtok($date," ");
116        $vend=strpos($line[1],":",$dend);
117        // if we can not find this, it might not have vol. first/last page
118        if ($vend) {
119           $volumeinfo=trim(substr($line[1],$dend+1,$vend-$dend-1));
120           $volume=$field_values["volume"]=trim(strtok($volumeinfo,"("));
121           $pages=trim(substr($line[1],$vend+1));
122           $fpage=strtok($pages,"-");
123           $lpage1=strtok("-");
124           $lpage=substr_replace($fpage,$lpage1,strlen($fpage)-strlen($lpage1));
125        }
126        // echo "$jstart,$jend,$journal,$date,$year,$volume,$fpage,$lpage1,$lpage.<br>";
127        $field_values["fpage"]=(int)$fpage;
128        $field_values["lpage"]=(int)$lpage;
129        // there can be a line 2 with 'Comment in:' put in notes and delete
130        // same for line with Erratum in:
131        // ugly shuffle to get everything right again
132        if ((substr($line[2],0,11)=="Comment in:") || (substr($line[2],0,11)=="Erratum in:") ) {
133           $field_values["notes"]=$line[2].$field_values["notes"];
134           $line[2]=$line[3];
135           $line[3]=$line[4];
136           $line[5]=$line[6];
137        }
138        $field_values["title"]=$line[2];
139        $field_values["author"]=$line[3];
140        // check whether there is an abstract
141        if ((substr($line[5],0,4)!="PMID"))
142           $field_values["abstract"]=$line[5];
143        // check wether the journal is in journaltable, if not, add it
144        $r=$db->Execute("SELECT id FROM $journaltable WHERE typeshort='$journal'");
145        if ($r && $r->fields("id"))
146           $field_values["journal"]=$r->fields("id");
147        else {
148           $tid=$db->GenID("$journaltable"."_id_seq");
149           if ($tid) {
150           $r=$db->Execute("INSERT INTO $journaltable (id,type,typeshort,sortkey) VALUES ($tid,'$journal','$journal',0)");
151           if ($r)
152           $field_values["journal"]=$tid;
153           }
154        }
155     }
156     else {
157        echo "<h3>Failed to import the Pubmed data</h3>\n";
158        set_magic_quotes_runtime(0);
159        return true;
160     }
161     // some stuff goes wrong when this remains on
162     set_magic_quotes_runtime(0);
163     return true;
164  }
165 
166 
167  ////
168  // !Overrides the standard 'show record'function
169  function plugin_show($db,$tableinfo,$id,$USER,$system_settings,$backbutton=true)
170  {
171     global $PHP_SELF;
172     $journaltable=get_cell($db,$tableinfo->desname,"associated_table","columnname","journal");
173     $categorytable=get_cell($db,$tableinfo->desname,"associated_table","columnname","category");
174     if (!may_read($db,$tableinfo,$id,$USER))
175        return false;
176 
177     // get values
178     $r=$db->Execute("SELECT $tableinfo->fields FROM $tableinfo->realname WHERE id=$id");
179     if ($r->EOF) {
180        echo "<h3>Could not find this record in the database</h3>";
181        return false;
182     }
183     $column=strtok($tableinfo->fields,",");
184     while ($column) {
185        ${$column}=$r->fields[$column];
186        $column=strtok(",");
187     }
188 
189     echo "&nbsp;<br>\n";
190     echo "<table border=0 align='center'>\n";
191     echo "<tr>\n";
192     echo "<th>Article: </th>\n";
193     echo "<td>$title<br>\n$author<br>\n";
194     $text=get_cell($db,$journaltable,"type","id",$journal);
195     echo "$text ($pubyear), <b>$volume</b>:$fpage-$lpage\n";
196     echo "</td></tr>\n";
197     
198     if ($abstract) {
199        echo "<tr>\n<th>Abstract</th>\n";
200        echo "<td>$abstract</td>\n</tr>\n";
201     }
202     // Category
203     if ($category) {
204        $type2=get_cell($db,$categorytable,"type","id",$category);
205        echo "<tr>\n<th>Category</th>\n";
206        echo "<td>$type2</td>\n</tr>\n";
207     }
208 
209     echo "<tr>";
210     $query="SELECT firstname,lastname,email FROM users WHERE id=$ownerid";
211     $r=$db->Execute($query);
212     if ($r->fields["email"]) {
213        echo "<th>Submitted by: </th><td><a href='mailto:".$r->fields["email"]."'>";
214        echo $r->fields["firstname"]." ".$r->fields["lastname"]."</a> ";
215     }
216     else {
217        echo "<th>Submitted by: </th><td>".$r->fields["firstname"]." ";
218        echo $r->fields["lastname"] ." ";
219     }
220     $dateformat=get_cell($db,"dateformats","dateformat","id",$system_settings["dateformat"]);
221     $date=date($dateformat,$date);
222     echo "($date)</td>\n";
223     echo "</tr>\n";
224 
225     if ($lastmodby && $lastmoddate) {
226        echo "<tr>";
227        $query="SELECT firstname,lastname,email FROM users WHERE id=$lastmodby";
228        $r=$db->Execute($query);
229        if ($r->fields["email"]) {
230           echo "<th>Last modified by: </th><td><a href='mailto:".$r->fields["email"]."'>";
231           echo $r->fields["firstname"]." ".$r->fields["lastname"]."</a>";
232        }
233        else {
234           echo "<th>Last modified by: </th><td>".$r->fields["firstname"]." ";
235           echo $r->fields["lastname"];
236        }
237        $dateformat=get_cell($db,"dateformats","dateformat","id",$system_settings["dateformat"]);
238        $lastmoddate=date($dateformat,$lastmoddate);
239        echo " ($lastmoddate)</td>\n";
240        echo "</tr>\n";
241     }
242 
243     echo "<tr>";
244     $notes=nl2br(htmlentities($notes));
245     echo "<th>Notes: </th><td>$notes</td>\n";
246     echo "</tr>\n";
247 
248     $columnid=get_cell($db,$tableinfo->desname,"id","columnname","file");
249     $files=get_files($db,$tableinfo->name,$id,$columnid,1);
250     if ($files) {
251        echo "<tr><th>Files:</th>\n<td>";
252        for ($i=0;$i<sizeof($files);$i++) {
253           echo $files[$i]["link"]." (".$files[$i]["type"]." file, ".$files[$i]["size"].")<br>\n";
254        }
255        echo "</tr>\n";
256     }
257     
258     echo "<tr><th>Links:</th><td colspan=7><a href='$PHP_SELF?tablename=".$tableinfo->name."&showid=$id&";
259     echo SID;
260     echo "'>".$system_settings["baseURL"].getenv("SCRIPT_NAME")."?tablename=".$tableinfo->name."&showid=$id</a> (This page)<br>\n";
261 
262     echo "<a href='http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?";
263     if ($system_settings["pdfget"])
264        $addget="&".$system_settings["pdfget"];
265     echo "cmd=Retrieve&db=PubMed&list_uids=$pmid&dopt=Abstract$addget'>This article at Pubmed</a><br>\n";
266     echo "<a href='http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?";
267     echo "cmd=Link&db=PubMed&dbFrom=PubMed&from_uid=$pmid$addget'>Related articles at Pubmed</a></td></tr>\n";
268     show_reports($db,$tableinfo,$id);
269 
270  ?>
271  <form method='post' id='pdfview' action='<?php echo "$PHP_SELF?tablename=".$tableinfo->name?>&<?=SID?>'>
272  <?php
273     if ($backbutton) {
274        echo "<tr>";
275        echo "<td colspan=7 align='center'><input type='submit' name='submit' value='Back'></td>\n";
276        echo "</tr>\n";
277     }
278     else
279        echo "<tr><td colspan=8 align='center'>&nbsp;<br><button onclick='self.close();window.opener.focus();' name='Close' value='close'>Close</button></td></tr>\n";
280     echo "</table></form>\n";
281  }
282 
283 
284  /*
285 
286  ////
287  // !Extends the search query
288  // $query is the complete query that you can change and must return
289  // $fieldvalues is an array with the column names as key.
290  // if there is an $existing_clause (boolean) you should prepend your additions
291  // with ' AND' or ' OR', otherwise you should not
292  function plugin_search($query,$fieldvalues,$existing_clause)
293  {
294     return $query;
295  }
296 
297 
298  ////
299  // !Extends function getvalues
300  // $allfields is a 2-D array containing the field names of the table in the first dimension
301  // and name,columnid,label,datatype,display_table,display_record,ass_t,ass_column,
302  // ass_local_key,required,modifiable,text,values in the 2nd D
303  function plugin_getvalues($db,&$allfields)
304  {
305  }
306  */
307 
308  ////
309  // !Extends display_add
310  function plugin_display_add($db,$tableid,$nowfield)
311  {
312     if ($nowfield[name]=="pmid") {
313        echo "<br>Find the Pubmed ID for this article at <a target='_BLANK' href='http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=PubMed'>PubMed</a>";
314     }
315  }
316 
317 
318  ?>


Generated: Sun Oct 5 21:17:35 2003 SourceForge Logo Generated by PHPXref 0.2