class.wpexcelcolumn.php
9.13 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
<?php
defined('ABSPATH') or die('Access denied.');
/**
* Created by PhpStorm.
* User: Milos Roksandic
* Date: 23.2.16.
* Time: 19.48
*/
class WDTExcelColumn {
private $wdtColumn = null;
public function __construct( WDTColumn $wdtColumn ) {
$this->wdtColumn = $wdtColumn;
}
/**
* Overloading methods that are not defined in this class.
* It will call corresponding method of WDTColumn object
*
* @param $name
* @param $arguments
*/
public function __call($name, $arguments) {
call_user_func_array(
array($this->wdtColumn, $name),
$arguments
);
}
// overrides
public function returnCellValue( $cellContent ) {
$cellValue = apply_filters( 'wpdatatables_excel_filter_cell_val', $cellContent );
return $cellValue;
}
// overrides
public static function generateColumn( $wdtColumnType = 'string', $properties = array( ) ) {
$column = WDTColumn::generateColumn( $wdtColumnType, $properties );
$excelColumn = new WDTExcelColumn( $column );
return $excelColumn;
}
// overrides
public function getColumnJSON() {
$colJsDefinition = $this->getCellTypeProps();
$colJsDefinition->title = $this->wdtColumn->getTitle();
$colJsDefinition->data = $this->wdtColumn->getOriginalHeader();
$colJsDefinition->defaultValue = $this->wdtColumn->getFilterDefaultValue();
$colJsDefinition->className = $this->wdtColumn->getCSSClasses();
if( $this->wdtColumn->getDataType() == 'float' ) {
$decimal_places = (int) (get_option('wdtDecimalPlaces'));
if ( $decimal_places > 0 ) {
$format = '0,0.' . str_repeat( '0', $decimal_places );
$colJsDefinition->format = $format;
}
} else if( $this->wdtColumn->getDataType() == 'formula' ) {
$colJsDefinition->readOnly = true;
}
$cell_editor_type = $this->getEditorType();
if( $cell_editor_type == 'wdt.dropdown' || $cell_editor_type == 'wdt.multi-select' ) {
if ($this->wdtColumn->getPossibleValuesType() === 'read') {
$colJsDefinition->source = WDTColumn::getPossibleValuesRead($this->wdtColumn, false, false);
} elseif ($this->wdtColumn->getPossibleValuesType() === 'list') {
$colJsDefinition->source = $this->wdtColumn->getPossibleValuesList();
}
}
if( $cell_editor_type == 'wdt.date' || $cell_editor_type == 'date') {
$colJsDefinition->allowEmpty = true;
unset($colJsDefinition->defaultValue);
}
if( ($cell_editor_type == 'wdt.date' || $cell_editor_type == 'date')
&& !empty( $colJsDefinition->defaultValue )) {
//TODO: check if default value is a valid date
$colJsDefinition->defaultDate = $colJsDefinition->defaultValue;
}
$colJsDefinition->search = $this->wdtColumn->isSearchable();
if( !$this->wdtColumn->isVisible() ) {
$colJsDefinition->readOnly = true;//don't want to allow editing of hidden columns(to actually be able to hide column need to purchase PRO version)
}
if($this->wdtColumn->getWidth() != 'auto'){
$colJsDefinition->width = $this->wdtColumn->getWidth();
}
$colJsDefinition = apply_filters( 'wpdatatables_excel_filter_column_js_definition', $colJsDefinition, $this->wdtColumn->getTitle() );
return $colJsDefinition;
}
/**
* Returning cell editor type based on set wpdt editor input type.
* Returns false if there is column has no editor.
* Also prevents wrong combinations of column types and editors.
*
* @return bool|string
*/
public function getEditorType() {
$editor_type = false;
switch( $this->wdtColumn->getInputType() ) {
case 'none':
$editor_type = false;
break;
case 'text':
case 'link':
case 'email':
$editor_type = 'text';
break;
case 'textarea':
$editor_type = 'wdt.text_multiline';
break;
case 'date':
$editor_type = 'wdt.date';
break;
case 'datetime':
$editor_type = 'wdt.datetime';
break;
case 'time':
$editor_type = 'wdt.time';
break;
case 'selectbox':
$editor_type = 'wdt.dropdown';
break;
case 'multi-selectbox':
$editor_type = 'wdt.multi-select';
break;
case 'attachment':
$editor_type = 'wdt.attachment';
break;
}
$renderer_type = $this->getRendererType();
//prevent errors that might be caused by wrong combination of column types and renderers
if( $editor_type != false ) {
switch( $renderer_type ) {
case 'numeric':
if( !in_array( $editor_type, array('numeric', 'wdt.dropdown', 'wdt.multi-select') ) ) {
$editor_type = 'numeric';
}
break;
case 'wdt.date':
$editor_type = 'wdt.date';
break;
case 'wdt.link':
if( !in_array( $editor_type, array('text', 'wdt.attachment', 'wdt.dropdown', 'wdt.multi-select') ) ) {
$editor_type = 'text';
}
break;
case 'wdt.email':
if( !in_array( $editor_type, array('text', 'wdt.dropdown', 'wdt.multi-select') ) ) {
$editor_type = 'text';
}
break;
case 'wdt.image':
if( !in_array( $editor_type, array('text', 'wdt.attachment', 'wdt.dropdown', 'wdt.multi-select') ) ) {
$editor_type = 'wdt.attachment';
}
break;
}
}
return $editor_type;
}
/**
* Returning cell renderer type based on set wpdt column type.
* @return string
*/
public function getRendererType() {
switch( $this->wdtColumn->getDataType() ) {
case 'string':
return 'text';
case 'int':
case 'float':
return 'numeric';//built in validator and editor
case 'date':
return 'wdt.date';
case 'datetime':
return 'wdt.datetime';
case 'time':
return 'wdt.time';
case 'link':
return 'wdt.link';
case 'email':
return 'wdt.email';
case 'icon':
return 'wdt.image';
default:
return 'text';
}
}
/**
* Returns cell validator type based on determined cell renderer type.
* @return null|string
*/
public function getValidatorType() {
switch( $this->getRendererType() ) {
case 'text':
return null;
case 'numeric':
return 'numeric';//builtin validator
case 'wdt.date':
return 'wdt.date';
case 'wdt.link':
return 'wdt.link';
case 'wdt.email':
return 'wdt.email';
case 'wdt.image':
return 'wdt.link';
default:
return null;
}
}
/**
* Creating cell definition properties based on determined renderer, editor and validator types.
* @return stdClass
*/
public function getCellTypeProps() {
$renderer_type = $this->getRendererType();
$editor_type = $this->getEditorType();
$validator_type = $this->getValidatorType();
$cellProps = new stdClass();//type, renderer, validator
//no editor
if( $editor_type == false ) {
$cellProps->editor = $editor_type;
}
if( $editor_type == 'wdt.dropdown' ) {
$cellProps->type = 'dropdown';//using built-in validator of dropdown cell type
} else if( $editor_type == 'wdt.multi-select' ) {
$cellProps->type = 'wdt.multi-select';
}
if( $renderer_type == 'wdt.date' ) {
$cellProps->type = 'wdt.date';
} else if( $renderer_type == 'wdt.datetime' ) {
$cellProps->type = 'wdt.datetime';
} else if( $renderer_type == 'wdt.time' ) {
$cellProps->type = 'wdt.time';
} else if( $renderer_type == 'numeric' && ($editor_type == 'numeric' || $editor_type == false ) ) {
$cellProps->type = 'numeric';
} else if ( $renderer_type == 'text' && ($editor_type == 'text' || $editor_type == false ) ) {
$cellProps->type = 'text';
} else {
$cellProps->renderer = $renderer_type;
$cellProps->editor = $editor_type;
if ( !empty($validator_type) ) {
$cellProps->validator = $validator_type;
}
}
return $cellProps;
}
}