Skip to content

Commit

Permalink
Added a front end module that displays the steps of a given form :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Aug 5, 2016
1 parent 8f3f6aa commit b0f17af
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 3 deletions.
19 changes: 19 additions & 0 deletions MPFormsFormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ public function getFieldsWithoutPageBreaks()
return $formFields;
}

/**
* Gets the label for a given step.
*
* @param int $step
*
* @return string
*/
public function getLabelForStep($step)
{
foreach ($this->getFieldsForStep($step) as $formField) {
if ($this->isPageBreak($formField) && '' !== $formField->label) {

return $formField->label;
}
}

return 'Step ' . $step;
}

/**
* Gets the current step.
*
Expand Down
96 changes: 96 additions & 0 deletions MPFormsStepsModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* mp_forms extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2015-2016, terminal42 gmbh
* @author terminal42 gmbh <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link https://github.com/terminal42/contao-mp_forms
*/

class MPFormsStepsModule extends \Module
{

/**
* Template
* @var string
*/
protected $strTemplate = 'mod_mp_forms_steps';


/**
* Display a wildcard in the back end
*
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE') {
/** @var \BackendTemplate|object $objTemplate */
$objTemplate = new \BackendTemplate('be_wildcard');

$objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['mp_form_steps'][0]) . ' ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&amp;table=tl_module&amp;act=edit&amp;id=' . $this->id;

return $objTemplate->parse();
}

return parent::generate();
}


/**
* Generate the module
*/
protected function compile()
{
$navTpl = new \FrontendTemplate($this->navigationTpl ?: 'nav_default');
$navTpl->level = 0;
$navTpl->items = $this->buildNavigationItems();
$this->Template->navigation = $navTpl->parse();
}

/**
* Builds the navigation array items.
*
* @return array
*/
private function buildNavigationItems()
{
$manager = new MPFormsFormManager($this->form);

$steps = range(0, $manager->getNumberOfSteps() - 1);
$items = [];
$firstFailingStep = $manager->validateSteps();

foreach ($steps as $step) {

// Check if step can be accessed
$cantBeAccessed = true !== $firstFailingStep && $step > $firstFailingStep;

// Only active if current step or step cannot be accessed because of
// previous steps
$isActive = $step === $manager->getCurrentStep() || $cantBeAccessed;

$items[] = [
'isActive' => $isActive,
'class' => 'step_' . $step . (($cantBeAccessed) ? ' forbidden' : ''),
'href' => $manager->getUrlForStep($step),
'title' => $manager->getLabelForStep($step),
'link' => $manager->getLabelForStep($step),
'nofollow' => true
];
}

\Haste\Generator\RowClass::withKey('class')
->addFirstLast()
->addEvenOdd()
->applyTo($items);

return $items;
}
}
4 changes: 3 additions & 1 deletion config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
'FormMPFormPageSwitch' => 'system/modules/mp_forms/FormMPFormPageSwitch.php',
'MPForms' => 'system/modules/mp_forms/MPForms.php',
'MPFormsFormManager' => 'system/modules/mp_forms/MPFormsFormManager.php',
'MPFormsStepsModule' => 'system/modules/mp_forms/MPFormsStepsModule.php',
]);

/**
* Register the templates
*/
TemplateLoader::addFiles([
'form_mp_forms_page_switch' => 'system/modules/mp_forms/templates'
'form_mp_forms_page_switch' => 'system/modules/mp_forms/templates',
'mod_mp_forms_steps' => 'system/modules/mp_forms/templates',
]);
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
$GLOBALS['TL_FFL']['mp_form_pageswitch'] = 'FormMPFormPageSwitch';

/**
* Front end modules
*/
$GLOBALS['FE_MOD']['application']['mp_form_steps'] = 'MPFormsStepsModule';

/**
* Hooks
*/
Expand Down
4 changes: 2 additions & 2 deletions dca/tl_form_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
$manager = new \MPFormsFormManager((int) $dc->activeRecord->pid);
$manager->resetData();
};
$GLOBALS['TL_DCA']['tl_form_field']['palettes']['mp_form_pageswitch'] = '{type_legend},type,mp_forms_backButton,slabel;{image_legend:hide},imageSubmit;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl';
$GLOBALS['TL_DCA']['tl_form_field']['palettes']['mp_form_pageswitch'] = '{type_legend},type,mp_forms_backButton,slabel,label;{image_legend:hide},imageSubmit;{expert_legend:hide},class,accesskey,tabindex;{template_legend:hide},customTpl';

$GLOBALS['TL_DCA']['tl_form_field']['fields']['mp_forms_backButton'] = [
'label' => &$GLOBALS['TL_LANG']['tl_form_field']['mp_forms_backButton'],
'exclude' => true,
'inputType' => 'text',
'eval' => ['tl_class' => 'w50', 'maxlength' => 255, 'mandatory' => true],
'eval' => ['tl_class' => 'clr', 'maxlength' => 255, 'mandatory' => true],
'sql' => "varchar(255) NOT NULL default ''"
];
20 changes: 20 additions & 0 deletions dca/tl_module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* mp_forms extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2015-2016, terminal42 gmbh
* @author terminal42 gmbh <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link https://github.com/terminal42/contao-mp_forms
*/

/**
* Table tl_module
*/
$GLOBALS['TL_DCA']['tl_module']['palettes']['mp_form_steps'] .= '
{title_legend},name,headline,type;
{config_legend},form,navigationTpl;
{template_legend:hide},customTpl;
{protected_legend:hide},protected;
{expert_legend:hide},guests,cssID,space';
15 changes: 15 additions & 0 deletions languages/de/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* mp_forms extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2015-2016, terminal42 gmbh
* @author terminal42 gmbh <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link https://github.com/terminal42/contao-mp_forms
*/

/**
* Front end modules
*/
$GLOBALS['TL_LANG']['FMD']['mp_form_steps'] = ['MPForms - Schritte', 'Zeigt eine Navigation für die Schritte eines mehrseitigen Formulars an.'];
15 changes: 15 additions & 0 deletions languages/en/modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* mp_forms extension for Contao Open Source CMS
*
* @copyright Copyright (c) 2015-2016, terminal42 gmbh
* @author terminal42 gmbh <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL
* @link https://github.com/terminal42/contao-mp_forms
*/

/**
* Front end modules
*/
$GLOBALS['TL_LANG']['FMD']['mp_form_steps'] = ['MPForms - Steps', 'Displays a step navigation for a given multipage form.'];
7 changes: 7 additions & 0 deletions templates/mod_mp_forms_steps.html5
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php $this->extend('block_unsearchable'); ?>

<?php $this->block('content'); ?>

<?= $this->navigation ?>

<?php $this->endblock(); ?>

0 comments on commit b0f17af

Please sign in to comment.