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