v2
The goal of v2 is to make the plugin more powerful. It sould be able to handle anything a user would like to to with a form and not only send the content by email. Ideas for v2:
Actions
Best explained by the new call of the plugin as it is currently planned:
$form = sendform(
'reservation-form',
array(
'required' => array(
'name' => '',
'street' => '',
'city' => '',
'date' => 'date',
'persons' => 'num',
'from' => 'email'
),
'validate' => array(
'phone' => 'num'
),
'actions' => array(
array(
'_action' => 'email',
'subject' => 'New reservation from {name}.',
'snippet' => 'sendform-reservation',
'to' => $page->mail(),
'service' => 'html-mail',
'service-options' => array()
),
array(
'_action' => 'email',
'subject' => 'Copy: New reservation from {name}.',
'snippet' => 'sendform-reservation',
'to' => $page->copy_mail(),
'service' => 'mail',
'service-options' => array()
),
array(
'_action' => 'db-store',
'db' => 'my_db',
'user' => 'my_db_user',
'pw' => 'my_secret_password'
),
)
)
);
So the sendform plugin will accept two arguments, the form ID and the array of options. The array of options can contain three keys, required
, validate
and actions
. The former two will behave as before. Note the presence of from
as a required field as this field will no longer be mandatory and automatically required. The actions
key contains an array of actions. Each action is represented by an array containing at least the _action
key, specifying the type of the action to perform.
For example there is the email
action that sends an email of the form data just like v1 of the plugin. You'll find it very similar to the options array of v1, only without required
and validate
, and with an additional to
. Specifying multiple email
actions like in the example above will result in a similar behavior to the copy
option of v1 which will be removed.
Like this, arbitrary and multiple actions can be performed with the form data. Think of storing the request in a database in addition to sending the email or doing filesystem operations or anything you can think of.
Extensibility
The new actions will allow easy extensibility by adding custom actions. A custom action will be nothing else as a function that gets called by the sendform plugin, receiving the form data and action array as arguments. The plugin will handle validation of the form so the action is only called when the data is valid. Any further checks can be done in the action function itself but a fail will not prevent any other actions to be performed. The email
action could look like this:
function($form, $options) {
$mailBody = "";
$snippet = $options['snippet'];
if (empty($snippet)) {
foreach ($form as $key => $value) {
if (str::startsWith($key, '_')) {
continue;
}
$mailBody .= ucfirst($key).': '.$value."\n\n";
}
} else {
$mailBody = snippet($snippet, compact('form', 'options'), true);
if ($mailBody === false) {
throw new Exception("The email snippet '" . $snippet . "' does not exist!");
}
}
$params = array(
'service' => $options['service'],
'options' => $options['service-options'],
'to' => $options['to'],
'from' => a::get($form, 'name', '') . ' <' . a::get($form, '_from') . '>',
'subject' => $options['subject'],
'body' => $mailBody
);
$email = email($params);
if($email->send()) {
return array(
'success' => true,
'message' => l::get('sendform-send-success')
);
} else {
return array(
'success' => false,
'message' => l::get('sendform-send-error') . " " . $email->error()
);
}
};
Adding new actions to the plugin will work similar to adding new email services to the Kirby Toolkit:
sendform::$actions['email'] = function($form, $options) {...};
New name
The plugin will be called kirby-uniform
to reflect it now being much more powerful and able to handle any actions a form can perform. GitHub repository redirects will handle the URL inconsitencies.
TODO
- Figure out where the sendform action extensions are best placed to be extensible by users, globally availabe (don't place them in template controllers) and compatible with Git submodules.
- Notify the guys of http://getkirby-plugins.com/ once v2 is complete.