Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE - pre-create row on new form #668

Open
cheesegrits opened this issue Mar 29, 2013 · 1 comment
Open

FEATURE - pre-create row on new form #668

cheesegrits opened this issue Mar 29, 2013 · 1 comment

Comments

@cheesegrits
Copy link
Member

New feature suggestion.

We often get asked about creating unique record identifiers, using the PK as part of a pre-formatted string, and quite often some other element value, like "SKU-{rowid}-{parts___part_name}" etc.

Of course the chicken and egg is that this can't be done till after the form is first submitted, and some folk want it set "now".

In the past, I have written form plugins for clients, running onLoad, which for various reasons need to know a rowid for one app specific reason or other. The plugin just checks for a rowid, and if 0, manually poke a row in to the table, along with enough other values to satisfy Fabrik (like default values), then redirects to an edit link for the newly inserted rowid. So basically, hitting "Add" ends up being "Edit a new row", so the rowid is known the first time the user sees the page.

Seems like this might be something which would be generally useful, which could just be a simple "Pre-Create Row" form, and an optional "Record ID" element type, although probably need to call it something other than ID, to avoid confusion with PK / internal ID.

The Record ID element probably wouldn't do anything a field element with a default and an sprintf() statement couldn't do, or even a calc with "AJAX calc" enabled. But it'd probably be cleaner to just build a specific plugin for it, that is read only, param wise just has a simple "Template string" (with optional %s formatting) that groks placeholders, and does AJAX updates like the calc.

-- hugh

@ghost ghost assigned cheesegrits Mar 29, 2013
@cheesegrits
Copy link
Member Author

I just had a thought which might make this generic chicken and rowid situation a little easier, for folk who need to do some processing involving the newling created PK of a new row, which is to add something like an onAfterStore plugin hook, right before we call the onBeforeCalculation hook the end of the form model processToDb().

If any of the onAfterStore hooks return true, that means they've updated $formModel->_formData, and would like use to re-write the main row. Don't worry about joined data, that would just be a nightmare. But basically do what we do for a one-to-one where the FK is on the main table, like:

if (in_array(true, $pluginManager->runPlugins('onAfterStore', $this))) { $listModel->getTable()->db_table_name = $origTableName; $listModel->getTable()->db_primary_key = $origTableKey; $listModel->storeRow($this->_formData, $insertId); }

Wouldn't be greatly used, but would be easy to then write a few lines in a PHP form plugin, set to only run on "New", like ...

$formModel->updateFormData('tablename___part_num', sprintf("SKU-%05d", $formModel->getRowId);
return true;

... which would set the part_num to SKU-00364 (or whatever) on submission of a new form.

Or, to make it slightly easier,build a little generic element type to build a string, as per the "Record ID" stuff in my previous post, automagically does that when creating a new row.

-- hugh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant