Calc Field Not Saving

talkinggoat

Member
I am trying to generate a randomish string, using php and clac, that is appended to the root URL, making a unique link. The calc element is called volunteer_unique_home_url, so it is running the php on itself.

The php should check to see if the field is empty. If it's empty, it generates a unique url. If it's not empty, it returns the data from the field.

I can get php to generate the URL, no problem. My issue is, when I submit the form, it doesn't save the generated link to the DB. When I re-open the record, the link is different. Here is the php:

PHP:
$unique_url = '{user_data___volunteer_unique_home_url}';

if (empty($unique_url)) {
  $username = str_shuffle(md5('{user_data___volunteer_user_name}'));
  $shortened = substr($username,0,8);
  return "https://mydomain.org?h=".$shortened;
} else {
   return $unique_url;
}

Save to DB is set to YES.
All the permissions are set to Public.
The element is not hidden.

Update: If I just call the $unique_url variable, it does return data and I verified there is data in the key, in mysql, so I'm confused why it won't detect there is data and skip to the else statement.
 
Last edited:
Nope, won't work ... that placeholder you are using will be generated from the calc code it is in.

Why not just use md5(), without str_shuffle()? Then it'll always be the same for a given volunteer_user_name, so it doesn't matter whether it gets regenerated or not.

Or, don't use a calc, use a hidden field, and put your code in a PHP form plugin, running onBeforeProcess, which checks the form data, $formModel->formData['user_data___volunteer_unique_home_url'] to see if that field has a value, and generates the string if it doesn't, and uses $formModel->updateFormData('user_data___volunteer_unique_home_url', $yourThing, true, true) to update it.

-- hugh
 
Or, don't use a calc, use a hidden field, and put your code in a PHP form plugin, running onBeforeProcess, which checks the form data, $formModel->formData['user_data___volunteer_unique_home_url'] to see if that field has a value, and generates the string if it doesn't, and uses $formModel->updateFormData('user_data___volunteer_unique_home_url', $yourThing, true, true) to update it.
Does this work for lists too? I noticed, for example, that if you eval PHP code in databasejoin fields, this evaluation works only in the form view but not in the list view...
 
Why not just use md5(), without str_shuffle()? Then it'll always be the same for a given volunteer_user_name, so it doesn't matter whether it gets regenerated or not.

I also have a clear button, in case anything ever happens to the user's link and it needs to be regenerated. If it's tied to the MD5, say, the first 8 characters, I won't ever be able to regenerate it. The algorithm will always generate the same hash.

Or, don't use a calc, use a hidden field, and put your code in a PHP form plugin, running onBeforeProcess, which checks the form data, $formModel->formData['user_data___volunteer_unique_home_url'] to see if that field has a value, and generates the string if it doesn't, and uses $formModel->updateFormData('user_data___volunteer_unique_home_url', $yourThing, true, true) to update it.

PERFECT @cheesegrits Here is how to do it, if I ever forget. Or maybe add it as an example, to the WIKI?
I switched the element 'user_data___volunteer_unique_home_url' to a field element.
Under forms, I selected the form responsible for that element.
On the last tab, Plugins, I added a PHP plugin.
I adapted your code above and read over these directions: http://fabrikar.com/forums/index.php?wiki/php-form-plugin/
Set the Process Script to onBeforeProcess.
Put this in the PHP Code box:
PHP:
$formData = $formModel->formData['user_data___volunteer_unique_home_url'];
function a() {
  $variable = str_shuffle(md5('{user_data___volunteer_user_name}'));
  $shortened = substr($variable,0,8);
  return "https://mywebsite.org/index.php?h=".$shortened;
}
if (empty($formData)){
  $formModel->updateFormData('user_data___volunteer_unique_home_url', a(), true);
}

Next thoughts:
How would I make it so it's not a field element? I like the way the calc element displays text, but this doesn't seem to work with calc. I made the field module read-only, which works, but it renders the field box. It doesn't really matter; it works. Just a personal preference and curiosity.

My next step will be to figure out how to integrate this into Joomla, disable this link, if the account is disabled and to make a "copy link" button for the end user.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top