summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Web/Form/Validator/NoDuplicateChildrenValidator.php
blob: 9676de032f028846f0e1bc0b45960a1f057dae8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

namespace Icinga\Module\Businessprocess\Web\Form\Validator;

use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Module\Businessprocess\BpNode;
use Icinga\Module\Businessprocess\Forms\EditNodeForm;
use Icinga\Module\Businessprocess\Web\Form\QuickForm;
use Zend_Validate_Abstract;

class NoDuplicateChildrenValidator extends Zend_Validate_Abstract
{
    const CHILD_FOUND = 'childFound';

    /** @var QuickForm */
    protected $form;

    /** @var BpConfig */
    protected $bp;

    /** @var BpNode */
    protected $parent;

    /** @var string */
    protected $label;

    public function __construct(QuickForm $form, BpConfig $bp, BpNode $parent = null)
    {
        $this->form = $form;
        $this->bp = $bp;
        $this->parent = $parent;

        $this->_messageVariables['label'] = 'label';
        $this->_messageTemplates = [
            self::CHILD_FOUND => mt('businessprocess', '%label% is already defined in this process')
        ];
    }

    public function isValid($value)
    {
        if ($this->parent === null) {
            $found = $this->bp->hasRootNode($value);
        } elseif ($this->form instanceof EditNodeForm && $this->form->getNode()->getName() === $value) {
            $found = false;
        } else {
            $found = $this->parent->hasChild($value);
        }

        if (! $found) {
            return true;
        }

        $this->label = $this->form->getElement('children')->getMultiOptions()[$value];
        $this->_error(self::CHILD_FOUND);
        return false;
    }
}