forked from friends-of-presta/training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.php
executable file
·209 lines (181 loc) · 6.24 KB
/
training.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Twig\Environment;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilder;
use PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\Training\Product\ProductHooks;
use PrestaShop\Training\Product\AlternativeDescription;
use PrestaShop\Training\Product\ProductsCollection;
use PrestaShop\Training\Menu\TabManager;
use PrestaShop\Training\Grid\Logs;
/**
* A complete module to demonstrate PrestaShop 1.7 features
*/
class Training extends Module
{
/**
* @var array list of available Product hooks
*/
private $productHooks;
/**
* Training constructor.
*/
public function __construct()
{
$this->name = 'training';
$this->version = '1.1.0';
$this->author = 'PrestaShop';
$this->need_instance = false;
$this->ps_versions_compliancy = [
'min' => '1.7.6.0',
'max' => _PS_VERSION_,
];
parent::__construct();
$this->displayName = $this->trans('PrestaShop Training module');
$this->description = $this->trans('Demonstrate all the features of the PrestaShop Back Office, in its version 1.7.6');
$this->productHooks = array_merge(ProductHooks::PRODUCT_LIST_HOOKS, ProductHooks::PRODUCT_FORM_HOOKS);
}
public function hookActionLogsGridDefinitionModifier(array $params)
{
/** @var GridDefinitionInterface $definition */
$definition = $params['definition'];
Logs::moveObjectTypeAfterErrorColumn($definition);
Logs::updateObjectTypeFilterForm($definition);
}
/**
* Adds a new option "Shop motto" to the General Page form.
*
* @param array $hookParams
*/
public function hookActionGeneralPageForm(array $hookParams)
{
/** @var FormBuilder $formBuilder */
$formBuilder = $hookParams['form_builder'];
$formBuilder->add('shop_motto', TextType::class, [
'data' => $this->get('prestashop.adapter.legacy.configuration')->get('PS_TRAINING_SHOP_MOTTO'),
]);
}
/**
* Save the extra configuration "Shop motto" value added to the General Page form.
*
* @param array $hookParams
*/
public function hookActionGeneralPageSave(array $hookParams)
{
$motto = $hookParams['form_data']['shop_motto'];
/* @var Configuration */
$this->get('prestashop.adapter.legacy.configuration')->set('PS_TRAINING_SHOP_MOTTO', $motto);
}
/**
* Display "alternative" in Product page.
*
* @param array $hookParams
*
* @return string
*/
public function hookDisplayAdminProductsMainStepLeftColumnMiddle($hookParams)
{
$productId = $hookParams['id_product'];
/** @var FormFactoryInterface $formFactory */
$formFactory = $this->get('form.factory');
/** @var Environment $twig */
$twig = $this->get('twig');
/** @var FormInterface $form */
$form = AlternativeDescription::addToForm($productId, $formFactory);
// You don't need to design your form, call only form_row(my_field) in
// your template.
return AlternativeDescription::setTemplateToProductPage($twig, $form);
}
/**
* Add the field "alternative_description to Product table.
*/
public function hookActionDispatcherBefore()
{
AlternativeDescription::addToProductDefinition();
}
/**
* Manage the list of product fields available in the Product Catalog page.
*
* @param array $hookParams
*/
public function hookActionAdminProductsListingFieldsModifier(&$hookParams)
{
$hookParams['sql_select']['alternative_description'] = [
'table' => 'p',
'field' => 'alternative_description',
'filtering' => "LIKE '%%%s%%'",
];
}
/**
* Manage the list of products available in the Product Catalog page.
*
* @param array $hookParams
*/
public function hookActionAdminProductsListingResultsModifier(&$hookParams)
{
$hookParams['products'] = ProductsCollection::make($hookParams['products'])
->sortBy('alternative_description')
->all()
;
}
/**
* Manage the information in a specific tab of Product Page.
*
* @return string
*/
public function hookDisplayAdminProductsExtra()
{
return $this->get('twig')->render('@PrestaShop/Products/module_panel.html.twig');
}
/**
* {@inheritdoc}
*/
public function install()
{
return parent::install() &&
$this->registerHook('actionLogsGridDefinitionModifier') &&
$this->registerHook('actionLogsGridQueryBuilderModifier') &&
$this->registerHook('actionLogsGridPresenterModifier') &&
$this->registerHook('actionLogsGridFilterFormModifier') &&
$this->registerHook('actionLogsGridGridDataModifier') &&
$this->registerHook('actionGeneralPageForm') &&
$this->registerHook('actionGeneralPageSave') &&
AlternativeDescription::addToProductTable() &&
$this->registerHook($this->productHooks) &&
$this->installTabs()
;
}
/**
* {@inheritdoc}
*/
public function uninstall()
{
return AlternativeDescription::removeToProductTable();
}
/**
* Setup the Training Menu as Modern Controllers are not managed (yet) by the $tabs property.
*
* @return bool
*
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public function installTabs()
{
TabManager::addTab('AdminTraining', 'Training Menu', 'training', 'AdminTools');
TabManager::addTab('AdminTrainingIndexClass', 'Controller exemple', 'training', 'AdminTraining');
TabManager::addTab('AdminTrainingGridClass', 'Grid exemple', 'training', 'AdminTraining');
return true;
}
/**
* @return bool forces the module to use the new translation system
*/
public function isUsingNewTranslationSystem()
{
return true;
}
}