| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Jet_Form_Builder\Admin\Table_Views; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 12 |
|
| 13 |
abstract class Column_Base { |
| 14 |
|
| 15 |
const CHOOSE = 'choose'; |
| 16 |
const ACTIONS = 'actions'; |
| 17 |
const CLASSES = 'classes'; |
| 18 |
const STATUS = 'icon_status'; |
| 19 |
const PRE = 'pre'; |
| 20 |
const LINK = 'link'; |
| 21 |
const STATUS_SUCCESS = 'success'; |
| 22 |
const STATUS_INFO = 'info'; |
| 23 |
const STATUS_WARNING = 'warning'; |
| 24 |
const STATUS_FAILED = 'failed'; |
| 25 |
const STATUS_PENDING = 'pending'; |
| 26 |
|
| 27 |
protected $column = ''; |
| 28 |
protected $type = 'string'; |
| 29 |
|
| 30 |
public function __construct( $column = '' ) { |
| 31 |
$this->set_column( $column ); |
| 32 |
} |
| 33 |
|
| 34 |
final public function set_column( $column ): Column_Base { |
| 35 |
if ( empty( $this->column ) && $column ) { |
| 36 |
$this->column = $column; |
| 37 |
} |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function get_value( array $record = array() ) { |
| 43 |
return $record[ $this->column ] ?? false; |
| 44 |
} |
| 45 |
|
| 46 |
public function get_type( array $record = array() ): string { |
| 47 |
return $this->type; |
| 48 |
} |
| 49 |
|
| 50 |
public function is_editable( array $record = array() ): bool { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
public function get_control( array $record = array() ): string { |
| 55 |
return 'input'; |
| 56 |
} |
| 57 |
|
| 58 |
public function get_control_options( array $record = array() ): array { |
| 59 |
return array( |
| 60 |
'type' => 'text', |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
public function get_options( array $record = array() ): array { |
| 65 |
return array(); |
| 66 |
} |
| 67 |
|
| 68 |
public function get_css_classes( array $record = array() ): array { |
| 69 |
return array(); |
| 70 |
} |
| 71 |
|
| 72 |
final public function get_column( array $record ): array { |
| 73 |
$type = $this->get_type( $record ); |
| 74 |
$value = $this->prepare_value( $this->get_value( $record ), $type ); |
| 75 |
$editable = $this->is_editable( $record ); |
| 76 |
$options = $this->get_options( $record ); |
| 77 |
$classes = $this->get_css_classes( $record ); |
| 78 |
$control = 'input'; |
| 79 |
$control_options = array(); |
| 80 |
|
| 81 |
if ( $editable ) { |
| 82 |
$control = $this->get_control( $record ); |
| 83 |
$control_options = $this->get_control_options( $record ); |
| 84 |
} |
| 85 |
|
| 86 |
return array( |
| 87 |
'type' => $type, |
| 88 |
'value' => $value, |
| 89 |
'editable' => $editable, |
| 90 |
'control' => $control, |
| 91 |
'control_options' => $control_options, |
| 92 |
'classes' => $classes, |
| 93 |
) + $options; |
| 94 |
} |
| 95 |
|
| 96 |
protected function prepare_value( $value, $type ) { |
| 97 |
switch ( $type ) { |
| 98 |
case 'integer': |
| 99 |
return (int) $value; |
| 100 |
case 'price': |
| 101 |
return number_format( $value, 2 ); |
| 102 |
case 'array': |
| 103 |
return (array) $value; |
| 104 |
case 'string': |
| 105 |
if ( is_array( $value ) && array_key_exists( 'text', $value ) ) { |
| 106 |
return (string) $value['text']; |
| 107 |
} |
| 108 |
|
| 109 |
return (string) $value; |
| 110 |
case 'rawArray': |
| 111 |
default: |
| 112 |
return $value; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public function set_type( string $type ): Column_Base { |
| 117 |
$this->type = $type; |
| 118 |
|
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|