The attached patch fixes the bug where submitted data does not show up in emails (albeit not ideally), and makes some text formatting and coding improvements in the code that puts together the email. It also includes one change turns off submission limiting if full debug is on, AND the user is not anonymous.

I should probably have rolled this up into 3 different patches with three different issues, but I got carried away, and in this moment I don't have the energy to split up the changes. I'll do it later if required.

The bug where submitted data does not show up in emails is described at http://drupal.org/node/151894. The solution here is not ideal, because it uses the key instead of the name of form components in the email. That means that if you didn't set the component key value when you created a component, your emails will have numbers next to the values, e.g. 118822383: me@mymail.com instead of email_address: me@mymail.com. However if you left the key values as the integers they defaulted to, then you probably aren't experiencing this bug anyway.

The problem is that data is fed into the email using $form_values['submitted_tree'], which uses keys (strings) and values, but the name and description data is stored in $node->webformcomponents and is indexed by the cid (component id), which is an integer, not a string. I'm not sure how this used to work before it broke.

The fundamental problem is that there are two unique IDs for each component, the integer CID, and the string name. Only one is necessary. 2 IDs will only create more problems. I don't know how the rest of webform works, and I didn't want to get that involved at this stage, but ideally, webform should either stop storing and using one of those ids, OR at least, index all components by the same unique ID. That could mean using the cid to index $form_values['submitted_tree'], or name to index $node->webformcomponents.

A crufty solution would be to have two indexes in $form_values['submitted_tree'].

CommentFileSizeAuthor
submission vars in email.patch3.51 KBBevan

Comments

quicksketch’s picture

Thanks for the patch. It needs some cleaning up and finishing before going in.

- I'm not so sure about this kind of formatting:

$message .= $indent . strtoupper($key) ."\n===============\n$value\n===============\n";

It seems a little strong just for a field value. Also, disabling the form submit limit when debugging doesn't right. All that debug does is output more information, not actually change the behavior.

- There shouldn't be a "TODO" item in such a small patch. We should fix this all at once.

I like the translation improvements.

The reason for having two keys makes more sense considering the common usages of webform. The form keys are changed so people can more easily traverse the results and make additions and validation. The numeric key is used so that webform stands a chance when handling thousands of submissions a day. Since all submitted data is stored in a single table, the integer key is necessary to ensure fast primary key lookups.

Bevan’s picture

Formatting:
I assume you mean ==============? My intention was to clearly define the beginning and end of multiline values, not for it to be strong. I find it quite difficult to read value if the key is on the same line as the first line of the value, or if there is no distinct indicator of where the value starts. ========== probably isn't the best way of doing that, but anything with text is going to be awkward.

Another option is to make an html email, use mime mail module to send it, and let mime mail convert it to text if necessary. I think it has a fairly standard library to textify html. Obviously that creates a dependency on mime mail module, but abstraction is a good thing.

TODO:
I agree. I wasn't sure what would be the best way to deal with the double-key issue. So I figured it'd be best to have this conversation first, then come back to it later.

Unique IDs:
I understand the reasoning for having two unique identifiers, however I wonder if it will cause more headaches than it will solve. If handling large numbers of submissions is important, then obviously the integer key makes a better unique id. Why not use the integer keys in $form_values['submitted_tree']? That will solve one issue here, and probably more in the future. Yes, debugging output from the form will be a little less easy, but code will be cleaner. Also php probably works faster on arrays indexed by integers rather than associative arrays.

Btw, I assume the keys in $form_values['submitted_tree'] are the keys of form components submitted to the the drupal form API. Is that right?

Also, why are the integers of CID seemingly random? Is there any reason not to use incremental natural numbers? Like with mySQL's autoincrement, or drupal's sequences table. i.e. 1, 2, 3, 4... That would save space and possible make indexing and database querying faster, in addition to being more readable.

Another option is to:

foreach ($form_values['submitted_tree'] as $submitted_key => $submitted_value) {
  foreach ($node->webformcomponents as $component) {
    if ($component['key'] == $submitted_key) {
      $message .= $component['name'] .": $submitted_value\n";
    }
  }
}

However that's quite crufty, and probably not efficient compared to

foreach ($form_values['submitted_tree'] as $submitted_key => $submitted_value) {
  $message .= $node->webformcomponents[$submitted_key]['name'] .": $submitted_value\n";
}

I noticed while creating a form yesterday that although the key value of a form component is expected to be a unique string, there are no checks for that. I created two components with the same key, and webform didn't complain. The only issue was that the first of the two components didn't display. If a string is going to be used for a unique identifier, the database should reflect this and webform should check for uniqueness. Should I create a new issue for this?

quicksketch’s picture

Good points all. There is already an issue for using sequences/auto-update instead of seemingly random integers (it's actually the timestamp the component is created).

The integer in the submitted_tree are equivalent to the IDs used in the database. The human-readable key is used to make the additional submit and validation easier. It's also useful for people using webform as a front-end for perl/php scripts. I wouldn't put the redirect POST thing if I'd written webform to begin with, but it's always been in webform so I'd prefer to leave it in there.

An optional setting for HTML emails would be totally fine. But it must be optional, as HTML email is an extremely touchy subject with a lot of people.

Yes the unique ID for the key is definitely a bug (/feature), and should be filed as a separate issue.

Bevan’s picture

I wouldn't put the redirect POST thing

I'm not sure what you're refering to here

Created new issue: http://drupal.org/node/153430

I guess that means the function needs to deal with the issue as I previously suggested, with the nested for statements. Or do you have any other suggestions?

quicksketch’s picture

Status: Needs work » Closed (fixed)

Several email patches have gone in to clean things up a bit. I'm going to close this issue to help clean up the queue.