forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableListHelper.php
More file actions
246 lines (213 loc) · 5.76 KB
/
TableListHelper.php
File metadata and controls
246 lines (213 loc) · 5.76 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
<?php
/**
* テーブルリスト用Helper
*
* @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');
/**
* テーブルリスト用Helper
*
* @package NetCommons\NetCommons\View\Helper
*/
class TableListHelper extends AppHelper {
/**
* 使用するHelper
*
* @var array
*/
public $helpers = array(
'NetCommons.Button',
'NetCommons.Date',
'NetCommons.LinkButton',
'NetCommons.MessageFlash',
'NetCommons.NetCommonsForm',
'NetCommons.NetCommonsHtml',
'Users.DisplayUser',
);
/**
* 各プラグインのHelperラップ用マジックメソッド
*
* 指定されたメソッドにより、各プラグインのHelperのメソッドを呼び出します。
*
* @param string $method メソッド
* @param array $params パラメータ
* @return mixed
*/
public function __call($method, $params) {
if ($method === 'addLink') {
$helper = $this->Button;
$html = '<div class="text-right nc-table-add">';
$html .= call_user_func_array(array($helper, $method), $params);
$html .= '</div>';
} else {
$helper = $this->NetCommonsForm;
$html = call_user_func_array(array($helper, $method), $params);
}
return $html;
}
/**
* `<table>`を表示する
*
* @param string $tableType テーブル種別 `table-hover`,`table-striped`,`table-bordered`,`table-condensed`
* @return string HTML
* @see http://getbootstrap.com/css/#tables
*/
public function startTable($tableType = 'table-hover') {
$html = '';
if ($tableType) {
$tableType = ' ' . $tableType;
}
$html .= '<div class="table-responsive">';
$html .= '<table class="table' . $tableType . '">';
return $html;
}
/**
* `</table>`を表示する
*
* @return string HTML
*/
public function endTable() {
$html = '';
$html .= '</table>';
$html .= '</div>';
return $html;
}
/**
* `<tr>`を表示する
*
* @param int $currentId カレントのID
* @param string $fieldName フィールド名(Model.field)
* @return string HTML
*/
public function startTableRow($currentId = null, $fieldName = null) {
if (isset($fieldName) &&
Hash::get($this->_View->request->data, $fieldName, false) === $currentId) {
$active = ' class="active"';
} else {
$active = '';
}
$html = '<tr' . $active . '>';
return $html;
}
/**
* `</tr>`を表示する
*
* @return string HTML
*/
public function endTableRow() {
$html = '</tr>';
return $html;
}
/**
* `<th>...</th>`を表示する
*
* @param string $fieldName フィールド名(Model.field)
* @param string $title タイトル
* @param array $options オプション
* - type
* - false
* - text
* - datetime
* - numeric
* - center
* - right
* - link
* - handle
* - sort<br>
* ソートを表示するかどうか
* @return string HTML
*/
public function tableHeader($fieldName, $title = '', $options = array()) {
$type = Hash::get($options, 'type', 'text');
if (in_array($type, ['text', 'datetime', 'numeric', 'center', 'right', 'link', 'handle'])) {
$start = '<th class="nc-table-' . $type . '">';
} else {
$start = '<th>';
}
if (Hash::get($options, 'sort', false)) {
$title = $this->_View->Paginator->sort($fieldName, $title);
}
$end = '</th>';
$output = $start . $title . $end;
if (Hash::get($options, 'editUrl', false)) {
$output .= '<th></th>';
}
return $output;
}
/**
* `<td>...</td>`を表示する
*
* @param string $fieldName フィールド名(Model.field)<br>
* ただし、$option['handle']の場合は、Model名(TrackableCreatorなど)をセットする
* @param string|array $value 値
* @param array $options オプション
* - type
* - false
* - text
* - datetime
* - numeric
* - center
* - right
* - link リンクを付ける
* - handle ハンドルを表示する
* - escape
* - editUrl<br>
* 編集ボタンを付与させる
* - format(数値の場合のみ有効)<br>
* 出力のフォーマット。詳しくは、[__dn()](http://book.cakephp.org/2.0/ja/core-libraries/global-constants-and-functions.html#__dn)
* - domain
* - singular
* - plural
* @return string HTML
*/
public function tableData($fieldName, $value = '', $options = array()) {
$type = Hash::get($options, 'type', 'text');
if ($type === 'datetime') {
$start = '<td class="nc-table-' . $type . '">';
$value = $this->Date->dateFormat($value);
} elseif ($type === 'link') {
$start = '<td class="nc-table-' . $type . '">';
$value = $this->NetCommonsHtml->link(
$value, $value, array('target' => '_blank')
);
$options = Hash::insert($options, 'escape', false);
} elseif ($type === 'handle') {
$start = '<td class="nc-table-' . $type . '">';
$value = $this->DisplayUser->handleLink($value, ['avatar' => true], [], $fieldName);
$options = Hash::insert($options, 'escape', false);
} elseif (in_array($type, ['text', 'numeric', 'center', 'right'])) {
$start = '<td class="nc-table-' . $type . '">';
} else {
$start = '<td>';
}
if (Hash::get($options, 'format')) {
$value = sprintf(
__dn(
Hash::get($options, 'format.domain', 'net_commons'),
Hash::get($options, 'format.singular', '%d'),
Hash::get($options, 'format.plural', '%d'),
(int)$value
),
(int)$value
);
}
if (Hash::get($options, 'escape', true)) {
$value = h($value);
}
$end = '</td>';
$output = $start . $value . $end;
if (Hash::get($options, 'editUrl', false)) {
$output .= '<td>';
$output .= $this->LinkButton->edit(
'', Hash::get($options, 'editUrl', []), ['iconSize' => ' btn-xs']
);
$output .= '</td>';
}
return $output;
}
}