forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsFormHelper.php
More file actions
478 lines (423 loc) · 16.4 KB
/
NetCommonsFormHelper.php
File metadata and controls
478 lines (423 loc) · 16.4 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* NetCommonsFormHelper
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('AppHelper', 'View/Helper');
/**
* NetCommonsFormHelper
*
* @package NetCommons\NetCommons\View\Helper
*/
class NetCommonsFormHelper extends AppHelper {
/**
* Other helpers used by FormHelper
*
* @var array
*/
public $helpers = array(
'Form',
'Html',
'NetCommons.Button',
'NetCommons.NetCommonsHtml',
'NetCommons.DatetimePicker',
'Wysiwyg.Wysiwyg',
);
/**
* @var array タイムゾーン変換対象フィールド
*/
protected $_convertFields = array();
/**
* @var null デフォルトモデル名
*/
protected $_model = null;
/**
* @var array アップロードされたファイルの元ファイル名
*/
protected $_uploadFileNames = array();
/**
* Returns an HTML FORM element.
*
* ### Options:
*
* - `type` Form method defaults to POST
* - `action` The controller action the form submits to, (optional).
* - `url` The URL the form submits to. Can be a string or a URL array. If you use 'url'
* you should leave 'action' undefined.
* - `default` Allows for the creation of Ajax forms. Set this to false to prevent the default event handler.
* Will create an onsubmit attribute if it doesn't not exist. If it does, default action suppression
* will be appended.
* - `onsubmit` Used in conjunction with 'default' to create ajax forms.
* - `inputDefaults` set the default $options for FormHelper::input(). Any options that would
* be set when using FormHelper::input() can be set here. Options set with `inputDefaults`
* can be overridden when calling input()
* - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')`
*
* @param mixed $model The model name for which the form is being defined. Should
* include the plugin name for plugin models. e.g. `ContactManager.Contact`.
* If an array is passed and $options argument is empty, the array will be used as options.
* If `false` no model is used.
* @param array $options An array of html attributes and options.
* @return string A formatted opening FORM tag.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
*/
public function create($model = null, $options = array()) {
$this->_model = $model;
if (!isset($options['ng-submit'])) {
$options['ng-submit'] = 'submit($event)';
}
if (!isset($options['novalidate'])) {
$options['novalidate'] = true;
}
$output = $this->Form->create($model, $options);
if (Hash::get($options, 'type') == 'file') {
$output .= $this->_setupFileUploadForm();
}
return $output;
}
/**
* Filesプラグインのアップロードフォームの準備
*
* 現在添付されてるファイルのID、フィールド名をhidden で埋め込む
*
* @return string
*/
protected function _setupFileUploadForm() {
// setup的な処理と定型のhidden埋め込み
$output = '';
if (isset($this->request->data['UploadFile'])) {
foreach (array_keys($this->request->data['UploadFile']) as $key) {
$output .= $this->input('UploadFile.' . $key . '.id', ['type' => 'hidden']);
$output .= $this->input('UploadFile.' . $key . '.field_name', ['type' => 'hidden']);
$output .= $this->input('UploadFile.' . $key . '.original_name', ['type' => 'hidden']);
}
// uploadされた元ファイル名のリスト
$this->_uploadFileNames = Hash::combine(
$this->request->data['UploadFile'],
'{s}.field_name',
'{s}.original_name'
);
}
return $output;
}
/**
* Overwrite FormHelper::input()
* Generates a form input element complete with label and wrapper div
*
* ### Options
*
* See each field type method for more information. Any options that are part of
* $attributes or $options for the different **type** methods can be included in `$options` for input().i
* Additionally, any unknown keys that are not in the list below, or part of the selected type's options
* will be treated as a regular html attribute for the generated input.
*
* - `type` - Force the type of widget you want. e.g. `type => 'select'`
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label().
* - `div` - Either `false` to disable the div, or an array of options for the div.
* See HtmlHelper::div() for more options.
* - `options` - For widgets that take options e.g. radio, select.
* - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
* error and error messages).
* - `errorMessage` - Boolean to control rendering error messages (field error will still occur).
* - `empty` - String or boolean to enable empty select box options.
* - `before` - Content to place before the label + input.
* - `after` - Content to place after the label + input.
* - `between` - Content to place between the label + input.
* - `format` - Format template for element order. Any element that is not in the array, will not be in the output.
* - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
* - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
* - Hidden input will not be formatted
* - Radio buttons cannot have the order of input and label elements controlled with these settings.
*
* @param string $fieldName This should be "Modelname.fieldname"
* @param array $options Each type of input takes different options.
* @return string Completed form widget.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements
*/
public function input($fieldName, $options = array()) {
if (Hash::get($options, 'type') === 'hidden') {
return $this->hidden($fieldName, $options);
}
$options = $this->DatetimePicker->beforeFormInput($fieldName, $options);
if (Hash::get($options, 'convert_timezone') === true) {
if (strpos($fieldName, '.') === false) {
$fieldName = $this->Form->defaultModel . '.' . $fieldName;
}
$this->_convertFields[] = $fieldName;
}
$options = Hash::merge(array(
'error' => array(),
), $options);
$defaultOptions = array(
'error' => false,
'class' => 'form-control',
'required' => null,
'label' => null,
);
if (Hash::get($options, 'type') === 'number') {
$defaultOptions['min'] = 0;
}
$inputOptions = Hash::merge($defaultOptions, $options);
$inputOptions['error'] = false;
if ($inputOptions['required']) {
if ($inputOptions['label']) {
$inputOptions['label'] .= $this->_View->element('NetCommons.required');
}
unset($inputOptions['required']);
}
$output = '';
$outputStart = '';
$outputEnd = '';
if (!isset($inputOptions['div'])) {
$outputStart = '<div class="form-group">';
$outputEnd = '</div>';
}
$output .= $outputStart;
$output .= $this->Form->input($fieldName, $inputOptions);
if (is_array($options['error'])) {
$output .= $this->error($fieldName, null, $options['error']);
}
$output .= $outputEnd;
return $output;
}
/**
* Overwrite FormHelper::radio()
*
* ### Options
*
* $options = array(
* array('name' => 'United states', 'value' => 'US', 'title' => 'My title'),
* array('name' => 'Germany', 'value' => 'DE', 'class' => 'de-de', 'title' => 'Another title'),
* );
*
* ### Attributes:
*
* - `separator` - define the string in between the radio buttons
* - `between` - the string between legend and input set or array of strings to insert
* strings between each input block
* - `legend` - control whether or not the widget set has a fieldset & legend
* - `value` - indicate a value that is should be checked
* - `label` - boolean to indicate whether or not labels for widgets show be displayed
* - `hiddenField` - boolean to indicate if you want the results of radio() to include
* a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
* - `disabled` - Set to `true` or `disabled` to disable all the radio buttons.
* - `empty` - Set to `true` to create an input with the value '' as the first option. When `true`
* the radio label will be 'empty'. Set this option to a string to control the label value.
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $options Radio button options array.
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
public function radio($fieldName, $options = array(), $attributes = array()) {
$defaultAttributes = array(
'error' => false,
'div' => false,
//'label' => false,
'legend' => false,
);
$attributes = Hash::merge($defaultAttributes, $attributes);
$output = $this->Form->radio($fieldName, $options, $attributes);
return $output;
}
/**
* Overwrite FormHelper::hidden()
*
* 値がfalseの場合、hiddenのvalueが消えてしまい、validationErrorになってしまう。
*
* @param string $fieldName フィールド名, like this "Modelname.fieldname"
* @param array $options hiddenのオプション
* @return string Completed hiddenタグ
* @link https://github.com/cakephp/cakephp/issues/5639
*/
public function hidden($fieldName, $options = array()) {
if (strpos($fieldName, '.')) {
//モデル名あり ex BlogEntry.pdf
$inputFieldName = $fieldName;
} else {
// モデル名ついてない
$modelName = $this->Form->defaultModel;
$inputFieldName = $modelName . '.' . $fieldName;
}
if (Hash::get($this->_View->data, $inputFieldName) === false) {
$options = Hash::merge(array(
'value' => (int)Hash::get($this->_View->data, $inputFieldName),
), $options);
}
$output = $this->Form->hidden($fieldName, $options);
return $output;
}
/**
* Filesプラグイン用のfileフォーム。ファイル削除チェックボックスとファイル名表示付き
*
* @param string $fieldName フィールド名
* @param array $options オプション
* filename => false でフィアル名非表示
* remove => falseで削除チェックボックス非表示。デフォルトはtrue
* @return string inputタグ等
*/
public function uploadFile($fieldName, $options = array()) {
if (strpos($fieldName, '.')) {
//モデル名あり ex BlogEntry.pdf
$inputFieldName = $fieldName;
$fieldName = substr($fieldName, strrpos($fieldName, '.') + 1); //BlogEntry.pdf -> pdf
} else {
// モデル名ついてない
$modelName = $this->Form->defaultModel;
$inputFieldName = $modelName . '.' . $fieldName;
}
$output = '<div class="form-group">';
$defaultOptions = [
'class' => '',
'div' => false,
'remove' => true,
'filename' => true,
];
$options = Hash::merge($defaultOptions, $options, ['type' => 'file']);
$remove = Hash::get($options, 'remove');
Hash::remove($options, 'remove');
$filename = Hash::get($options, 'filename');
Hash::remove($options, 'filename');
$help = $help = Hash::get($options, 'help', false);
Hash::remove($options, 'help');
$output .= $this->input($inputFieldName, $options);
// help-block
if ($help) {
$output .= $this->Html->tag('p', $help, ['class' => 'help-block']);
}
if (isset($this->_uploadFileNames[$fieldName])) {
if ($filename) {
$output .= $this->_uploadFileNames[$fieldName];
}
if ($remove) {
$output .= $this->checkbox(
$inputFieldName . '.remove',
['type' => 'checkbox', 'div' => false, 'error' => false]
);
$output .= $this->Form->label($inputFieldName . '.remove', __d('net_commons', 'Delete'));
}
}
$output .= '</div>';
return $output;
}
/**
* Overwrite FormHelper::checkbox()
*
* ### Options
*
* $options = array(
* array('name' => 'United states', 'value' => 'US', 'title' => 'My title'),
* array('name' => 'Germany', 'value' => 'DE', 'class' => 'de-de', 'title' => 'Another title'),
* );
*
* ### Attributes:
*
* - `separator` - define the string in between the radio buttons
* - `between` - the string between legend and input set or array of strings to insert
* strings between each input block
* - `legend` - control whether or not the widget set has a fieldset & legend
* - `value` - indicate a value that is should be checked
* - `label` - boolean to indicate whether or not labels for widgets show be displayed
* - `hiddenField` - boolean to indicate if you want the results of radio() to include
* a hidden input with a value of ''. This is useful for creating radio sets that non-continuous
* - `disabled` - Set to `true` or `disabled` to disable all the radio buttons.
* - `empty` - Set to `true` to create an input with the value '' as the first option. When `true`
* the radio label will be 'empty'. Set this option to a string to control the label value.
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
public function inlineCheckbox($fieldName, $attributes = array()) {
$defaultAttributes = array(
'error' => false,
'div' => array('class' => 'form-inline'),
'label' => false,
'legend' => false,
);
$inputAttributes = Hash::merge($defaultAttributes, $attributes);
$output = '<div class="form-group">';
$output .= $this->Form->input($fieldName, $inputAttributes);
if (!isset($attributes['error']) || $attributes['error']) {
$output .= $this->error($fieldName);
}
$output .= '</div>';
return $output;
}
/**
* Overwrite FormHelper::input($fieldName, array('type' => 'textarea'))
*
* @param string $fieldName Name of a field, like this "Modelname.fieldname"
* @param array $attributes Array of HTML attributes, and special attributes above.
* @return string Completed radio widget set.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
public function wysiwyg($fieldName, $attributes = array()) {
$ngModel = Hash::expand(array($fieldName => 0));
$ngModel = NetCommonsAppController::camelizeKeyRecursive($ngModel);
$ngModel = Hash::flatten($ngModel);
$ngModel = array_flip($ngModel);
$defaultAttributes = array(
'type' => 'textarea',
'ui-tinymce' => 'tinymce.options',
'ng-model' => $ngModel[0],
'rows' => 5,
);
$attributes = Hash::merge($defaultAttributes, $attributes);
// wysiwygに関連する js読み込みを Wysiwygプラグインから行う
$html = '';
$html .= $this->Wysiwyg->wysiwygScript();
$html .= $this->input($fieldName, $attributes);
return $html;
}
/**
* FormHelperラップ用マジックメソッド。
*
* @param string $method メソッド
* @param array $params パラメータ
* @return mixed
*/
public function __call($method, $params) {
return call_user_func_array(array($this->Form, $method), $params);
}
/**
* Timezone変換の準備を組み込んだForm::end
*
* @param null|array $options オプション
* @param array $secureAttributes secureAttributes
* @return string
*/
public function end($options = null, $secureAttributes = array()) {
$out = '';
$this->DatetimePicker->beforeFormEnd();
// modelをみてdatetime
$out .= $this->Form->hidden('_NetCommonsTime.user_timezone', array('value' => Current::read('User.timezone')));
$out .= $this->Form->hidden('_NetCommonsTime.convert_fields', array('value' => implode(',', $this->_convertFields)));
$this->_convertFields = array();
$out .= $this->Form->end($options, $secureAttributes);
return $out;
}
/**
* Overwrite FormHelper::error()
*
* @param string $fieldName A field name, like "Modelname.fieldname"
* @param string|array $text Error message as string or array of messages.
* If array contains `attributes` key it will be used as options for error container
* @param array $options Rendering options for <div /> wrapper tag
* @return string error html
*/
public function error($fieldName, $text = null, $options = array()) {
$output = '';
$output .= '<div class="has-error">';
$output .= $this->Form->error($fieldName, $text, Hash::merge(array('class' => 'help-block'), $options));
$output .= '</div>';
return $output;
}
}