[mod] Conditional Placeholder / Attribute Display

3rd party code for phpList

Re: [mod] Conditional Placeholder / Attribute Display

Postby silaspalmer » 8:07am, Tue 13 Dec, 2011

Just in case you need it, I've compiled the last 2 code hacks into one piece of code ... much easier to copy and paste.

This hack allows you to insert conditional placeholders in your email messages:
Example 1:
{CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Greetings!}

This checks whether attribute "first name" in the database is filled.
If yes: then print "Dear [FIRST NAME]," (ie. Dear Paul,)
If no: then print "Greetings!"


Example 2:
Sehr {CONDITIONAL|[Se_x=male]|geehrter Herr|geehrte Frau} [FIRST NAME],

This checks whether attribute "se-X" in the database matches the value behind the equals-sign (=)
If yes: then print "geehrter Herr", otherwise print "geehrte Frau"


Example 3:
Sehr {CONDITIONAL|[FIRST NAME=Paul]|We all love you [FIRST NAME]!|I don't like you ;)} [FIRST NAME],

This checks whether attribute "first name" in the database matches the value behind the equals-sign (=)
If yes: then print "We all love you Paul!", otherwise print "I don't like you ;)"


INSTALLATION:
edit lists/admin/sendemaillib.php

At the point indicated below:
Code: Select all
 

// INSERT THE CODE HERE

$destinationemail = '';
  if (is_array($user_att_values)) {



replace // INSERT THE CODE HERE with the below code.

Code: Select all
 
//-- START of Conditional Statements modification --

  /**
   * @since 2007/09/30
   * @version 2008/07/18
   * @author flug @ forums.phplist.com
   * @author pd @ forums.phplist.com
   * @link http://forums.phplist.com/viewtopic.php?t=11225
   * @todo several characters -like {} and |- should be able to be escaped like \} and \{ and \|
   * @todo maybe it should be possible as well to put several conditions in one conditional statement
   *       although this might be a little bit to much as well...
   *
   * Example 1:
   * {CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Greetings!}
   *
   * This checks whether attribute "first name" in the database is filled.
   * If yes: then print "Dear [FIRST NAME]," (ie. Dear Paul,)
   * If no: then print "Greetings!"
   *
   *
   * Example 2:
   * Sehr {CONDITIONAL|[***SPAM***=male]|geehrter Herr|geehrte Frau} [FIRST NAME],
   *
   * This checks whether attribute "***SPAM***" in the database matches the value behind the equals-sign (=)
   * If yes: then print "geehrter Herr", otherwise print "geehrte Frau"
   *
   *
   * Example 3:
   * Sehr {CONDITIONAL|[FIRST NAME=Paul]|We all love you [FIRST NAME]!|I don't like you ;)} [FIRST NAME],
   *
   * This checks whether attribute "first name" in the database matches the value behind the equals-sign (=)
   * If yes: then print "We all love you Paul!", otherwise print "I don't like you ;)"
   *
   */

  //upcase the keys here because the format within email messages is the [UPPER CASE]
  $user_att_values_upper=array_change_key_case($user_att_values, CASE_UPPER);
  $condition_regex = '/\{CONDITIONAL\|\[(.*?)\]\|(.*?)\|(.*?)\}/si';

  /**
   * We want to modify both the htmlmessage and the textmessage
   * We want to do that that by using the same code for both, but without using a function
   * The easiest way to do that is by putting both variables in an array by reference and loop through
   * that array.
   */
  $changemessages = Array(&$htmlmessage, &$textmessage);

  for ($i=0; $i<count($changemessages); $i++) {
      //do conditional substitution for HTML message:
      if (preg_match_all ($condition_regex,
             $changemessages[$i],$matches, PREG_SET_ORDER))
      {
        foreach ($matches as $regs) {
         
          $conditional=""; # if the attribute isn't found or there are no attributes we will still just erase the matching conditional statement
          if (is_array($user_att_values_upper)) {
            #$check_conditional_value = ( strpos('=', $regs[1])===false ? false : true );
            ###EDIT
            $cut=explode('=', $regs[1]);
            $cut=$cut[1];
            if ($cut!='') $check_conditional_value = true;
            else $check_conditional_value = false;
            ### /EDIT
            list($field_name,$match_conditional_value) = explode('=', $regs[1], 2);

            if (  array_key_exists( $field_name, $user_att_values_upper )) {
               $att_value=$user_att_values_upper[ $field_name ];

              /**
               * When there is no conditional value given (so there is no equals-sign)
               *  we want to check whether the $att_value is not empty
               *
               * Otherwise we want the $att_value to match our conditional value
               */

             ###EDIT
              if ( ($check_conditional_value==false && $att_value!="") || ($check_conditional_value==true && $att_value==$match_conditional_value)) {
              ### /EDIT
                //note: no need to replace [ATTRIBUTE] placeholders here, it will be done later
                $conditional=$regs[2];
              } else {
                $conditional=$regs[3];
              }
           }
          }
          //now substitute the conditional expression into the message (or erase it if no matching attributes found)
          $changemessages[$i] = str_replace($regs[0] , $conditional, $changemessages[$i]);
        }
      }
  }
 
  /**
   * A little cleanup
   */
  unset($changemessages, $check_conditional_value, $field_name, $match_conditional_value, $condition_regex,
        $matches, $regs, $conditional);
 
  //-- END of Conditional Statements modification --
silaspalmer
phpList newbie
 
Posts: 2
Joined: 7:52am, Tue 13 Dec, 2011

Re: [mod] Conditional Placeholder / Attribute Display

Postby Austria » 7:00pm, Thu 16 Feb, 2012

It seems that this hack has been going for while. But it seems to address my need if I understand correctly.

I have a WordPress site where a custom plugin has been used for Registering Users for a series of Events. So, without using PHPList forms this has stored the registrants information in a custom table called attendees in the WordPress database. When the Registrant has signed in, not as a WordPress user (by the way), they fill out the form with different options that vary depending on the town they live in and where they work. About 14 different values are stored besides first name, last name, phone, email, etc, etc.

When they submit the completed form the next page is the Confirmation page that mirrors back all the values they have filled in. And, since I installed a Confirmation email script yesterday this online confirmation page also contains a line that says that they have been sent a Confirmation email with the same values and text message seen on the screen.

The problem I have now and why I am looking at PHPList is that I have a little over 60 records of registrants in the attendees table who did NOT receive an email confirmation because I had not installed the code to do this until yesterday.

So I am looking at PHPList to see if I can have one function that looks at that attendees table, pulls out all the values, places them into the text message template where they make "sense", and then sends out that email and then loops to the next record and does the same thing all over again. Until every record has been sent a customized confirmation email the same way my one-time email script does.

This is to be an html email first. And my own one query script pulls up an html message with [firstname] [lastname] [eventdate][location] etc etc bracketed values that get filled in by the variables of that particular record.

So, ladies and gentlemen, is there now a proven way of doing this with either a hack or a plugin of PHPlist?

Either from a table CSV import file or directly from the database table attendees itself?

Thanks for your suggestions!

Vienna
Austria
phpList newbie
 
Posts: 2
Joined: 5:25pm, Thu 16 Feb, 2012

Re: [mod] Conditional Placeholder / Attribute Display

Postby JoeTorma » 5:15am, Mon 27 Aug, 2012

silaspalmer wrote:Just in case you need it, I've compiled the last 2 code hacks into one piece of code ... much easier to copy and paste.

This hack allows you to insert conditional placeholders in your email messages:
Example 1:
{CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Greetings!}

This checks whether attribute "first name" in the database is filled.
If yes: then print "Dear [FIRST NAME]," (ie. Dear Paul,)
If no: then print "Greetings!"


Example 2:
Sehr {CONDITIONAL|[Se_x=male]|geehrter Herr|geehrte Frau} [FIRST NAME],

This checks whether attribute "se-X" in the database matches the value behind the equals-sign (=)
If yes: then print "geehrter Herr", otherwise print "geehrte Frau"


Example 3:
Sehr {CONDITIONAL|[FIRST NAME=Paul]|We all love you [FIRST NAME]!|I don't like you ;)} [FIRST NAME],

This checks whether attribute "first name" in the database matches the value behind the equals-sign (=)
If yes: then print "We all love you Paul!", otherwise print "I don't like you ;)"


INSTALLATION:
edit lists/admin/sendemaillib.php

At the point indicated below:
Code: Select all
 

// INSERT THE CODE HERE

$destinationemail = '';
  if (is_array($user_att_values)) {



replace // INSERT THE CODE HERE with the below code.

Code: Select all
 
//-- START of Conditional Statements modification --

  /**
   * @since 2007/09/30
   * @version 2008/07/18
   * @author flug @ forums.phplist.com
   * @author pd @ forums.phplist.com
   * @link http://forums.phplist.com/viewtopic.php?t=11225
   * @todo several characters -like {} and |- should be able to be escaped like \} and \{ and \|
   * @todo maybe it should be possible as well to put several conditions in one conditional statement
   *       although this might be a little bit to much as well...
   *
   * Example 1:
   * {CONDITIONAL|[FIRST NAME]|Dear [FIRST NAME],|Greetings!}
   *
   * This checks whether attribute "first name" in the database is filled.
   * If yes: then print "Dear [FIRST NAME]," (ie. Dear Paul,)
   * If no: then print "Greetings!"
   *
   *
   * Example 2:
   * Sehr {CONDITIONAL|[***SPAM***=male]|geehrter Herr|geehrte Frau} [FIRST NAME],
   *
   * This checks whether attribute "***SPAM***" in the database matches the value behind the equals-sign (=)
   * If yes: then print "geehrter Herr", otherwise print "geehrte Frau"
   *
   *
   * Example 3:
   * Sehr {CONDITIONAL|[FIRST NAME=Paul]|We all love you [FIRST NAME]!|I don't like you ;)} [FIRST NAME],
   *
   * This checks whether attribute "first name" in the database matches the value behind the equals-sign (=)
   * If yes: then print "We all love you Paul!", otherwise print "I don't like you ;)"
   *
   */

  //upcase the keys here because the format within email messages is the [UPPER CASE]
  $user_att_values_upper=array_change_key_case($user_att_values, CASE_UPPER);
  $condition_regex = '/\{CONDITIONAL\|\[(.*?)\]\|(.*?)\|(.*?)\}/si';

  /**
   * We want to modify both the htmlmessage and the textmessage
   * We want to do that that by using the same code for both, but without using a function
   * The easiest way to do that is by putting both variables in an array by reference and loop through
   * that array.
   */
  $changemessages = Array(&$htmlmessage, &$textmessage);

  for ($i=0; $i<count($changemessages); $i++) {
      //do conditional substitution for HTML message:
      if (preg_match_all ($condition_regex,
             $changemessages[$i],$matches, PREG_SET_ORDER))
      {
        foreach ($matches as $regs) {
         
          $conditional=""; # if the attribute isn't found or there are no attributes we will still just erase the matching conditional statement
          if (is_array($user_att_values_upper)) {
            #$check_conditional_value = ( strpos('=', $regs[1])===false ? false : true );
            ###EDIT
            $cut=explode('=', $regs[1]);
            $cut=$cut[1];
            if ($cut!='') $check_conditional_value = true;
            else $check_conditional_value = false;
            ### /EDIT
            list($field_name,$match_conditional_value) = explode('=', $regs[1], 2);

            if (  array_key_exists( $field_name, $user_att_values_upper )) {
               $att_value=$user_att_values_upper[ $field_name ];

              /**
               * When there is no conditional value given (so there is no equals-sign)
               *  we want to check whether the $att_value is not empty
               *
               * Otherwise we want the $att_value to match our conditional value
               */

             ###EDIT
              if ( ($check_conditional_value==false && $att_value!="") || ($check_conditional_value==true && $att_value==$match_conditional_value)) {
              ### /EDIT
                //note: no need to replace [ATTRIBUTE] placeholders here, it will be done later
                $conditional=$regs[2];
              } else {
                $conditional=$regs[3];
              }
           }
          }
          //now substitute the conditional expression into the message (or erase it if no matching attributes found)
          $changemessages[$i] = str_replace($regs[0] , $conditional, $changemessages[$i]);
        }
      }
  }
 
  /**
   * A little cleanup
   */
  unset($changemessages, $check_conditional_value, $field_name, $match_conditional_value, $condition_regex,
        $matches, $regs, $conditional);
 
  //-- END of Conditional Statements modification --


This is working PERFECTLY on 2.10.19. Definitely should be part of CORE...
JoeTorma
phpList newbie
 
Posts: 3
Joined: 1:46am, Sun 05 Aug, 2012

Re: [mod] Conditional Placeholder / Attribute Display

Postby johntrot » 4:22pm, Wed 26 Sep, 2012

This is an excellent hack... perhaps someone with the proper knowledge can turn it into a plugin
Conditional statement embeded within another conditional statement would be a great addition...

working perfectly on a shared Linux host using:
PHPList: v2.10.19
PHP: 5.3.13
MySql: 5.5.27
John B
johntrot
PL Geek
 
Posts: 71
Joined: 8:28pm, Fri 01 Apr, 2005

Re: [mod] Conditional Placeholder / Attribute Display

Postby djpyle » 11:33am, Sat 29 Dec, 2012

I think this is about what I need, but will it work with checkbox group attributes? I have one group that lists book genres and I'd like to send information for only the genres each user is interested in. Which sort of leads me to my next question: can the conditional content include html, or am I limited to plain text?

Thanks to anyone who has any information!
djpyle
phpList newbie
 
Posts: 1
Joined: 11:26am, Sat 29 Dec, 2012

Re: [mod] Conditional Placeholder / Attribute Display

Postby kaon » 5:07am, Fri 01 Feb, 2013

I am using the code by pd, with the change by szlig.
Seems to be working as expected on my version 2.10.18 .
kaon
PL Nut
 
Posts: 36
Joined: 7:18am, Fri 23 Dec, 2005

Re: [mod] Conditional Placeholder / Attribute Display

Postby Helveticus » 7:13pm, Tue 06 Aug, 2013

For me it does not work. I inserted the code posted by silaspalmer and then I tried Dear [FIRST NAME] [LAST NAME] and it does not work.

If I try the examples it does also not work. Nothing is printed, just a white line.

What is going wrong?
Helveticus
PL Geek
 
Posts: 64
Joined: 10:22pm, Sun 28 Jul, 2013

Re: [mod] Conditional Placeholder / Attribute Display

Postby Dragonrider » 8:09pm, Tue 06 Aug, 2013

Helveticus wrote:For me it does not work. I inserted the code posted by silaspalmer and then I tried Dear [FIRST NAME] [LAST NAME] and it does not work.

If I try the examples it does also not work. Nothing is printed, just a white line.

What is going wrong?

Have you defined [FIRST NAME] & [LAST NAME] as attributes required on your subscribe form? If they don't exist, then can't be used.

You'd define them by the way from the Main Page, select attributes, create them on the resulting page, then allocate them to your list(s) on the subscribe page and edit the list as required.
My sites:- http://wharfedalefestival.co.uk, http://ilkleygardeners.org.uk, http://emergencyaid.net, http://dragonrider.co.uk
Latest phpList version is now 3.0.12 (3 February 2015) and requires a minimum of MySQL 5.0 and PHP 5.3.x
Dragonrider
Moderator
 
Posts: 3460
Joined: 6:58am, Sun 02 Jul, 2006
Location: Ilkley, West Yorkshire, United Kingdom

Re: [mod] Conditional Placeholder / Attribute Display

Postby Helveticus » 10:01pm, Tue 06 Aug, 2013

This was the problem. Now it works. Thank you very much.
Helveticus
PL Geek
 
Posts: 64
Joined: 10:22pm, Sun 28 Jul, 2013

Re: [mod] Conditional Placeholder / Attribute Display

Postby jpaquit » 1:20pm, Mon 09 Sep, 2013

I changed line 64 of the mod, so that it converts attributes to upper case in the content:

Code: Select all
list($field_name,$match_conditional_value) = array_flip(array_change_key_case(array_flip(explode('=', $regs[1], 2)),CASE_UPPER));
jpaquit
phpList newbie
 
Posts: 1
Joined: 1:09pm, Mon 09 Sep, 2013

Previous

Return to Contributions: Plug-ins, Add-ons, Mods

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest