Skip to content

Commit

Permalink
Email template is done.
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-webkul committed Jul 24, 2024
1 parent 64b90d6 commit 1b15d21
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ public function prepareQueryBuilder()
public function prepareColumns()
{
$this->addColumn([
'index' => 'id',
'label' => trans('admin::app.datagrid.id'),
'type' => 'string',
'sortable' => true,
'index' => 'id',
'label' => trans('admin::app.settings.email-template.index.datagrid.id'),
'type' => 'string',
'sortable' => true,
'searchable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'name',
'label' => trans('admin::app.datagrid.name'),
'type' => 'string',
'sortable' => true,
'index' => 'name',
'label' => trans('admin::app.settings.email-template.index.datagrid.name'),
'type' => 'string',
'sortable' => true,
'searchable' => true,
'filterable' => true,
]);

$this->addColumn([
'index' => 'subject',
'label' => trans('admin::app.datagrid.subject'),
'label' => trans('admin::app.settings.email-template.index.datagrid.subject'),
'type' => 'string',
'sortable' => true,
]);
Expand All @@ -63,17 +67,17 @@ public function prepareActions()
$this->addAction([
'index' => 'delete',
'icon' => 'icon-edit',
'title' => trans('ui::app.datagrid.edit'),
'title' => trans('admin::app.settings.email-template.index.datagrid.edit'),
'method' => 'GET',
'url' => fn($row) => route('admin.settings.email_templates.edit', $row->id),
'url' => fn ($row) => route('admin.settings.email_templates.edit', $row->id),
]);

$this->addAction([
'index' => 'delete',
'icon' => 'icon-delete',
'title' => trans('ui::app.datagrid.delete'),
'method' => 'DELETE',
'url' => fn($row) => route('admin.settings.email_templates.delete', $row->id),
'icon' => 'icon-delete',
'title' => trans('admin::app.settings.email-template.index.datagrid.delete'),
'method' => 'DELETE',
'url' => fn ($row) => route('admin.settings.email_templates.delete', $row->id),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Webkul\Admin\Http\Controllers\Setting;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Event;
use Illuminate\View\View;
use Webkul\Admin\DataGrids\Setting\EmailTemplateDataGrid;
use Webkul\Admin\Http\Controllers\Controller;
use Webkul\EmailTemplate\Repositories\EmailTemplateRepository;
use Webkul\Workflow\Helpers\Entity;
Expand All @@ -21,13 +25,11 @@ public function __construct(

/**
* Display a listing of the email template.
*
* @return \Illuminate\View\View
*/
public function index()
public function index(): View|JsonResponse
{
if (request()->ajax()) {
return app(\Webkul\Admin\DataGrids\Setting\EmailTemplateDataGrid::class)->toJson();
return datagrid(EmailTemplateDataGrid::class)->process();
}

return view('admin::settings.email-templates.index');
Expand All @@ -47,10 +49,8 @@ public function create()

/**
* Store a newly created email templates in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
public function store(): RedirectResponse
{
$this->validate(request(), [
'name' => 'required',
Expand All @@ -64,18 +64,15 @@ public function store()

Event::dispatch('settings.email_templates.create.after', $emailTemplate);

session()->flash('success', trans('admin::app.settings.email-templates.create-success'));
session()->flash('success', trans('admin::app.settings.email-template.index.create-success'));

return redirect()->route('admin.settings.email_templates.index');
}

/**
* Show the form for editing the specified email template.
*
* @param int $id
* @return \Illuminate\View\View
*/
public function edit($id)
public function edit(int $id): View
{
$emailTemplate = $this->emailTemplateRepository->findOrFail($id);

Expand All @@ -86,11 +83,8 @@ public function edit($id)

/**
* Update the specified email template in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update($id)
public function update(int $id): RedirectResponse
{
$this->validate(request(), [
'name' => 'required',
Expand All @@ -104,39 +98,36 @@ public function update($id)

Event::dispatch('settings.email_templates.update.after', $emailTemplate);

session()->flash('success', trans('admin::app.settings.email-templates.update-success'));
session()->flash('success', trans('admin::app.settings.email-template.index.update-success'));

return redirect()->route('admin.settings.email_templates.index');
}

/**
* Remove the specified email template from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function destroy(int $id): JsonResponse
{
$emailTemplate = $this->emailTemplateRepository->findOrFail($id);

try {
Event::dispatch('settings.email_templates.delete.before', $id);

$this->emailTemplateRepository->delete($id);
$emailTemplate->delete($id);

Event::dispatch('settings.email_templates.delete.after', $id);

return response()->json([
'message' => trans('admin::app.settings.email-templates.delete-success'),
'message' => trans('admin::app.settings.email-template.index.delete-success'),
], 200);
} catch (\Exception $exception) {
return response()->json([
'message' => trans('admin::app.settings.email-templates.delete-failed'),
'message' => trans('admin::app.settings.email-template.index.delete-failed'),
], 400);
}

return response()->json([
'message' => trans('admin::app.settings.email-templates.delete-failed'),
'message' => trans('admin::app.settings.email-template.index.delete-failed'),
], 400);
}
}
1 change: 0 additions & 1 deletion packages/Webkul/Admin/src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@
Route::delete('{id}', 'SourceController@destroy')->name('admin.settings.sources.delete');
});


// Email Templates Routes
Route::prefix('email-templates')->group(function () {
Route::get('', 'EmailTemplateController@index')->name('admin.settings.email_templates.index');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Illuminate\Foundation\AliasLoader;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Webkul\Admin\Http\Middleware\Locale;
use Illuminate\Support\Facades\Route;

class AdminServiceProvider extends ServiceProvider
{
Expand Down
41 changes: 41 additions & 0 deletions packages/Webkul/Admin/src/Resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,47 @@
],
],
],

'email-template' => [
'index' => [
'create-btn' => 'Create Email Template',
'title' => 'Email Templates',
'create-success' => 'Email Template created successfully.',
'update-success' => 'Email Template updated successfully.',
'delete-success' => 'Email Template deleted successfully.',
'delete-failed' => 'Email Template can not be deleted.',

'datagrid' => [
'delete' => 'Delete',
'edit' => 'Edit',
'id' => 'ID',
'name' => 'Name',
'subject' => 'Subject',
],
],

'create' => [
'title' => 'Create Email Template',
'save-btn' => 'Save Email Template',
'email-template' => 'Email Template',
'subject' => 'Subject',
'content' => 'Content',
'subject-placeholders' => 'Subject Placeholders',
'general' => 'General',
'name' => 'Name',
],

'edit' => [
'title' => 'Edit Email Template',
'save-btn' => 'Save Email Template',
'email-template' => 'Email Template',
'subject' => 'Subject',
'content' => 'Content',
'subject-placeholders' => 'Subject Placeholders',
'general' => 'General',
'name' => 'Name',
],
],
],

'activities' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ class="flex w-full items-center overflow-hidden rounded-md border text-sm text-g
<x-admin::tinymce
:selector="'textarea#' . $attributes->get('id')"
::field="field"
:placeholders="$attributes->get('placeholders', [])"
>
</x-admin::tinymce>
/>
@endif
</v-field>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@php($placeholders = app('\Webkul\Workflow\Helpers\Entity')->getEmailTemplatePlaceholders())

<v-tinymce {{ $attributes }}></v-tinymce>

@pushOnce('scripts')
Expand All @@ -21,7 +23,7 @@
app.component('v-tinymce', {
template: '#v-tinymce-template',
props: ['selector', 'placeholders'],
props: ['selector', 'field'],
data() {
return {
Expand Down Expand Up @@ -160,38 +162,42 @@
tinyMCEHelper.initTinyMCE({
selector: this.selector,
plugins: 'image media wordcount save fullscreen code table lists link',
toolbar: 'formatselect | bold italic strikethrough forecolor backcolor image alignleft aligncenter alignright alignjustify | link hr | numlist bullist outdent indent | removeformat | code | table | placeholder',
toolbar: 'placeholders | bold italic strikethrough forecolor backcolor image alignleft aligncenter alignright alignjustify | link hr | numlist bullist outdent indent | removeformat | code | table',
image_advtab: true,
directionality: 'ltr',
setup: (editor) => {
let toggleState = false;
setup: editor => {
editor.ui.registry.addMenuButton('placeholder', {
editor.ui.registry.addMenuButton('placeholders', {
text: 'Placeholders',
fetch: callback => {
let placeholders;
// If placeholders is a JSON string, parse it
try {
placeholders = JSON.parse(this.placeholders);
} catch (e) {
// If JSON parsing fails, fallback to using the string directly if it's an array
placeholders = Array.isArray(this.placeholders) ? this.placeholders : [];
}
const items = placeholders.map(placeholder => ({
type: 'menuitem',
text: placeholder.text, // The display text in the menu
value: placeholder.value, // The value to be inserted into the editor
}));
fetch: function (callback) {
const items = [
@foreach($placeholders as $placeholder)
{
type: 'nestedmenuitem',
text: '{{ $placeholder['text'] }}',
getSubmenuItems: () => [
@foreach($placeholder['menu'] as $child)
{
type: 'menuitem',
text: '{{ $child['text'] }}',
onAction: function () {
editor.insertContent('{{ $child['value'] }}');
},
},
@endforeach
],
},
@endforeach
];
callback(items);
},
onAction: (api) => {
api.close(); // Close the menu after selection
}
});
editor.on('keyup', () => this.field.onInput(editor.getContent()));
}
});
},
},
})
Expand Down
Loading

0 comments on commit 1b15d21

Please sign in to comment.