-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheverpsimgimport.php
446 lines (400 loc) · 16 KB
/
everpsimgimport.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/**
* 2019-2021 Team Ever
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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 license@prestashop.com so we can send you a copy immediately.
*
* @author Team Ever <https://www.team-ever.com/>
* @copyright 2019-2021 Team Ever
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Everpsimgimport extends Module
{
private $html;
private $postErrors = array();
private $postSuccess = array();
public function __construct()
{
$this->name = 'everpsimgimport';
$this->tab = 'administration';
$this->version = '1.0.2';
$this->author = 'Team Ever';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Ever PS products image Import');
$this->description = $this->l('Import images to products based on EAN13 or product reference');
$this->isSeven = Tools::version_compare(_PS_VERSION_, '1.7', '>=') ? true : false;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
Configuration::updateValue('EVERPSIMGIMPORT_EAN13', false);
Configuration::updateValue('EVERPSIMGIMPORT_PREFIX', '');
Configuration::updateValue('EVERPSIMGIMPORT_PRODUCT_PREFIX', '');
Configuration::updateValue('EVERPSIMGIMPORT_FOLDER', '');
return parent::install();
}
public function uninstall()
{
Configuration::deleteByName('EVERPSIMGIMPORT_EAN13');
Configuration::deleteByName('EVERPSIMGIMPORT_PREFIX');
Configuration::deleteByName('EVERPSIMGIMPORT_PRODUCT_PREFIX');
Configuration::deleteByName('EVERPSIMGIMPORT_FOLDER');
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent()
{
if (((bool)Tools::isSubmit('submitEverpspdfimportModule')) == true) {
$this->postValidation();
if (!count($this->postErrors)) {
$this->postProcess();
}
}
if (((bool)Tools::isSubmit('submitImportPdf')) == true) {
$this->postValidation();
if (!count($this->postErrors)) {
$files_dir = glob(
_PS_ROOT_DIR_
.'/'
.Configuration::get('EVERPSIMGIMPORT_FOLDER')
.'/*'
);
foreach ($files_dir as $file) {
if (is_file($file)
&& !strpos(basename($file), 'index')
) {
// && pathinfo($file, PATHINFO_EXTENSION) == 'jpg'
// $fileExist = false;
// $fileExist = Db::getInstance()->getValue('SELECT file
// FROM `'._DB_PREFIX_.'image`
// WHERE file_name = "'.basename($file).'"');
// if ($fileExist) {
// continue;
// }
$this->addImageImport(
$file,
basename($file)
);
}
}
}
}
// Display errors
if (count($this->postErrors)) {
foreach ($this->postErrors as $error) {
$this->html .= $this->displayError($error);
}
}
// Display confirmations
if (count($this->postSuccess)) {
foreach ($this->postSuccess as $success) {
$this->html .= $this->displayConfirmation($success);
}
}
$this->context->smarty->assign(array(
'everpsimgimport_dir' => $this->_path
));
$this->html .= $this->context->smarty->fetch($this->local_path.'views/templates/admin/header.tpl');
$this->html .= $this->renderForm();
$this->html .= $this->context->smarty->fetch($this->local_path.'views/templates/admin/footer.tpl');
return $this->html;
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitEverpspdfimportModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'buttons' => array(
'importPdf' => array(
'name' => 'submitImportPdf',
'type' => 'submit',
'class' => 'btn btn-default pull-right',
'icon' => 'process-icon-refresh',
'title' => $this->l('Import product images')
),
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->l('Files are ean13 named'),
'desc' => $this->l('Set yes for ean13.pdf named files'),
'hint' => $this->l('Else product reference will be used'),
'name' => 'EVERPSIMGIMPORT_EAN13',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->l('Disabled')
)
),
),
array(
'col' => 3,
'type' => 'text',
'label' => $this->l('Enter images folder name'),
'desc' => $this->l('Place all your images on this folder'),
'hint' => $this->l('If folder doesn\'t exist, import won\'t work'),
'name' => 'EVERPSIMGIMPORT_FOLDER',
'required' => true,
),
array(
'col' => 3,
'type' => 'text',
'label' => $this->l('Enter images file prefix'),
'desc' => $this->l('You can set a specific prefix to pdf names'),
'hint' => $this->l('Leave empty for not use'),
'name' => 'EVERPSIMGIMPORT_PREFIX',
),
array(
'col' => 3,
'type' => 'text',
'label' => $this->l('Enter product reference prefix'),
'desc' => $this->l('You can set a specific prefix to product references'),
'hint' => $this->l('Leave empty for not use'),
'name' => 'EVERPSIMGIMPORT_PRODUCT_PREFIX',
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
/**
* Set values for the inputs.
*/
protected function getConfigFormValues()
{
return array(
'EVERPSIMGIMPORT_EAN13' => Configuration::get(
'EVERPSIMGIMPORT_EAN13'
),
'EVERPSIMGIMPORT_PREFIX' => Configuration::get(
'EVERPSIMGIMPORT_PREFIX'
),
'EVERPSIMGIMPORT_PRODUCT_PREFIX' => Configuration::get(
'EVERPSIMGIMPORT_PRODUCT_PREFIX'
),
'EVERPSIMGIMPORT_FOLDER' => Configuration::get(
'EVERPSIMGIMPORT_FOLDER'
),
);
}
private function postValidation()
{
if (Tools::isSubmit('submitEverpspdfimportModule')) {
if (Tools::getValue('EVERPSIMGIMPORT_EAN13')
&& !Validate::isBool(Tools::getValue('EVERPSIMGIMPORT_EAN13'))
) {
$this->postErrors[] = $this->l('Error: ean13 or reference is not valid');
}
if (!Tools::getValue('EVERPSIMGIMPORT_FOLDER')
|| !Validate::isDirName(Tools::getValue('EVERPSIMGIMPORT_FOLDER'))
) {
$this->postErrors[] = $this->l('Error: folder name is not valid');
}
if (Tools::getValue('EVERPSIMGIMPORT_FOLDER')
&& Validate::isDirName(Tools::getValue('EVERPSIMGIMPORT_FOLDER'))
) {
$img_folder = Tools::getValue('EVERPSIMGIMPORT_FOLDER');
$valid = false;
foreach(glob(_PS_ROOT_DIR_.'/*', GLOB_ONLYDIR) as $dir) {
$dirname = basename($dir);
if ($dirname == $img_folder) {
$valid = true;
}
}
if (!$valid) {
$this->postErrors[] = $this->l('Error: folder does not exists');
}
}
if (Tools::getValue('EVERPSIMGIMPORT_PREFIX')
&& !Validate::isString(Tools::getValue('EVERPSIMGIMPORT_PREFIX'))
) {
$this->postErrors[] = $this->l('Error: prefix is not valid');
}
if (Tools::getValue('EVERPSIMGIMPORT_PRODUCT_PREFIX')
&& !Validate::isString(Tools::getValue('EVERPSIMGIMPORT_PRODUCT_PREFIX'))
) {
$this->postErrors[] = $this->l('Error: product prefix is not valid');
}
}
}
/**
* Save form data.
*/
protected function postProcess()
{
$form_values = $this->getConfigFormValues();
foreach (array_keys($form_values) as $key) {
Configuration::updateValue($key, Tools::getValue($key));
}
}
/**
* Import and attach files to product
* @param string filename, string name, string description
*/
public function addImageImport($filename, $description)
{
if (Configuration::get('EVERPSIMGIMPORT_EAN13')) {
$ref = 'ean13';
} else {
$ref = 'reference';
}
// Save product reference with prefix or not
if (Configuration::get('EVERPSIMGIMPORT_PRODUCT_PREFIX')) {
$based_data = Configuration::get('EVERPSIMGIMPORT_PRODUCT_PREFIX')
.pathinfo($filename, PATHINFO_FILENAME);
} else {
$based_data = pathinfo($filename, PATHINFO_FILENAME);
}
// Save base_data using prefix or not
if (Configuration::get('EVERPSIMGIMPORT_PREFIX')) {
$based_data = str_replace(
Configuration::get('EVERPSIMGIMPORT_PREFIX'),
'',
$based_data
);
}
// Remove trailing slash
$img_folder = rtrim(
Configuration::get('EVERPSIMGIMPORT_FOLDER'),
'/'
);
$folder_url = Tools::getHttpHost(true)
.__PS_BASE_URI__
.$img_folder;
foreach(glob(_PS_ROOT_DIR_.'/*', GLOB_ONLYDIR) as $dir) {
$dirname = basename($dir);
if ($dirname == $img_folder) {
$valid = true;
}
}
if (!$valid) {
die('folder not found. Please add folder '.$img_folder.' to root folder');
}
if (Configuration::get('EVERPSIMGIMPORT_EAN13')) {
$id_product = Product::getIdByEan13($based_data);
} else {
$id_product = Db::getInstance()->getValue('SELECT id_product
FROM `'._DB_PREFIX_.'product`
WHERE reference = "'.pSQL($based_data).'"');
}
if (!(int)$id_product || empty($id_product)) {
return;
}
// Create image
$image = new Image();
$image->id_product = (int)$id_product;
$image->cover = true;
$image->position = Image::getHighestPosition($id_product) + 1;
$languages = Language::getLanguages();
foreach ($languages as $language) {
$image->legend[$language['id_lang']] = strval(basename($based_data));
}
$image->add();
if (!$this->copyImgImport($id_product, $image->id, $filename, 'products', !Tools::getValue('regenerate'))) {
$image->delete();
}
}
private function copyImgImport($id_entity, $id_image, $url, $entity = 'products', $regenerate = true) {
$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
switch ($entity) {
default:
case 'products':
$image_obj = new Image(
(int)$id_image
);
$path = $image_obj->getPathForCreation();
break;
case 'categories':
$path = _PS_CAT_IMG_DIR_ . (int)$id_entity;
break;
case 'manufacturers':
$path = _PS_MANU_IMG_DIR_ . (int)$id_entity;
break;
case 'suppliers':
$path = _PS_SUPP_IMG_DIR_ . (int)$id_entity;
break;
}
$url = str_replace(' ', '%20', trim($url));
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($url))
return false;
// 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
// Just hide the warning, the processing will be the same.
if (Tools::copy($url, $tmpfile)) {
ImageManager::resize($tmpfile, $path . '.jpg');
$images_types = ImageType::getImagesTypes($entity);
if ($regenerate) {
foreach ($images_types as $image_type) {
ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
}
unlink($url);
}
else {
unlink($tmpfile);
return false;
}
unlink($tmpfile);
return true;
}
}