yii:passing parameter in js - java

I am trying to pass selected id to js on click of button.
My code is like this:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
(
'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function($data->userId){
var y=confirm("Are you sure to send mail to this user?");
if(y==true){
window.location="todoList/sendEmail/$data->userId";
}
}',
),
),
),
Here i am not able to get userid inside js.
What can i do here?

Ok i found out answer to my question myself.My working code is:
array(
'class'=>'CButtonColumn',
'template'=>'{email}',
'buttons'=>array
( 'email' => array
(
'label'=>'Send an e-mail to this user',
'click'=>'function(){
return confirm("Are you sure to send mail to this user?");
}',
'url'=>'Yii::app()->createUrl("todoList/sendEmail", array("id"=>$data->userId))',
),
),
),

Related

'message' is not defined no-undef React Error

So basically I am trying to learn promises, The only thing that is bothering me is , I am not able to render the message of promise to the frontend page. Everything else is going fine.I also need to understand what more can we do inside resolve, is it a function ? can we do more activity there ?
import "./testAPI.css"
// import react from "react"
function testAPI() {
let test = new Promise(async (resolve, reject) => {
const res = await fetch("/testAPI").then((res) => res.json());
if(res.status === "Successful"){
resolve("API Fetch is Successful")
}
else {
reject("Could not fetch API")
}
})
test.then((message) => {
console.log(message)
return message;
}).catch((message) => {
console.log(message)
})
return (
<>
<div className="message" >This is message {message} </div>
</>
);
}
export default testAPI;
The error I am getting is
src/components/testAPI/testAPI.js
Line 24:52: 'message' is not defined no-undef
How to render the message variable's value on the page ?
You need to rewrite your component to make use of
State - to change the value over time. For example you have a message which doesn't exist unless you made an api call . To preserve the values which changes over time we use state in react . setMessage is to trigger a re-render for the component. So you made the API call and have the message but how will the react know that you need to show this message which you got from the Api call ? . so do that react useState has the second element in the array a state updater function. if you call it the component will re-render causing the UI to be sync in with the latest changes .
useEffect - to trigger the api call when the component is mounted .
import "./testAPI.css"
import { useState } from "react"
function testAPI() {
const [ message , setMessage ] = useState(null);
const makeApiCall = async () => {
try {
const response = await fetch("/testAPI").then((res) => res.json());
const result = await response.json();
if(result.status === "Successful"){
setMessage("API Fetch is Successful")
}
}catch(error){
setMessage("Could not fetch API")
}
}
useEffect(() => {
makeApiCall()
}, [])
return (
<>
{ message && (<div className="message" >This is message {message } </div>)}
</>
);
}
export default testAPI;

Create PHP array with the same key name

i have created an api in java that accepts (name="sample" multipart[]) and when i'm requesting from php curl,
curl file objects in the array overwrites the old one to new, it means, only one value in the array is accepted.
This is one of the code that i tried:
$files = array(
'path/sample1.png',
'path/sample2.png',
);
$postfields = array();
foreach ($files as $index => $file) {
if (function_exists('curl_file_create')) {
$file = new CURLFILE(realpath($file), mime_content_type($file), basename($file));
}
else {
$file = '#' . realpath($file);
}
$postfields["sample"] = $file;
}
Note: I already tried sending a request in postman and it works perfectly with same keyname. The generated code have the same keyname in the post fields like this..
"sample" => "",
"sample" => ""
I don't know what to follow since the postman request is success, i tried also using the generated code from postman but the output is the same as stated above.
CURL:
CURLOPT_HEADER => 0,
CURLOPT_VERBOSE => 0,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SAFE_UPLOAD => true,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",

How create table use j2html?

I get acquainted with j2html and I try a make table, but have some problems:
main().with(
table(
tr(
td().with(
img().withSrc(imagePath+photo)
)
td().with(
span(name)
),
td().with(
span(String.valueOf(quantity))
)
after img().withSrc(imagePath+photo) I see mistake, but I don't understand what want from me Idea
May be you can show how create table with image+name+ some quontity + colspan for several cells
(pre v.1.0.0): The tag-methods don't accept DomContent children, so you need to do table().with(, td().with( etc, not just table( and td(:
String htmlTable = table().with(
tr().with(
td().with(
img().withSrc("path" + "photo")
),
td().with(
span("name")
),
td().with(
span(String.valueOf("quantity"))
)
)
).render();

PHPRunner: email array , can't personalize message

I’m using PHPRunner and I'm trying to create a button to send an email to selected users with personalized body message. I found an example in PHPRunner support page (Send an email to selected users) but it is only with an hardcoded message, so I’m trying to modify the example.
I managed to send correctly the email with personalized message if I only select one user, but if I select more than one all of them will receive the same message.
This is what I have now:
$emails = array();
while( $data = $button->getNextSelectedRecord() )
{
if( $data["EMAIL_FIELD"] )
$emails[] = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
}
// send the email
$email = implode(", ", $emails);
$subject = "";
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
$result["txt"] = "Email were sent.";
// if error happened print a message on the web page
if( !$arr["mailed"] )
{
$errmsg = "Error happened: <br>";
$errmsg.= "File: " . $arr["errors"][0]["file"] . "<br>";
$errmsg.= "Line: " . $arr["errors"][0]["line"] . "<br>";
$errmsg.= "Description: " . $arr["errors"][0]["description"] . "<br>";
$result["txt"] = $errmsg;
}
What should I change? Thank you.
Problem:
In the foreach loop you are appending every new email address to one variable:
$emails[] = $data["EMAIL_FIELD"];
and you are assigning new value to $body variable (overriding the old one) in each iteration:
$body = $data["MESSAGE_FIELD"];
Therefore when you are calling runner_mail just once outside of the while loop, you will send same body (from the last iteration) to all recipients (you built an array of all recipients using while loop).
Solution:
You need to call runner_mail for each recipient (as the body is different). You can achieve it by moving the call to runner_mail inside the while loop:
while( $data = $button->getNextSelectedRecord() )
{
// Send only if email field not empty
if( $data["EMAIL_FIELD"] ) {
// This no longer should be an array of emails
$email = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
$subject = "";
// Email will be sent for each record
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
}
}

Write binary stream to browser using PHP

Background
Trying to stream a PDF report written using iReport through PHP to the browser. The general problem is: how do you write binary data to the browser using PHP?
Working Code
header('Cache-Control: no-cache private');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment, filename=climate-report.pdf');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
$path = realpath( "." ) . "/output.pdf";
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
$fh = fopen( $path, 'w' );
fwrite( $fh, $result );
fclose( $fh );
readfile( $path );
Non-working Code
header('Cache-Control: no-cache private');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment, filename=climate-report.pdf');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
$path = realpath( "." ) . "/output.pdf";
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
echo $result;
Question
How can I take out the middle step of writing to the file and write directly to the browser so that the PDF is not corrupted?
Update
PDF file sizes:
Working: 594778 bytes
Non-working: 1059365 bytes
Thank you!
I've previously experienced problems writing from Java because it'll use UTF-16. The function outputPDF from http://zeronine.org/lab/index.php uses java_set_file_encoding("ISO-8859-1");. Thus:
java_set_file_encoding("ISO-8859-1");
$em = java('net.sf.jasperreports.engine.JasperExportManager');
$result = $em->exportReportToPdf($pm);
header('Content-Length: ' . strlen( $result ) );
echo $result;
You just write the output. Make sure that:
correct headers have been set
no other character have been accidentally printed before or after the binary (this includes whitespace).

Categories

Resources