Skip to content

A free open source IT asset/license management system

add create new label modal, partial create logic

Godfrey Md75632c2b35b
diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php
index cd422295e6..482c1466ad 100755
--- a/app/Http/Controllers/LabelsController.php
+++ b/app/Http/Controllers/LabelsController.php
@@ -22,6 +22,8 @@ use App\View\Label as LabelView;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use App\Services\CustomLabelImportValidator;
+use Illuminate\Validation\Rule;
+use App\Models\Labels\RectangleSheet;
class LabelsController extends Controller
{
/**
@@ -310,59 +312,104 @@ class LabelsController extends Controller
public function create(Request $request)
{
- $customLabelId = $request->get('custom_label_id');
-
- if ($customLabelId) {
- // creating a custom label from another custom label
- $customLabel = CustomUserLabel::findOrFail($customLabelId);
-
- $config = $customLabel->config_snapshot;
- $config['name'] = 'Copy of ' . $customLabel->name;
-
- return view('settings.label-edit', [
- 'config' => $config,
- 'selectedLabel' => $customLabel->base_label,
- 'selectedType' => $customLabel->type,
- 'importedConfig' => null,
- 'customLabel' => null,
- 'formMethod' => 'POST',
- 'formAction' => route('settings.labels.store'),
- ]);
+ if ($request->filled('custom_label_id') || $request->filled('label') || session()->has('imported_label_config')) {
+ return $this->createFromExisting($request);
}
- $selectedLabel = $request->get('label');
- if ($selectedLabel) {
- $selectedLabel = str_replace('/', '\\', $selectedLabel);
- }
- $importedConfig = session('imported_label_config');
+ $validated = $request->validate([
+ 'type' => ['required', 'in:sheet,tape'],
+ 'page_size' => ['required_if:type,sheet', 'nullable',
+ Rule::in(array_keys(RectangleSheet::supportedPageSizes())),
+ ],
+ 'label_width' => ['required', 'numeric', 'gt:0'],
+ 'label_height' => ['required', 'numeric', 'gt:0'],
+ ]);
- if ($importedConfig) {
- $selectedLabel = data_get($importedConfig, 'template', $selectedLabel);
- }
- try {
- $template = $selectedLabel
- ? Label::find(str_replace('/', '\\', $selectedLabel))
- : new DefaultLabel;
- } catch (\Throwable $e) {
- $template = new DefaultLabel;
- $selectedLabel = 'DefaultLabel';
- }
+ if ($validated['type'] === 'sheet') {
+ $page = RectangleSheet::supportedPageSize($validated['page_size']);
- $label = $this->previewLabelForTemplate($template);
- $config = $importedConfig ?: $label->toEditorConfig();
- $type = data_get($importedConfig, 'type');
+ if ($page === null) {
+ throw ValidationException::withMessages([
+ 'page_size' => trans('validation.in', [
+ 'attribute' => 'page size',
+ ]),
+ ]);
+ }
- if ($type === null) {
- $type = str_starts_with($selectedLabel ?? '', 'Tapes\\')
- ? 'tape'
- : 'sheet';
+ $label = new CustomSheetLabel(
+ pageWidth: $page['width'],
+ pageHeight: $page['height'],
+ labelWidth: (float)$validated['label_width'],
+ labelHeight: (float)$validated['label_height'],
+ );
+ } else {
+ $label = new CustomTapeLabel(
+ width: (float)$validated['label_width'],
+ height: (float)$validated['label_height'],
+ );
}
+// $customLabelId = $request->get('custom_label_id');
+//
+// if ($customLabelId) {
+// // creating a custom label from another custom label
+// $customLabel = CustomUserLabel::findOrFail($customLabelId);
+//
+// $config = $customLabel->config_snapshot;
+// $config['name'] = 'Copy of ' . $customLabel->name;
+//
+// return view('settings.label-edit', [
+// 'config' => $config,
+// 'selectedLabel' => $customLabel->base_label,
+// 'selectedType' => $customLabel->type,
+// 'importedConfig' => null,
+// 'customLabel' => null,
+// 'formMethod' => 'POST',
+// 'formAction' => route('settings.labels.store'),
+// ]);
+// }
+// $selectedLabel = $request->get('label');
+//
+// if ($selectedLabel) {
+// $selectedLabel = str_replace('/', '\\', $selectedLabel);
+// }
+// $importedConfig = session('imported_label_config');
+//
+// if ($importedConfig) {
+// $selectedLabel = data_get($importedConfig, 'template', $selectedLabel);
+// }
+// try {
+// $template = $selectedLabel
+// ? Label::find(str_replace('/', '\\', $selectedLabel))
+// : new DefaultLabel;
+// } catch (\Throwable $e) {
+// $template = new DefaultLabel;
+// $selectedLabel = 'DefaultLabel';
+// }
+//
+// $label = $this->previewLabelForTemplate($template);
+// $config = $importedConfig ?: $label->toEditorConfig();
+// $type = data_get($importedConfig, 'type');
+//
+// if ($type === null) {
+// $type = str_starts_with($selectedLabel ?? '', 'Tapes\\')
+// ? 'tape'
+// : 'sheet';
+// }
+// $page = RectangleSheet::supportedPageSize($validated['page_size']);
+//
+// if ($page === null) {
+// throw ValidationException::withMessages([
+// 'page_size' => trans('validation.in', [
+// 'attribute' => 'page size',
+// ]),
+// ]);
+// }
return view('settings.label-edit', [
- 'config' => $config,
- 'selectedLabel' => $selectedLabel,
- 'selectedType' => $type,
- 'importedConfig' => $importedConfig,
+ 'config' => $label->toEditorConfig(),
+ 'selectedLabel' => null,
+ 'selectedType' => $validated['type'],
+ 'importedConfig' => null,
'customLabel' => null,
'formMethod' => 'POST',
'formAction' => route('settings.labels.store'),
@@ -485,6 +532,69 @@ class LabelsController extends Controller
}
+ public function createFromExisting(Request $request)
+ {
+ if ($request->filled('custom_label_id')) {
+ $customLabel = CustomUserLabel::findOrFail($request->get('custom_label_id'));
+
+ $config = $customLabel->config_snapshot;
+ $config['name'] = 'Copy of ' . $customLabel->name;
+
+ return view('settings.label-edit', [
+ 'config' => $config,
+ 'selectedLabel' => $customLabel->base_label,
+ 'selectedType' => $customLabel->type,
+ 'importedConfig' => null,
+ 'customLabel' => null,
+ 'formMethod' => 'POST',
+ 'formAction' => route('settings.labels.store'),
+ ]);
+ }
+
+ $selectedLabel = $request->get('label');
+
+ if ($selectedLabel) {
+ $selectedLabel = str_replace('/', '\\', $selectedLabel);
+ }
+
+ $importedConfig = session('imported_label_config');
+
+ if ($importedConfig) {
+ $selectedLabel = data_get($importedConfig, 'template', $selectedLabel);
+ }
+
+ try {
+ $template = $selectedLabel
+ ? Label::find($selectedLabel)
+ : new DefaultLabel;
+ } catch (\Throwable $e) {
+ $template = new DefaultLabel;
+ $selectedLabel = 'DefaultLabel';
+ }
+
+ $label = $this->previewLabelForTemplate($template);
+ $config = $importedConfig ?: $label->toEditorConfig();
+
+ $type = data_get($config, 'type');
+
+ if ($type === null) {
+ $type = str_starts_with($selectedLabel ?? '', 'Tapes\\')
+ ? 'tape'
+ : 'sheet';
+ }
+
+ $config['name'] = data_get($config, 'name', 'Copy of ' . class_basename($selectedLabel));
+
+ return view('settings.label-edit', [
+ 'config' => $config,
+ 'selectedLabel' => $selectedLabel,
+ 'selectedType' => $type,
+ 'importedConfig' => $importedConfig,
+ 'customLabel' => null,
+ 'formMethod' => 'POST',
+ 'formAction' => route('settings.labels.store'),
+ ]);
+ }
protected function previewLabelForTemplate($template)
{
$name = $template->getName();
diff --git a/app/Models/Labels/RectangleSheet.php b/app/Models/Labels/RectangleSheet.php
index 32d80ba872..8eafd018b3 100644
--- a/app/Models/Labels/RectangleSheet.php
+++ b/app/Models/Labels/RectangleSheet.php
@@ -77,4 +77,27 @@ abstract class RectangleSheet extends Sheet
'supports' => $this->getSupportsEditorConfig(),
];
}
+
+ public static function supportedPageSizes(): array
+ {
+ return [
+ 'letter' => [
+ 'name' => 'Letter',
+ 'width' => 215.9,
+ 'height' => 279.4,
+ 'unit' => 'mm',
+ ],
+ 'a4' => [
+ 'name' => 'A4',
+ 'width' => 210.0,
+ 'height' => 297.0,
+ 'unit' => 'mm',
+ ],
+ ];
+ }
+
+ public static function supportedPageSize(string $key): ?array
+ {
+ return static::supportedPageSizes()[$key] ?? null;
+ }
}
diff --git a/resources/views/blade/table/labels.blade.php b/resources/views/blade/table/labels.blade.php
index 3a0a4cf3dc..fadce32d32 100644
--- a/resources/views/blade/table/labels.blade.php
+++ b/resources/views/blade/table/labels.blade.php
@@ -24,6 +24,6 @@
data-pagination="true"
data-page-size="50"
data-height="600"
- buttons="labelButtons"
+ data-buttons="labelButtons"
class="table table-striped snipe-table"
></table>
\ No newline at end of file
diff --git a/resources/views/modals/new-label-setup.blade.php b/resources/views/modals/new-label-setup.blade.php
new file mode 100644
index 0000000000..b36c4470e8
--- /dev/null
+++ b/resources/views/modals/new-label-setup.blade.php
@@ -0,0 +1,93 @@
+<div class="modal fade" id="new-label-modal" tabindex="-1" role="dialog" aria-labelledby="newLabelModalLabel">
+ <div class="modal-dialog" role="document">
+ <div class="modal-content">
+
+ <form action="{{ route('settings.labels.create') }}" method="GET">
+
+ <div class="modal-header">
+ <button type="button" class="close" data-dismiss="modal">
+ <span>&times;</span>
+ </button>
+
+ <h4 class="modal-title" id="newLabelModalLabel">
+ Create New Label
+ </h4>
+ </div>
+
+ <div class="modal-body">
+
+ <div class="form-group">
+ <label>Label Type</label>
+
+ <div class="radio">
+ <label>
+ <input type="radio" name="type" value="sheet" checked>
+ Sheet Label
+ </label>
+ </div>
+
+ <div class="radio">
+ <label>
+ <input type="radio" name="type" value="tape">
+ Tape Label
+ </label>
+ </div>
+ </div>
+
+ <div id="sheet-options">
+ <div class="form-group">
+ <label>Page Size</label>
+
+ <select name="page_size" class="form-control">
+ @foreach(\App\Models\Labels\RectangleSheet::supportedPageSizes() as $key => $page)
+ <option value="{{ $key }}">
+ {{ $page['name'] }}
+ </option>
+ @endforeach
+ </select>
+ </div>
+ </div>
+
+ <div class="row">
+ <div class="col-md-6">
+ <label>Width (mm)</label>
+ <input
+ type="number"
+ step="0.1"
+ min="0.1"
+ name="label_width"
+ class="form-control"
+ required
+ >
+ </div>
+
+ <div class="col-md-6">
+ <label>Height (mm)</label>
+ <input
+ type="number"
+ step="0.1"
+ min="0.1"
+ name="label_height"
+ class="form-control"
+ required
+ >
+ </div>
+ </div>
+
+ </div>
+
+ <div class="modal-footer">
+ <button type="button" class="btn btn-default" data-dismiss="modal">
+ Cancel
+ </button>
+
+ <button type="submit" class="btn btn-primary">
+ Continue
+ </button>
+ </div>
+
+ </form>
+
+ </div>
+ </div>
+</div>
diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php
index a61e4d8cc7..02ea5e610d 100644
--- a/resources/views/partials/bootstrap-table.blade.php
+++ b/resources/views/partials/bootstrap-table.blade.php
@@ -1235,6 +1235,22 @@
},
});
@endcan
+ window.labelButtons = () => ({
+ btnAdd: {
+ icon: 'fa fa-plus',
+ text: 'Create New Label',
+ title: 'Create New Label',
+ event() {
+ console.log('create label clicked');
+ $('#new-label-modal').modal('show');
+
+ },
+ attributes: {
+ class: 'btn-info',
+ title: 'Create New Label'
+ }
+ }
+ });
@can('create', \App\Models\Statuslabel::class)
// Status label table buttons
diff --git a/resources/views/settings/labels.blade.php b/resources/views/settings/labels.blade.php
index 02db9747aa..fefb9f104f 100644
--- a/resources/views/settings/labels.blade.php
+++ b/resources/views/settings/labels.blade.php
@@ -707,6 +707,7 @@
</div> <!-- /.row-->
</form>
+ @include('modals.new-label-setup')
<form id="delete-custom-label-form" method="POST" style="display:none;">
@csrf
@method('DELETE')