Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _define.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
acls: [
'objectslend_preferences' => 'admin',
'store_objectlend_preferences' => 'admin',
'objectslend_sample_data' => 'admin',
'objectslend_category_add' => 'staff',
'objectslend_category_edit' => 'staff',
'objectslend_category_action_add' => 'staff',
Expand Down Expand Up @@ -56,5 +57,5 @@
'objectslend_object_dotake' => 'member',
'objectslend_object_doreturn' => 'member'
],
dbver: 1.00
dbver: 1.1
);
5 changes: 5 additions & 0 deletions _routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
[MainController::class, 'storePreferences']
)->setName('store_objectlend_preferences')->add(Authenticate::class);

$app->post(
'/sample-data',
[MainController::class, 'loadSampleData']
)->setName('objectslend_sample_data')->add(Authenticate::class);

$app->get(
'/category/add',
[CategoriesController::class, 'add']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use GaletteObjectsLend\Entity\CategoryPicture;
use GaletteObjectsLend\Entity\LendCategory;
use GaletteObjectsLend\Entity\LendStatus;
use GaletteObjectsLend\Entity\Preferences;
use GaletteObjectsLend\LendPreferences;
use GaletteObjectsLend\Filters\CategoriesList;
use GaletteObjectsLend\Repository\Categories;
use Slim\Psr7\Request;
Expand Down Expand Up @@ -79,7 +79,7 @@ protected function getListView(Pagination $filters): array
'page_title' => _T("Categories list", "objectslend"),
'categories' => $list,
'nb_categories' => count($list),
'olendsprefs' => new Preferences($this->zdb),
'olendsprefs' => new LendPreferences($this->preferences),
'time' => time()
]
];
Expand Down Expand Up @@ -126,7 +126,7 @@ protected function getEditView(LendCategory|LendStatus $entity, string $action):
: _T("New category", "objectslend"),
'category' => $entity,
'time' => time(),
'olendsprefs' => new Preferences($this->zdb),
'olendsprefs' => new LendPreferences($this->preferences),
'picture' => new CategoryPicture($entity->getId())
]
];
Expand All @@ -144,7 +144,8 @@ protected function getEditView(LendCategory|LendStatus $entity, string $action):
protected function afterStore(LendCategory|LendStatus $entity, Request $request, array $post): array
{
$errors = [];
$picture = new CategoryPicture($entity->getId());
$picture = (new CategoryPicture($entity->getId()))
->setMaxLength((new LendPreferences($this->preferences))->getUploadSize());
if (!$picture->upload($request->getUploadedFiles(), 'picture')) {
$errors = $picture->uploadErrors();
}
Expand Down
23 changes: 12 additions & 11 deletions lib/GaletteObjectsLend/Controllers/Crud/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use GaletteObjectsLend\Repository\Status;
use GaletteObjectsLend\Entity\LendObject;
use GaletteObjectsLend\Entity\LendRent;
use GaletteObjectsLend\Entity\Preferences;
use GaletteObjectsLend\LendPreferences;
use GaletteObjectsLend\LendException;
use GaletteObjectsLend\LendService;
use Galette\Controllers\Crud\AbstractPluginController;
Expand Down Expand Up @@ -126,7 +126,7 @@ public function list(Request $request, Response $response, ?string $option = nul
}
}

$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);
$objects = new Objects($this->zdb, $this->preferences, $this->login, $lendsprefs, $filters);
$list = $objects->getObjectsList(true);

Expand Down Expand Up @@ -156,7 +156,7 @@ public function list(Request $request, Response $response, ?string $option = nul
'objects' => $list,
'nb_objects' => count($list),
'filters' => $filters,
'lendsprefs' => $lendsprefs->getPreferences(),
'lendsprefs' => $lendsprefs->toArray(),
'olendsprefs' => $lendsprefs,
'time' => time(),
'module_id' => $this->getModuleId(),
Expand Down Expand Up @@ -314,14 +314,14 @@ public function edit(Request $request, Response $response, ?int $id = null, stri
$statuses = new Status($this->zdb, $this->preferences, $this->login, $sfilter);
$slist = $statuses->getStatusList(true);

$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);
$params = [
'page_title' => $title,
'object' => $object,
'rents' => $object->getId() !== null ? (new Rents($this->zdb))->getForObject($object->getId()) : [],
'time' => time(),
'action' => $action,
'lendsprefs' => $lendsprefs->getPreferences(),
'lendsprefs' => $lendsprefs->toArray(),
'olendsprefs' => $lendsprefs,
'categories' => $categories_list,
'statuses' => $slist,
Expand Down Expand Up @@ -392,6 +392,7 @@ public function doEdit(Request $request, Response $response, ?int $id = null, st
}

// picture upload
$object->getPicture()->setMaxLength((new LendPreferences($this->preferences))->getUploadSize());
if (!$object->getPicture()->upload($request->getUploadedFiles(), 'picture')) {
$error_detected = $object->getPicture()->uploadErrors();
}
Expand Down Expand Up @@ -592,7 +593,7 @@ public function doClone(Request $request, Response $response, int $id): Response
*/
public function lend(Request $request, Response $response, string $action, int $id): Response
{
$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);

$params = [
'page_title' => (
Expand All @@ -604,7 +605,7 @@ public function lend(Request $request, Response $response, string $action, int $
'statuses' => ($action == 'take'
? (new Status($this->zdb, $this->preferences, $this->login))->getActiveTakeAwayStatuses()
: (new Status($this->zdb, $this->preferences, $this->login))->getActiveStockStatuses()),
'lendsprefs' => $lendsprefs->getPreferences(),
'lendsprefs' => $lendsprefs->toArray(),
'olendsprefs' => $lendsprefs,
'ajax' => $this->isAjax($request),
'takeorgive' => $action,
Expand Down Expand Up @@ -826,11 +827,11 @@ private function lendResponse(Request $request, Response $response): Response
/**
* Get lend service
*
* @param ?Preferences $lendsprefs Plugin preferences, loaded if not provided
* @param ?LendPreferences $lendsprefs Plugin preferences, loaded if not provided
*/
private function getLendService(?Preferences $lendsprefs = null): LendService
private function getLendService(?LendPreferences $lendsprefs = null): LendService
{
return new LendService($this->zdb, $this->preferences, $this->login, $lendsprefs ?? new Preferences($this->zdb));
return new LendService($this->zdb, $this->preferences, $this->login, $lendsprefs ?? new LendPreferences($this->preferences));
}

// /CRUD - Update
Expand Down Expand Up @@ -915,7 +916,7 @@ public function confirmRemoveTitle(array $args): string
protected function doDelete(array $args, array $post): bool
{
$filters = $this->getFilters();
$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);
$objects = new Objects($this->zdb, $this->preferences, $this->login, $lendsprefs, $filters);

if (!is_array($post['id'])) {
Expand Down
14 changes: 4 additions & 10 deletions lib/GaletteObjectsLend/Controllers/ImagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace GaletteObjectsLend\Controllers;

use Galette\Controllers\ImagesController as GImagesController;
use GaletteObjectsLend\Entity\Preferences;
use GaletteObjectsLend\LendPreferences;
use Slim\Psr7\Request;
use Slim\Psr7\Response;

Expand All @@ -23,7 +23,7 @@

class ImagesController extends GImagesController
{
private Preferences $lendsprefs;
private LendPreferences $lendsprefs;

/**
* Objects lends category or object route
Expand All @@ -40,14 +40,8 @@ public function lendPicture(Request $request, Response $response, string $type,
. ($type == 'category' ? 'CategoryPicture' : 'ObjectPicture');
$picture = new $class($id);

$this->lendsprefs = new Preferences($this->zdb);
$thumb = false;
if (!$this->lendsprefs->showFullsize() || $mode == 'thumbnail') {
//force thumbnail display from preferences
$thumb = true;
}

if ($thumb) {
$this->lendsprefs = new LendPreferences($this->preferences);
if ($mode == 'thumbnail') {
return $picture->displayThumb($response, $this->lendsprefs);
} else {
return $picture->display($response);
Expand Down
98 changes: 72 additions & 26 deletions lib/GaletteObjectsLend/Controllers/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace GaletteObjectsLend\Controllers;

use Analog\Analog;
use DI\Attribute\Inject;
use Galette\Controllers\AbstractPluginController;
use Galette\Entity\ContributionsTypes;
use GaletteObjectsLend\Entity\Preferences;
use GaletteObjectsLend\LendPreferences;
use GaletteObjectsLend\SampleData;
use Slim\Psr7\Request;
use Slim\Psr7\Response;

Expand All @@ -39,25 +41,13 @@ class MainController extends AbstractPluginController
*/
public function preferences(Request $request, Response $response): Response
{
if ($this->session->objectslend_preferences !== null) {
$lendsprefs = $this->session->objectslend_preferences;
$this->session->objectslend_preferences = null;
} else {
$lendsprefs = new Preferences($this->zdb);
}

$ctypes = new ContributionsTypes($this->zdb);
$types_list = $ctypes->getList();
$ctypes_params = [0 => _T("Choose a contribution type", "objectslend")];
foreach ($types_list as $id => $type) {
$ctypes_params[$id] = $type['label'];
}

$params = [
'page_title' => _T('ObjectsLend preferences', 'objectslend'),
'type_cotis_options' => $ctypes->getList(),
'ctypes' => $ctypes_params,
'lendsprefs' => $lendsprefs->getPreferences()
'lendsprefs' => (new LendPreferences($this->preferences))->toArray(),
'sample_data' => !(new SampleData($this->zdb))->hasObjects()
];

// display page
Expand All @@ -70,37 +60,93 @@ public function preferences(Request $request, Response $response): Response
}

/**
* Objects lends preferences
* Store objects lends preferences
*
* Only declared preferences are read from the request. A yes/no one
* missing from it is an unchecked box, and is set off.
*
* @param Request $request PSR Request
* @param Response $response PSR Response
*/
public function storePreferences(Request $request, Response $response): Response
{
$post = $request->getParsedBody();
$lendsprefs = new Preferences($this->zdb);
$booleans = LendPreferences::getBooleans();

$stored = true;
$errors = [];
foreach (array_keys(LendPreferences::getSchema()) as $name) {
if (isset($booleans[$name])) {
$value = (int)isset($post[$name]);
} elseif (isset($post[$name])) {
$value = trim((string)$post[$name]);
} else {
continue;
}

if (!$this->preferences->setValue($name, $value, $this->login)) {
$stored = false;
$errors = array_merge($errors, $this->preferences->getErrors());
}
}

$error_detected = [];
if ($lendsprefs->store($post)) {
if ($stored) {
$this->flash->addMessage(
'success_detected',
_T("Preferences have been successfully stored!", "objectslend")
);
} else {
$this->session->objectslend_preferences = $lendsprefs;
foreach ($error_detected as $error) {
$this->flash->addMessage(
'error_detected',
$error
);
foreach (array_unique($errors) as $error) {
$this->flash->addMessage('error_detected', $error);
}
}

return $response
->withStatus(301)
->withStatus(302)
->withHeader(
'Location',
$this->routeparser->urlFor('objectslend_preferences')
);
}

/**
* Load sample data into an empty catalog
*
* @param Request $request PSR Request
* @param Response $response PSR Response
*/
public function loadSampleData(Request $request, Response $response): Response
{
$sample = new SampleData($this->zdb);

if ($sample->hasObjects()) {
$this->flash->addMessage(
'error_detected',
_T("Sample data can only be loaded into an empty catalog.", "objectslend")
);
} else {
try {
$created = $sample->load($sample->getMembers());
$this->flash->addMessage(
'success_detected',
sprintf(
//TRANS: %1$d is the number of objects, %2$d the number of rents
_T('Sample data loaded: %1$d objects, %2$d rents.', 'objectslend'),
$created['objects'],
$created['rents']
)
);
} catch (\Throwable $e) {
Analog::log('Unable to load sample data | ' . $e->getMessage(), Analog::ERROR);
$this->flash->addMessage(
'error_detected',
_T("Sample data could not be loaded.", "objectslend")
);
}
}

return $response
->withStatus(302)
->withHeader('Location', $this->routeparser->urlFor('objectslend_objects'));
}
}
6 changes: 3 additions & 3 deletions lib/GaletteObjectsLend/Controllers/PdfController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use Galette\Controllers\PdfController as GPdfController;
use GaletteObjectsLend\Controllers\Crud\ObjectsController;
use GaletteObjectsLend\Entity\Preferences;
use GaletteObjectsLend\LendPreferences;
use GaletteObjectsLend\Filters\ObjectsList;
use GaletteObjectsLend\Repository\Objects;
use GaletteObjectsLend\IO\PdfObject;
Expand All @@ -37,7 +37,7 @@ class PdfController extends GPdfController
*/
public function printObject(Request $request, Response $response, int $id): Response
{
$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);
$object = (new Objects($this->zdb, $this->preferences, $this->login, $lendsprefs))->getWithCurrentRent($id);

$pdf = new PdfObject(
Expand All @@ -57,7 +57,7 @@ public function printObject(Request $request, Response $response, int $id): Resp
*/
public function printObjects(Request $request, Response $response): Response
{
$lendsprefs = new Preferences($this->zdb);
$lendsprefs = new LendPreferences($this->preferences);

$filters = clone ($this->session->{$this->getFilterName(ObjectsController::getDefaultFilterName())}
?? new ObjectsList());
Expand Down
Loading
Loading