ConditionalFormWidget   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 55
c 0
b 0
f 0
ccs 0
cts 22
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A begin() 0 7 1
A init() 0 7 2
A run() 0 6 2
A widget() 0 4 1
1
<?php
2
/**
3
 * HiPanel tickets module
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-ticket
6
 * @package   hipanel-module-ticket
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\ticket\widgets;
12
13
use yii\base\InvalidCallException;
14
use yii\base\Widget;
15
use yii\widgets\ActiveForm;
16
17
/**
18
 * Class ConditionalFormWidget is designed for the following use case in ticket module:
19
 * On ticket create page all fields must be wrapped in single form,
20
 * however on the view page each block must be wrapped in a separate form in order to prevent
21
 * logical conflicts. With this widget we can use the same view files for both pages.
22
 *
23
 * Use example:
24
 *
25
 * ```php
26
 * // Use `$form` without any changes, or creates new one with `options`
27
 * $form = ConditionalFormWidget::begin([
28
 *     'form' => isset($form) ? $form : null,
29
 *     'options' => [
30
 *         'action'  => $action,
31
 *         'options' => ['enctype' => 'multipart/form-data'],
32
 *     ]
33
 * ]);
34
 *
35
 * $form->field($model, 'input');
36
 *
37
 * ConditionalFormWidget::end(); // Do not use $form->end() here!
38
 * ```
39
 *
40
 * IMPORTANT: the `::begin()` method returns instance of [[ActiveForm]] instead of this widget object.
41
 * Is is by design and done in order to give transparent variables.
42
 *
43
 * This class must be used only with `::begin()` and `::end()` methods.
44
 *
45
 * @author Dmitry Naumenko <[email protected]>
46
 */
47
class ConditionalFormWidget extends Widget
48
{
49
    /**
50
     * @var ActiveForm
51
     */
52
    public $form;
53
54
    /**
55
     * @var array options that will be used to create [[$form]] object
56
     */
57
    public $options;
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $closeForm = false;
63
64
    /**
65
     * @param array $config
66
     * @return ActiveForm
67
     */
68
    public static function begin($config = [])
69
    {
70
        /** @var $widget static */
71
        $widget = parent::begin($config);
72
73
        return $widget->form;
74
    }
75
76
    /** {@inheritdoc} */
77
    public function init()
78
    {
79
        if (!isset($this->form)) {
80
            $this->closeForm = true;
81
            $this->form = ActiveForm::begin($this->options);
82
        }
83
    }
84
85
    /** {@inheritdoc} */
86
    public function run()
87
    {
88
        if ($this->closeForm) {
89
            $this->form->end();
90
        }
91
    }
92
93
    /**
94
     * @throws InvalidCallException
95
     * @internal
96
     */
97
    public static function widget($config = [])
98
    {
99
        throw new InvalidCallException('Class must be used with `::begin()` and `::end()` methods.');
100
    }
101
}
102