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