0 Replies Latest reply on Jun 10, 2013 2:58 PM by Balance

    Parsing links

    Balance Level 1

      I have a utility that lets users store custom fields (first name, favorite color, address, URL, etc).

      One of those fields is used to store the contact's work/personal URL.

      In addition, users can send their contacts an email including these fields. So an email might say: "Dear *fname*, Your *URL* is pretty cool!"

      Now my client wants to re-write the URL so they can track it using Google Analytics, and that's where I'm stuck.

      Basically, I'm struggling with how to re-write the URLs since there can be other fields for one contact that may need to be re-written.

      In other words, I need to check every field to see if there's an href, and if there is, replace it with another href, for *every contact*.

      The code below re-writes the URL only for one field and for one contact, but not if there's more than one field with an href and/or more than one contact being returned.

       

      Here is the code I'm using:

       

      <!---get user defined field info--->

      <cfquery name="getUDFields" datasource="#attributes.dsn#">

      SELECT field_id, field_name, column_name

      FROM contacts_fields

      ORDER BY field_order

      </cfquery>

       

      <!--- grab the user-defined fields --->

      <cfloop query="getUDFields">

       

                <cfscript>

                          tmpHTMLcontent = attributes.content;

                          tmpHTMLcontent = ReplaceNoCase(tmpHTMLcontent,"*"&getUDFields.column_name&"*", "##getRecpsGroupHTML['"&getUDFields.column_name&"'][currentRow]##","ALL");

                          HTMLcontent = attributes.content;

                </cfscript>

       

       

      <cfoutput query="getRecpsGroupHTML"> <!--- query object returning contacts that will receive the email --->

       

       

                <cfset arrHTMLcontentLinks = getLinks(evaluate(de(tmpHTMLcontent)))><!--- returns an associative array with hrefs stripped out --->

       

       

                          <cfloop from="1" to="#arrayLen(arrHTMLcontentLinks)#" index="i">

                                    <cfloop collection="#arrHTMLcontentLinks[i]#" item="j">

                                              <cfif j is "href">

                                                        <cfset URLtoInsert = arrHTMLcontentLinks[i][j]>

                                                        <cfquery name="insertlinks" datasource="#attributes.dsn#">

                                                                  INSERT INTO links (url, email_id)

                                                                  VALUES ('#trim(urltoinsert)#', #last_email_ID#)

                                                        </cfquery>

                                                        <cfquery name="getMax" datasource="#attributes.dsn#">

                                                                  SELECT IDENT_CURRENT('links') AS MaxID

                                                        </cfquery>

                                                        <cfset last_link_ID = getMax.MaxID>

                                                        <cfset newlink = "http://"&clientDomain&"/test.cfm?"&last_link_ID>

                                                        <cfset HTMLcontent = ReplaceNoCase(evaluate(de(tmpHTMLcontent)),URLtoInsert,newlink)>

       

                                                        <cfscript>

                                                                  //added so the de() doesn't choke in cfmail

                                                                  variables.HTMLcontent = replace(variables.HTMLcontent,"##","####","ALL");

                                                        </cfscript>

       

       

                                                        <!--- replace user-defined fields in the HTML with their literal values --->

                                                        <cfloop query="getUDFields">

                                                        <cfscript>

                                                                  variables.HTMLcontent = ReplaceNoCase(variables.HTMLcontent,"*"&getUDFields.column_name&"*", "##getRecpsGroupHTML['"&getUDFields.column_name&"'][currentRow]##","ALL");

                                                        </cfscript>

                                                        </cfloop>

                               <!--- CFMAIL CODE WOULD GO HERE, ONCE THE DATA IS PARSED PROPERLY --->

                                              </cfif>

                                    </cfif>

                          </cfloop>

                </cfloop>

       

                </cfoutput>

       

       

      </cfloop>