Verified Commit 87f3fc6f authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(public-forms) Add TOS checkbox feature to forms

issue #5879
parent 3eedafc9
......@@ -86,6 +86,10 @@ class publicForm extends simplePlugin
'fdPublicFormCreationBase', TRUE,
$config->current['BASE']
),
new URLAttribute(
_('TOS URL'), _('Terms of service URL. If filled, this adds a mandatory checkbox to agree to the terms at this URL.'),
'fdPublicFormTosUrl', FALSE
),
)
),
);
......
......@@ -60,6 +60,12 @@ attributetype ( 1.3.6.1.4.1.38414.67.10.8 NAME 'fdPublicFormAlteredAttributes'
SUBSTR caseExactIA5SubstringsMatch
SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )
attributetype ( 1.3.6.1.4.1.38414.67.10.9 NAME 'fdPublicFormTosUrl'
DESC 'FusionDirectory - Form TOS URL'
EQUALITY caseExactMatch
SUBSTR caseExactSubstringsMatch
SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
# Objectclasses
objectclass ( 1.3.6.1.4.1.38414.67.2.1 NAME 'fdPublicForm'
......@@ -69,4 +75,4 @@ objectclass ( 1.3.6.1.4.1.38414.67.2.1 NAME 'fdPublicForm'
fdPublicFormTitle $ fdPublicFormText $
fdPublicFormTemplateType $ fdPublicFormTemplateDN $
fdPublicFormFinalText $ fdPublicFormPrivate $ fdPublicFormCreationBase $
fdPublicFormAlteredAttributes ) )
fdPublicFormAlteredAttributes $ fdPublicFormTosUrl ) )
......@@ -32,6 +32,9 @@
{$template_dialog}
{/if}
</div>
{if (!$done and $form.fdPublicFormTosUrl)}
<label for="tosCheckBox"><input type="checkbox" id="tosCheckBox" name="tosCheckBox"/> {t 1=$form.fdPublicFormTosUrl escape=no}I agree to the <a target="_blank" href="%1">terms of service</a>{/t}</label>
{/if}
</div>
{if !$done}
<div id="window-footer" class="plugbottom">
......
......@@ -116,6 +116,7 @@ class publicFormPage extends standAlonePage
'fdPublicFormTemplateDN' => 1,
'fdPublicFormPrivate' => 1,
'fdPublicFormCreationBase' => 1,
'fdPublicFormTosUrl' => 1,
'fdPublicFormAlteredAttributes' => '*',
),
$this->invitation['fdInvitationFormDN'],
......@@ -136,6 +137,7 @@ class publicFormPage extends standAlonePage
'fdPublicFormTemplateDN' => 1,
'fdPublicFormPrivate' => 1,
'fdPublicFormCreationBase' => 1,
'fdPublicFormTosUrl' => 1,
'fdPublicFormAlteredAttributes' => '*',
),
NULL,
......@@ -161,6 +163,92 @@ class publicFormPage extends standAlonePage
}
}
protected function prepareTemplateObject()
{
$this->template = new template($this->form['fdPublicFormTemplateType'], $this->form['fdPublicFormTemplateDN']);
$values = $this->getPrefilledValues();
$this->alteredAttributes = array(array(),array(),array());
$this->providedAttributes = array();
$this->providedMultiValueAttributes = array();
foreach ($this->form['fdPublicFormAlteredAttributes'] as $alteredAttribute) {
list($attribute, $mandatory, $readonly, $imported, $hidden) = explode('|', $alteredAttribute, 5);
$tab = $this->template->getAttributeTab($attribute);
if ($mandatory) {
$this->alteredAttributes[0][$tab][] = $attribute;
}
if ($this->prefill) {
if ($readonly) {
/* Check that mandatory readonly attributes are prefilled */
if (
isset($values[$tab][$attribute]) ||
(!$mandatory && !$this->template->getAttribute($tab, $attribute)->isRequired())
) {
$this->alteredAttributes[1][$tab][] = $attribute;
}
}
if ($imported) {
$this->providedAttributes[] = array($tab, $attribute);
if ($this->template->getAttribute($tab, $attribute) instanceof SetAttribute) {
$this->providedMultiValueAttributes[] = $attribute;
}
}
}
if ($hidden) {
$this->alteredAttributes[2][$tab][] = $attribute;
}
}
$this->template->alterAttributes(...$this->alteredAttributes);
$this->template->setValues($values, TRUE);
}
protected function submitForm()
{
/* Check TOS checkbox */
if (isset($this->form['fdPublicFormTosUrl']) && !isset($_POST['tosCheckBox'])) {
msg_dialog::display(_('Error'), _('You need to accept the terms of service to continue!'), ERROR_DIALOG);
return;
}
// Save values for later
$values = $this->template->getValues();
// Apply the template
$userTabObject = $this->template->apply();
/* Fill base depending on publicForm creation base and invitation base */
$base = $this->form['fdPublicFormCreationBase'];
if (isset($this->invitation)) {
$invitationBase = preg_replace('/,?'.preg_quote($config->current['BASE'], '/').'$/', '', dn2base($this->invitation['dn'], get_ou('invitationRDN')));
if (!empty($invitationBase)) {
$base = $invitationBase.','.$base;
}
}
$userTabObject->getBaseObject()->base = $base;
// Try to save the object
$errors = $userTabObject->save();
if (empty($errors)) {
$this->done = TRUE;
if (isset($this->registration)) {
$tabObject = objects::open($this->registration['dn'], 'registration');
$baseObject = $tabObject->getBaseObject();
$baseObject->fdRegistrationUserDN = $userTabObject->dn;
$baseObject->fdRegistrationState = 'filled';
$baseObject->fdRegistrationLastChange = date('c').'|'.$ui->dn;
$messages = $tabObject->save();
msg_dialog::displayChecks($messages);
}
} else {
msg_dialog::displayChecks($errors);
$this->template->reset();
/* After the reset we need to call alterAttributes again */
$this->template->alterAttributes(...$this->alteredAttributes);
// This fill the saved values into the resetted template
$this->template->setValues($values);
}
}
protected function getPrefilledValues($values = array())
{
if (isset($this->registration['fdRegistrationEmailAddress']) && !isset($values['mailAccount']['mail'])) {
......@@ -194,84 +282,13 @@ class publicFormPage extends standAlonePage
$this->errorMessage = $e->getMessage();
return;
}
$this->template = new template($this->form['fdPublicFormTemplateType'], $this->form['fdPublicFormTemplateDN']);
$values = $this->getPrefilledValues();
$this->alteredAttributes = array(array(),array(),array());
$this->providedAttributes = array();
$this->providedMultiValueAttributes = array();
foreach ($this->form['fdPublicFormAlteredAttributes'] as $alteredAttribute) {
list($attribute, $mandatory, $readonly, $imported, $hidden) = explode('|', $alteredAttribute, 5);
$tab = $this->template->getAttributeTab($attribute);
if ($mandatory) {
$this->alteredAttributes[0][$tab][] = $attribute;
}
if ($this->prefill) {
if ($readonly) {
/* Check that mandatory readonly attributes are prefilled */
if (
isset($values[$tab][$attribute]) ||
(!$mandatory && !$this->template->getAttribute($tab, $attribute)->isRequired())
) {
$this->alteredAttributes[1][$tab][] = $attribute;
}
}
if ($imported) {
$this->providedAttributes[] = array($tab, $attribute);
if ($this->template->getAttribute($tab, $attribute) instanceof SetAttribute) {
$this->providedMultiValueAttributes[] = $attribute;
}
}
}
if ($hidden) {
$this->alteredAttributes[2][$tab][] = $attribute;
}
}
$this->template->alterAttributes(...$this->alteredAttributes);
$this->template->setValues($values, TRUE);
$this->prepareTemplateObject();
}
$this->template->save_object();
if (isset($_POST['form_submit'])) {
// Save values for later
$values = $this->template->getValues();
// Apply the template
$userTabObject = $this->template->apply();
/* Fill base depending on publicForm creation base and invitation base */
$base = $this->form['fdPublicFormCreationBase'];
if (isset($this->invitation)) {
$invitationBase = preg_replace('/,?'.preg_quote($config->current['BASE'], '/').'$/', '', dn2base($this->invitation['dn'], get_ou('invitationRDN')));
if (!empty($invitationBase)) {
$base = $invitationBase.','.$base;
}
}
$userTabObject->getBaseObject()->base = $base;
// Try to save the object
$errors = $userTabObject->save();
if (empty($errors)) {
$this->done = TRUE;
if (isset($this->registration)) {
$tabObject = objects::open($this->registration['dn'], 'registration');
$baseObject = $tabObject->getBaseObject();
$baseObject->fdRegistrationUserDN = $userTabObject->dn;
$baseObject->fdRegistrationState = 'filled';
$baseObject->fdRegistrationLastChange = date('c').'|'.$ui->dn;
$messages = $tabObject->save();
msg_dialog::displayChecks($messages);
}
} else {
msg_dialog::displayChecks($errors);
$this->template->reset();
/* After the reset we need to call alterAttributes again */
$this->template->alterAttributes(...$this->alteredAttributes);
// This fill the saved values into the resetted template
$this->template->setValues($values);
}
$this->submitForm();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment