Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-05-16 20:07:46

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

PHP question - help with zem_contact to_form

I typically use the php “to_form” from the example here to create user selectable form recipients, without showing email addresses. Now, I’d like to make it so that a txp user can specify the topic names and email addresses via a custom field in a textpattern article. I’m utilizing Gil’s great txp:tip for this.

I have one article the “contact” section of my site. It contains a custom field named “contacts” with the following data:

General Inquiry~|~email@address.com~||~Another Inquiry~|~email@address.com~||~Another Topic~|~email@address.com~||~Another Subject~|~email@address.com~||~A Topic~|~email@address.com~||~~|~

I know how to parse the above string with the rah_repeat plugin. However, I do not know how to write this in raw php for use in the “to_form”. Would anyone mind helping me out?

This is how I would output it with rah_repeat (<txp:php>but obviously I can’t use plugin tags in raw php</txp:php>!):

<txp:article_custom section="contact" limit="1">
	<txp:rah_repeat value='<txp:custom_field name="contacts" escape="" />' delimiter="~||~">
	<txp:rah_repeat value='<txp:rah_repeat_value />' delimiter="~|~" assign="field1, field2" />
<txp:smd_if field="txpvar:field1,txpvar:field2" operator="isused,isused">
    <txp:rah_repeat_if_first>default:
      echo '<txp:variable name="field1" />';<txp:else />
    case '<txp:variable name="field1" />':
      echo '<txp:variable name="field2" />';
      <txp:rah_repeat_if_last><txp:else />break;</txp:rah_repeat_if_last>
    </txp:rah_repeat_if_first></txp:smd_if>
</txp:rah_repeat></txp:article_custom>

Offline

#2 2015-05-16 21:34:36

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: PHP question - help with zem_contact to_form

Stacey, it would be much better to store your data in some standard format, say, JSON:

{"General Inquiry":"email@address.com"},{"Another Inquiry":"email@address.com"},{"Another Topic":"email@address.com"},{"Another Subject":"email@address.com"},{"A Topic":"email@address.com"}

Then the to_form example you cite can be reduced to

<txp:etc_query data='[<txp:custom_field name="contacts" escape="" />]' globals="zem_contact_form" query="*/{?Department}">{?}<txp:else />default@address.com</txp:etc_query>

Just be sure to download the latest etc_query version.

Offline

#3 2015-05-16 21:36:54

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: PHP question - help with zem_contact to_form

photonomad wrote #290840:

but obviously I can’t use plugin tags in raw php</txp:php>!):

Look at how Oleg/etc applied parse e.g. in this topic (several times).

Edit: Too late (and sub-optimal). ;)

Last edited by uli (2015-05-16 21:39:09)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#4 2015-05-16 21:58:33

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: PHP question - help with zem_contact to_form

uli wrote #290842:

Look at how Oleg/etc applied parse e.g. in this topic (several times).

Edit: Too late (and sub-optimal). ;)

Sorry, I haven’t replied to your question, thanks Uli. If you custom field were JSON-encoded, and you want to do it php-way, you could put this into your to_form:

<txp:php>
	global $zem_contact_form;
	$emails = json_decode(parse('[<txp:custom_field name="contacts" escape="" />]'), true);
	echo isset($emails[$zem_contact_form['Department']]) ? $emails[$zem_contact_form['Department']] : 'default@address.com';
</txp:php>

You can parse plugins too, but as Uli says, what you are trying to obtain is very… sub-optimal.

Edit: this would require adapting the great Gil’s tip, though. Here is a plugin-less to_form that should work without modifications:

<txp:php>
	global $zem_contact_form;
	$department = $zem_contact_form['Department'];
	$contacts = parse('[<txp:custom_field name="contacts" />]');
	foreach(do_list($contacts, '~||~') as $contact) {
		@list($case, $email) = explode('~|~', $contact);
		if($case == $department) break; else unset($email);
	}
	echo isset($email) ? $email : 'default@address.com';
</txp:php>

Last edited by etc (2015-05-17 09:51:31)

Offline

#5 2015-05-16 22:20:54

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: PHP question - help with zem_contact to_form

etc wrote #290843:

but as Uli says, what you are trying to obtain is very… sub-optimal.

No, I was judging my own reply, I thought you’d do it all in one go with this etc_query tag that I’m not having the slightest clue of :)


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB