Json.php
5 months ago
JsonColumnFactory.php
5 months ago
JsonListScreenSettingsFactory.php
5 months ago
JsonListScreenSettingsFactory.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Response; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Admin\Banner\BannerContextResolver; |
| 9 | use AC\ListScreen; |
| 10 | use AC\Setting\Encoder; |
| 11 | use AC\Storage\EncoderFactory; |
| 12 | use AC\Type\StartingPrice; |
| 13 | use AC\Type\Url\Preview; |
| 14 | use AC\Type\Url\Site; |
| 15 | use AC\Type\Url\UtmTags; |
| 16 | |
| 17 | class JsonListScreenSettingsFactory |
| 18 | { |
| 19 | |
| 20 | private EncoderFactory $encoder_factory; |
| 21 | |
| 22 | private AC\ColumnTypeRepository $type_repository; |
| 23 | |
| 24 | private AC\ColumnGroups $column_groups; |
| 25 | |
| 26 | private BannerContextResolver $banner_context_resolver; |
| 27 | |
| 28 | public function __construct( |
| 29 | EncoderFactory $encoder_factory, |
| 30 | AC\ColumnTypeRepository $type_repository, |
| 31 | AC\ColumnGroups $column_groups, |
| 32 | BannerContextResolver $banner_context_resolver |
| 33 | ) { |
| 34 | $this->encoder_factory = $encoder_factory; |
| 35 | $this->type_repository = $type_repository; |
| 36 | $this->column_groups = $column_groups; |
| 37 | $this->banner_context_resolver = $banner_context_resolver; |
| 38 | } |
| 39 | |
| 40 | public function create(ListScreen $list_screen, bool $is_stored = true, bool $is_template = false): Json |
| 41 | { |
| 42 | $encoder = $this->encoder_factory |
| 43 | ->create() |
| 44 | ->set_list_screen($list_screen); |
| 45 | |
| 46 | $table_screen = $list_screen->get_table_screen(); |
| 47 | |
| 48 | $context = $this->banner_context_resolver->resolve($table_screen); |
| 49 | |
| 50 | return (new Json())->set_parameters([ |
| 51 | 'read_only' => $list_screen->is_read_only(), |
| 52 | 'table_url' => $is_template |
| 53 | ? (string)new Preview($list_screen->get_table_url(), 'columns') |
| 54 | : (string)$list_screen->get_table_url(), |
| 55 | 'settings' => $encoder->encode(), |
| 56 | 'column_types' => $this->get_column_types($table_screen), |
| 57 | 'column_settings' => $this->encode_column_settings($list_screen->get_columns()), |
| 58 | 'is_stored' => $is_stored, |
| 59 | 'is_template' => $is_template, |
| 60 | 'labels' => [ |
| 61 | 'singular' => $table_screen->get_labels()->get_singular(), |
| 62 | 'plural' => $table_screen->get_labels()->get_plural(), |
| 63 | ], |
| 64 | 'pro_banner' => $context |
| 65 | ? $context->get_arguments($table_screen) |
| 66 | : $this->get_default_banner_arguments($table_screen), |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | private function get_column_types(AC\TableScreen $table_screen): array |
| 71 | { |
| 72 | $column_types = []; |
| 73 | |
| 74 | $original_types = $this->get_original_types($table_screen); |
| 75 | |
| 76 | foreach ($this->type_repository->find_all($table_screen) as $column) { |
| 77 | $group = $this->column_groups->find($column->get_group()); |
| 78 | $label = $this->get_clean_label($column); |
| 79 | |
| 80 | $column_type = [ |
| 81 | 'label' => $label, |
| 82 | 'value' => $column->get_type(), |
| 83 | 'group' => $group ? $group->get_label() : __('Default', 'codepress-admin-columns'), |
| 84 | 'group_key' => $column->get_group(), |
| 85 | 'original' => in_array($column->get_type(), $original_types, true), |
| 86 | 'searchable_label' => $label, |
| 87 | ]; |
| 88 | |
| 89 | if ('custom' !== $column->get_group()) { |
| 90 | $column_type['searchable_label'] .= ' ' . $column_type['group']; |
| 91 | } |
| 92 | |
| 93 | $description = $column->get_description(); |
| 94 | if ($description !== null) { |
| 95 | $column_type['description'] = $description; |
| 96 | } |
| 97 | |
| 98 | $column_types[] = $column_type; |
| 99 | } |
| 100 | |
| 101 | usort($column_types, function ($a, $b) { |
| 102 | // ignore original columns |
| 103 | if ($a['original'] || $b['original']) { |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | return strcasecmp($a['label'], $b['label']); |
| 108 | }); |
| 109 | |
| 110 | return $column_types; |
| 111 | } |
| 112 | |
| 113 | private function get_original_types(AC\TableScreen $table_screen): array |
| 114 | { |
| 115 | $types = []; |
| 116 | foreach ($this->type_repository->find_all_by_original($table_screen) as $column) { |
| 117 | $types[] = $column->get_type(); |
| 118 | } |
| 119 | |
| 120 | return $types; |
| 121 | } |
| 122 | |
| 123 | private function get_clean_label(AC\Column $column): string |
| 124 | { |
| 125 | $label = $column->get_label(); |
| 126 | |
| 127 | if (strip_tags($label) === '') { |
| 128 | $label = ucfirst(str_replace('_', ' ', $column->get_type())); |
| 129 | } |
| 130 | |
| 131 | return strip_tags($label); |
| 132 | } |
| 133 | |
| 134 | private function encode_column_settings(AC\ColumnIterator $columns): array |
| 135 | { |
| 136 | $settings = []; |
| 137 | |
| 138 | foreach ($columns as $column) { |
| 139 | $settings[(string)$column->get_id()] = (new Encoder($column->get_settings()))->encode(); |
| 140 | } |
| 141 | |
| 142 | return $settings; |
| 143 | } |
| 144 | |
| 145 | private function get_default_banner_arguments(AC\TableScreen $table_screen): array |
| 146 | { |
| 147 | $upgrade_url = new UtmTags(Site::create_admin_columns_pro(), 'banner'); |
| 148 | |
| 149 | $plural = $table_screen->get_labels()->get_plural(); |
| 150 | $singular = $table_screen->get_labels()->get_singular(); |
| 151 | |
| 152 | if (mb_strlen($plural) > 30) { |
| 153 | $plural = __('content', 'codepress-admin-columns'); |
| 154 | $singular = __('item', 'codepress-admin-columns'); |
| 155 | } |
| 156 | |
| 157 | $plural_lower = mb_strtolower($plural); |
| 158 | $singular_lower = mb_strtolower($singular); |
| 159 | |
| 160 | return [ |
| 161 | 'title' => sprintf( |
| 162 | __('Manage your %s faster', 'codepress-admin-columns'), |
| 163 | $plural_lower |
| 164 | ), |
| 165 | 'description' => sprintf( |
| 166 | __('Turn your %1$s overview into a workspace for sorting, editing, filtering, and exporting - without opening a single %2$s.', 'codepress-admin-columns'), |
| 167 | $plural_lower, |
| 168 | $singular_lower |
| 169 | ), |
| 170 | 'upgrade_cta' => sprintf(__('Manage your %s faster', 'codepress-admin-columns'), $plural_lower), |
| 171 | 'upgrade_cta_price' => sprintf( |
| 172 | '%s · %s', |
| 173 | sprintf( |
| 174 | /* translators: %s: price (e.g. $79) */ |
| 175 | __('from %s/year', 'codepress-admin-columns'), |
| 176 | StartingPrice::get() |
| 177 | ), |
| 178 | __('all features included', 'codepress-admin-columns') |
| 179 | ), |
| 180 | 'features' => [ |
| 181 | [ |
| 182 | 'url' => $upgrade_url->with_content('usp-editing')->get_url(), |
| 183 | 'label' => __('Inline edit directly in the table', 'codepress-admin-columns'), |
| 184 | ], |
| 185 | [ |
| 186 | 'url' => $upgrade_url->with_content('usp-sorting')->get_url(), |
| 187 | 'label' => __('Sort and filter on any column', 'codepress-admin-columns'), |
| 188 | ], |
| 189 | [ |
| 190 | 'url' => $upgrade_url->with_content('usp-bulk-edit')->get_url(), |
| 191 | 'label' => sprintf( |
| 192 | __('Bulk edit hundreds of %s at once', 'codepress-admin-columns'), |
| 193 | $plural_lower |
| 194 | ), |
| 195 | ], |
| 196 | [ |
| 197 | 'url' => $upgrade_url->with_content('usp-export')->get_url(), |
| 198 | 'label' => __('Export table data to CSV', 'codepress-admin-columns'), |
| 199 | ], |
| 200 | [ |
| 201 | 'url' => $upgrade_url->with_content('usp-column-sets')->get_url(), |
| 202 | 'label' => __('Multiple views per screen', 'codepress-admin-columns'), |
| 203 | ], |
| 204 | ], |
| 205 | 'promo_url' => $upgrade_url->get_url(), |
| 206 | ]; |
| 207 | } |
| 208 | |
| 209 | } |