iterate an array in my jrxml file without using a datasource - php

I'm using jasperserver in my php/Symfony to generate my reports, and i pass parameters ad everything works fine. But now i want to pass an array containing the list of my user's name to jrxml file i really don't know if it's possible. i mean i know that we can do it with javabeans or creating a datasource, but in my case i mustn't use a database plus i use php.
So if anyone know how to pass the array and fetch it in the jrxml file to display all the values i would be so grateful
so here is the code i'm want to use
$users = $em->getRepository('UserBundle:Utilisateur')->getAll();
$params = array(
"montantEnLettre" => $montantEnLettre,
"policeGroupeLibelle" => $contrat->getProduit()->getLibelle(),
"montanttotal" => $contrat->getMontantTotale(),
"utilisateurs" => $users
);
$d = new Client(
"http://127.0.0.1:8080/jasperserver",
"jasperadmin",
"jasperadmin"
);
$js = $d->jobService();
$d->setRequestTimeout(60);
$info = $d->serverInfo();
$report = $d->reportService()->runReport('/reports/epargne', 'pdf',null,null,$params);
I don't know if i can pass the users as a parameter and i have no idea how to fetch it my jrxml file

Related

MongoDB & PHP | How to use multiple query parameters

So I am using MongoDB and PHP for the first time and I am getting stuck at the following problem:
I want to insert an object into an array. The inserting is not really the problem but the specifying in which document and in which place in the document is more the problem.
My mongoDB document
_id:6235ef5e2a9b6d72904a1fc3
username:"Josse"
email:"josse#mail.nl"
password:"1234"
level:1
domainArray:Array
0:Object
domainname:"josse.com"
domainvalue:9
ouputArray:Array
0:Object
1:Object
domainname:"example.com"
domainvalue:9
ouputArray:Array
0:Object
The desired document:
_id:6235ef5e2a9b6d72904a1fc3
username:"Josse"
email:"josse#mail.nl"
password:"1234"
level:1
domainArray:Array
0:Object
domainname:"josse.com"
domainvalue:9
ouputArray:Array
0:Object
1:Object
domainname:"example.com"
domainvalue:9
ouputArray:Array
0:Object
outputname:"output1"
outputdate:"23/03/2022"
The problem:
I would like to be able to specify that I want to add the outputname and date into the ouputArray that is inside the object with domainname "example.com"
What I have tried:
After some research I found that in MongoDB multiple query parameters is a thing, but I can't seem to make it work using PHP.
My code:
public function addOuput($ouputData, $username){
$collection = $collection = (new MongoDB\Client('mongodb://localhost:27017'))->mydb->users;
$insertOneResult = $collection->updateOne(
["username" => "josse.com"],
['$addToSet' => ["domainArray.$[].ouputArray" => $ouputData]] );
}
When I use this code the $outputData gets added into every object that is inside the domainArray which I do not want. I only want it added into the object that is inside domainArray that I specify.
public function addOuput($ouputData, $username){
$collection = $collection = (new MongoDB\Client('mongodb://localhost:27017'))->mydb->users;
$insertOneResult = $collection->updateOne(
["username" => "josse.com", "domainArray.domainname"=>"josse.com"],
['$addToSet' => ["domainArray.$[].ouputArray" => $ouputData]] );
}
The code above is what I have tried to make it work, but this is not the way to go since it did not give me errors, but also added nothing into the database.
Can anyone help me with my problem?
Thanks in advance!

Use Google Spreadsheet API to obtain values filtered by Filter view

I'm trying to use the Google Sheets API (in PHP using google/apiclient and OAuth 2.0) to get data from a spreadsheet.
This spreadsheet has defined some "Filter views" (defined in Data > Filter views) where for example one of the filter display only rows where the Price column is greater than x.
I'm trying to find a method to get already filtered data using one of the existing Filter views, but I can't find it. Is there any way to do it?
Although I'm not sure about your current script from your question, when you want to retrieve the filtered values from the sheet using the filter view, unfortunately, in the current stage, there are no methods for directly achieving it. So, in this case, it is required to use a workaround. The flow of the workaround is as follows.
Flow of this workaround:
Retrieve the settings of the filter view (filterViews) you want to use.
In this case, the method of "spreadsheets.get" can be used.
Create new basic filter to the sheet you want to use using the retrieved settings of the filter view.
In this case, the method of "spreadsheets.batchUpdate" can be used.
Retrieve the values of rowMetadata of the sheet.
In this case, the method of "spreadsheets.get" can be used.
At the values of rowMetadata, the filtered rows have the property of "hiddenByFilter": true,. Using this, you can retrieve the hidden rows and/or the showing rows.
Delete the created basic filter.
By above flow, the filtered values can be retrieved.
Sample script:
When you have already been able to get and put values for Google Spreadsheet using Sheets API with googleapis for PHP, as a sample script of above workaround, you can use the following script.
$service = new Google_Service_Sheets($client); // Please use $client from your script.
$spreadSheetId = '###'; // Please set the Spreadsheet ID.
$sheetName = 'Sheet1'; // Please set the sheet name.
$filterViewName = 'sampleFilter1'; // Please set the filter view name.
// 1. Retrieve the settings of the filter view (`filterViews`) you want to use.
$sheets = $service->spreadsheets->get($spreadSheetId, ["ranges" => [$sheetName], "fields" => "sheets"])->getSheets();
$sheetId = $sheets[0]->getProperties()->getSheetId();
$filterViews = $sheets[0]->getFilterViews();
$filterView = array();
foreach ($filterViews as $i => $f) {
if ($f->getTitle() == $filterViewName) {
array_push($filterView, $f);
};
};
if (count($filterView) == 0) return;
// 2. Create new basic filter to the sheet you want to use using the retrieved settings of the filter view.
$obj = $filterView[0];
$obj['range']['sheetId'] = $sheetId;
$requests = [
new Google_Service_Sheets_Request(['clearBasicFilter' => ['sheetId' => $sheetId]]),
new Google_Service_Sheets_Request([
'setBasicFilter' => [
'filter' => [
'criteria' => $obj['criteria'],
'filterSpecs' => $obj['filterSpecs'],
'range' => $obj['range'],
'sortSpecs' => $obj['sortSpecs'],
]
]
])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
$service->spreadsheets->batchUpdate($spreadSheetId, $batchUpdateRequest);
// 3. Retrieve the values of `rowMetadata` of the sheet.
$sheets = $service->spreadsheets->get($spreadSheetId, ["ranges" => [$sheetName], "fields" => "sheets"])->getSheets();
$rowMetadata = $sheets[0]->getData()[0]->getRowMetadata();
$filteredRows = array(
'hiddenRows' => array(),
'showingRows' => array()
);
foreach ($rowMetadata as $i => $r) {
if (isset($r['hiddenByFilter']) && $r['hiddenByFilter'] === true) {
array_push($filteredRows['hiddenRows'], $i + 1);
} else {
array_push($filteredRows['showingRows'], $i + 1);
};
};
// 4. Delete the created basic filter.
$requests = [new Google_Service_Sheets_Request(['clearBasicFilter' => ['sheetId' => $sheetId]])];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
$service->spreadsheets->batchUpdate($spreadSheetId, $batchUpdateRequest);
print($filteredRows);
Result:
When above script is used for the following sample Spreadsheet,
Before filter view is not set.
After filter view was set.
Result value
From above Spreadsheet, the following result is obtained.
{
"hiddenRows": [2, 3, 5, 6, 8, 9],
"showingRows": [1, 4, 7, 10, 11, 12, 13, 14, 15]
}
hiddenRows is the hidden row numbers.
showingRows is the showingRows row numbers.
Note:
IMPORTANT: In this sample script, when the basic filter is used in the sheet, the basic filter is cleared. Please be careful this. When you test this script, please use the sample Spreadsheet.
In this sample script, the hidden rows and showing rows are retrieved. Using these values, you can retrieve the filtered values.
References:
Method: spreadsheets.get
Method: spreadsheets.batchUpdate
Related thread.
Is there a way to check if a row is hidden by a filter view in Google Sheets using Apps Script?
This thread is for Google Apps Script. Unfortunately, I couldn't find the PHP script for this. So I added the sample script.

How to import multiple image?

Field image is accepting this type of format for multiple image import:
a:10:{i:0;
a:1:{s:6:"imgurl";s:71:"http://example.com/wp-content/uploads/2015/01/image.jpg";}
i:1;
a:1:{s:6:"imgurl";s:71:"http://example.com/wp-content/uploads/2015/01/image.jpg";}
};
I am trying to find a way to return a json_encode with the above output.
In the process I can't figure out a:10:{} when converted to array format.
$arr = array('a' => 10, // <-- I have a problem here what to use
array( 'i' => 0, 'a' => 1,
array('s' => [6, 'imgurl'], // <-- I have a problem here what to use
's' => (71,"http://example.com/wp-content/uploads/2015/01/image.jpg")
)
)
);
Am I doing this correctly?
Update:
Sorry I can't response sooner. Since I'm studying how serialize and unserialize works.
A bit of history, I am using WP All Import- Import XML / CSV WordPress plugin to import XML data.
The WP custom post type property contain a field name _property_slider_image that store an image(s). Standard drag and drop of XML container to that field won't work. Not working as, it's not linking the downloaded images per property.
After checking the mysql database the field accept this type of synthax I mention above. I honestly do not know what it is. Since I only know is json_encode and json_decode. That is why my post mention json_encode.
Thanks to Kiyan. He gave me a hint where to look.
Now I won't be manually mapping each image to each property record.
Result:
Here's my output after a weeks of research.
function sl_output_property_slider(){
$image_url_list = array();
foreach (func_get_args() as $n) {
// get image name
$img_name = get_image_name($n);
// add directory location with the following format
// http://localhost/dev_slrealestate/wp-content/uploads/2015/01/1478_Image.jpeg
$imgurl = 'http://localhost/dev_site/wp-content/uploads/'. date('Y') .'/'. date('m') . '/' . $img_name;
array_push($image_url_list, array('imgurl'=>$imgurl));
}
$serialized_data = serialize($image_url_list);
printf($serialized_data);
}
where function get_image_name($url) - to return the image name only from original url string.
Sample Usage - short code
[sl_output_property_slider({Images[1]/Image[1]/ImageURL[1]},
{Images[1]/Image[2]/ImageURL[1]},{Images[1]/Image[3]/ImageURL[1]},
{Images[1]/Image[4]/ImageURL[1]},{Images[1]/Image[5]/ImageURL[1]},
{Images[1]/Image[6]/ImageURL[1]},{Images[1]/Image[7]/ImageURL[1]},
{Images[1]/Image[8]/ImageURL[1]},{Images[1]/Image[9]/ImageURL[1]},
{Images[1]/Image[10]/ImageURL[1]}
)
]
this is not a json string, you have to use unserialize function
http://php.net/manual/en/function.unserialize.php
As Kiyan stated, you're confusing between json and serialize formats.
The array representation you given can be created using:
$arr = array(
array('imgurl' => 'http://example.com/wp-content/uploads/2015/01/image.jpg'),
array('imgurl' => 'http://example.com/wp-content/uploads/2015/01/image.jpg'),
);
$serialized = serialize($arr);
Example:
echo $serialized;
Gives:
a:2:{i:0;a:1:{s:6:"imgurl";s:55:"http://example.com/wp-content/uploads/2015/01/image.jpg";}i:1;a:1:{s:6:"imgurl";s:55:"http://example.com/wp-content/uploads/2015/01/image.jpg";}}

How to apply PHP function to a column returned from CakePHP query

In my controller I am retrieving records from my institutions table with the following fields
$params = array(
'fields' => array(
'Institution.id',
'Institution.name',
'Institution.about',
'Institution.picture'),
);
$institutions = $this->Institution->find('all',$params);
How can I prefix each 'Institution.picture' field with the full URL address, 'Institution.picture' itself only holds the name of the file.
I would also like to perform html_entity_decode() on each 'Institution.about' value from the returned set.
I know how to do this only without the framework if I make custom queries from scratch, then I would iterate each row and apply PHP functions to the field of interest. But is there a place in CakePHP (find or paginator) that I can specify such PHP manipulation on each field value from the returned set?
NOTE: I don't like to do this in the View, as I want to output it as json directly
You can define a virtualField for model:
public $virtualFields = array('image_url' => "CONCAT('/img/', Institution.picture)");
$params = array(
'fields' => array(
'Institution.id',
'Institution.name',
'Institution.about',
'Institution.picture',
'Institution.image_url'),
);
$institutions = $this->Institution->find('all',$params);
Unfortunaly MySQL doesn't have a function to decode HTML entities. You may utilize an afterFind() callback instead of virtualField. This lets you to decode entities as well as add a prefix.
CakePHP is php
Just iterate over the array and prepare it however you want:
$institutions = $this->Institution->find('all',$params);
$prefix = '/img/'; // <- define this
foreach ($institutions as &$row) {
$row['Institution']['about'] = html_entity_decode($row['Institution']['about']);
$row['Institution']['picture'] = $prefix . $row['Institution']['picture'];
}
If this is always required it can be applied to all finds via an afterFind method in the institution class.
I think you should do it in the View. See this example.
Hash::map can be very useful here. By specifying path you can only modify slices of the set.

PHP SOAP Request includes multiple identical tags

OK, so I have this external SOAP based webservice, and PHP SoapClient. Everything is fine with basic requests, but I need to create a parameter set that looks like this:
<DatasetList>
<DatasetID>K0001</DatasetID>
<DatasetID>K0002</DatasetID>
</DatasetList>
For a single nested DatasetID tag I'd do:
$req = array( "DatasetList" => array( "DatasetId" => "K0001" ));
$client->getWebserviceCall($req);
but I need multiple DatasetID tags... I've tried assigning DatasetID as an array, but I just get:
<DatasetList>
<DatasetID>Array</DatasetID>
</DatasetList>
Anyone help?
Did you try the array this way?
$req = array( "DatasetList" => array("DatasetID" => array("K0001", "K0002));
You can do this only by wrote the Part with the identical tags by hand. But, the rest of values can you define in a array:
// Define multiple identical Tags for a part of the Array
$soap_var= new SoapVar('
<DatasetID>1</DatasetID>
<DatasetID>2</DatasetID>
';
// Define the other Values in the normal Way as an array
$req = array(
"DatasetList" => $soap_var,
'value2'=>array('other'=>'values'
);

Categories