forked from NeoRazorX/facturascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductImagesTrait.php
More file actions
185 lines (165 loc) · 5.89 KB
/
ProductImagesTrait.php
File metadata and controls
185 lines (165 loc) · 5.89 KB
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
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Lib\ExtendedController;
use Exception;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Model\AttachedFile;
use FacturaScripts\Dinamic\Model\AttachedFileRelation;
use FacturaScripts\Dinamic\Model\ProductoImagen;
/**
* Auxiliar Method for images of the product.
*
* @author Carlos García Gómez <carlos@facturascripts.com>
* @author Jose Antonio Cuello Principal <yopli2000@gmail.com>
*/
trait ProductImagesTrait
{
abstract protected function addHtmlView(string $viewName, string $fileName, string $modelName, string $viewTitle, string $viewIcon = 'fa-brands fa-html5');
abstract protected function validateFormToken(): bool;
/**
* Add a list of images.
*
* @return bool
*/
protected function addImageAction(): bool
{
if (false === $this->permissions->allowUpdate) {
Tools::log()->warning('not-allowed-modify');
return false;
} elseif (false === $this->validateFormToken()) {
return false;
}
$count = 0;
$uploadFiles = $this->request->files->getArray('newfiles');
foreach ($uploadFiles as $uploadFile) {
if (false === $uploadFile->isValid()) {
Tools::log()->error($uploadFile->getErrorMessage());
continue;
}
if (false === strpos($uploadFile->getMimeType(), 'image/')) {
Tools::log()->error('file-not-supported');
continue;
}
try {
$folder = Tools::folder('MyFiles');
Tools::folderCheckOrCreate($folder);
$uploadFile->move($folder, $uploadFile->getClientOriginalName());
$idfile = $this->createAttachedFile($uploadFile->getClientOriginalName());
if (empty($idfile)) {
Tools::log()->error('record-save-error');
return true;
}
$idproduct = $this->createProductImage($idfile);
if (empty($idproduct)) {
Tools::log()->error('record-save-error');
return true;
}
$this->createFileRelation($idproduct, $idfile);
++$count;
} catch (Exception $exc) {
Tools::log()->error($exc->getMessage());
return true;
}
}
Tools::log()->notice('images-added-correctly', ['%count%' => $count]);
return true;
}
/**
* Add view for product images.
*
* @param string $viewName
*/
protected function createViewsProductImages(string $viewName = 'EditProductoImagen'): void
{
$this->addHtmlView($viewName, 'Tab/ProductoImagen', 'ProductoImagen', 'images', 'fa-solid fa-images');
}
/**
* Delete an image.
*
* @return bool
*/
protected function deleteImageAction(): bool
{
if (false === $this->permissions->allowDelete) {
Tools::log()->warning('not-allowed-delete');
return true;
} elseif (false === $this->validateFormToken()) {
return false;
}
$id = $this->request->request->get('idimage');
$productImage = new ProductoImagen();
if (false === $productImage->loadFromCode($id)) {
return true;
}
$this->dataBase->beginTransaction();
if ($productImage->delete() && $productImage->getFile()->delete()) {
$this->dataBase->commit();
Tools::log()->notice('record-deleted-correctly');
return true;
}
$this->dataBase->rollback();
Tools::log()->error('record-delete-error');
return true;
}
/**
* Create the record in the AttachedFile model
* and returns its identifier.
*
* @param string $path
* @return int
*/
protected function createAttachedFile(string $path): int
{
$newFile = new AttachedFile();
$newFile->path = $path;
$newFile->save();
return $newFile->idfile;
}
/**
* Create the record in the ProductoImagen model
* and returns its idproducto.
*
* @param int $idfile
* @return ?int
*/
protected function createProductImage(int $idfile): ?int
{
$productImage = new ProductoImagen();
$productImage->idproducto = $this->request->request->get('idproducto');
$productImage->idfile = $idfile;
$reference = $this->request->request->get('referencia', '');
$productImage->referencia = empty($reference) ? null : $reference;
return $productImage->save() ? $productImage->idproducto : null;
}
/**
* Create the record in the AttachedFileRelation model.
*
* @param int $idproduct
* @param int $idfile
*/
protected function createFileRelation(int $idproduct, int $idfile): void
{
$fileRelation = new AttachedFileRelation();
$fileRelation->idfile = $idfile;
$fileRelation->model = 'Producto';
$fileRelation->modelid = $idproduct;
$fileRelation->nick = $this->user->nick;
$fileRelation->save();
}
}