feat(main): Initial Module
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtml;
|
||||
use Context;
|
||||
use Hook;
|
||||
use Module;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlDataHandler implements FormDataHandlerInterface
|
||||
{
|
||||
public function create(array $formData)
|
||||
{
|
||||
$block = new CustomHtml();
|
||||
$block->hook_name = (string) $formData['hook_name'];
|
||||
$block->title = (string) $formData['title'];
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->active = (int) $formData['active'];
|
||||
$block->ordering = 0;
|
||||
|
||||
if (!$block->add()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->checkAndRegisterHookForModule((string) $formData['hook_name']);
|
||||
|
||||
return $block->id;
|
||||
}
|
||||
|
||||
public function update($id, array $formData)
|
||||
{
|
||||
$block = new CustomHtml((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($block)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$block->hook_name = (string) $formData['hook_name'];
|
||||
$block->title = (string) $formData['title'];
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->active = (int) $formData['active'];
|
||||
|
||||
if (!$block->update()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->checkAndRegisterHookForModule((string) $formData['hook_name']);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function checkAndRegisterHookForModule(string $hookName): void
|
||||
{
|
||||
$module = Module::getInstanceByName('ws_customhtml');
|
||||
$idShop = (int) Context::getContext()->shop->id;
|
||||
|
||||
if (!Hook::isModuleRegisteredOnHook($module, $hookName, $idShop)) {
|
||||
Hook::registerHook($module, $hookName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\DataProvider;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider\FormDataProviderInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtml;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlDataProvider implements FormDataProviderInterface
|
||||
{
|
||||
public function getData($id)
|
||||
{
|
||||
$block = new CustomHtml((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($block)) {
|
||||
return $this->getDefaultData();
|
||||
}
|
||||
|
||||
return [
|
||||
'hook_name' => $block->hook_name,
|
||||
'title' => $block->title,
|
||||
'show_title' => (bool) $block->show_title,
|
||||
'content' => $block->content,
|
||||
'active' => (bool) $block->active,
|
||||
];
|
||||
}
|
||||
|
||||
public function getDefaultData()
|
||||
{
|
||||
return [
|
||||
'hook_name' => '',
|
||||
'title' => '',
|
||||
'show_title' => false,
|
||||
'content' => '',
|
||||
'active' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use PrestaShopBundle\Form\Admin\Type\SwitchType;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class CustomHtmlType extends AbstractType
|
||||
{
|
||||
private $translator;
|
||||
|
||||
public function __construct(TranslatorInterface $translator, array $locales)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $formBuilder, array $options): void
|
||||
{
|
||||
$formBuilder
|
||||
->add('hook_name', TextType::class, [
|
||||
'label' => $this->translator->trans('Hook name', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('title', TextType::class, [
|
||||
'label' => $this->translator->trans('Title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('show_title', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Show title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
])
|
||||
->add('content', TextareaType::class, [
|
||||
'label' => $this->translator->trans('Content', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'attr' => ['class' => 'autoload_rte'],
|
||||
])
|
||||
->add('active', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Active', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user