Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/CrudPanelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function setupCrudPanel(string $controller, ?string $operation = null): C
$crud->setOperation($operation);

$primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest();
if (! $crud->isInitialized()) {
if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) {
self::setActiveController($controller::class);
$crud->initialized = false;
$controller->initializeCrudPanel($primaryControllerRequest, $crud);
self::unsetActiveController();
$crud = $this->cruds[$controller::class];
Expand Down Expand Up @@ -106,6 +107,14 @@ public function getInitializedOperations(string $controller): array
return $this->initializedOperations[$controller] ?? [];
}

/**
* Check if a specific operation has been initialized for a controller.
*/
public function isOperationInitialized(string $controller, string $operation): bool
{
return in_array($operation, $this->getInitializedOperations($controller), true);
}

/**
* Store a CrudPanel instance for a specific controller.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/app/Http/Controllers/Operations/CreateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public function create()
$this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add').' '.$this->crud->entity_name;

// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getCreateView(), $this->data);
return request()->ajax() ?
view('crud::components.form.ajax_response', $this->data) :
view($this->crud->getCreateView(), $this->data);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/app/Http/Controllers/Operations/UpdateOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public function edit($id)
$this->data['id'] = $id;

// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getEditView(), $this->data);
return request()->ajax() ?
view('crud::components.form.ajax_response', $this->data) :
view($this->crud->getEditView(), $this->data);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/app/Library/CrudPanel/CrudButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public function section($stack)
* The HTML itself of the button.
*
* @param object|null $entry The eloquent Model for the current entry or null if no current entry.
* @param CrudPanel|null $crud The CrudPanel object, if not passed it will be retrieved from the service container.
* @return \Illuminate\Contracts\View\View
*/
public function getHtml($entry = null, ?CrudPanel $crud = null)
Expand Down
61 changes: 61 additions & 0 deletions src/app/View/Components/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Backpack\CRUD\app\View\Components;

use Backpack\CRUD\CrudManager;
use Illuminate\View\Component;

class Form extends Component
{
public $crud;

/**
* Create a new component instance.
*
* @param string $controller The CRUD controller class name
* @param string $operation The operation to use (create, update, etc.)
* @param string|null $action Custom form action URL
* @param string $method Form method (post, put, etc.)
*/
public function __construct(
public string $controller,
public string $id = 'backpack-form',
public string $operation = 'create',
public ?string $action = null,
public string $method = 'post',
public bool $hasUploadFields = false,

) {
// Get CRUD panel instance from the controller
if (CrudManager::hasCrudPanel($controller)) {
$previousOperation = CrudManager::getCrudPanel($controller)->getOperation();
}

$this->crud = CrudManager::setupCrudPanel($controller, $operation);

if (isset($previousOperation)) {
$this->crud->setOperation($previousOperation);
}

$this->operation = $operation;
$this->action = $action ?? url($this->crud->route);
$this->hasUploadFields = $this->crud->hasUploadFields($operation);
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('crud::components.form.form', [
'crud' => $this->crud,
'saveAction' => $this->crud->getSaveAction(),
'id' => $this->id,
'operation' => $this->operation,
'action' => $this->action,
'method' => $this->method,
]);
}
}
53 changes: 53 additions & 0 deletions src/app/View/Components/FormModal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Backpack\CRUD\app\View\Components;

class FormModal extends Form
{
/**
* Create a new component instance.
*
* @param string $controller The CRUD controller class name
* @param string $operation The operation to use (create, update, etc.)
* @param string|null $action Custom form action URL
* @param string $method Form method (post, put, etc.)
* @param string $modalTitle Title for the modal
* @param string $modalClasses CSS classes for the modal dialog
* @param string $formRouteOperation The operation to use for the form route (defaults to 'create')
* @param string $id The ID for the form element (defaults to 'backpack-form')
*/
public function __construct(
public string $controller,
public string $id = 'backpack-form',
public string $operation = 'create',
public string $formRouteOperation = 'create',
public ?string $action = null,
public string $method = 'post',
public string $title = 'Form',
public string $classes = 'modal-dialog modal-lg',
public bool $refreshDatatable = false,
) {
parent::__construct($controller, $id, $operation, $action, $method);
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('crud::components.form.modal', [
'crud' => $this->crud,
'id' => $this->id,
'operation' => $this->operation,
'formRouteOperation' => $this->formRouteOperation,
'hasUploadFields' => $this->hasUploadFields,
'refreshDatatable' => $this->refreshDatatable,
'action' => $this->action,
'method' => $this->method,
'title' => $this->title,
'classes' => $this->classes,
]);
}
}
5 changes: 5 additions & 0 deletions src/resources/assets/css/common.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
:root {
--table-row-hover: #f2f1ff;
--btn-close-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000000'%3e%3cpath d='M.293.293a1 1 0 0 1 1.414 0L8 6.586 14.293.293a1 1 0 1 1 1.414 1.414L9.414 8l6.293 6.293a1 1 0 0 1-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 0 1-1.414-1.414L6.586 8 .293 1.707a1 1 0 0 1 0-1.414z'/%3e%3c/svg%3e");
}

.sidebar .nav-dropdown-items .nav-dropdown {
Expand Down Expand Up @@ -212,6 +213,10 @@ form .select2.select2-container {
overflow: visible;
}

.modal .btn-close, .modal .close {
background: transparent var(--btn-close-bg) center / .75rem auto no-repeat;
}

/* SELECT 2 */
.select2-container--bootstrap .select2-selection {
box-shadow: none !important;
Expand Down
Loading