Skip to content

Commit b033a6b

Browse files
committed
Fix file uplaod for Galette 1.2.0
Remove useless dependency on plugins Fix on uplaod error
1 parent 4c5bc6e commit b033a6b

File tree

20 files changed

+103
-218
lines changed

20 files changed

+103
-218
lines changed

lib/GaletteObjectsLend/Controllers/Crud/CategoriesController.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use Analog\Analog;
2727
use DI\Attribute\Inject;
28+
use GaletteObjectsLend\Entity\CategoryPicture;
2829
use GaletteObjectsLend\Filters\CategoriesList;
2930
use GaletteObjectsLend\Repository\Categories;
3031
use GaletteObjectsLend\Entity\LendCategory;
@@ -107,7 +108,7 @@ public function list(Request $request, Response $response, ?string $option = nul
107108
}
108109
}
109110

110-
$categories = new Categories($this->zdb, $this->login, $this->plugins, $filters);
111+
$categories = new Categories($this->zdb, $this->login, $filters);
111112
$list = $categories->getCategoriesList(true);
112113

113114
$this->session->objectslend_filter_categories = $filters;
@@ -198,7 +199,7 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
198199
$category = $this->session->objectslend_category;
199200
$this->session->objectslend_category = null;
200201
} else {
201-
$category = new LendCategory($this->zdb, $this->plugins, $id);
202+
$category = new LendCategory($this->zdb, $id);
202203
}
203204

204205
if ($category->category_id !== null) {
@@ -208,12 +209,14 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
208209
}
209210

210211
$lendsprefs = new Preferences($this->zdb);
212+
$picture = new CategoryPicture($category->category_id);
211213
$params = [
212214
'page_title' => $title,
213215
'category' => $category,
214216
'time' => time(),
215217
'action' => $action,
216-
'olendsprefs' => $lendsprefs
218+
'olendsprefs' => $lendsprefs,
219+
'picture' => $picture
217220
];
218221

219222
// display page
@@ -238,36 +241,20 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
238241
public function doEdit(Request $request, Response $response, ?int $id = null, string $action = 'edit'): Response
239242
{
240243
$post = $request->getParsedBody();
241-
$category = new LendCategory($this->zdb, $this->plugins, $id);
244+
$category = new LendCategory($this->zdb, $id);
242245
$error_detected = [];
243246

244247
$category->name = $post['name'];
245248
$category->is_active = ($post['is_active'] ?? false) == true;
246249
if ($category->store()) {
247250
// picture upload
248-
if (isset($_FILES['picture'])) {
249-
if ($_FILES['picture']['error'] === UPLOAD_ERR_OK) {
250-
if ($_FILES['picture']['tmp_name'] != '') {
251-
if (is_uploaded_file($_FILES['picture']['tmp_name'])) {
252-
$res = $category->picture->store($_FILES['picture']);
253-
if ($res < 0) {
254-
$error_detected[] = $category->picture->getErrorMessage($res);
255-
}
256-
}
257-
}
258-
} elseif ($_FILES['picture']['error'] !== UPLOAD_ERR_NO_FILE) {
259-
Analog::log(
260-
$category->picture->getPhpErrorMessage($_FILES['picture']['error']),
261-
Analog::WARNING
262-
);
263-
$error_detected[] = $category->picture->getPhpErrorMessage(
264-
$_FILES['picture']['error']
265-
);
266-
}
251+
$picture = new CategoryPicture($category->category_id);
252+
if (!$picture->upload($request->getUploadedFiles(), 'picture')) {
253+
$error_detected = $picture->uploadErrors();
267254
}
268255

269-
if (isset($post['del_picture'])) {
270-
if (!$category->picture->delete($category->category_id)) {
256+
if (isset($post['del_photo'])) {
257+
if (!$picture->delete()) {
271258
$error_detected[] = _T("Delete failed", "objectslend");
272259
Analog::log(
273260
'Unable to delete picture for category ' . $category->name,
@@ -350,7 +337,7 @@ public function formUri(array $args): string
350337
*/
351338
public function confirmRemoveTitle(array $args): string
352339
{
353-
$category = new LendCategory($this->zdb, $this->plugins, (int)$args['id']);
340+
$category = new LendCategory($this->zdb, (int)$args['id']);
354341
return sprintf(
355342
_T('Remove category %1$s', 'objectslend'),
356343
$category->name
@@ -367,7 +354,7 @@ public function confirmRemoveTitle(array $args): string
367354
*/
368355
protected function doDelete(array $args, array $post): bool
369356
{
370-
$category = new LendCategory($this->zdb, $this->plugins, (int)$args['id']);
357+
$category = new LendCategory($this->zdb, (int)$args['id']);
371358
return $category->delete();
372359
}
373360

lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
use Analog\Analog;
2727
use DI\Attribute\Inject;
28+
use GaletteObjectsLend\Entity\ObjectPicture;
2829
use GaletteObjectsLend\Filters\CategoriesList;
2930
use GaletteObjectsLend\Filters\ObjectsList;
3031
use GaletteObjectsLend\Filters\StatusList;
@@ -124,7 +125,7 @@ public function list(Request $request, Response $response, ?string $option = nul
124125
}
125126

126127
$lendsprefs = new Preferences($this->zdb);
127-
$objects = new Objects($this->zdb, $this->plugins, $lendsprefs, $filters);
128+
$objects = new Objects($this->zdb, $lendsprefs, $filters);
128129
$list = $objects->getObjectsList(true);
129130

130131
$this->session->objectslend_filter_objects = $filters;
@@ -137,7 +138,7 @@ public function list(Request $request, Response $response, ?string $option = nul
137138
$cat_filters->active_filter = Categories::ACTIVE_CATEGORIES; //retrieve only active categories
138139
$cat_filters->not_empty = true; //retrieve only categories with objects
139140
$cat_filters->setObjectsFilter($filters); //search for categories corresponding to filtered objects
140-
$categories = new Categories($this->zdb, $this->login, $this->plugins, $cat_filters);
141+
$categories = new Categories($this->zdb, $this->login, $cat_filters);
141142
$categories_list = $categories->getCategoriesList(true, null, false);
142143

143144
// display page
@@ -226,7 +227,6 @@ public function show(Request $request, Response $response, int $id): Response
226227
];
227228
$object = new LendObject(
228229
$this->zdb,
229-
$this->plugins,
230230
$id,
231231
$deps
232232
);
@@ -315,10 +315,10 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
315315
$this->session->objectslend_object = null;
316316
} else {
317317
$deps = ['rents' => true];
318-
$object = new LendObject($this->zdb, $this->plugins, $id, $deps);
318+
$object = new LendObject($this->zdb, $id, $deps);
319319
}
320320

321-
$categories = new Categories($this->zdb, $this->login, $this->plugins);
321+
$categories = new Categories($this->zdb, $this->login);
322322
$categories_list = $categories->getCategoriesList(true);
323323

324324
if ($object->object_id !== null) {
@@ -333,6 +333,7 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
333333
$slist = $statuses->getStatusList(true);
334334

335335
$lendsprefs = new Preferences($this->zdb);
336+
$picture = new ObjectPicture($object->object_id);
336337
$params = [
337338
'page_title' => $title,
338339
'object' => $object,
@@ -341,7 +342,8 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
341342
'lendsprefs' => $lendsprefs->getPreferences(),
342343
'olendsprefs' => $lendsprefs,
343344
'categories' => $categories_list,
344-
'statuses' => $slist
345+
'statuses' => $slist,
346+
'picture' => $picture
345347
];
346348

347349
// members
@@ -384,7 +386,7 @@ public function doEdit(Request $request, Response $response, ?int $id = null, st
384386
{
385387
$post = $request->getParsedBody();
386388

387-
$object = new LendObject($this->zdb, $this->plugins, $id);
389+
$object = new LendObject($this->zdb, $id);
388390
$error_detected = [];
389391

390392
$object->name = $post['name'];
@@ -417,29 +419,12 @@ public function doEdit(Request $request, Response $response, ?int $id = null, st
417419
}
418420

419421
// picture upload
420-
if (isset($_FILES['picture'])) {
421-
if ($_FILES['picture']['error'] === UPLOAD_ERR_OK) {
422-
if ($_FILES['picture']['tmp_name'] != '') {
423-
if (is_uploaded_file($_FILES['picture']['tmp_name'])) {
424-
$res = $object->picture->store($_FILES['picture']);
425-
if ($res < 0) {
426-
$error_detected[] = $object->picture->getErrorMessage($res);
427-
}
428-
}
429-
}
430-
} elseif ($_FILES['picture']['error'] !== UPLOAD_ERR_NO_FILE) {
431-
Analog::log(
432-
$object->picture->getPhpErrorMessage($_FILES['picture']['error']),
433-
Analog::WARNING
434-
);
435-
$error_detected[] = $object->picture->getPhpErrorMessage(
436-
$_FILES['picture']['error']
437-
);
438-
}
422+
if (!$object->picture->upload($request->getUploadedFiles(), 'picture')) {
423+
$error_detected = $object->picture->uploadErrors();
439424
}
440425

441426
if (isset($post['del_picture'])) {
442-
if (!$object->picture->delete($object->object_id)) {
427+
if (!$object->picture->delete()) {
443428
$error_detected[] = _T("Delete failed", "objectslend");
444429
Analog::log(
445430
'Unable to delete picture for object ' . $object->name,
@@ -500,7 +485,7 @@ public function doUpdateStatus(Request $request, Response $response, ?int $id =
500485
{
501486
$post = $request->getParsedBody();
502487

503-
$object = new LendObject($this->zdb, $this->plugins, $id);
488+
$object = new LendObject($this->zdb, $id);
504489

505490
LendRent::closeAllRentsForObject($object->getId(), $post['new_comment']);
506491

@@ -537,7 +522,7 @@ public function doUpdateStatus(Request $request, Response $response, ?int $id =
537522
*/
538523
public function doClone(Request $request, Response $response, int $id): Response
539524
{
540-
$object = new LendObject($this->zdb, $this->plugins, $id);
525+
$object = new LendObject($this->zdb, $id);
541526

542527
if ($object->clone()) {
543528
$this->flash->addMessage(
@@ -606,7 +591,6 @@ public function lend(Request $request, Response $response, string $action, int $
606591
];
607592
$object = new LendObject(
608593
$this->zdb,
609-
$this->plugins,
610594
$id,
611595
$deps
612596
);
@@ -774,7 +758,6 @@ public function doTake(Request $request, Response $response, int $id): Response
774758
//retrieve object information
775759
$object = new LendObject(
776760
$this->zdb,
777-
$this->plugins,
778761
$object_id
779762
);
780763

@@ -896,7 +879,6 @@ public function doReturn(Request $request, Response $response, int $id): Respons
896879
//retrieve object information
897880
$object = new LendObject(
898881
$this->zdb,
899-
$this->plugins,
900882
$object_id,
901883
$deps
902884
);
@@ -1024,7 +1006,7 @@ public function confirmRemoveTitle(array $args): string
10241006
{
10251007
if (isset($args['id'])) {
10261008
//one object removal
1027-
$object = new LendObject($this->zdb, $this->plugins, (int)$args['id']);
1009+
$object = new LendObject($this->zdb, (int)$args['id']);
10281010
return sprintf(
10291011
_T('Remove object %1$s', 'objectslend'),
10301012
$object->name
@@ -1056,7 +1038,7 @@ protected function doDelete(array $args, array $post): bool
10561038
$filters = new ObjectsList();
10571039
}
10581040
$lendsprefs = new Preferences($this->zdb);
1059-
$objects = new Objects($this->zdb, $this->plugins, $lendsprefs, $filters);
1041+
$objects = new Objects($this->zdb, $lendsprefs, $filters);
10601042

10611043
if (!is_array($post['id'])) {
10621044
$ids = (array)$post['id'];

lib/GaletteObjectsLend/Controllers/ImagesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function lendPicture(Request $request, Response $response, string $type,
5353
{
5454
$class = '\GaletteObjectsLend\Entity\\'
5555
. ($type == 'category' ? 'CategoryPicture' : 'ObjectPicture');
56-
$picture = new $class($this->plugins, $id);
56+
$picture = new $class($id);
5757

5858
$this->lendsprefs = new Preferences($this->zdb);
5959
$thumb = false;

lib/GaletteObjectsLend/Controllers/PdfController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public function printObject(Request $request, Response $response, int $id): Resp
6262
];
6363
$object = new LendObject(
6464
$this->zdb,
65-
$this->plugins,
6665
$id,
6766
$deps
6867
);
@@ -98,16 +97,15 @@ public function printObjects(Request $request, Response $response): Response
9897
if ($filters->orderby !== Objects::ORDERBY_CATEGORY) {
9998
$filters->orderby = Objects::ORDERBY_CATEGORY;
10099
}
101-
$objects = new Objects($this->zdb, $this->plugins, $lendsprefs, $filters);
100+
$objects = new Objects($this->zdb, $lendsprefs, $filters);
102101
$list = $objects->getObjectsList(true, null, true, false);
103102

104103
$pdf = new PdfObjects(
105104
$this->zdb,
106105
$this->preferences,
107106
$lendsprefs,
108107
$filters,
109-
$this->login,
110-
$this->plugins
108+
$this->login
111109
);
112110

113111
$pdf->drawList($list);

lib/GaletteObjectsLend/Entity/CategoryPicture.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
namespace GaletteObjectsLend\Entity;
2525

26-
use Galette\Core\Plugins;
27-
2826
/**
2927
* Picture for category
3028
*
@@ -39,12 +37,11 @@ class CategoryPicture extends Picture
3937
/**
4038
* Default constructor.
4139
*
42-
* @param Plugins $plugins Plugins
4340
* @param mixed|null $objectid Object id
4441
*/
45-
public function __construct(Plugins $plugins, mixed $objectid = null)
42+
public function __construct(mixed $objectid = null)
4643
{
4744
$this->store_path = GALETTE_PHOTOS_PATH . 'objectslend/categories/';
48-
parent::__construct($plugins, $objectid);
45+
parent::__construct($objectid);
4946
}
5047
}

lib/GaletteObjectsLend/Entity/LendCategory.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Analog\Analog;
2727
use ArrayObject;
2828
use Galette\Core\Db;
29-
use Galette\Core\Plugins;
3029
use Laminas\Db\Sql\Predicate;
3130

3231
/**
@@ -66,20 +65,17 @@ class LendCategory
6665
];
6766

6867
private Db $zdb;
69-
private Plugins $plugins;
7068

7169
/**
7270
* Default constructor
7371
*
74-
* @param Db $zdb Database instance
75-
* @param Plugins $plugins Plugins instance
76-
* @param int|ArrayObject<string,int|string>|null $args Maybe null, an RS object or an id from database
77-
* @param ?array<string,bool> $deps Dependencies configuration, see LendCategory::$deps
72+
* @param Db $zdb Database instance
73+
* @param int|ArrayObject<string,int|string>|null $args Maybe null, an RS object or an id from database
74+
* @param ?array<string,bool> $deps Dependencies configuration, see LendCategory::$deps
7875
*/
79-
public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject|null $args = null, ?array $deps = null)
76+
public function __construct(Db $zdb, int|ArrayObject|null $args = null, ?array $deps = null)
8077
{
8178
$this->zdb = $zdb;
82-
$this->plugins = $plugins;
8379

8480
if ($deps !== null) {
8581
$this->deps = array_merge(
@@ -89,7 +85,7 @@ public function __construct(Db $zdb, Plugins $plugins, int|ArrayObject|null $arg
8985
}
9086

9187
if ($this->deps['picture'] === true) {
92-
$this->picture = new CategoryPicture($this->plugins);
88+
$this->picture = new CategoryPicture();
9389
}
9490

9591
if (is_int($args)) {
@@ -134,7 +130,7 @@ private function loadFromRS(ArrayObject $r): void
134130
}
135131

136132
if ($this->deps['picture'] === true) {
137-
$this->picture = new CategoryPicture($this->plugins, (int)$this->category_id);
133+
$this->picture = new CategoryPicture((int)$this->category_id);
138134
}
139135
}
140136

0 commit comments

Comments
 (0)