feat(main): Groups and blocks
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\ChoiceProvider;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
|
||||
use PrestaShop\PrestaShop\Core\Form\FormChoiceAttributeProviderInterface;
|
||||
use PrestaShop\PrestaShop\Core\Form\FormChoiceProviderInterface;
|
||||
use Db;
|
||||
|
||||
final class CustomHtmlGroupChoiceProvider implements
|
||||
FormChoiceProviderInterface,
|
||||
FormChoiceAttributeProviderInterface,
|
||||
ConfigurableFormChoiceProviderInterface
|
||||
{
|
||||
public function getChoices(array $options = []): array
|
||||
{
|
||||
$groups = Db::getInstance()->executeS(
|
||||
'SELECT `id_ws_customhtml_group`, `title`
|
||||
FROM `' . _DB_PREFIX_ . 'ws_customhtml_group`
|
||||
ORDER BY `title` ASC'
|
||||
);
|
||||
|
||||
$choices = [];
|
||||
|
||||
foreach ($groups as $group) {
|
||||
$choices[$group['title']] = (int) $group['id_ws_customhtml_group'];
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
public function getChoicesAttributes(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
{
|
||||
public function create(array $formData)
|
||||
{
|
||||
$block = new CustomHtmlBlock();
|
||||
$block->id_ws_customhtml_group = (int) $formData['id_ws_customhtml_group'];
|
||||
$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;
|
||||
}
|
||||
|
||||
return $block->id;
|
||||
}
|
||||
|
||||
public function update($id, array $formData)
|
||||
{
|
||||
$block = new CustomHtmlBlock((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($block)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$block->id_ws_customhtml_group = (int) $formData['id_ws_customhtml_group'];
|
||||
$block->title = (string) $formData['title'];
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->active = (int) $formData['active'];
|
||||
|
||||
return (bool) $block->update();
|
||||
}
|
||||
}
|
||||
+15
-19
@@ -3,48 +3,44 @@
|
||||
namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtml;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
||||
use Context;
|
||||
use Hook;
|
||||
use Module;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlDataHandler implements FormDataHandlerInterface
|
||||
final class CustomHtmlGroupDataHandler 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;
|
||||
$group = new CustomHtmlGroup();
|
||||
$group->title = (string) $formData['title'];
|
||||
$group->hook_name = (string) $formData['hook_name'];
|
||||
$group->active = (int) $formData['active'];
|
||||
$group->ordering = 0;
|
||||
|
||||
if (!$block->add()) {
|
||||
if (!$group->add()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->checkAndRegisterHookForModule((string) $formData['hook_name']);
|
||||
|
||||
return $block->id;
|
||||
return $group->id;
|
||||
}
|
||||
|
||||
public function update($id, array $formData)
|
||||
{
|
||||
$block = new CustomHtml((int) $id);
|
||||
$group = new CustomHtmlGroup((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($block)) {
|
||||
if (!Validate::isLoadedObject($group)) {
|
||||
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'];
|
||||
$group->title = (string) $formData['title'];
|
||||
$group->hook_name = (string) $formData['hook_name'];
|
||||
$group->active = (int) $formData['active'];
|
||||
|
||||
if (!$block->update()) {
|
||||
if (!$group->update()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\DataProvider;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider\FormDataProviderInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
{
|
||||
public function getData($id)
|
||||
{
|
||||
$block = new CustomHtmlBlock((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($block)) {
|
||||
return $this->getDefaultData();
|
||||
}
|
||||
|
||||
return [
|
||||
'id_ws_customhtml_group' => (int) $block->id_ws_customhtml_group,
|
||||
'title' => $block->title,
|
||||
'show_title' => (bool) $block->show_title,
|
||||
'content' => $block->content,
|
||||
'active' => (bool) $block->active,
|
||||
];
|
||||
}
|
||||
|
||||
public function getDefaultData()
|
||||
{
|
||||
return [
|
||||
'id_ws_customhtml_group' => 0,
|
||||
'title' => '',
|
||||
'show_title' => false,
|
||||
'content' => '',
|
||||
'active' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?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,34 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\DataProvider;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider\FormDataProviderInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlGroupDataProvider implements FormDataProviderInterface
|
||||
{
|
||||
public function getData($id)
|
||||
{
|
||||
$group = new CustomHtmlGroup((int) $id);
|
||||
|
||||
if (!Validate::isLoadedObject($group)) {
|
||||
return $this->getDefaultData();
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => $group->title,
|
||||
'hook_name' => $group->hook_name,
|
||||
'active' => (bool) $group->active,
|
||||
];
|
||||
}
|
||||
|
||||
public function getDefaultData()
|
||||
{
|
||||
return [
|
||||
'title' => '',
|
||||
'hook_name' => '',
|
||||
'active' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,25 +4,33 @@ namespace Websmid\CustomHtml\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
|
||||
use PrestaShopBundle\Form\Admin\Type\SwitchType;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class CustomHtmlType extends AbstractType
|
||||
class CustomHtmlBlockType extends AbstractType
|
||||
{
|
||||
private $translator;
|
||||
private $groupChoiceProvider;
|
||||
|
||||
public function __construct(TranslatorInterface $translator, array $locales)
|
||||
{
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
array $locales,
|
||||
ConfigurableFormChoiceProviderInterface $groupChoiceProvider
|
||||
) {
|
||||
$this->translator = $translator;
|
||||
$this->groupChoiceProvider = $groupChoiceProvider;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $formBuilder, array $options): void
|
||||
{
|
||||
$formBuilder
|
||||
->add('hook_name', TextType::class, [
|
||||
'label' => $this->translator->trans('Hook name', [], 'Modules.Wscustomhtml.Admin'),
|
||||
->add('id_ws_customhtml_group', ChoiceType::class, [
|
||||
'label' => $this->translator->trans('Group', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'choices' => $this->groupChoiceProvider->getChoices(),
|
||||
'required' => true,
|
||||
])
|
||||
->add('title', TextType::class, [
|
||||
@@ -0,0 +1,36 @@
|
||||
<?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 PrestaShopBundle\Form\Admin\Type\SwitchType;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class CustomHtmlGroupType extends AbstractType
|
||||
{
|
||||
private $translator;
|
||||
|
||||
public function __construct(TranslatorInterface $translator, array $locales)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $formBuilder, array $options): void
|
||||
{
|
||||
$formBuilder
|
||||
->add('title', TextType::class, [
|
||||
'label' => $this->translator->trans('Title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('hook_name', TextType::class, [
|
||||
'label' => $this->translator->trans('Hook name', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('active', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Active', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user