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