Column.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Tables\Columns; |
| 4 | |
| 5 | use IAWP\Plugin_Group_Option; |
| 6 | use IAWP\Tables\Groups\Group; |
| 7 | /** @internal */ |
| 8 | class Column implements Plugin_Group_Option |
| 9 | { |
| 10 | private $id; |
| 11 | private $name; |
| 12 | private $plugin_group; |
| 13 | private $plugin_group_header; |
| 14 | private $visible; |
| 15 | private $type; |
| 16 | private $exportable; |
| 17 | private $options; |
| 18 | private $filter_placeholder; |
| 19 | private $unavailable_for; |
| 20 | private $database_column; |
| 21 | private $is_nullable; |
| 22 | private $is_plugin_active; |
| 23 | public function __construct($attributes) |
| 24 | { |
| 25 | $this->id = $attributes['id']; |
| 26 | $this->name = $attributes['name']; |
| 27 | $this->plugin_group = $attributes['plugin_group'] ?? 'general'; |
| 28 | $this->plugin_group_header = $attributes['plugin_group_header'] ?? null; |
| 29 | $this->visible = $attributes['visible'] ?? \false; |
| 30 | $this->type = $attributes['type']; |
| 31 | $this->exportable = $attributes['exportable'] ?? \true; |
| 32 | $this->options = $attributes['options'] ?? []; |
| 33 | $this->filter_placeholder = $attributes['filter_placeholder'] ?? ''; |
| 34 | $this->unavailable_for = $attributes['unavailable_for'] ?? []; |
| 35 | $this->database_column = $attributes['database_column'] ?? null; |
| 36 | $this->is_nullable = $attributes['is_nullable'] ?? \false; |
| 37 | $this->is_plugin_active = $attributes['is_subgroup_plugin_active'] ?? \true; |
| 38 | } |
| 39 | public function is_enabled_for_group(Group $group) : bool |
| 40 | { |
| 41 | return !\in_array($group->id(), $this->unavailable_for); |
| 42 | } |
| 43 | public function is_group_dependent() : bool |
| 44 | { |
| 45 | return \count($this->unavailable_for) > 0; |
| 46 | } |
| 47 | public function id() : string |
| 48 | { |
| 49 | return $this->id; |
| 50 | } |
| 51 | public function name() : string |
| 52 | { |
| 53 | return $this->name; |
| 54 | } |
| 55 | public function plugin_group() : string |
| 56 | { |
| 57 | return $this->plugin_group; |
| 58 | } |
| 59 | public function database_column() : string |
| 60 | { |
| 61 | return !\is_null($this->database_column) ? $this->database_column : $this->id; |
| 62 | } |
| 63 | public function is_group_plugin_enabled() : bool |
| 64 | { |
| 65 | switch ($this->plugin_group) { |
| 66 | case "ecommerce": |
| 67 | return \IAWPSCOPED\iawp()->is_ecommerce_support_enabled(); |
| 68 | case "forms": |
| 69 | return \IAWPSCOPED\iawp()->is_form_submission_support_enabled(); |
| 70 | default: |
| 71 | return \true; |
| 72 | } |
| 73 | } |
| 74 | public function is_subgroup_plugin_enabled() : bool |
| 75 | { |
| 76 | return $this->is_plugin_active; |
| 77 | } |
| 78 | public function is_visible() : bool |
| 79 | { |
| 80 | return $this->visible; |
| 81 | } |
| 82 | public function is_member_of_plugin_group(string $plugin_group) : bool |
| 83 | { |
| 84 | return $this->plugin_group === $plugin_group; |
| 85 | } |
| 86 | public function plugin_group_header() : ?string |
| 87 | { |
| 88 | return $this->plugin_group_header; |
| 89 | } |
| 90 | public function type() : string |
| 91 | { |
| 92 | return $this->type; |
| 93 | } |
| 94 | /** |
| 95 | * @return string[] |
| 96 | */ |
| 97 | public function filter_operators() : array |
| 98 | { |
| 99 | switch ($this->type) { |
| 100 | case 'string': |
| 101 | return ['contains', 'exact']; |
| 102 | case 'date': |
| 103 | return ['before', 'after', 'on']; |
| 104 | case 'select': |
| 105 | return ['is', 'isnt']; |
| 106 | default: |
| 107 | // int |
| 108 | return ['greater', 'lesser', 'equal']; |
| 109 | } |
| 110 | } |
| 111 | public function is_valid_filter_operator(string $operator) : bool |
| 112 | { |
| 113 | return \in_array($operator, $this->filter_operators()); |
| 114 | } |
| 115 | public function sort_direction() : string |
| 116 | { |
| 117 | $descending_types = ['int', 'date']; |
| 118 | return \in_array($this->type, $descending_types) ? 'desc' : 'asc'; |
| 119 | } |
| 120 | public function set_visibility(bool $visible) : void |
| 121 | { |
| 122 | $this->visible = $visible; |
| 123 | } |
| 124 | public function exportable() : bool |
| 125 | { |
| 126 | return $this->exportable; |
| 127 | } |
| 128 | /** |
| 129 | * @return array List of possible options for this filter such as a list of authors or list of post categories |
| 130 | */ |
| 131 | public function options() : array |
| 132 | { |
| 133 | return $this->options; |
| 134 | } |
| 135 | public function filter_placeholder() : string |
| 136 | { |
| 137 | return $this->filter_placeholder; |
| 138 | } |
| 139 | public function is_nullable() : bool |
| 140 | { |
| 141 | return $this->is_nullable; |
| 142 | } |
| 143 | } |
| 144 |