| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die("Cannot access pages directly."); |
| 4 |
|
| 5 |
/** |
| 6 |
* Created by PhpStorm. |
| 7 |
* User: Milos Roksandic |
| 8 |
* Date: 23.2.16. |
| 9 |
* Time: 19.48 |
| 10 |
*/ |
| 11 |
class WDTExcelColumn { |
| 12 |
private $wdtColumn = null; |
| 13 |
|
| 14 |
public function __construct( WDTColumn $wdtColumn ) { |
| 15 |
$this->wdtColumn = $wdtColumn; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Overloading methods that are not defined in this class. |
| 20 |
* It will call corresponding method of WDTColumn object |
| 21 |
* |
| 22 |
* @param $name |
| 23 |
* @param $arguments |
| 24 |
*/ |
| 25 |
public function __call($name, $arguments) { |
| 26 |
return call_user_func_array( |
| 27 |
array($this->wdtColumn, $name), |
| 28 |
$arguments |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
// overrides |
| 33 |
public function returnCellValue( $cellContent ) { |
| 34 |
$cellValue = apply_filters( 'wpdatatables_excel_filter_cell_val', $cellContent ); |
| 35 |
return $cellValue; |
| 36 |
} |
| 37 |
|
| 38 |
// overrides |
| 39 |
public static function generateColumn( $wdtColumnType = 'string', $properties = array( ) ) { |
| 40 |
$column = WDTColumn::generateColumn( $wdtColumnType, $properties ); |
| 41 |
|
| 42 |
$excelColumn = new WDTExcelColumn( $column ); |
| 43 |
|
| 44 |
return $excelColumn; |
| 45 |
} |
| 46 |
|
| 47 |
// overrides |
| 48 |
public function getColumnJSON() { |
| 49 |
$colJsDefinition = $this->getCellTypeProps(); |
| 50 |
|
| 51 |
$colJsDefinition->title = $this->wdtColumn->getTitle(); |
| 52 |
$colJsDefinition->data = $this->wdtColumn->getOriginalHeader(); |
| 53 |
$colJsDefinition->defaultValue = $this->wdtColumn->getFilterDefaultValue(); |
| 54 |
$colJsDefinition->className = $this->wdtColumn->getCSSClasses(); |
| 55 |
|
| 56 |
if( $this->wdtColumn->getDataType() == 'float' ) { |
| 57 |
$decimal_places = (int) (get_option('wdtDecimalPlaces')); |
| 58 |
if ( $decimal_places > 0 ) { |
| 59 |
$format = '0,0.' . str_repeat( '0', $decimal_places ); |
| 60 |
$colJsDefinition->format = $format; |
| 61 |
} |
| 62 |
} else if( $this->wdtColumn->getDataType() == 'formula' ) { |
| 63 |
$colJsDefinition->readOnly = true; |
| 64 |
} |
| 65 |
|
| 66 |
$cell_editor_type = $this->getEditorType(); |
| 67 |
|
| 68 |
if( $cell_editor_type == 'wdt.dropdown' || $cell_editor_type == 'wdt.multi-select' ) { |
| 69 |
if ($this->wdtColumn->getPossibleValuesType() === 'read') { |
| 70 |
$colJsDefinition->source = WDTColumn::getColumnDistinctValues($this->wdtColumn); |
| 71 |
} elseif ($this->wdtColumn->getPossibleValuesType() === 'list') { |
| 72 |
$colJsDefinition->source = $this->wdtColumn->getPossibleValues(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if( $cell_editor_type == 'wdt.date' || $cell_editor_type == 'date') { |
| 77 |
$colJsDefinition->allowEmpty = true; |
| 78 |
unset($colJsDefinition->defaultValue); |
| 79 |
} |
| 80 |
|
| 81 |
if( ($cell_editor_type == 'wdt.date' || $cell_editor_type == 'date') |
| 82 |
&& !empty( $colJsDefinition->defaultValue )) { |
| 83 |
//TODO: check if default value is a valid date |
| 84 |
$colJsDefinition->defaultDate = $colJsDefinition->defaultValue; |
| 85 |
} |
| 86 |
|
| 87 |
$colJsDefinition->search = $this->wdtColumn->isSearchable(); |
| 88 |
|
| 89 |
if( !$this->wdtColumn->isVisible() ) { |
| 90 |
$colJsDefinition->readOnly = true;//don't want to allow editing of hidden columns(to actually be able to hide column need to purchase PRO version) |
| 91 |
} |
| 92 |
|
| 93 |
if($this->wdtColumn->getWidth() != 'auto'){ |
| 94 |
$colJsDefinition->width = $this->wdtColumn->getWidth(); |
| 95 |
} |
| 96 |
|
| 97 |
$colJsDefinition = apply_filters( 'wpdatatables_excel_filter_column_js_definition', $colJsDefinition, $this->wdtColumn->getTitle() ); |
| 98 |
return $colJsDefinition; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Returning cell editor type based on set wpdt editor input type. |
| 104 |
* Returns false if there is column has no editor. |
| 105 |
* Also prevents wrong combinations of column types and editors. |
| 106 |
* |
| 107 |
* @return bool|string |
| 108 |
*/ |
| 109 |
public function getEditorType() { |
| 110 |
$editor_type = false; |
| 111 |
|
| 112 |
switch( $this->wdtColumn->getInputType() ) { |
| 113 |
case 'none': |
| 114 |
$editor_type = false; |
| 115 |
break; |
| 116 |
case 'text': |
| 117 |
case 'link': |
| 118 |
case 'email': |
| 119 |
$editor_type = 'text'; |
| 120 |
break; |
| 121 |
case 'textarea': |
| 122 |
$editor_type = 'wdt.text_multiline'; |
| 123 |
break; |
| 124 |
case 'date': |
| 125 |
$editor_type = 'wdt.date'; |
| 126 |
break; |
| 127 |
case 'datetime': |
| 128 |
$editor_type = 'wdt.datetime'; |
| 129 |
break; |
| 130 |
case 'time': |
| 131 |
$editor_type = 'wdt.time'; |
| 132 |
break; |
| 133 |
case 'selectbox': |
| 134 |
$editor_type = 'wdt.dropdown'; |
| 135 |
break; |
| 136 |
case 'multi-selectbox': |
| 137 |
$editor_type = 'wdt.multi-select'; |
| 138 |
break; |
| 139 |
case 'attachment': |
| 140 |
$editor_type = 'wdt.attachment'; |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
$renderer_type = $this->getRendererType(); |
| 145 |
|
| 146 |
//prevent errors that might be caused by wrong combination of column types and renderers |
| 147 |
if( $editor_type != false ) { |
| 148 |
switch( $renderer_type ) { |
| 149 |
case 'numeric': |
| 150 |
if( !in_array( $editor_type, array('numeric', 'wdt.dropdown', 'wdt.multi-select') ) ) { |
| 151 |
$editor_type = 'numeric'; |
| 152 |
} |
| 153 |
break; |
| 154 |
case 'wdt.date': |
| 155 |
$editor_type = 'wdt.date'; |
| 156 |
break; |
| 157 |
case 'wdt.link': |
| 158 |
if( !in_array( $editor_type, array('text', 'wdt.attachment', 'wdt.dropdown', 'wdt.multi-select') ) ) { |
| 159 |
$editor_type = 'text'; |
| 160 |
} |
| 161 |
break; |
| 162 |
case 'wdt.email': |
| 163 |
if( !in_array( $editor_type, array('text', 'wdt.dropdown', 'wdt.multi-select') ) ) { |
| 164 |
$editor_type = 'text'; |
| 165 |
} |
| 166 |
break; |
| 167 |
case 'wdt.image': |
| 168 |
if( !in_array( $editor_type, array('text', 'wdt.attachment', 'wdt.dropdown', 'wdt.multi-select') ) ) { |
| 169 |
$editor_type = 'wdt.attachment'; |
| 170 |
} |
| 171 |
break; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $editor_type; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Returning cell renderer type based on set wpdt column type. |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public function getRendererType() { |
| 184 |
switch( $this->wdtColumn->getDataType() ) { |
| 185 |
case 'string': |
| 186 |
return 'text'; |
| 187 |
case 'int': |
| 188 |
case 'float': |
| 189 |
return 'numeric';//built in validator and editor |
| 190 |
case 'date': |
| 191 |
return 'wdt.date'; |
| 192 |
case 'datetime': |
| 193 |
return 'wdt.datetime'; |
| 194 |
case 'time': |
| 195 |
return 'wdt.time'; |
| 196 |
case 'link': |
| 197 |
return 'wdt.link'; |
| 198 |
case 'email': |
| 199 |
return 'wdt.email'; |
| 200 |
case 'icon': |
| 201 |
return 'wdt.image'; |
| 202 |
default: |
| 203 |
return 'text'; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Returns cell validator type based on determined cell renderer type. |
| 209 |
* @return null|string |
| 210 |
*/ |
| 211 |
public function getValidatorType() { |
| 212 |
switch( $this->getRendererType() ) { |
| 213 |
case 'text': |
| 214 |
return null; |
| 215 |
case 'numeric': |
| 216 |
return 'numeric';//builtin validator |
| 217 |
case 'wdt.date': |
| 218 |
return 'wdt.date'; |
| 219 |
case 'wdt.link': |
| 220 |
return 'wdt.link'; |
| 221 |
case 'wdt.email': |
| 222 |
return 'wdt.email'; |
| 223 |
case 'wdt.image': |
| 224 |
return 'wdt.link'; |
| 225 |
default: |
| 226 |
return null; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Creating cell definition properties based on determined renderer, editor and validator types. |
| 232 |
* @return stdClass |
| 233 |
*/ |
| 234 |
public function getCellTypeProps() { |
| 235 |
$renderer_type = $this->getRendererType(); |
| 236 |
$editor_type = $this->getEditorType(); |
| 237 |
$validator_type = $this->getValidatorType(); |
| 238 |
|
| 239 |
$cellProps = new stdClass();//type, renderer, validator |
| 240 |
|
| 241 |
//no editor |
| 242 |
if( $editor_type == false ) { |
| 243 |
$cellProps->editor = $editor_type; |
| 244 |
} |
| 245 |
|
| 246 |
if( $editor_type == 'wdt.dropdown' ) { |
| 247 |
$cellProps->type = 'dropdown';//using built-in validator of dropdown cell type |
| 248 |
} else if( $editor_type == 'wdt.multi-select' ) { |
| 249 |
$cellProps->type = 'wdt.multi-select'; |
| 250 |
} |
| 251 |
|
| 252 |
if( $renderer_type == 'wdt.date' ) { |
| 253 |
$cellProps->type = 'wdt.date'; |
| 254 |
} else if( $renderer_type == 'wdt.datetime' ) { |
| 255 |
$cellProps->type = 'wdt.datetime'; |
| 256 |
} else if( $renderer_type == 'wdt.time' ) { |
| 257 |
$cellProps->type = 'wdt.time'; |
| 258 |
} else if( $renderer_type == 'numeric' && ($editor_type == 'numeric' || $editor_type == false ) ) { |
| 259 |
$cellProps->type = 'numeric'; |
| 260 |
} else if ( $renderer_type == 'text' && ($editor_type == 'text' || $editor_type == false ) ) { |
| 261 |
$cellProps->type = 'text'; |
| 262 |
} else { |
| 263 |
$cellProps->renderer = $renderer_type; |
| 264 |
$cellProps->editor = $editor_type; |
| 265 |
|
| 266 |
if ( !empty($validator_type) ) { |
| 267 |
$cellProps->validator = $validator_type; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
return $cellProps; |
| 272 |
} |
| 273 |
} |