Skip to content

Commit 0375dd9

Browse files
committed
add custom checkbox parser (opt-in mode)
1 parent 1eddba7 commit 0375dd9

File tree

1 file changed

+114
-4
lines changed

1 file changed

+114
-4
lines changed

src/fpdm.php

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@
3333
* V1.0 (03.11.2010) First working release *
3434
*******************************************************************************/
3535

36-
global $FPDM_FILTERS, $FPDM_REGEXPS; //needs explicit global scope, otherwise autoloading will br incomplete.
36+
global $FPDM_FILTERS, $FPDM_REGEXPS; //needs explicit global scope, otherwise autoloading will be incomplete.
3737
$FPDM_FILTERS=array(); //holds all supported filters
3838
$FPDM_REGEXPS= array(
39+
//FIX: parse checkbox definition
40+
"/AS"=>"/\/AS\s+\/(\w+)$/",
41+
"name"=>"/\/(\w+)/",
42+
// "/AP_D_SingleLine"=>"/\/D\s+\/(\w+)\s+\d+\s+\d+\s+R\s+\/(\w+)$/",
43+
//ENDFIX
3944
"/Type"=>"/\/Type\s+\/(\w+)$/",
4045
"/Subtype" =>"/^\/Subtype\s+\/(\w+)$/"
4146
);
@@ -66,7 +71,7 @@
6671

6772
class FPDM {
6873
//@@@@@@@@@
69-
74+
var $useCheckboxParser = false; //boolean: allows activation of custom checkbox parser (not available in original FPDM source)
7075

7176
var $pdf_source = ''; //string: full pathname to the input pdf , a form file
7277
var $fdf_source = ''; //string: full pathname to the input fdf , a form data file
@@ -867,7 +872,10 @@ function set_field_value($type,$name,$value) {
867872

868873
$offset_shift=$this->set_field_tooltip($name,$value);
869874

870-
} else {//if(isset($this->value_entries["$name"]["values"]["$type"])) {
875+
} elseif ($this->useCheckboxParser && isset($this->value_entries["$name"]['infos']['checkbox_state'])) { //FIX: set checkbox value
876+
$offset_shift=$this->set_field_checkbox($name, $value);
877+
//ENDFIX
878+
} else {//if(isset($this->value_entries["$name"]["values"]["$type"])) {
871879
// echo $this->value_entries["$name"]["values"]["$type"];
872880
/* $field_value_line=$this->value_entries["$name"]["values"]["$type"];
873881
$field_value_maxlen=$this->value_entries["$name"]["constraints"]["maxlen"];
@@ -921,6 +929,60 @@ function set_field_tooltip($name,$value) {
921929
return $offset_shift;
922930
}
923931

932+
//FIX: parse checkbox definition
933+
/**
934+
*Changes the checkbox state.
935+
*
936+
*@param string $name name of the field to change the state
937+
*@param string $value the new state to set
938+
*@return int offset_shift the size variation
939+
**/
940+
public function set_field_checkbox($name, $value)
941+
{
942+
//------------------------------------
943+
$offset_shift=0;
944+
$verbose_set=($this->verbose&&($this->verbose_level>1));
945+
//Get the line(s) of the misc field values
946+
if (isset($this->value_entries["$name"])) {
947+
if (isset($this->value_entries["$name"]["infos"]["checkbox_state_line"])
948+
&& isset($this->value_entries["$name"]["infos"]["checkbox_no"])
949+
&& isset($this->value_entries["$name"]["infos"]["checkbox_yes"])) {
950+
$field_checkbox_line=$this->value_entries["$name"]["infos"]["checkbox_state_line"];
951+
if ($field_checkbox_line) {
952+
if ($verbose_set) {
953+
echo "<br>Change checkbox of the field $name at line $field_checkbox_line to value [$value]";
954+
}
955+
$state = $this->value_entries["$name"]["infos"]["checkbox_no"];
956+
if ($value) {
957+
$state = $this->value_entries["$name"]["infos"]["checkbox_yes"];
958+
}
959+
$CurLine =$this->pdf_entries[$field_checkbox_line];
960+
$OldLen=strlen($CurLine);
961+
$CurLine = '/AS /'.$state;
962+
$NewLen=strlen($CurLine);
963+
$Shift=$NewLen-$OldLen;
964+
$this->shift=$this->shift+$Shift;
965+
//Saves
966+
$this->pdf_entries[$field_checkbox_line]=$CurLine;
967+
return $Shift;
968+
// $offset_shift=$this->_set_field_value($field_checkbox_line, $state);
969+
} else {
970+
if ($verbose_set) {
971+
echo "<br>Change checkbox value aborted, parsed checkbox definition incomplete.";
972+
}
973+
}
974+
} else {
975+
if ($verbose_set) {
976+
echo "<br>Change checkbox value aborted, the field $name has no checkbox definition.";
977+
}
978+
}
979+
} else {
980+
$this->Error("set_field_checkbox failed as the field $name does not exist");
981+
}
982+
return $offset_shift;
983+
}
984+
//ENDFIX
985+
924986
/**
925987
*Dumps the line entries
926988
*
@@ -1566,7 +1628,14 @@ function parsePDFEntries(&$lines){
15661628

15671629
$Counter=0;
15681630
$obj=0; //this is an invalid object id, we use it to know if we are into an object
1569-
$type='';
1631+
//FIX: parse checkbox definition
1632+
$ap_d_yes='';
1633+
$ap_d_no='';
1634+
$ap_line=0;
1635+
$ap_d_line=0;
1636+
$as='';
1637+
//ENDFIX
1638+
$type='';
15701639
$subtype='';
15711640
$name='';
15721641
$value='';
@@ -1642,6 +1711,13 @@ function parsePDFEntries(&$lines){
16421711

16431712
$object=null;
16441713
$obj=0;
1714+
//FIX: parse checkbox definition
1715+
$ap_d_yes='';
1716+
$ap_d_no='';
1717+
$ap_line=0;
1718+
$ap_d_line=0;
1719+
$as='';
1720+
//ENDFIX
16451721
$type='';
16461722
$subtype='';
16471723
$name='';
@@ -1768,6 +1844,40 @@ function parsePDFEntries(&$lines){
17681844
//=== DEFINITION ====
17691845
//preg_match("/^\/Type\s+\/(\w+)$/",$CurLine,$match)
17701846
$match=array();
1847+
//FIX: parse checkbox definition
1848+
if($this->useCheckboxParser && ('' == $ap_d_yes || '' == $ap_d_no || '' == $as)) {
1849+
if (!$ap_line && '/AP' == substr($CurLine, 0, 3)) {
1850+
if ($verbose_parsing) {
1851+
echo("<br>Found AP Line '<i>$Counter</i>'");
1852+
}
1853+
$ap_line = $Counter;
1854+
} elseif (!$ap_d_line && '/D' == substr($CurLine, 0, 2)) {
1855+
if ($verbose_parsing) {
1856+
echo("<br>Found D Line '<i>$Counter</i>'");
1857+
}
1858+
$ap_d_line = $Counter;
1859+
} elseif (($ap_line==$Counter-4)&&($ap_d_line==$Counter-2)&&($ap_d_yes=='')&&$this->extract_pdf_definition_value("name", $CurLine, $match)) {
1860+
$ap_d_yes=$match[1];
1861+
if ($verbose_parsing) {
1862+
echo("<br>Object's checkbox_yes is '<i>$ap_d_yes</i>'");
1863+
}
1864+
$object["infos"]["checkbox_yes"]=$ap_d_yes;
1865+
} elseif (($ap_line==$Counter-5)&&($ap_d_line==$Counter-3)&&($ap_d_no=='')&&$this->extract_pdf_definition_value("name", $CurLine, $match)) {
1866+
$ap_d_no=$match[1];
1867+
if ($verbose_parsing) {
1868+
echo("<br>Object's checkbox_no is '<i>$ap_d_no</i>'");
1869+
}
1870+
$object["infos"]["checkbox_no"]=$ap_d_no;
1871+
} elseif (($as=='')&&$this->extract_pdf_definition_value("/AS", $CurLine, $match)) {
1872+
$as=$match[1];
1873+
if ($verbose_parsing) {
1874+
echo("<br>Object's AS is '<i>$as</i>'");
1875+
}
1876+
$object["infos"]["checkbox_state"]=$as;
1877+
$object["infos"]["checkbox_state_line"]=$Counter;
1878+
}
1879+
}
1880+
//ENDFIX
17711881
if(($type=='')||($subtype=='')||($name=="")) {
17721882

17731883
if(($type=='')&&$this->extract_pdf_definition_value("/Type",$CurLine,$match)) {

0 commit comments

Comments
 (0)