Skip to content

Commit b5a0c57

Browse files
author
AntikCz
committed
Initial commit
0 parents  commit b5a0c57

36 files changed

Lines changed: 4074 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tests/temp/log
2+
tests/temp/cache

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Rozšiření pro WebChemistry\Forms\Form
2+
3+
## Instalace
4+
5+
**Composer**
6+
```
7+
composer require webchemistry/forms-wizard
8+
```
9+
10+
**Config**
11+
12+
```yaml
13+
extensions:
14+
- WebChemistry\Forms\Controls\DI\WizardExtension
15+
```
16+
17+
## Komponenta
18+
19+
```php
20+
class Wizard extends WebChemistry\Forms\Wizard\Component {
21+
22+
protected function finish() {
23+
$values = $this->getValues();
24+
}
25+
26+
protected function createStep1() {
27+
$form = $this->getForm();
28+
29+
$form->addText('name', 'Uživatelské jméno')
30+
->setRequired();
31+
32+
$form->addSubmit(self::NEXT_SUBMIT_NAME, 'Další');
33+
34+
return $form;
35+
}
36+
37+
protected function createStep2() {
38+
$form = $this->getForm();
39+
40+
$form->addText('email', 'Email')
41+
->setRequired();
42+
43+
$form->addSubmit(self::PREV_SUBMIT_NAME, 'Zpět');
44+
$form->addSubmit(self::FINISH_SUBMIT_NAME, 'Registrovat');
45+
46+
return $form;
47+
}
48+
}
49+
```
50+
51+
## Šablona
52+
53+
```html
54+
<div n:wizard="wizard">
55+
<ul n:if="!$wizard->isSuccess()">
56+
<li n:foreach="$wizard->steps as $step" n:class="$wizard->isDisabled($step) ? disabled, $wizard->isActive($step) ? active"><a n:tag-if="$wizard->useLink($step)" n:href="changeStep! $step">{$step}</a></li>
57+
</ul>
58+
59+
{step 1}
60+
{control $form}
61+
{/step}
62+
63+
{step 2}
64+
{control $form}
65+
{/step}
66+
67+
{step success}
68+
Úspěšně jste se registroval/a.
69+
{/step}
70+
</div>
71+
```

codeception.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
actor: Tester
2+
paths:
3+
tests: tests
4+
log: tests/_output
5+
data: tests/_data
6+
support: tests/_support
7+
envs: tests/_envs
8+
settings:
9+
bootstrap: _bootstrap.php
10+
colors: false
11+
memory_limit: 1024M
12+
reset: true
13+
extensions:
14+
enabled:
15+
- Codeception\Extension\RunFailed
16+
modules:
17+
config:
18+
Db:
19+
dsn: ''
20+
user: ''
21+
password: ''
22+
dump: tests/_data/dump.sql

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "webchemistry/forms-wizard",
3+
"type": "library",
4+
"description": "Component for WebChemistry\\Forms\\Form",
5+
"keywords": ["nette", "webchemistry", "forms", "wizard"],
6+
"require": {
7+
"webchemistry/forms": "@dev"
8+
},
9+
"autoload": {
10+
"classmap": ["src/"]
11+
}
12+
}

src/DI/WizardExtension.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace WebChemistry\Forms\Controls\DI;
4+
5+
use Nette\DI\CompilerExtension;
6+
7+
class WizardExtension extends CompilerExtension {
8+
9+
/**
10+
* Adjusts DI container before is compiled to PHP class. Intended to be overridden by descendant.
11+
*
12+
* @return void
13+
*/
14+
public function beforeCompile() {
15+
$builder = $this->getContainerBuilder();
16+
17+
$builder->getDefinition('nette.latteFactory')
18+
->addSetup('WebChemistry\Forms\Controls\Wizard\Macros::install(?)', array('@self'));
19+
}
20+
21+
}

src/Facade.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace WebChemistry\Forms\Controls\Wizard;
4+
5+
use Nette;
6+
use WebChemistry\Forms\Controls\IWizard;
7+
use WebChemistry\Forms\Form;
8+
9+
class Facade extends Nette\Object {
10+
11+
/** @var IWizard */
12+
private $wizard;
13+
14+
/** @var int */
15+
private $steps = NULL;
16+
17+
/**
18+
* @param IWizard $wizard
19+
*/
20+
public function __construct(IWizard $wizard) {
21+
$this->wizard = $wizard;
22+
}
23+
24+
/**
25+
* @return Form
26+
*/
27+
public function getCurrentComponent() {
28+
return $this->wizard->create();
29+
}
30+
31+
/**
32+
* @param int $step
33+
* @return bool
34+
*/
35+
public function useLink($step) {
36+
return !$this->isDisabled($step) && !$this->isCurrent($step);
37+
}
38+
39+
/**
40+
* @param int $step
41+
* @return bool
42+
*/
43+
public function isCurrent($step) {
44+
return $this->getCurrentStep() === $step;
45+
}
46+
47+
/**
48+
* @return boolean
49+
*/
50+
public function isSuccess() {
51+
return $this->wizard->isSuccess();
52+
}
53+
54+
/**
55+
* @return int
56+
*/
57+
public function getTotalSteps() {
58+
if ($this->steps !== NULL) {
59+
return $this->steps;
60+
}
61+
62+
$isEnd = FALSE;
63+
$iterator = 1;
64+
65+
while (!$isEnd) {
66+
$component = $this->wizard->getComponent('step' . $iterator, FALSE);
67+
68+
if ($component) {
69+
$iterator++;
70+
} else {
71+
$isEnd = TRUE;
72+
}
73+
}
74+
75+
return $this->steps = $iterator - 1;
76+
}
77+
78+
/**
79+
* @return array
80+
*/
81+
public function getSteps() {
82+
return range(1, $this->getTotalSteps());
83+
}
84+
85+
/**
86+
* @return mixed
87+
*/
88+
public function render() {
89+
return $this->wizard->render();
90+
}
91+
92+
/**
93+
* @return int
94+
*/
95+
public function getCurrentStep() {
96+
return $this->wizard->getCurrentStep();
97+
}
98+
99+
/**
100+
* @return int
101+
*/
102+
public function getLastStep() {
103+
return $this->wizard->getLastStep();
104+
}
105+
106+
/**
107+
* @param int $step
108+
* @return bool
109+
*/
110+
public function isActive($step) {
111+
return $step === $this->getCurrentStep();
112+
}
113+
114+
/**
115+
* @param int $step
116+
* @return bool
117+
*/
118+
public function isDisabled($step) {
119+
return $step > $this->getLastStep();
120+
}
121+
}

src/IWizard.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace WebChemistry\Forms\Controls;
4+
5+
use Nette\ComponentModel\IComponent;
6+
use WebChemistry\Forms\Form;
7+
use WebChemistry\Forms\IProvider;
8+
9+
interface IWizard {
10+
11+
const PREV_SUBMIT_NAME = 'prev';
12+
const NEXT_SUBMIT_NAME = 'next';
13+
const FINISH_SUBMIT_NAME = 'finish';
14+
15+
/**
16+
* @return IWizard
17+
*/
18+
public function setProvider(IProvider $provider);
19+
20+
/**
21+
* @return int
22+
*/
23+
public function getCurrentStep();
24+
25+
/**
26+
* @return int
27+
*/
28+
public function getLastStep();
29+
30+
/**
31+
* @param int $step
32+
* @return IWizard
33+
*/
34+
public function setStep($step);
35+
36+
/**
37+
* Returns component specified by name or path.
38+
* @param string
39+
* @param bool throw exception if component doesn't exist?
40+
* @return IComponent|NULL
41+
*/
42+
public function getComponent($name, $need = TRUE);
43+
44+
/**
45+
* @return mixed
46+
*/
47+
public function render();
48+
49+
50+
/**
51+
* @param string $name
52+
* @return Form
53+
*/
54+
public function create($step = NULL);
55+
56+
/**
57+
* @return boolean
58+
*/
59+
public function isSuccess();
60+
}

src/Macros.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace WebChemistry\Forms\Controls\Wizard;
4+
5+
use Latte\CompileException;
6+
use Latte\Engine;
7+
use Latte\Macros\MacroSet;
8+
use Latte\MacroNode;
9+
use Latte\PhpWriter;
10+
use Nette\Bridges\ApplicationLatte\UIMacros;
11+
use Nette\Utils\Strings;
12+
13+
class Macros extends MacroSet {
14+
15+
public static function install(Engine $latte) {
16+
$me = new self($latte->getCompiler());
17+
18+
$me->addMacro('wizard', array($me, 'wizardStart'), array($me, 'wizardEnd'));
19+
$me->addMacro('step', array($me, 'stepStart'), '}');
20+
}
21+
22+
public function wizardStart(MacroNode $node, PhpWriter $writer) {
23+
$words = $node->tokenizer->fetchWords();
24+
if (!$words) {
25+
throw new CompileException('Missing control name in {wizard}');
26+
}
27+
$name = $writer->formatWord($words[0]);
28+
$method = isset($words[1]) ? ucfirst($words[1]) : '';
29+
$method = Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";
30+
$param = $writer->formatArray();
31+
if (!Strings::contains($node->args, '=>')) {
32+
$param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or []
33+
}
34+
35+
return ($name[0] === '$' ? "if (is_object($name)) \$_l->tmp = $name; else " : '')
36+
. '$_l->tmp = $_control->getComponent(' . $name . '); '
37+
. 'if (!$_l->tmp instanceof WebChemistry\Forms\Controls\IWizard) throw new \Exception(\'Wizard must be instance of WebChemistry\Forms\Controls\IWizard\');'
38+
. '$wizard = new WebChemistry\Forms\Controls\Wizard\Facade($_l->tmp);';
39+
}
40+
41+
public function stepStart(MacroNode $node, PhpWriter $writer) {
42+
$word = $node->tokenizer->fetchWord();
43+
if (!is_numeric($word) && !in_array($word, array('success', '"success"', "'success'"))) {
44+
throw new CompileException('First parameter in {step} must be a numeric.');
45+
}
46+
47+
if ($word === 'success') {
48+
return 'if ($wizard->isSuccess()) {';
49+
}
50+
51+
return 'if ($wizard->getCurrentStep() === ' . $word . ' && !$wizard->isSuccess()) { $wizardForm = $form = $wizard->getCurrentComponent(); ';
52+
}
53+
54+
public function wizardEnd() {}
55+
}

0 commit comments

Comments
 (0)