Skip to content

Commit 1997d2b

Browse files
committed
2.1.3
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.
1 parent 22f5b97 commit 1997d2b

13 files changed

+585
-61
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,28 @@ Style template is ignore, and not effect element at the moment.
250250
2. Mysql
251251
3. PDO (the rest of database)
252252
4. Array (prepare associate array outside of lib)
253-
5. mongodb (experimental, use mongodb-ql)
253+
5. mongodb (experimental, use mongodb-ql)
254+
255+
256+
# Mongodb usage
257+
Mongodb very different with others dbms due to mongodb using hierarchy document structure. PHPJasperXML implement
258+
similar syntax with jaspersoft `mongodbql`.
259+
Refer `mongodbql` [here](https://community.jaspersoft.com/knowledgebase/faq/jaspersoft-mongodb-query-language/)
260+
261+
It only support 2 method of queries, `find` and `aggregate`:
262+
263+
1. Find (findQuery)
264+
* findQuery : { Details },
265+
* findFields : { Details },
266+
* sort : { Details },
267+
* limit : int Details,
268+
269+
2. Aggregate (aggregate)
270+
271+
* Unsupported function:
272+
mapReduce
273+
rowsToProcess
274+
batchSize
275+
maxTime
276+
collation
277+

src/Exports/Pdf_driver.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ public function PageNo():int
153153
public function export(string $filename='')
154154
{
155155
// $this->console($this->pagecolumnoccupation);
156-
if(!empty($filename))
156+
if(empty($filename))
157157
{
158-
$this->Output($filename,'F');
158+
$this->Output('dummy.pdf','I');
159159
}
160-
else
160+
else if($filename=='return')
161161
{
162-
// echo 'asdad';
163-
$filename='sample.pdf';
164-
$this->Output($filename,'I');
162+
return $this->Output($filename,'S');
163+
164+
}else{
165+
$this->Output($filename,'F');
165166
}
166167

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

14361437
protected function formatValue(mixed $value, string $pattern) : string
14371438
{
1439+
14381440
// scientific
14391441
$data = $value;
14401442
$prepattern = $pattern;
@@ -1499,17 +1501,15 @@ protected function formatValue(mixed $value, string $pattern) : string
14991501
//number
15001502
else if(str_contains($pattern,'#') )
15011503
{
1502-
$fmt = numfmt_create( 'en_US', \NumberFormatter::DECIMAL );
1503-
numfmt_set_pattern($fmt,$pattern);
1504+
$fmt = numfmt_create( 'en_US',\NumberFormatter::DECIMAL );
1505+
$issetpattern = numfmt_set_pattern($fmt,$pattern);
15041506
try{
1505-
$data = numfmt_format($fmt,$value);
1507+
$data = $fmt->format($value);
15061508
}
15071509
catch(Throwable $e)
15081510
{
15091511
return $data;
1510-
}
1511-
1512-
1512+
}
15131513
}
15141514
return $data;
15151515
}

src/PHPJasperXML_elements.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public function draw_textField(string $uuid,array $prop)
243243
// {
244244
// $prop['hyperlinkReferenceExpression'] = $this->executeExpression($link);
245245
// }
246+
// echo "$uuid".print_r($prop,true);
246247
$this->output->draw_textField($uuid,$prop,function(){
247248
$this->newPage();
248249
});
@@ -341,6 +342,8 @@ public function draw_subreport(string $uuid,array $prop)
341342

342343

343344
$connectionExpression = $this->executeExpression($prop['connectionExpression']);
345+
if(empty($$connectionExpression)) $connectionExpression='REPORT_CONNECTION';
346+
// echo '$connectionExpression---'.$connectionExpression;die;
344347
$connection = [];
345348
if($connectionExpression=='REPORT_CONNECTION')
346349
{
@@ -370,6 +373,7 @@ public function draw_subreport(string $uuid,array $prop)
370373
}
371374

372375

376+
373377
$subreport
374378
->setParameter($paras)
375379
->setDataSource($connection)
@@ -495,6 +499,7 @@ protected function drawElement(string $uuid,array $prop,int $offsetx,int $offset
495499
$this->output->setPosition($x,$y,$prop);
496500
$methodname = 'draw_'.$prop['elementtype'];
497501
call_user_func([$this,$methodname],$uuid,$prop);
502+
// echo $methodname."<br/>";
498503
}
499504

500505
protected function addBorders(array $prop, object $obj): array

src/PHPJasperXML_expression.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,29 @@ protected function isDisplay(string $expression)
2121
// $this->console("expression $expression === $result");
2222
return $result;
2323
}
24-
protected function executeExpression(string $expression,int $addrowqty=0,string $evaluationTime=''): mixed
24+
protected function executeExpression(string $expression,int $addrowqty=0,string $evaluationTime='',$isstring=false): mixed
2525
{
2626
// $this->console( "executeExpression: $expression");
27-
$value = $this->parseExpression($expression,$addrowqty,$evaluationTime);
27+
$value = $this->parseExpression($expression,$addrowqty,$evaluationTime,true);
2828

2929
//special result, direct return raw value
3030
if(gettype($value)=='object' || gettype($value)=='array')
3131
{
3232
return $value;
3333
}
3434
//it consist of string, use concate instead of maths operation
35+
// if($isstring){
36+
37+
// }
38+
// $value = str_replace('+',' . ',$value);
39+
//
40+
$value = str_replace(["<{{","}}>"],'"',$value);
3541
if(str_contains($value,'"') || str_contains($value,"'"))
3642
{
37-
$value = str_replace('+',' . ',$value);
43+
$findquotepattern = '/\+(?=(?:[^"]*"[^"]*")*[^"]*\Z)/';
44+
$value = preg_replace($findquotepattern, '.', $value);
3845
}
3946

40-
4147
$evalstr = "return $value;";
4248
// $this->console( $evalstr);
4349
try{
@@ -52,7 +58,7 @@ protected function executeExpression(string $expression,int $addrowqty=0,string
5258
}
5359
}
5460

55-
protected function parseExpression(string $expression,int $addrowqty=0,string $evaluationTime=''): mixed
61+
protected function parseExpression(string $expression,int $addrowqty=0,string $evaluationTime='',$specialtag=false): mixed
5662
{
5763
$value = $expression;
5864
$fieldpattern = '/\$F{(.*?)}/';
@@ -64,7 +70,7 @@ protected function parseExpression(string $expression,int $addrowqty=0,string $e
6470
preg_match_all($fieldpattern, $value, $matchfield);
6571
preg_match_all($varpattern, $value, $matchvar);
6672
preg_match_all($parapattern, $value, $matchpara);
67-
73+
6874
$fieldstrings = $matchfield[0];
6975
$fieldnames = $matchfield[1];
7076
$parastrings = $matchpara[0];
@@ -73,18 +79,18 @@ protected function parseExpression(string $expression,int $addrowqty=0,string $e
7379
$varnames = $matchvar[1];
7480
// $this->console($expression);
7581
foreach($fieldnames as $f => $fieldname)
76-
{
77-
$data = $this->getFieldValue($fieldname,$addrowqty,$evaluationTime);
82+
{
83+
$data = $this->getFieldValue($fieldname,$addrowqty,$evaluationTime,$specialtag);
7884
$value = str_replace($fieldstrings[$f], $data,$value);
7985
}
8086
foreach($varnames as $v => $varname)
8187
{
82-
$data = $this->getVariableValue($varname,$evaluationTime);
88+
$data = $this->getVariableValue($varname,$evaluationTime,$specialtag);
8389
$value = str_replace($varstrings[$v], $data,$value);
8490
}
8591
foreach($paranames as $p => $paraname)
8692
{
87-
$data = $this->getParameterValue($paraname,$evaluationTime);
93+
$data = $this->getParameterValue($paraname,$evaluationTime,$specialtag);
8894
if(gettype($data)=='array' || gettype($data)=='object')
8995
{
9096
return $data;
@@ -100,26 +106,32 @@ protected function overrideJavaFunctions(string $expression,string $evaluationTi
100106
$expression = str_replace('new java.util.Date()','"'.date('Y-m-d H:i:s').'"',$expression);
101107
return $expression;
102108
}
103-
protected function getFieldValue(string $name,int $addrowqty=0,string $evaluationTime='')
109+
protected function getFieldValue(string $name,int $addrowqty=0,string $evaluationTime='',$specialtag=false)
104110
{
105111
$rowno = $this->currentRow+$addrowqty - $this->reducerowno;
106112
$datatype = $this->fields[$name]['datatype'];
107113
if(isset($this->rows[$rowno]))
108114
{
109115
$row=$this->rows[$rowno] ;
110-
$value=$row[$name];
116+
if(isset($row[$name])) {
117+
$value=$row[$name];
118+
}
119+
else {
120+
echo "field not exist :".$name."<br/>";
121+
die;
122+
}
111123
}
112124
else
113125
{
114126
$value=null;
115127
}
116128

117-
$value = $this->escapeIfRequire($value,$datatype);
129+
$value = $this->escapeIfRequire($value,$datatype,$specialtag);
118130
return $value;
119131
}
120132

121133

122-
protected function getParameterValue(string $key,string $evaluationTime='')
134+
protected function getParameterValue(string $key,string $evaluationTime='',$specialtag=false)
123135
{
124136
$value=null;
125137
if(!isset($this->parameters[$key]))
@@ -155,12 +167,12 @@ protected function getParameterValue(string $key,string $evaluationTime='')
155167
$value = $this->parameters[$key]['value'];
156168
}
157169
$datatype = $this->parameters[$key]['datatype']??'string';
158-
$value = $this->escapeIfRequire($value,$datatype);
170+
$value = $this->escapeIfRequire($value,$datatype,$specialtag);
159171
return $value ;
160172
}
161173

162174

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

227239

228240
// echo "\nvar $key type = $datatype, data = $data \n";
229-
$result = $this->escapeIfRequire($data,$datatype);
241+
$result = $this->escapeIfRequire($data,$datatype,$specialtag);
230242
break;
231243
}
232244
return $result ;
@@ -238,8 +250,10 @@ protected function getVariableValue($key,string $evaluationTime='')
238250
* @param mixed $datatype string, number, boolean or null
239251
* @return mixed $data string or number value;
240252
*/
241-
public function escapeIfRequire(mixed $value,mixed $datatype): mixed
253+
public function escapeIfRequire(mixed $value,mixed $datatype,$specialtag): mixed
242254
{
255+
$opentag = $specialtag ? '<{{' : '"';
256+
$closetag = $specialtag ? '}}>' : '"';
243257
if(gettype($datatype)=='NULL')
244258
{
245259
$datatype = 'string';
@@ -265,7 +279,7 @@ public function escapeIfRequire(mixed $value,mixed $datatype): mixed
265279
}
266280
$data = addslashes($value);
267281
$data = str_replace('$','\$',$data);
268-
$data = '"'.$data.'"';
282+
$data = $opentag.$data.$closetag;
269283
break;
270284
}
271285
return (string) $data;

src/PHPJasperXML_load.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ trait PHPJasperXML_load
2323
protected array $sortFields=[];
2424
protected string $path = '';
2525
protected array $scriptlets=[];
26+
27+
public function setPath(string $path):self{
28+
if(!$path)die('you shouldnt setPath with empty string');
29+
$this->path = $path;
30+
return $this;
31+
}
2632
/**
2733
* read jrxml file and load into memeory
2834
* @param string $filename
@@ -32,7 +38,7 @@ public function load_xml_file(string $file ): self
3238
{
3339
$pathinfo = pathinfo($file);
3440
$this->filename = $pathinfo['basename'];
35-
$this->path = $pathinfo['dirname'];
41+
$this->setPath($pathinfo['dirname']);
3642
$xml = file_get_contents($file);
3743
$this->load_xml_string($xml);
3844
// print_r($this->bandelements);

src/PHPJasperXML_output.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,14 @@ public function export(string $type,string $filename='')
7373

7474
}
7575
// echo "export";die;
76-
if(!empty($filename))
76+
if(empty($filename))
7777
{
78-
// $filename = '/tmp/'.str_replace('.jrxml','.pdf',$this->filename);
78+
$this->output->export();
79+
7980

80-
$this->output->export($filename);
8181
}
82-
else
83-
{
84-
// echo 'export';die;
85-
$this->output->export();
82+
else{
83+
return $this->output->export($filename);
8684
}
8785

8886
}

src/datadrivers/Mongodb_driver.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,47 @@ public function __construct(array $config)
2424
public function fetchData(mixed $querypara):array
2525
{
2626
$dbname = $this->dbname;
27+
2728
$query = \OviDigital\JsObjectToJson\JsConverter::convertToArray($querypara);
28-
$collectionName = $query['collectionName'];
29-
$aggregate = $query['aggregate'];
30-
// print_r($this->conn->$collectionName);
31-
$result = $this->conn->$dbname->$collectionName->aggregate($aggregate);
32-
$array = json_decode(json_encode($result->toArray(),true), true);
33-
// echo "<pre>".print_r($array,true)."</pre>";
3429
$newarr=[];
35-
for($i=0;$i<count($array);$i++){
36-
$l = $array[$i];
37-
$tmp = $this->convertObjectToArray($l);
38-
array_push($newarr,$tmp);
30+
31+
if(isset($query['collectionName'])){
32+
$collectionName = $query['collectionName'];
33+
$result = [];
34+
$projection = [];
35+
$sort = [];
36+
$limit = 0;
37+
if(isset($query['findFields'])) $projection = $query['findFields'];
38+
if(isset($query['sort'])) $sort = $query['sort'];
39+
if(isset($query['limit'])) $limit = $query['limit'];
40+
41+
42+
if(isset($query['findQuery'])){
43+
$findquery = $query['findQuery'];
44+
$moreoptions =[];
45+
if($projection) $moreoptions['projection']=$projection;
46+
if($limit) $moreoptions['limit']=$limit;
47+
if($sort) $moreoptions['sort']=$sort;
48+
$result = $this->conn->$dbname->$collectionName->find($findquery,$moreoptions);
49+
}else if(isset($query['aggregate'])){
50+
$aggregate = $query['aggregate'];
51+
// print_r($this->conn->$collectionName);
52+
53+
$result = $this->conn->$dbname->$collectionName->aggregate($aggregate);
54+
}
55+
56+
$array = json_decode(json_encode($result->toArray(),true), true);
57+
// echo "<pre>".print_r($array,true)."</pre>";
58+
59+
for($i=0;$i<count($array);$i++){
60+
$l = $array[$i];
61+
$tmp = $this->convertObjectToArray($l);
62+
array_push($newarr,$tmp);
63+
}
64+
65+
3966
}
67+
4068
return $newarr;
4169
}
4270

vendor/composer/autoload_classmap.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
$baseDir = dirname($vendorDir);
77

88
return array(
9+
'Attribute' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Attribute.php',
10+
'CURLStringFile' => $vendorDir . '/symfony/polyfill-php81/Resources/stubs/CURLStringFile.php',
911
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
1012
'Datamatrix' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/datamatrix.php',
1113
'PDF417' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/pdf417.php',
14+
'PhpToken' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/PhpToken.php',
1215
'QRcode' => $vendorDir . '/tecnickcom/tcpdf/include/barcodes/qrcode.php',
16+
'ReturnTypeWillChange' => $vendorDir . '/symfony/polyfill-php81/Resources/stubs/ReturnTypeWillChange.php',
17+
'Stringable' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/Stringable.php',
1318
'TCPDF' => $vendorDir . '/tecnickcom/tcpdf/tcpdf.php',
1419
'TCPDF2DBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_2d.php',
1520
'TCPDFBarcode' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_barcodes_1d.php',
@@ -21,4 +26,6 @@
2126
'TCPDF_IMPORT' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_import.php',
2227
'TCPDF_PARSER' => $vendorDir . '/tecnickcom/tcpdf/tcpdf_parser.php',
2328
'TCPDF_STATIC' => $vendorDir . '/tecnickcom/tcpdf/include/tcpdf_static.php',
29+
'UnhandledMatchError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php',
30+
'ValueError' => $vendorDir . '/symfony/polyfill-php80/Resources/stubs/ValueError.php',
2431
);

0 commit comments

Comments
 (0)