Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use conditions inside Email template in SugarCRM

How I can use if/else conditions inside the email template in SugarCRM? I am trying use conditions equal pdf template and smarty template but I have no success.

No success

<?php if ({::past::Opportunities::name::} != {::future::Opportunities::name::}){ ?>

No success

{if {::past::Opportunities::name::} neq {::future::Opportunities::name::}}

no success

<!-- {if {::past::Opportunities::name::} neq {::future::Opportunities::name::}} -->

Any success (?)

??????

Thanks

like image 975
ErasmoOliveira Avatar asked Feb 19 '15 16:02

ErasmoOliveira


2 Answers

It seems, that official SugarCRM docs don't provide any information about using if/else conditions in email templates. I didn't believed them, so i dug into SugarCRM code.

Research:

Sending e-mail is done in EmailMan class in method sendEmail:

$template_data = $this->current_emailtemplate
                    ->parse_email_template(
                    array(
                        'subject' => $this->current_emailtemplate->subject,
                        'body_html' => $this->current_emailtemplate->body_html,
                        'body' => $this->current_emailtemplate->body,
                    )
                    , $focus_name, $module
                    , $macro_nv);

It uses parse_email_template method from class EmailTemplate. It's not so well-written, as i was thinking. And it's only providing basic variable replacing. Let's look at it a little closer:

function parse_email_template($template_text_array, $focus_name, $focus, &$macro_nv)
    {
        [...] //variable initiation
        //preparing prefixes to search for variables (all variables are in "$some_name" format
        $pattern_prefix = '$' . strtolower($beanList[$focus_name]) . '_';
        $pattern_prefix_length = strlen($pattern_prefix);
        $pattern = '/\\' . $pattern_prefix . '[A-Za-z_0-9]*/';


        foreach ($template_text_array as $key => $template_text) {
            [...] //searching for variables matching $pattern and replacing them with proper values

            $return_array[$key] = $template_text;
        }

        return $return_array;
    }

Conclusion:

What i can say more - SugarCRM at this point doesn't provide any conditions nor smarty or other template engine. You can try to modify their code to implement it, but i wouldn't recommend that as it is a litlle spaghetti ;)

like image 100
Paweł Tomkiel Avatar answered Oct 22 '22 02:10

Paweł Tomkiel


handlebarsjs may help?
http://handlebarsjs.com/builtin_helpers.html

{{#if yourcondition}} action {{else}} action{{/if}}
like image 44
mg33dev Avatar answered Oct 22 '22 01:10

mg33dev