CustomersController.php
2 years ago
CustomersListTable.php
3 years ago
CustomersScriptsController.php
3 years ago
CustomersController.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Admin\Customers; |
| 4 | |
| 5 | use SureCart\Controllers\Admin\AdminController; |
| 6 | use SureCart\Models\Product; |
| 7 | use SureCartCore\Responses\RedirectResponse; |
| 8 | use SureCart\Controllers\Admin\Customers\CustomersListTable; |
| 9 | |
| 10 | /** |
| 11 | * Handles product admin requests. |
| 12 | */ |
| 13 | class CustomersController extends AdminController { |
| 14 | |
| 15 | /** |
| 16 | * Products index. |
| 17 | */ |
| 18 | public function index() { |
| 19 | $table = new CustomersListTable(); |
| 20 | $table->prepare_items(); |
| 21 | $this->withHeader( |
| 22 | [ |
| 23 | 'customers' => [ |
| 24 | 'title' => __( 'Customers', 'surecart' ), |
| 25 | ], |
| 26 | ] |
| 27 | ); |
| 28 | return \SureCart::view( 'admin/customers/index' )->with( [ 'table' => $table ] ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Customers edit. |
| 33 | */ |
| 34 | public function edit( $request ) { |
| 35 | // enqueue needed script. |
| 36 | add_action( 'admin_enqueue_scripts', \SureCart::closure()->method( CustomersScriptsController::class, 'enqueue' ) ); |
| 37 | |
| 38 | $this->preloadPaths( |
| 39 | [ |
| 40 | '/wp/v2/users/me', |
| 41 | '/wp/v2/types?context=view', |
| 42 | '/wp/v2/types?context=edit', |
| 43 | '/surecart/v1/customers/' . $request->query( 'id' ) . '?context=edit&expand%5B0%5D=balances', |
| 44 | ] |
| 45 | ); |
| 46 | |
| 47 | // return view. |
| 48 | return '<div id="app"></div>'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Change the archived attribute in the model |
| 53 | * |
| 54 | * @param \SureCartCore\Requests\RequestInterface $request Request. |
| 55 | * @return function |
| 56 | */ |
| 57 | public function toggleArchive( $request ) { |
| 58 | $product = Product::find( $request->query( 'id' ) ); |
| 59 | |
| 60 | if ( is_wp_error( $product ) ) { |
| 61 | \SureCart::flash()->add( 'errors', $product->get_error_message() ); |
| 62 | return $this->redirectBack( $request ); |
| 63 | } |
| 64 | |
| 65 | $updated = $product->update( |
| 66 | [ |
| 67 | 'archived' => ! $product->archived, |
| 68 | ] |
| 69 | ); |
| 70 | |
| 71 | if ( is_wp_error( $updated ) ) { |
| 72 | \SureCart::flash()->add( 'errors', $updated->get_error_message() ); |
| 73 | return $this->redirectBack( $request ); |
| 74 | } |
| 75 | |
| 76 | \SureCart::flash()->add( |
| 77 | 'success', |
| 78 | $updated->archived ? __( 'Product archived.', 'surecart' ) : __( 'Product restored.', 'surecart' ) |
| 79 | ); |
| 80 | |
| 81 | return $this->redirectBack( $request ); |
| 82 | } |
| 83 | |
| 84 | public function redirectBack( $request ) { |
| 85 | return ( new RedirectResponse( $request ) )->back(); |
| 86 | } |
| 87 | } |
| 88 |