Completed
Push — master ( 5ceb59...4f9424 )
by Cheren
02:03
created

FormHelper::_addDefaultContextProviders()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 1
nop 0
1
<?php
2
/**
3
 * CakeCMS Core
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   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\View\Helper;
17
18
use Cake\Form\Form;
19
use Cake\View\View;
20
use Cake\Utility\Hash;
21
use Cake\Core\Configure;
22
use Core\View\Form\FormContext;
23
use Cake\Collection\Collection;
24
use Core\View\Form\ArrayContext;
25
use Core\View\Form\EntityContext;
26
use Cake\Datasource\EntityInterface;
27
use Core\View\Helper\Traits\HelperTrait;
28
use Cake\View\Helper\FormHelper as CakeFormHelper;
29
30
/**
31
 * Class FormHelper
32
 *
33
 * @package Core\View\Helper
34
 * @property \Core\View\Helper\UrlHelper $Url
35
 * @property \Core\View\Helper\HtmlHelper $Html
36
 */
37
class FormHelper extends CakeFormHelper
38
{
39
40
    use HelperTrait;
41
42
    /**
43
     * List of helpers used by this helper.
44
     *
45
     * @var array
46
     */
47
    public $helpers = [
48
        'Url'  => ['className' => 'Core.Url'],
49
        'Html' => ['className' => 'Core.Html'],
50
    ];
51
52
    /**
53
     * Hold js form type.
54
     *
55
     * @var bool
56
     */
57
    protected $_isJsForm = false;
58
59
    /**
60
     * HtmlHelper constructor.
61
     *
62
     * @param View $View
63
     * @param array $config
64
     */
65
    public function __construct(View $View, array $config = [])
66
    {
67
        parent::__construct($View, $config);
68
        $this->_configWrite('btnPref', Configure::read('Cms.btnPref'));
69
        $this->_configWrite('iconPref', Configure::read('Cms.iconPref'));
70
        $this->_configWrite('classPrefix', Configure::read('Cms.classPrefix'));
71
    }
72
73
    /**
74
     * Creates a `<button>` tag.
75
     *
76
     * @param string $title
77
     * @param array $options
78
     * @return string
79
     */
80
    public function button($title, array $options = [])
81
    {
82
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
83
        $options = $this->_getBtnClass($options);
84
85
        list($title, $options) = $this->_createIcon($this->Html, $title, $options);
86
87
        return parent::button($title, $options);
88
    }
89
90
    /**
91
     * Input check all.
92
     *
93
     * @return string
94
     */
95
    public function checkAll()
96
    {
97
        return $this->input('check-all', ['type' => 'checkbox', 'class' => 'jsCheckAll']);
98
    }
99
100
    /**
101
     * Create html form.
102
     *
103
     * @param mixed $model
104
     * @param array $options
105
     * @return string
106
     */
107
    public function create($model = null, array $options = [])
108
    {
109
        $options += ['process' => false, 'jsForm' => false];
110
        $options = $this->addClass($options, $this->_class('form'));
111
112
        $isProcess = $options['process'];
113
114
        if ($isProcess != false) {
115
            $_options = [
116
                'url' => [
117
                    'plugin'     => $this->request->param('plugin'),
118
                    'controller' => $this->request->param('controller'),
119
                    'action'     => 'process'
120
                ]
121
            ];
122
123
            $options['jsForm'] = true;
124
            $options = Hash::merge($_options, $options);
125
        }
126
127
        $isJsForm = $options['jsForm'];
128
        if ($isJsForm) {
129
            $this->_isJsForm = true;
130
            $options = $this->addClass($options, 'jsForm');
131
        }
132
133
        unset($options['process']);
134
        unset($options['jsForm']);
135
136
        return parent::create($model, $options);
137
    }
138
139
    /**
140
     * End html form.
141
     *
142
     * @param array $secureAttributes
143
     * @return string
144
     */
145
    public function end(array $secureAttributes = [])
146
    {
147
        if ($this->_isJsForm) {
148
            return implode('', [
149
                $this->hidden('action', ['value' => '', 'class' => 'jsFormAction']),
150
                parent::end($secureAttributes)
151
            ]);
152
        }
153
154
        return parent::end($secureAttributes);
155
    }
156
157
    /**
158
     * Table row process checkbox.
159
     *
160
     * @param string $name
161
     * @param string $type
162
     * @return string
163
     */
164
    public function processCheck($type, $name)
165
    {
166
        return $this->input($type . '.id.' . $name, ['type' => 'checkbox']);
167
    }
168
169
    /**
170
     * Add the default suite of context providers provided.
171
     *
172
     * @return void
173
     */
174
    protected function _addDefaultContextProviders()
175
    {
176
        $this->addContextProvider('orm', function ($request, $data) {
177
            if (is_array($data['entity']) || $data['entity'] instanceof \Traversable) {
178
                $pass = (new Collection($data['entity']))->first() !== null;
179
                if ($pass) {
180
                    return new EntityContext($request, $data);
181
                }
182
            }
183
184
            return $this->_addEntityContent($request, $data);
185
        });
186
187
        $this->_addFormContextProvider();
188
        $this->_addFormArrayProvider();
189
    }
190
191
    /**
192
     * Add the entity suite of context providers provided.
193
     *
194
     * @param $request
195
     * @param $data
196
     * @return EntityContext
197
     */
198
    protected function _addEntityContent($request, $data)
199
    {
200
        if ($data['entity'] instanceof EntityInterface) {
201
            return new EntityContext($request, $data);
202
        }
203
204
        if (is_array($data['entity']) && empty($data['entity']['schema'])) {
205
            return new EntityContext($request, $data);
206
        }
207
    }
208
209
    /**
210
     * Add the array suite of context providers provided.
211
     *
212
     * @return void
213
     */
214
    protected function _addFormArrayProvider()
215
    {
216
        $this->addContextProvider('array', function ($request, $data) {
217
            if (is_array($data['entity']) && isset($data['entity']['schema'])) {
218
                return new ArrayContext($request, $data['entity']);
219
            }
220
        });
221
    }
222
223
    /**
224
     * Add the form suite of context providers provided.
225
     *
226
     * @return void
227
     */
228
    protected function _addFormContextProvider()
229
    {
230
        $this->addContextProvider('form', function ($request, $data) {
231
            if ($data['entity'] instanceof Form) {
232
                return new FormContext($request, $data);
233
            }
234
        });
235
    }
236
}
237