Skip to content

Commit

Permalink
2.1.3
Browse files Browse the repository at this point in the history
1. support more mongodb queries beside aggregate
2. support use 'return' to obtain string value of pdf
3. some bug fix for subreport with mongodb
4. better algorithm to solve randomly can't eval data during execute expression
5. allow set current working directory
6.
  • Loading branch information
kstan79 committed Apr 6, 2024
1 parent 22f5b97 commit 1997d2b
Show file tree
Hide file tree
Showing 13 changed files with 585 additions and 61 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,28 @@ Style template is ignore, and not effect element at the moment.
2. Mysql
3. PDO (the rest of database)
4. Array (prepare associate array outside of lib)
5. mongodb (experimental, use mongodb-ql)
5. mongodb (experimental, use mongodb-ql)


# Mongodb usage
Mongodb very different with others dbms due to mongodb using hierarchy document structure. PHPJasperXML implement
similar syntax with jaspersoft `mongodbql`.
Refer `mongodbql` [here](https://community.jaspersoft.com/knowledgebase/faq/jaspersoft-mongodb-query-language/)

It only support 2 method of queries, `find` and `aggregate`:

1. Find (findQuery)
* findQuery : { Details },
* findFields : { Details },
* sort : { Details },
* limit : int Details,

2. Aggregate (aggregate)

* Unsupported function:
mapReduce
rowsToProcess
batchSize
maxTime
collation

24 changes: 12 additions & 12 deletions src/Exports/Pdf_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,16 @@ public function PageNo():int
public function export(string $filename='')
{
// $this->console($this->pagecolumnoccupation);
if(!empty($filename))
if(empty($filename))
{
$this->Output($filename,'F');
$this->Output('dummy.pdf','I');
}
else
else if($filename=='return')
{
// echo 'asdad';
$filename='sample.pdf';
$this->Output($filename,'I');
return $this->Output($filename,'S');

}else{
$this->Output($filename,'F');
}

// echo $filename;
Expand Down Expand Up @@ -1435,6 +1436,7 @@ public function groupCount(): int

protected function formatValue(mixed $value, string $pattern) : string
{

// scientific
$data = $value;
$prepattern = $pattern;
Expand Down Expand Up @@ -1499,17 +1501,15 @@ protected function formatValue(mixed $value, string $pattern) : string
//number
else if(str_contains($pattern,'#') )
{
$fmt = numfmt_create( 'en_US', \NumberFormatter::DECIMAL );
numfmt_set_pattern($fmt,$pattern);
$fmt = numfmt_create( 'en_US',\NumberFormatter::DECIMAL );
$issetpattern = numfmt_set_pattern($fmt,$pattern);
try{
$data = numfmt_format($fmt,$value);
$data = $fmt->format($value);
}
catch(Throwable $e)
{
return $data;
}


}
}
return $data;
}
Expand Down
5 changes: 5 additions & 0 deletions src/PHPJasperXML_elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function draw_textField(string $uuid,array $prop)
// {
// $prop['hyperlinkReferenceExpression'] = $this->executeExpression($link);
// }
// echo "$uuid".print_r($prop,true);
$this->output->draw_textField($uuid,$prop,function(){
$this->newPage();
});
Expand Down Expand Up @@ -341,6 +342,8 @@ public function draw_subreport(string $uuid,array $prop)


$connectionExpression = $this->executeExpression($prop['connectionExpression']);
if(empty($$connectionExpression)) $connectionExpression='REPORT_CONNECTION';
// echo '$connectionExpression---'.$connectionExpression;die;
$connection = [];
if($connectionExpression=='REPORT_CONNECTION')
{
Expand Down Expand Up @@ -370,6 +373,7 @@ public function draw_subreport(string $uuid,array $prop)
}



$subreport
->setParameter($paras)
->setDataSource($connection)
Expand Down Expand Up @@ -495,6 +499,7 @@ protected function drawElement(string $uuid,array $prop,int $offsetx,int $offset
$this->output->setPosition($x,$y,$prop);
$methodname = 'draw_'.$prop['elementtype'];
call_user_func([$this,$methodname],$uuid,$prop);
// echo $methodname."<br/>";
}

protected function addBorders(array $prop, object $obj): array
Expand Down
52 changes: 33 additions & 19 deletions src/PHPJasperXML_expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,29 @@ protected function isDisplay(string $expression)
// $this->console("expression $expression === $result");
return $result;
}
protected function executeExpression(string $expression,int $addrowqty=0,string $evaluationTime=''): mixed
protected function executeExpression(string $expression,int $addrowqty=0,string $evaluationTime='',$isstring=false): mixed
{
// $this->console( "executeExpression: $expression");
$value = $this->parseExpression($expression,$addrowqty,$evaluationTime);
$value = $this->parseExpression($expression,$addrowqty,$evaluationTime,true);

//special result, direct return raw value
if(gettype($value)=='object' || gettype($value)=='array')
{
return $value;
}
//it consist of string, use concate instead of maths operation
// if($isstring){

// }
// $value = str_replace('+',' . ',$value);
//
$value = str_replace(["<{{","}}>"],'"',$value);
if(str_contains($value,'"') || str_contains($value,"'"))
{
$value = str_replace('+',' . ',$value);
$findquotepattern = '/\+(?=(?:[^"]*"[^"]*")*[^"]*\Z)/';
$value = preg_replace($findquotepattern, '.', $value);
}


$evalstr = "return $value;";
// $this->console( $evalstr);
try{
Expand All @@ -52,7 +58,7 @@ protected function executeExpression(string $expression,int $addrowqty=0,string
}
}

protected function parseExpression(string $expression,int $addrowqty=0,string $evaluationTime=''): mixed
protected function parseExpression(string $expression,int $addrowqty=0,string $evaluationTime='',$specialtag=false): mixed
{
$value = $expression;
$fieldpattern = '/\$F{(.*?)}/';
Expand All @@ -64,7 +70,7 @@ protected function parseExpression(string $expression,int $addrowqty=0,string $e
preg_match_all($fieldpattern, $value, $matchfield);
preg_match_all($varpattern, $value, $matchvar);
preg_match_all($parapattern, $value, $matchpara);

$fieldstrings = $matchfield[0];
$fieldnames = $matchfield[1];
$parastrings = $matchpara[0];
Expand All @@ -73,18 +79,18 @@ protected function parseExpression(string $expression,int $addrowqty=0,string $e
$varnames = $matchvar[1];
// $this->console($expression);
foreach($fieldnames as $f => $fieldname)
{
$data = $this->getFieldValue($fieldname,$addrowqty,$evaluationTime);
{
$data = $this->getFieldValue($fieldname,$addrowqty,$evaluationTime,$specialtag);
$value = str_replace($fieldstrings[$f], $data,$value);
}
foreach($varnames as $v => $varname)
{
$data = $this->getVariableValue($varname,$evaluationTime);
$data = $this->getVariableValue($varname,$evaluationTime,$specialtag);
$value = str_replace($varstrings[$v], $data,$value);
}
foreach($paranames as $p => $paraname)
{
$data = $this->getParameterValue($paraname,$evaluationTime);
$data = $this->getParameterValue($paraname,$evaluationTime,$specialtag);
if(gettype($data)=='array' || gettype($data)=='object')
{
return $data;
Expand All @@ -100,26 +106,32 @@ protected function overrideJavaFunctions(string $expression,string $evaluationTi
$expression = str_replace('new java.util.Date()','"'.date('Y-m-d H:i:s').'"',$expression);
return $expression;
}
protected function getFieldValue(string $name,int $addrowqty=0,string $evaluationTime='')
protected function getFieldValue(string $name,int $addrowqty=0,string $evaluationTime='',$specialtag=false)
{
$rowno = $this->currentRow+$addrowqty - $this->reducerowno;
$datatype = $this->fields[$name]['datatype'];
if(isset($this->rows[$rowno]))
{
$row=$this->rows[$rowno] ;
$value=$row[$name];
if(isset($row[$name])) {
$value=$row[$name];
}
else {
echo "field not exist :".$name."<br/>";
die;
}
}
else
{
$value=null;
}

$value = $this->escapeIfRequire($value,$datatype);
$value = $this->escapeIfRequire($value,$datatype,$specialtag);
return $value;
}


protected function getParameterValue(string $key,string $evaluationTime='')
protected function getParameterValue(string $key,string $evaluationTime='',$specialtag=false)
{
$value=null;
if(!isset($this->parameters[$key]))
Expand Down Expand Up @@ -155,12 +167,12 @@ protected function getParameterValue(string $key,string $evaluationTime='')
$value = $this->parameters[$key]['value'];
}
$datatype = $this->parameters[$key]['datatype']??'string';
$value = $this->escapeIfRequire($value,$datatype);
$value = $this->escapeIfRequire($value,$datatype,$specialtag);
return $value ;
}


protected function getVariableValue($key,string $evaluationTime='')
protected function getVariableValue($key,string $evaluationTime='',$specialtag=false)
{
// echo "\n getVariableValue $key: \n";
$datatype = "number";//by default all datatype is number, unless variable class defined
Expand Down Expand Up @@ -226,7 +238,7 @@ protected function getVariableValue($key,string $evaluationTime='')


// echo "\nvar $key type = $datatype, data = $data \n";
$result = $this->escapeIfRequire($data,$datatype);
$result = $this->escapeIfRequire($data,$datatype,$specialtag);
break;
}
return $result ;
Expand All @@ -238,8 +250,10 @@ protected function getVariableValue($key,string $evaluationTime='')
* @param mixed $datatype string, number, boolean or null
* @return mixed $data string or number value;
*/
public function escapeIfRequire(mixed $value,mixed $datatype): mixed
public function escapeIfRequire(mixed $value,mixed $datatype,$specialtag): mixed
{
$opentag = $specialtag ? '<{{' : '"';
$closetag = $specialtag ? '}}>' : '"';
if(gettype($datatype)=='NULL')
{
$datatype = 'string';
Expand All @@ -265,7 +279,7 @@ public function escapeIfRequire(mixed $value,mixed $datatype): mixed
}
$data = addslashes($value);
$data = str_replace('$','\$',$data);
$data = '"'.$data.'"';
$data = $opentag.$data.$closetag;
break;
}
return (string) $data;
Expand Down
8 changes: 7 additions & 1 deletion src/PHPJasperXML_load.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ trait PHPJasperXML_load
protected array $sortFields=[];
protected string $path = '';
protected array $scriptlets=[];

public function setPath(string $path):self{
if(!$path)die('you shouldnt setPath with empty string');
$this->path = $path;
return $this;
}
/**
* read jrxml file and load into memeory
* @param string $filename
Expand All @@ -32,7 +38,7 @@ public function load_xml_file(string $file ): self
{
$pathinfo = pathinfo($file);
$this->filename = $pathinfo['basename'];
$this->path = $pathinfo['dirname'];
$this->setPath($pathinfo['dirname']);
$xml = file_get_contents($file);
$this->load_xml_string($xml);
// print_r($this->bandelements);
Expand Down
12 changes: 5 additions & 7 deletions src/PHPJasperXML_output.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ public function export(string $type,string $filename='')

}
// echo "export";die;
if(!empty($filename))
if(empty($filename))
{
// $filename = '/tmp/'.str_replace('.jrxml','.pdf',$this->filename);
$this->output->export();


$this->output->export($filename);
}
else
{
// echo 'export';die;
$this->output->export();
else{
return $this->output->export($filename);
}

}
Expand Down
48 changes: 38 additions & 10 deletions src/datadrivers/Mongodb_driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,47 @@ public function __construct(array $config)
public function fetchData(mixed $querypara):array
{
$dbname = $this->dbname;

$query = \OviDigital\JsObjectToJson\JsConverter::convertToArray($querypara);
$collectionName = $query['collectionName'];
$aggregate = $query['aggregate'];
// print_r($this->conn->$collectionName);
$result = $this->conn->$dbname->$collectionName->aggregate($aggregate);
$array = json_decode(json_encode($result->toArray(),true), true);
// echo "<pre>".print_r($array,true)."</pre>";
$newarr=[];
for($i=0;$i<count($array);$i++){
$l = $array[$i];
$tmp = $this->convertObjectToArray($l);
array_push($newarr,$tmp);

if(isset($query['collectionName'])){
$collectionName = $query['collectionName'];
$result = [];
$projection = [];
$sort = [];
$limit = 0;
if(isset($query['findFields'])) $projection = $query['findFields'];
if(isset($query['sort'])) $sort = $query['sort'];
if(isset($query['limit'])) $limit = $query['limit'];


if(isset($query['findQuery'])){
$findquery = $query['findQuery'];
$moreoptions =[];
if($projection) $moreoptions['projection']=$projection;
if($limit) $moreoptions['limit']=$limit;
if($sort) $moreoptions['sort']=$sort;
$result = $this->conn->$dbname->$collectionName->find($findquery,$moreoptions);
}else if(isset($query['aggregate'])){
$aggregate = $query['aggregate'];
// print_r($this->conn->$collectionName);

$result = $this->conn->$dbname->$collectionName->aggregate($aggregate);
}

$array = json_decode(json_encode($result->toArray(),true), true);
// echo "<pre>".print_r($array,true)."</pre>";

for($i=0;$i<count($array);$i++){
$l = $array[$i];
$tmp = $this->convertObjectToArray($l);
array_push($newarr,$tmp);
}


}

return $newarr;
}

Expand Down
7 changes: 7 additions & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
$baseDir = dirname($vendorDir);

return array(
'Attribute' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
'CURLStringFile' => $vendorDir . '/symfony/polyfill-php81/Resources/stubs/CURLStringFile.php',
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'Datamatrix' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/datamatrix.php',
'PDF417' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/pdf417.php',
'PhpToken' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
'QRcode' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/qrcode.php',
'ReturnTypeWillChange' => $vendorDir . '/symfony/polyfill-php81/Resources/stubs/ReturnTypeWillChange.php',
'Stringable' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Stringable.php',
'TCPDF' => $vendorDir . '/tecnickcom/tcpdf/tcpdf.php',
'TCPDF2DBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_2d.php',
'TCPDFBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_1d.php',
Expand All @@ -21,4 +26,6 @@
'TCPDF_IMPORT' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_import.php',
'TCPDF_PARSER' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_parser.php',
'TCPDF_STATIC' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_static.php',
'UnhandledMatchError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php',
'ValueError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/ValueError.php',
);
Loading

0 comments on commit 1997d2b

Please sign in to comment.