-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMultidatabaseContentEditElementHelper.php
More file actions
216 lines (195 loc) · 6 KB
/
MultidatabaseContentEditElementHelper.php
File metadata and controls
216 lines (195 loc) · 6 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
<?php
/**
* MultidatabaseContentEditElementHelper Helper
* 汎用データベースコンテンツ編集 フォームエレメントヘルパー
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Tomoyuki OHNO (Ricksoft Co., Ltd.) <ohno.tomoyuki@ricksoft.jp>
* @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');
/**
* MultidatabaseContentEditElementHelper Helper
*
* @author Tomoyuki OHNO (Ricksoft, Co., LTD.) <ohno.tomoyuki@ricksoft.jp>
* @package NetCommons\Multidatabase\View\Helper
*
*/
class MultidatabaseContentEditElementHelper extends AppHelper {
/**
* 使用するHelpers
*
* @var array
*/
public $helpers = [
'NetCommons.Button',
'NetCommons.NetCommonsHtml',
'NetCommons.NetCommonsForm',
];
/**
* フォーム部品を出力する
*
* @param array $metadata メタデータ
* @param array $options オプション
* @param null|array $content コンテンツデータ
* @return string HTML
*/
public function renderFormElement($metadata, $options = [], $content = []) {
$name = 'MultidatabaseContent.value' . $metadata['col_no'];
$options['id'] = $name;
$options['label'] = $metadata['name'];
if ((int)$metadata['is_require'] === 1) {
$options['required'] = true;
}
$elementType = $metadata['type'];
switch ($elementType) {
case 'textarea':
return $this->__renderFormElementTextArea($name, $options);
case 'wysiwyg':
$options['rows'] = 12;
return $this->__renderFormElementWysiwyg($name, $options);
case 'date':
return $this->__renderFormElementDate($name, $options);
case 'hidden':
return $this->__renderFormElementHidden($name, $options);
}
if (in_array($elementType, ['select', 'checkbox'])) {
$options['options'] = $this->__cnvMetaSelToArr($metadata['selections']);
return $this->__renderFormElementSelect($name, $options, $elementType);
}
if (in_array($elementType, ['file', 'image'])) {
return $this->__renderFormElementFile($name, $options, $metadata, $content);
}
if (in_array($elementType, ['text', 'link', 'mail'])) {
return $this->__renderFormElementText($name, $options);
}
return $this->__renderFormElementReadOnly($name, $options);
}
/**
* 値を出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementReadOnly($name, $options = []) {
if (isset($options['value'])) {
return $options['value'];
}
return '';
}
/**
* テキストボックスを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementText($name, $options = []) {
$options['type'] = 'text';
return $this->NetCommonsForm->input($name, $options);
}
/**
* WYSIWYGを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementWysiwyg($name, $options = []) {
return $this->NetCommonsForm->wysiwyg($name, $options);
}
/**
* テキストエリアを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementTextArea($name, $options = []) {
$options['type'] = 'textarea';
return $this->NetCommonsForm->input($name, $options);
}
/**
* セレクトボックス・チェックボックスを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @param string $elementType エレメントタイプ
* @return string HTML
*/
private function __renderFormElementSelect($name, $options = [], $elementType = '') {
if ($elementType == 'checkbox') {
$options += [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $options,
'class' => 'checkbox-inline nc-checkbox',
];
} else {
$options['type'] = 'select';
}
return $this->NetCommonsForm->input($name, $options);
}
/**
* デートピッカー対応のテキストボックスを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementDate($name, $options = []) {
$options['type'] = 'datetime';
$netCommonsTime = new NetCommonsTime();
$value = Hash::get($this->request->data, $name);
// 空 or DateTime型以外は、空セット
if (empty($value)) {
// datetimepickerは、falseにして空表示。falseにしないと値を現在日時が表示される
$options['value'] = false;
} elseif (! $netCommonsTime->isDatetime($value)) {
$options['value'] = false;
}
return $this->NetCommonsForm->input($name, $options);
}
/**
* ファイルアップロードエレメントを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @param array $metadata メタデータ配列
* @param array $content コンテンツ配列
* @return string HTML
*/
private function __renderFormElementFile($name, $options = [], $metadata = [], $content = []) {
$options['remove'] = false;
$options['filename'] = false;
$result = $this->NetCommonsForm->uploadFile($name, $options);
return $result;
}
/**
* 隠し属性のエレメントを出力する
*
* @param string $name フィールド名(key)
* @param array $options オプション
* @return string HTML
*/
private function __renderFormElementHidden($name, $options = []) {
$options['type'] = 'hidden';
return $this->NetCommonsForm->input($name, $options);
}
/**
* メタデータ選択肢の値を配列に変換する
*
* @param array $metaSelections メタデータの選択肢の値
* @return array
*/
private function __cnvMetaSelToArr($metaSelections) {
$result = [];
foreach ($metaSelections as $metaSelection) {
$result[md5($metaSelection)] = $metaSelection;
}
return $result;
}
}