-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMultidatabaseContentViewElementHelper.php
More file actions
344 lines (313 loc) · 9.67 KB
/
MultidatabaseContentViewElementHelper.php
File metadata and controls
344 lines (313 loc) · 9.67 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
<?php
/**
* MultidatabaseContentViewElementHelper 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');
App::uses('NetCommonsTime', 'NetCommons.Utility');
/**
* MultidatabaseContentViewElementHelper Helper
*
* @author Tomoyuki OHNO (Ricksoft, Co., LTD.) <ohno.tomoyuki@ricksoft.jp>
* @package NetCommons\Multidatabase\View\Helper
*
*/
class MultidatabaseContentViewElementHelper extends AppHelper {
/**
* 使用するHelpers
*
* @var array
*/
public $helpers = [
'NetCommons.Button',
'NetCommons.NetCommonsHtml',
'NetCommons.NetCommonsForm',
'Form',
];
/**
* フォーム部品に合った表示出力する
*
* @param array $content コンテンツ配列
* @param array $metadata メタデータ配列
* @param string $elementType 部品タイプ
* @return string HTML
*/
public function renderViewElement($content, $metadata, $elementType = null) {
if (is_null($elementType)) {
$elementType = $metadata['type'];
}
$colNo = $metadata['col_no'];
if (
in_array($elementType, ['date', 'created', 'updated'])
) {
return $this->__renderViewElementDate($content, $colNo, $elementType);
}
switch ($elementType) {
case 'link':
return $this->__renderViewElementLink($content, $colNo);
case 'wysiwyg':
return $this->__renderViewElementWysiwyg($content, $colNo);
case 'file':
return $this->__renderViewElementFile($content, $colNo,
$metadata['is_visible_file_dl_counter']);
case 'image':
return $this->__renderViewElementImage($content, $colNo);
case 'autonumber':
return $this->__renderViewElementAutoNumber($content, $colNo);
case 'mail':
return $this->__renderViewElementEmail($content, $colNo);
default:
// text, textarea, checkbox, select, hiddenはこちらの処理
return $this->__renderViewElementGeneral($content, $colNo);
}
}
/**
* 汎用的な値出力方法
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementGeneral($content, $colNo) {
$value = (string)trim(h($content['MultidatabaseContent']['value' . $colNo]));
if ($value === '') {
return '';
}
if (strstr($value, '||') <> false) {
return str_replace('||', '<br>', $value);
}
$value = nl2br($value);
return $value;
}
/**
* WYSIWYGの値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementWysiwyg($content, $colNo) {
return $content['MultidatabaseContent']['value' . $colNo];
}
/**
* 日付の値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @param string $type 種別(created, updated, その他)
* @return string HTML
*/
private function __renderViewElementDate($content, $colNo, $type = null) {
$netCommonsTime = new NetCommonsTime();
switch ($type) {
case 'created':
// 作成日時を出力
return $netCommonsTime->toUserDatetime($content['MultidatabaseContent']['created']);
case 'updated':
// 更新日時を出力
return $netCommonsTime->toUserDatetime($content['MultidatabaseContent']['modified']);
default:
$value = $this->__renderViewElementGeneral($content, $colNo);
// 空 or DateTime型以外は、空セット
if (empty($value)) {
return '';
} elseif (! $netCommonsTime->isDatetime($value)) {
return '';
}
return $netCommonsTime->toUserDatetime($value);
}
}
/**
* ファイルアップロードの値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @param int $showCounter カウンターを表示するか 1:表示する
* @return string HTML
*/
private function __renderViewElementFile($content, $colNo, $showCounter = 0) {
// アップロードされたファイルのリンクを表示&パスワード入力ダイアログ
if (! $fileInfo = $this->__getFileInfo($content, $colNo)) {
return '';
}
$ContentFile = ClassRegistry::init('Multidatabases.MultidatabaseContentFile');
if (! $ContentFile->getAuthKey(
$content['MultidatabaseContent']['id'], 'value' . $colNo)
) {
$fileUrl = $this->__fileDlUrl($content, $colNo);
$result = '<span class="glyphicon glyphicon-file text-primary"></span> ';
$result .= '<a href="' . $fileUrl . '" target="_blank">';
// 表示が全てダウンロードとなる。ファイル名を表示する
//$result .= __d('multidatabases', 'Download');
$result .= h($fileInfo['UploadFile']['original_name']);
$result .= '</a>';
} else {
$result = $this->__renderViewElementFileReqAuth($content, $colNo, $fileInfo);
}
if ((int)$showCounter === 1) {
$result .= ' <span class="badge">';
$result .= $fileInfo['UploadFile']['total_download_count'];
$result .= '</span>';
}
return $result;
}
/**
* ファイルアップロードの値を出力する(認証が必要な場合)
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @param array $fileInfo ダウンロードファイル
* @return string HTML
*/
private function __renderViewElementFileReqAuth($content, $colNo, $fileInfo) {
// 認証キー必要
$result = '<span class="glyphicon glyphicon-file text-primary"></span> ';
$result .= $this->NetCommonsHtml->link(
// 表示が全てダウンロードとなる。ファイル名を表示する
//__d('multidatabases', 'Download'),
$fileInfo['UploadFile']['original_name'],
'#',
[
'authorization-keys-popup-link',
'url' => NetCommonsUrl::actionUrl($this->__fileDlArrayReqAuth($content, $colNo)),
'frame-id' => Current::read('Frame.id'),
'popup-title' => __d('authorization_keys', 'Authorization key confirm dialog'),
'popup-label' => __d('authorization_keys', 'Authorization key'),
'popup-placeholder' =>
__d('authorization_keys', 'Please input authorization key'),
]
);
return $result;
}
/**
* 画像用のファイルアップロードの値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementImage($content, $colNo) {
// アップロードされた画像を表示
if (! $this->__getFileInfo($content, $colNo)) {
return '';
}
$fileUrl = $this->__fileDlUrl($content, $colNo);
$result = '<img src="' . $fileUrl . '" alt="" style="max-width:100%">';
return $result;
}
/**
* リンクの値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementLink($content, $colNo) {
$value = $this->__renderViewElementGeneral($content, $colNo);
$result = '<a href="' . $value . '" target="_blank">' . $value . '</a>';
return $result;
}
/**
* メールアドレスの値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementEmail($content, $colNo) {
$value = $this->__renderViewElementGeneral($content, $colNo);
$result = '<a href="mailto:' . $value . '">' . $value . '</a>';
return $result;
}
/**
* 自動採番の値を出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __renderViewElementAutoNumber($content, $colNo) {
// 自動採番のフィールドを作成してここに表示させる
return $this->__renderViewElementGeneral($content, $colNo);
}
/**
* ファイルダウンロードURL出力用の配列を返す
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return array
*/
private function __fileDlArray($content, $colNo) {
return [
'controller' => 'multidatabase_contents',
'action' => 'download',
$content['MultidatabaseContent']['key'],
$content['MultidatabaseContent']['id'],
'?' => ['col_no' => $colNo]
];
}
/**
* ファイルダウンロードURL出力用の配列を返す(認証あり)
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return array
*/
private function __fileDlArrayReqAuth($content, $colNo) {
return [
'controller' => 'multidatabase_contents',
'action' => 'download',
Current::read('Block.id'),
$content['MultidatabaseContent']['key'],
$content['MultidatabaseContent']['id'],
'?' => [
'col_no' => $colNo,
'frame_id' => Current::read('Frame.id')
]
];
}
/**
* ファイルダウンロードURLを出力する
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return string HTML
*/
private function __fileDlUrl($content, $colNo) {
return $this->NetCommonsHtml->url(
$this->__fileDlArray($content, $colNo)
);
}
/**
* ダウンロードファイルが存在するかチェックする
*
* @param array $content コンテンツ配列
* @param int $colNo カラムNo
* @return bool|array
*/
private function __getFileInfo($content, $colNo) {
if (
empty($content['MultidatabaseContent']['id']) ||
empty($colNo)
) {
return false;
}
$UploadFile = ClassRegistry::init('Files.UploadFile');
$pluginKey = 'multidatabases';
$file = $UploadFile->getFile(
$pluginKey,
$content['MultidatabaseContent']['id'],
'value' . $colNo . '_attach'
);
if (! empty($file)) {
return $file;
}
return false;
}
}