-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdisplayordercarrier.php
357 lines (313 loc) · 11.2 KB
/
displayordercarrier.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require __DIR__ . '/vendor/autoload.php';
class DisplayOrderCarrier extends Module
{
/**
* List of hooks used
*/
const HOOKS = [
'actionOrderGridDefinitionModifier',
'actionOrderGridDataModifier',
'actionOrderGridQueryBuilderModifier',
'actionAdminOrdersListingFieldsModifier',
'actionAdminOrdersListingResultsModifier',
];
/**
* Name of ModuleAdminController used for configuration
*/
const MODULE_ADMIN_CONTROLLER = 'AdminDisplayOrderCarrier';
/**
* Configuration key used to store toggle for display logo
*/
const CONFIGURATION_KEY_SHOW_LOGO = 'DISPLAYORDERCARRIER_SHOW_LOGO';
/**
* Constructor.
*/
public function __construct()
{
$this->name = 'displayordercarrier';
$this->tab = 'administration';
$this->version = '1.1.0';
$this->author = 'Matt75';
$this->need_instance = 0;
$this->ps_versions_compliancy = [
'min' => '1.6.1.0',
'max' => _PS_VERSION_,
];
parent::__construct();
$this->displayName = $this->l('Display Carrier on Orders list');
$this->description = $this->l('Adds Carrier on Order list');
}
/**
* Install Module.
*
* @return bool
*/
public function install()
{
return parent::install()
&& $this->registerHook(static::HOOKS)
&& $this->installTabs()
&& Configuration::updateValue(static::CONFIGURATION_KEY_SHOW_LOGO, false);
}
/**
* Install Tabs
*
* @return bool
*/
public function installTabs()
{
if (Tab::getIdFromClassName(static::MODULE_ADMIN_CONTROLLER)) {
return true;
}
$tab = new Tab();
$tab->class_name = static::MODULE_ADMIN_CONTROLLER;
$tab->module = $this->name;
$tab->active = true;
$tab->id_parent = -1;
$tab->name = array_fill_keys(
Language::getIDs(false),
$this->displayName
);
return $tab->add();
}
/**
* Uninstall Module
*
* @return bool
*/
public function uninstall()
{
return parent::uninstall()
&& $this->uninstallTabs()
&& Configuration::deleteByName(static::CONFIGURATION_KEY_SHOW_LOGO);
}
/**
* Uninstall Tabs
*
* @return bool
*/
public function uninstallTabs()
{
$id_tab = (int) Tab::getIdFromClassName(static::MODULE_ADMIN_CONTROLLER);
if ($id_tab) {
$tab = new Tab($id_tab);
return $tab->delete();
}
return true;
}
/**
* Redirect to our ModuleAdminController when click on Configure button
*/
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink(static::MODULE_ADMIN_CONTROLLER));
}
/**
* Hook allows to modify Order grid definition since 1.7.7.0
*
* @param array $params
*/
public function hookActionOrderGridDefinitionModifier(array $params)
{
if (empty($params['definition'])) {
return;
}
/** @var PrestaShop\PrestaShop\Core\Grid\Definition\GridDefinitionInterface $definition */
$definition = $params['definition'];
if (Configuration::get(static::CONFIGURATION_KEY_SHOW_LOGO)) {
$column = new PrestaShop\PrestaShop\Core\Grid\Column\Type\Common\ImageColumn('carrier_reference');
$column->setName($this->l('Carrier'));
$column->setOptions([
'src_field' => 'carrier_logo',
'clickable' => false,
]);
} else {
$column = new PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn('carrier_reference');
$column->setName($this->l('Carrier'));
$column->setOptions([
'field' => 'carrier_name',
]);
}
$definition
->getColumns()
->addAfter(
'payment',
$column
)
;
/** @var PrestaShop\PrestaShop\Core\Form\ChoiceProvider\CarrierByReferenceChoiceProvider $carrierByReferenceChoiceProvider */
$carrierByReferenceChoiceProvider = $this->get('prestashop.core.form.choice_provider.carrier_by_reference_id');
$definition->getFilters()->add(
(new PrestaShop\PrestaShop\Core\Grid\Filter\Filter('carrier_reference', Symfony\Component\Form\Extension\Core\Type\ChoiceType::class))
->setAssociatedColumn('carrier_reference')
->setTypeOptions([
'required' => false,
'choices' => $carrierByReferenceChoiceProvider->getChoices(),
'translation_domain' => false,
])
);
}
/**
* Hook allows to modify Order grid data since 1.7.7.0
*
* @param array $params
*/
public function hookActionOrderGridDataModifier(array $params)
{
if (empty($params['data'])) {
return;
}
/** @var PrestaShop\PrestaShop\Core\Grid\Data\GridData $gridData */
$gridData = $params['data'];
$modifiedRecords = $gridData->getRecords()->all();
/** @var PrestaShop\PrestaShop\Core\Image\Parser\ImageTagSourceParserInterface $imageTagSourceParser */
$imageTagSourceParser = $this->get('prestashop.core.image.parser.image_tag_source_parser');
$carrierLogoThumbnailProvider = new \PrestaShop\Module\DisplayOrderCarrier\CarrierLogoThumbnailProvider($imageTagSourceParser);
foreach ($modifiedRecords as $key => $data) {
if (empty($data['carrier_name'])) {
$modifiedRecords[$key]['carrier_name'] = Carrier::getCarrierNameFromShopName();
}
$modifiedRecords[$key]['carrier_logo'] = $carrierLogoThumbnailProvider->getPath($data['id_carrier']);
}
$params['data'] = new PrestaShop\PrestaShop\Core\Grid\Data\GridData(
new PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection($modifiedRecords),
$gridData->getRecordsTotal(),
$gridData->getQuery()
);
}
/**
* Hook allows to modify Order query builder and add custom sql statements since 1.7.7.0
*
* @param array $params
*/
public function hookActionOrderGridQueryBuilderModifier(array $params)
{
if (empty($params['search_query_builder']) || empty($params['search_criteria'])) {
return;
}
/** @var Doctrine\DBAL\Query\QueryBuilder $searchQueryBuilder */
$searchQueryBuilder = $params['search_query_builder'];
/** @var PrestaShop\PrestaShop\Core\Search\Filters\OrderFilters $searchCriteria */
$searchCriteria = $params['search_criteria'];
$searchQueryBuilder->addSelect(
'o.`id_carrier`, car.`id_reference` AS `carrier_reference`, car.`name` AS `carrier_name`'
);
$searchQueryBuilder->leftJoin(
'o',
'`' . _DB_PREFIX_ . 'carrier`',
'car',
'car.`id_carrier` = o.`id_carrier`'
);
if ('carrier_reference' === $searchCriteria->getOrderBy()) {
$searchQueryBuilder->orderBy('car.`id_reference`', $searchCriteria->getOrderWay());
}
foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
if ('carrier_reference' === $filterName) {
$searchQueryBuilder->andWhere('car.`id_reference` = :carrier_reference');
$searchQueryBuilder->setParameter('carrier_reference', $filterValue);
}
}
}
/**
* Hook allows to modify Order grid data before 1.7.7.0
*
* @param array $params
*/
public function hookActionAdminOrdersListingFieldsModifier(array $params)
{
// If hook is called in AdminController::processFilter() we have to check existence
if (isset($params['select'])) {
$params['select'] .= ', a.id_carrier, car.id_reference AS carrier_reference, car.name AS carrier_name';
}
// If hook is called in AdminController::processFilter() we have to check existence
if (isset($params['join'])) {
$params['join'] .= 'LEFT JOIN ' . _DB_PREFIX_ . 'carrier AS car ON (a.id_carrier = car.id_carrier)';
}
$list = [];
$carriers = Carrier::getCarriers(
(int) $this->context->employee->id_lang,
false,
false,
false,
null,
Carrier::ALL_CARRIERS
);
if (false === empty($carriers)) {
foreach ($carriers as $carrier) {
$list[(int) $carrier['id_reference']] = $carrier['name'];
}
}
$params['fields']['carrier_name'] = [
'title' => $this->l('Carrier'),
'align' => 'text-center',
'class' => 'fixed-width-xs',
'filter_key' => 'car!id_reference',
'order_key' => 'car!id_reference',
'type' => 'select',
'list' => $list,
];
if (Configuration::get(static::CONFIGURATION_KEY_SHOW_LOGO)) {
$params['fields']['carrier_name']['callback'] = 'renderCarrierLogo';
$params['fields']['carrier_name']['callback_object'] = $this;
}
}
/**
* Hook allows to modify Order grid data before 1.7.7.0
*
* @param array $params
*/
public function hookActionAdminOrdersListingResultsModifier(array $params)
{
foreach ($params['list'] as $key => $fields) {
if (empty($fields['carrier_name'])) {
$params['list'][$key]['carrier_name'] = Carrier::getCarrierNameFromShopName();
}
}
}
/**
* Callback function used by legacy HelperList to retrieve Carrier logo before 1.7.7.0
*
* @param string $echo Carrier name
* @param array $tr Data
*
* @return string
*/
public function renderCarrierLogo($echo, $tr)
{
$logoPath = _PS_SHIP_IMG_DIR_ . (int) $tr['id_carrier'] . '.jpg';
if (false === Tools::file_exists_cache($logoPath)) {
$logoPath = _PS_IMG_DIR_ . '404.gif';
}
return str_replace(
'alt=""',
'alt="' . $echo . '" title="' . $echo . '"',
ImageManager::thumbnail(
$logoPath,
'carrier_mini_' . (int) $tr['id_carrier'] . '.jpg',
34
)
);
}
}