AdminGeneralOptionsGet.php
3 days ago
AdminGeneralOptionsPersist.php
3 days ago
CustomFieldKeys.php
3 days ago
EditorMenuFavorites.php
3 days ago
EditorMenuStatus.php
3 days ago
ExtendedValue.php
3 days ago
IntegrationToggle.php
3 days ago
Integrations.php
3 days ago
ListScreenAddColumn.php
3 days ago
ListScreenDelete.php
3 days ago
ListScreenOriginalColumns.php
3 days ago
ListScreenSave.php
3 days ago
ListScreenSelectColumn.php
3 days ago
ListScreenSettings.php
3 days ago
NetworkPostStati.php
3 days ago
NumberFormat.php
3 days ago
RestoreSettingsRequest.php
3 days ago
ScreenOptions.php
3 days ago
ListScreenOriginalColumns.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\RequestHandler\Ajax; |
| 6 | |
| 7 | use AC\Capabilities; |
| 8 | use AC\Column; |
| 9 | use AC\ColumnCollection; |
| 10 | use AC\ColumnTypeRepository; |
| 11 | use AC\DefaultColumnHandler; |
| 12 | use AC\Nonce; |
| 13 | use AC\Request; |
| 14 | use AC\RequestAjaxHandler; |
| 15 | use AC\Response; |
| 16 | use AC\Setting\Encoder; |
| 17 | use AC\TableScreenFactory\Aggregate; |
| 18 | use AC\Type\TableId; |
| 19 | |
| 20 | class ListScreenOriginalColumns implements RequestAjaxHandler |
| 21 | { |
| 22 | private Aggregate $table_screen_factory; |
| 23 | |
| 24 | private ColumnTypeRepository $column_type_repository; |
| 25 | |
| 26 | private DefaultColumnHandler $default_column_handler; |
| 27 | |
| 28 | public function __construct( |
| 29 | Aggregate $table_screen_factory, |
| 30 | ColumnTypeRepository $column_type_repository, |
| 31 | DefaultColumnHandler $default_column_handler |
| 32 | ) { |
| 33 | $this->table_screen_factory = $table_screen_factory; |
| 34 | $this->column_type_repository = $column_type_repository; |
| 35 | $this->default_column_handler = $default_column_handler; |
| 36 | } |
| 37 | |
| 38 | public function handle(): void |
| 39 | { |
| 40 | if (! current_user_can(Capabilities::MANAGE)) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $request = new Request(); |
| 45 | $response = new Response\Json(); |
| 46 | |
| 47 | if (! (new Nonce\Ajax())->verify($request)) { |
| 48 | $response->error(); |
| 49 | } |
| 50 | |
| 51 | $list_key = new TableId($request->get('list_key')); |
| 52 | |
| 53 | if (! $this->table_screen_factory->can_create($list_key)) { |
| 54 | $response->error(); |
| 55 | } |
| 56 | |
| 57 | $table_screen = $this->table_screen_factory->create($list_key); |
| 58 | |
| 59 | $default_columns = $this->default_column_handler->handle( |
| 60 | $table_screen, |
| 61 | $this->column_type_repository->find_all_original($table_screen) |
| 62 | ); |
| 63 | |
| 64 | $response->set_parameter('columns', $this->get_columns($default_columns)); |
| 65 | $response->set_parameter('config', $this->get_config($default_columns)); |
| 66 | $response->success(); |
| 67 | } |
| 68 | |
| 69 | private function get_name(Column $column): string |
| 70 | { |
| 71 | return (string)$column->get_id(); |
| 72 | } |
| 73 | |
| 74 | private function get_config(ColumnCollection $columns): array |
| 75 | { |
| 76 | $settings = []; |
| 77 | |
| 78 | foreach ($columns as $column) { |
| 79 | $settings[$this->get_name($column)] = (new Encoder($column->get_settings()))->encode(); |
| 80 | } |
| 81 | |
| 82 | return $settings; |
| 83 | } |
| 84 | |
| 85 | private function get_columns(ColumnCollection $columns): array |
| 86 | { |
| 87 | $abstracts = []; |
| 88 | |
| 89 | foreach ($columns as $column) { |
| 90 | $abstracts[] = [ |
| 91 | 'type' => $column->get_type(), |
| 92 | 'label' => $column->get_label(), |
| 93 | 'name' => $this->get_name($column), |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | return $abstracts; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 |