Completed
Push — master ( 97a922...38b0c2 )
by Cheren
05:46
created

FormHelper::file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
/**
3
 * CakeCMS Backend
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Backend
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Backend".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Backend\View\Helper;
17
18
use JBZoo\Utils\Arr;
19
use Cake\Core\Exception\Exception;
20
use Backend\View\Helper\Traits\PrepareHelpers;
21
use Core\View\Helper\FormHelper as CoreFormHelper;
22
23
/**
24
 * Class FormHelper
25
 *
26
 * @package Backend\View\Helper
27
 */
28
class FormHelper extends CoreFormHelper
29
{
30
31
    use PrepareHelpers;
32
33
    /**
34
    * List of helpers used by this helper.
35
    *
36
    * @var array
37
    */
38
    public $helpers = [
39
        'Url'  => ['className' => 'Core.Url'],
40
        'Html' => ['className' => 'Backend.Html']
41
    ];
42
43
    /**
44
     * Creates file input widget.
45
     *
46
     * @param string $fieldName Name of a field, in the form "modelname.fieldname"
47
     * @param array $options Array of HTML attributes.
48
     *
49
     * @return string A generated file input.
50
     */
51
    public function file($fieldName, array $options = [])
52
    {
53
        $content = parent::file($fieldName, $options);
54
        $options = $this->_parseOptions($fieldName, $options);
55
56
        $options['type'] = __FUNCTION__;
57
58
        $result = $this->_inputContainerTemplate([
59
            'error'       => null,
60
            'errorSuffix' => null,
61
            'content'     => $content,
62
            'options'     => $options
63
        ]);
64
65
        return $result;
66
    }
67
68
    /**
69
     * Constructor hook method.
70
     *
71
     * @param array $config
72
     * @throws Exception
73
     */
74
    public function initialize(array $config)
75
    {
76
        $this->_configWrite('prepareBtnClass', function (FormHelper $form, $options, $button) {
77
            return $this->_prepareBtn($form, $options, $button);
78
        });
79
80
        $this->_configWrite('templates', 'Backend.templates/form');
81
82
        parent::initialize($config);
83
    }
84
85
    /**
86
     * Form switcher.
87
     *
88
     * @param string $fieldName
89
     * @param array $options
90
     * @return string
91
     */
92
    public function switcher($fieldName, array $options = [])
93
    {
94
        $input = parent::checkbox($fieldName, $options);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (checkbox() instead of switcher()). Are you sure this is correct? If so, you might want to change this to $this->checkbox().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
95
96
        $options += [
97
            'before' => __d('backend', 'Off'),
98
            'after'  => __d('backend', 'On')
99
        ];
100
101
        $title = (Arr::key('title', $options)) ? $options['title'] : $fieldName;
102
103
        if (!empty($title)) {
104
            $title = $this->Html->div('switch-title', $title);
105
        }
106
107
        $content = $this->formatTemplate(__FUNCTION__, [
108
            'input'  => $input,
109
            'title'  => $title,
110
            'after'  => $options['after'],
111
            'before' => $options['before'],
112
            'lever'  => '<span class="lever"></span>'
113
        ]);
114
115
        return $content;
116
    }
117
}
118