-
Notifications
You must be signed in to change notification settings - Fork 11
/
democustomfields17.php
149 lines (119 loc) · 4.91 KB
/
democustomfields17.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
require_once __DIR__.'/vendor/autoload.php';
use PrestaShop\Module\Democustomfields17\Form\Product\Hooks\HookFieldsBuilderInterface;
use PrestaShop\Module\Democustomfields17\Form\Product\Hooks\HookFieldsBuilderFinder;
use PrestaShop\Module\Democustomfields17\Form\Product\Democustomfields17AdminForm;
use PrestaShop\Module\Democustomfields17\Form\Product\ProductFormDataHandler;
class Democustomfields17 extends Module
{
private $productFormDataHandler;
public function __construct()
{
$this->name = 'democustomfields17';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'PululuK';
$this->need_instance = 1;
parent::__construct();
$this->displayName = $this->l('Demo products custom fields prestashop 1.7');
$this->description = $this->l('Demo products custom fields prestashop 1.7');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall my module?');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->productFormDataHandler = new ProductFormDataHandler();
}
public function isUsingNewTranslationSystem()
{
return true;
}
public function install()
{
include(dirname(__FILE__).'/sql/install.php');
return parent::install() && $this->registerHook($this->getHooks());
}
public function uninstall()
{
include(dirname(__FILE__).'/sql/uninstall.php');
return parent::uninstall();
}
public function __call($hookName, $params) {
$hookFieldsBuilder = (new HookFieldsBuilderFinder())->find($hookName);
if (null != $hookFieldsBuilder){
return $this->displayProductAdminHookFields($hookFieldsBuilder, $params);
}
}
public function hookActionGetProductPropertiesAfter($params)
{
$productCustomsData = $this->productFormDataHandler->getData(
[
'id_product' => (int) $params['product']['id_product'],
'id_lang' => $this->context->language->id,
'id_shop' => $this->context->shop->id
]
);
$params['product'][$this->name] = $productCustomsData;
}
public function hookActionAdminProductsControllerSaveAfter($params)
{
$data = Tools::getValue($this->name);
if (!is_array($data) || !isset($data[$this->getModuleFormDatasID()])) { // Make sure data come from this form
return;
}
if(!isset($data['id_product'])) {
$data['id_product'] = (int) Tools::getValue('id_product');
}
$this->productFormDataHandler->save($data);
}
private function getProductAdminHookFieldsDefinition(HookFieldsBuilderInterface $hookFieldsBuilder, array $data)
{
$formFactory = $this->get('form.factory');
$options = [
'csrf_protection' => false,
'hookFieldsBuilder' => $hookFieldsBuilder,
'module' => $this,
];
return $formFactory->createNamed($this->name, Democustomfields17AdminForm::class, $data, $options);
}
private function displayProductAdminHookFields(HookFieldsBuilderInterface $hookFieldsBuilder, array $params)
{
if (!isset($params['id_product'])){
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$params['id_product'] = (int) $request->attributes->get('id');
}
$productFieldsData = $this->productFormDataHandler->getData($params);
$form = $this->getProductAdminHookFieldsDefinition($hookFieldsBuilder, $productFieldsData);
return $this->get('twig')
->render('@PrestaShop/'.$this->name.'/admin/product/customfields.html.twig', [
'form' => $form->createView(),
]);
}
public function getHooks()
{
// @see https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/#full-list
return [
'displayAdminProductsExtra',
'displayAdminProductsMainStepLeftColumnMiddle',
'displayAdminProductsMainStepLeftColumnBottom',
'displayAdminProductsMainStepRightColumnBottom',
'displayAdminProductsQuantitiesStepBottom',
'displayAdminProductsPriceStepBottom',
'displayAdminProductsOptionsStepTop',
'displayAdminProductsOptionsStepBottom',
'displayAdminProductsSeoStepBottom',
'actionAdminProductsControllerSaveAfter',
'actionObjectProductDeleteAfter',
'actionGetProductPropertiesAfter'
];
}
public function getLocales()
{
return $this->get('prestashop.adapter.data_provider.language')->getLanguages();
}
public function getModuleFormDatasID()
{
return 'fields_from_'.$this->name.'_'.$this->id ;
}
}