Seems not to work (php) if in email template (form plugin)

Hi,
My standard own email templates are .html files but second time i need include some php code in my own template.
So, this ---if--- doesn't work at all. Works only ---echo---

Is any advice what to do? I very need this sample of php code in my email template :-(

myemailtemplate1.php
PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
  <meta name="Description" content="[ #DESCRIPTION# ]">
  <meta name="author" content="blue-mouse">
  <meta name="Keywords" content="[ #C&C, computers & control,  zabezpieczenia dla elektroenergetyki, elektroenergetyka# ]">
  <meta name="generator" content="Bluefish 1.0.7">
  <title>C&C - System ISO</title>
</head>
 
<body text="darkblue">
 
 
<center>
 
 
 
 
 
<table style="width: 450px; background-color: #cceedd;" border="5" bordercolor="black">
 
<tr><td>Zg?oszenie nr: </td><td><strong>
C&C/{ccjom_cc_klienci_zgl_naprawy___id}</strong></td></tr>
 
<tr><td>Zg?aszaj?cy: </td><td><strong>
{ccjom_cc_klienci_zgl_naprawy___name_zlibl}</strong></td></tr>
 
<tr><td>Email i telefon zg?aszaj?cego:</td><td><strong>
{ccjom_cc_klienci_zgl_naprawy___email_bl} {ccjom_cc_klienci_zgl_naprawy___email},
{ccjom_cc_klienci_zgl_naprawy___telefon_bl} {ccjom_cc_klienci_zgl_naprawy___telefon}</strong></td></tr>
 
 
<tr><td>Nazwa i adres firmy:</td><td>
{ccjom_cc_klienci_zgl_naprawy___company_name_bl} {ccjom_cc_klienci_zgl_naprawy___company_name_zl}<br/>
{ccjom_cc_klienci_zgl_naprawy___address_bl} {ccjom_cc_klienci_zgl_naprawy___address}</td></tr>
 
<tr><td>Adres wysy?ki:</td><td>
{ccjom_cc_klienci_zgl_naprawy___address_wys_razem}</td></tr>
 
<tr><td>Nazwa urz?dzenia:</td><td>
{ccjom_cc_klienci_zgl_naprawy___urzadz_razem} <br/> {ccjom_cc_klienci_zgl_naprawy___urzadz_opis}</td></tr>
 
 
<tr><td>Numer urz?dzenia:</td><td>
{ccjom_cc_klienci_zgl_naprawy___urzadz_num}</td></tr>
 
<tr><td>Opis uszkodzenia:</td><td>
{ccjom_cc_klienci_zgl_naprawy___opis_uszkodzenia}</td></tr>
 
<tr><td>Aktualizacja oprogramowania:</td><td>
{ccjom_cc_klienci_zgl_naprawy___aktual_oprog}</td></tr>
 
 
<tr><td colspan="2" align="center" style="background-color: #faf8a5;"><strong>Akceptacja zg?oszenia</strong></td></tr>
 
<tr><td style="background-color: #faf8a5;">Akceptacja zg?oszenia: </td><td style="background-color: #faf8a5;"><strong>
{ccjom_cc_klienci_zgl_naprawy___akceptacja_zgl}</strong></td></tr>
 
<tr><td style="background-color: #faf8a5;">Rodzaj naprawy: </td>
<td style="background-color: #faf8a5;">
<?php
$db =& JFactory::getDBO();
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
 
if ($rodz_napr == "Wybierz")
{
$rodz_napra = "";
}
if ($rodz_napr == 'Naprawa pogwarancyjna')
{
$rodz_naprb = 'Naprawa pogwarancyjna---bbb';
}
if ($rodz_napr == "Naprawa pogwarancyjna")
{
$rodz_naprc = "Naprawa pogwarancyjna---ccc";
}
 
?>
<strong>
<?php echo $rodz_naprc; ?></strong></td></tr>
 
<tr><td style="background-color: #faf8a5;" >
Uwagi: --- <?php $rodz_naprc; ?> -----<?php echo $rodz_naprb; ?> --------- {ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla} -- <?php echo $rodz_napr; ?></td><td style="background-color: #faf8a5;"><strong>
{ccjom_cc_klienci_zgl_naprawy___uwagi}</strong></td></tr>
 
<tr><td style="background-color: #faf8a5;">Akceptuj?cy: </td><td style="background-color: #faf8a5;">
{ccjom_cc_klienci_zgl_naprawy___akceptujacy}</td></tr>
 
<tr><td style="background-color: #faf8a5;">Data akceptacji: </td><td style="background-color: #faf8a5;">
{ccjom_cc_klienci_zgl_naprawy___data_akceptacji}</td></tr>
 
<tr><td colspan="2"><img src="http://candc.katowice.pl/images/cc-loga/logo-small.png"/></td></tr>
</table>
 
-----------------------------------------------------------------------------------<br/>
 
Aby przyj?? urz?dzenie do naprawy zaloguj si? do <a href="http://candc.katowice.pl/index.php/menu-zgloszenie-naprawy/klienci-adm-zgloszenie-naprawy">  Panelu ISO</a><br/>
 
-----------------------------------------------------------------------------------<br/>
</center>
</body></html>


Screen shot are for testing this with input value:
$rodz_napr == 'Naprawa pogwarancyjna')
 

Attachments

  • Zaznaczenie_412.png
    Zaznaczenie_412.png
    12 KB · Views: 240
  • Zaznaczenie_413.png
    Zaznaczenie_413.png
    75.3 KB · Views: 240
Hi,
I very need this email template with "if", so i test it - but seems - there is some bug in fabrik - becouse nothing work properly,

When I have code:
PHP:
<?php
$db =& JFactory::getDBO();
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
 
if ($rodz_napr == 'Wybierz')
{
$rodz_napra = '';
}
if ($rodz_napr == 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - b';
}
if ($rodz_napr == 'Naprawa pogwarancyjna')
{
$rodz_naprc = 'Naprawa pogwarancyjna - c';
}
 
?>
<strong>
<?php echo $rodz_napra,$rodz_naprb,$rodz_naprc; ?></strong></td></tr>

I get nothing

When i have code:
PHP:
<?php
$db =& JFactory::getDBO();
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
 
if ($rodz_napr = 'Wybierz')
{
$rodz_napra = '';
}
if ($rodz_napr = 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - b';
}
if ($rodz_napr = 'Naprawa pogwarancyjna')
{
$rodz_naprc = 'Naprawa pogwarancyjna - c';
}
 
?>
<strong>
<?php echo $rodz_napra,$rodz_naprb,$rodz_naprc; ?></strong></td></tr>
I get all 3 values - see screenshots.
 

Attachments

  • Zaznaczenie_415.png
    Zaznaczenie_415.png
    6.9 KB · Views: 264
  • Zaznaczenie_416.png
    Zaznaczenie_416.png
    10.4 KB · Views: 226
In any case it's
if ($a == $b)
I think you can't use placeholders in your php code (they seem to be replaced AFTER the code has been executed, so they are working inside of echoed text or pure HTML)

Try
$rodz_napr = $formModel->_formData['ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla'];

$db =& JFactory::getDBO();
is only needed if you want to access the database.
 
Just to explain a little further, in php = is ALWAYS an assignment. So where you say:
PHP:
if ($rodz_napr = 'Wybierz')
this assigns the string 'Wybierz' to the variable $rodz_napr and then tests whether $rodz_napr is true.

To compare values in php you need to use == (case insensitive) or === for an exact match. So the correct code for the if statement would be:
PHP:
if ($rodz_napr === 'Wybierz')

Hope this helps.
 
This isn't a simple answer. :)

Placeholders in code itself will only get replaced prior to execution in eval'ed code, that is, code you put directly in a "code to eval" box on the backend. What we do with "code to eval" is we read your code in, we run it through the placeholder replacement, then we eval it. A typical example of how we do it would be ...

Code:
        $code_to_eval = $params->get('code_to_eval', '');
        if (!empty(trim($code_to_eval)))
        {
            $code_to_eval = $w->parseMessageForPlaceholder($code_to_eval, $data);
            $something = @eval($code_to_eval);
        }

In case you aren't familiar with the term 'eval', it means "run the code". So the PHP eval() command takes whatever you give it, and runs that as a PHP fragment.

So in the above example, if your code had something like this:

Code:
$foo = '{yourtable___foo}';
if ($foo == 'some_value') {
  // do something
}

... and the value of the 'yourtable___foo' element in the submitted form data is 'some_value', the code actually being eval'ed looks like:

Code:
$foo = 'some_value';
if ($foo == 'some_value') {
  // do something
}

Code run from files, placeholders don't get replaced, as we simply do something like ...

Code:
$file_to_include = $params->get('file_to_include, '');
if (!empty($file_to_include))
{
        require $file_to_include;
}

Well, it's usually a little more complex than that, as we typically clean the input, prepend the folder location, check that the file exists, etc. And in the case of the email plugin, we wrap the code in output buffering and capture any output:

Code:
        ob_start();
        $result = require $tmpl;
        $message = ob_get_contents();
        ob_end_clean();

But that's the basic logic. Bottom line, we don't do any "pre processing" of the file content, we just ask PHP to 'require' it, which runs the code within the current context / scope.

So in the previous example, the code being executed would look like:

Code:
$foo = '{yourtable___foo}';
if ($foo == 'some_value') {
  // do something
}

... where the placeholder has not been replaced.

So when using PHP files, if you need form data in your logic, you need to that "by hand", which would typically be via the $formModel, either using $formModel->getElementData('youtable___yourelement'), or accessing the $formModel->_formData array directly (or just $formModel->formData in 3.1, no _ ).

Or, you can fetch it yourself using the usual J! input API, or even by accessing the built in $_REQUEST array. But we recommend using the $formModel, as you are then getting whatever Fabrik thinks the data should be at that point, i.e. post validation (which may have changed the data), with the concept of values vs labels (non-raw vs raw), etc.

Depending where your code is running, there may be a $data array you can use instead of the $formModel. The tooltip will usually tell you if there is a $data you can use. In the form email plugin, we provide a $data, which contains the form data AFTER running it through the "format for email" phase of processing.

So, using the 'foo'' example again, you would typically do something like:

Code:
$foo = $formModel->getElementData('yourtable___foo', true);
if ($foo == 'some_value') {
  // do something
}

... which will fetch the raw value for that element (omit the 'true' arg to getElementData() if you don't want the raw data).

So the next question is, what about placeholders in whatever it is your code is outputting? Well, again, this
depends entirely on where your code is and what it is doing. In, say, a PHP validation, no, we don't do any placeholder replacement, we just expect you to retun true or false.

But in this case, where you are creating an email template, where you "echo" your template content, and we capture that output as the template to use for the email, then yes, the text you output is going to get run through the placeholder replacement, when we actually send the email. Whatever your PHP generated get's assigned to $message (using the ob_get_content output buffer capture described above), then for each recipient, we run it through placeholder replacement:

Code:
// ... blah blah, including stuff where your PHP output gets assigned to $message
        foreach ($email_to as $email)
        {
// ... blah blah stuff blah blah ..
                $thisMessage = $w->parseMessageForPlaceholder($message, $this->data, true, false, $thisUser);
// ... blah blah more stuff ...
                $mail = JFactory::getMailer();
                $res = $mail->sendMail(
                    $email_from, $email_from_name, $email, $thisSubject, $thisMessage,
                    $htmlEmail, $cc, $bcc, $thisAttachments, $return_path, $return_path_name
                );
        }

This is important to note. We don't directly use the $message your template produced, rather we assign what you produced to $thisMessage, and do a placeholder replacement on it each time round the "for each recipient" loop. This allows you to use the {$your->name} (or whatever) syntax for placeholders on a per-recipient basis, where $your is like $my, only it has the J! user data for the recipient (if there is a matching account with the recipient email), not the logged on user.

OK ... so does that make sense? I'd like to turn that in to a Wiki entry somewhere, but first I'd like to know if it made any sense at all, LOL!

-- hugh
 
Hi, nothing works for me
Not this what write troester:
Try
$rodz_napr = $formModel->_formData['ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla'];

Not this what write hugh
$foo = $formModel->getElementData('yourtable___foo', true);
if ($foo == 'some_value') {
// do something
}

No i try:
. the value of the 'ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla' element in the submitted form data is 'Naprawa gwarancyjna',

I try php code in my email template:
PHP:
<tr><td style="background-color: #faf8a5;">Rodzaj naprawy: </td>
<td style="background-color: #faf8a5;">
<?php
$rodz_napr = $formModel->getElementData('{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}', true);
if ($rodz_napr == 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - test b';
}
if ($rodz_napr == '1')
{
$rodz_naprb = 'Naprawa gwarancyjna - bbbb';
}
if ($rodz_napr == '2')
{
$rodz_naprc = 'Naprawa pogwarancyjna - cccc';
}
 
?>
<strong>
<?php echo $rodz_naprb,$rodz_napr; ?></strong></td></tr>

I get in email empty ....
------------------------------------
May be this is important:
my element: ___napr_gwa_pla is a databasejoin to table: CC-11 - Rodzaje napraw to element {ccjom_cc_klienci_zgl_naprawy_rodzaj___rodzaj_naprawy}

an element {ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla} can have 3 values:
Wybierz (Select)
Naprawa gwarancyjna
Naprawa pogwarancyjna

I need in my email template code which do exactly this:
PHP:
<tr><td style="background-color: #faf8a5;">Rodzaj naprawy: </td>
<td style="background-color: #faf8a5;">
<?php
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
if ($rodz_napr == 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - bbbb';
}
else if ($rodz_napr == 'Naprawa pogwarancyjna')
{
$rodz_naprb = 'Naprawa pogwarancyjna - cccc';
}
else if ($rodz_napr == 'Wybierz')
{
$rodz_naprb = '';
}
?>
<strong>
<?php echo $rodz_naprb; ?></strong></td></tr>

But, i try a lot possibilities and nothing work...
I have no idea what is wrong
 

Attachments

  • Zaznaczenie_417.png
    Zaznaczenie_417.png
    26.2 KB · Views: 207
  • Zaznaczenie_418.png
    Zaznaczenie_418.png
    65.8 KB · Views: 202
  • Zaznaczenie_419.png
    Zaznaczenie_419.png
    63.6 KB · Views: 194
  • Zaznaczenie_421.png
    Zaznaczenie_421.png
    11.1 KB · Views: 207
$rodz_napr = $formModel->getElementData('{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}', true);
must be
Code:
$rodz_napr = $formModel->getElementData('ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla', true);
(without {})
(omit the 'true' arg to getElementData() if you don't want the raw data)
the TRUE parameter is giving you the raw data, are you really comparing against the raw=keys/values? Your texts are looking like labels.
<?php echo $rodz_naprb,$rodz_napr; ?>
must be
<?php echo $rodz_naprb.$rodz_napr; ?>
(. not ,)
 
are you really comparing against the raw=keys/values?
my element: ___napr_gwa_pla is a databasejoin element to : {ccjom_cc_klienci_zgl_naprawy_rodzaj___rodzaj_naprawy}
id: 1 ----- rodzaj: Naprawa gwarancyjna
id: 2 ----- rodzaj: Naprawa pogwarancyjna

When use:
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
<?php echo $rodz_napr; ?>

I get:
Wybierz
or
Naprawa gwarancyjna
or
Naprawa pogwarancyjna


But i need get
'' ("empty")
or
Naprawa gwarancyjna - something bbbb
or
Naprawa pogwarancyjna - something cccc
 
this doesn't work also:
PHP:
<tr><td style="background-color: #faf8a5;">Rodzaj naprawy: </td>
<td style="background-color: #faf8a5;">
<?php
 
$rodz_napr = $formModel->getElementData('ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla');
 
if ($rodz_napr == 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - bbbb';
}
else if ($rodz_napr == 'Naprawa pogwarancyjna')
{
$rodz_naprb = 'Naprawa pogwarancyjna - cccc';
}
else if ($rodz_napr == 'Wybierz')
{
$rodz_naprb = '';
}
?>
<strong>
<?php echo $rodz_naprb; ?></strong></td></tr>

Added:
"wybierz" is not a value it is "select" option for element that is databasejoin.
User can make not any selection and then i need in email template <td></td>
not <td> wybierz</td>
when no selection i need in email template "empty"
--------------------
when write simply:
<td> {ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}</td>
i get "Wybierz" (=Select) when no selection --- i need then "empty" in email template...
 
So what do you get in $rodz_napr?
Put
var_dump($rodz_napr);exit;
into your code.

When use:
$rodz_napr = '{ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla}';
<?php echo $rodz_napr; ?>
Don't use {element} in your php file inside php code. As Hugh has explained this is only replaced AFTER the code has been run, so an "echo" will do but no comparison.
 
I get error code:
array(1) { [0]=> string(1) "1" }

I use code in my php file:
PHP:
<tr><td style="background-color: #faf8a5;">Rodzaj naprawy: </td>
<td style="background-color: #faf8a5;">
<?php
 
$rodz_napr = $formModel->getElementData('ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla');
echo $rodz_napr.' aa aa aa aa aa aa aa ';
 
if ($rodz_napr == 'Naprawa gwarancyjna')
{
$rodz_naprb = 'Naprawa gwarancyjna - bbbb';
}
else if ($rodz_napr == 'Naprawa pogwarancyjna')
{
$rodz_naprb = 'Naprawa pogwarancyjna - cccc';
}
else if ($rodz_napr == 'Wybierz')
{
$rodz_naprb = '';
}
?>
<strong>
<?php echo $rodz_naprb; ?></strong></td></tr>
<?php
var_dump($rodz_napr); exit;
?>

See screen shot with exact error code,
 

Attachments

  • Zaznaczenie_423.png
    Zaznaczenie_423.png
    79.3 KB · Views: 201
I get error code:
array(1) { [0]=> string(1) "1" }
It's no error code, it's the dump output showing $rodz_napr
So $rodz_napr is an array with the value 1, so it's containing the values (keys) and your code has to be something like
Code:
$rodz_napr = $formModel->getElementData('ccjom_cc_klienci_zgl_naprawy___napr_gwa_pla');
$rodz_napr = $rodz_napr[0];
echo $rodz_napr.' aa aa aa aa aa aa aa ';
 
if ($rodz_napr == '1')
{
$rodz_naprb = 'Naprawa gwarancyjna - bbbb';
}
else if ($rodz_napr == '2')
{
$rodz_naprb = 'Naprawa pogwarancyjna - cccc';
}
else
{
$rodz_naprb = '';
}
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top